Added Bank Transfer edition.
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index 18939822d8676a704d579c3c1b4c9674573a25bd..8193f0ddb573c6fd15a852225c7e4fbc4035dc36 100644 (file)
@@ -200,12 +200,15 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                                $taxitem['tax_type_id'], $taxitem['rate'], $supp_trans->tax_included, $taxitem['Value'],
                                $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
 
-                       if ($trans_type == ST_SUPPCREDIT)
-                               $taxitem['Value'] = -$taxitem['Value'];
-                       $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_,
-                               $taxitem['purchasing_gl_code'], 0, 0, $taxitem['Value'],
-                               $supp_trans->supplier_id,
-                               "A general ledger transaction for the tax amount could not be added");
+                       if (isset($taxitem['purchasing_gl_code']))
+                       {
+                               if ($trans_type == ST_SUPPCREDIT)
+                                       $taxitem['Value'] = -$taxitem['Value'];
+                               $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_,
+                                       $taxitem['purchasing_gl_code'], 0, 0, $taxitem['Value'],
+                                       $supp_trans->supplier_id,
+                                       "A general ledger transaction for the tax amount could not be added");
+                       }
                }
     }
     if ($trans_type == ST_SUPPCREDIT)
@@ -217,6 +220,8 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                $supp_trans->supplier_id,
                "The general ledger transaction for the control total could not be added");
 
+       $to_allocate = ($invoice_items_total + $item_added_tax + $supp_trans->ov_discount);
+
     foreach ($supp_trans->gl_codes as $entered_gl_code)
     {
            /*GL Items are straight forward - just do the debit postings to the GL accounts specified -
@@ -363,17 +368,18 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                if ($invoice_alloc_balance > 0) 
                {       //the invoice is not already fully allocated 
 
-                       $trans = get_supp_trans($supp_trans->src_docs, ST_SUPPINVOICE);
-                       $total = $trans['Total'];
+                       $trans = get_supp_trans($invoice_id, ST_SUPPCREDIT);
+                       $total = -$trans['Total'];
 
                        $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
+
                        /*Now insert the allocation record if > 0 */
                        if ($allocate_amount != 0) 
                        {
-                               update_supp_trans_allocation(ST_SUPPINVOICE, $supp_trans->src_docs);
-                               update_supp_trans_allocation(ST_SUPPCREDIT, $invoice_id);
-                               add_supp_allocation($allocate_amount, ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $supp_trans->src_docs,
+                               add_supp_allocation($allocate_amount, ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $invoice_no,
                                        $date_);
+                               update_supp_trans_allocation(ST_SUPPINVOICE, $invoice_no);
+                               update_supp_trans_allocation(ST_SUPPCREDIT, $invoice_id);
 
                                exchange_variation(ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $supp_trans->src_docs, $date_,
                                        $allocate_amount, PT_SUPPLIER);
@@ -386,6 +392,7 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
 //_vd(get_payments_for($sales_order, ST_PURCHORDER));
 //_vd(get_payments_for($invoice_id, ST_SUPPINVOICE));
 //exit;
+       $supp_trans->trans_no = $invoice_id;
        hook_db_postwrite($supp_trans, $supp_trans->trans_type);
     commit_transaction();