Merged changes in main trunk up to 2.0.6 (see CHANGELOG)
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index 689cd95606873b8833b4fdc9b7d8d39ddeb5ff6e..1c9837f9fbd3ca56b6e6ab757e21cee54a3d89cc 100644 (file)
@@ -61,7 +61,7 @@ function write_sales_invoice(&$invoice)
                $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
                $items_added_tax, $invoice->freight_cost, $freight_added_tax,
                $invoice->sales_type, $sales_order, $delivery_no,
-               $invoice->ship_via, $invoice->due_date, $alloc, $invoice->dimension_id, $invoice->dimension2_id); 
+               $invoice->ship_via, $invoice->due_date, $alloc, 0, $invoice->dimension_id, $invoice->dimension2_id); 
                // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
 
        if ($trans_no == 0) {
@@ -73,7 +73,7 @@ function write_sales_invoice(&$invoice)
                void_cust_allocations(10, $invoice_no); // ?
                void_customer_trans_tax_details(10, $invoice_no);
        }
-
+       $total = 0;
        foreach ($invoice->line_items as $invoice_line) {
 
                $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
@@ -105,15 +105,17 @@ function write_sales_invoice(&$invoice)
                                $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
                                // 2008-08-01. If there is a Customer Dimension, then override with this,
                                // else take the Item Dimension (if any)
-                               $dim = ($invoice->dimension_id > 0 ? $invoice->dimension_id : ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
-                               $dim2 = ($invoice->dimension2_id > 0 ? $invoice->dimension2_id : ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
-                               add_gl_trans_customer(10, $invoice_no, $date_, $sales_account, $dim, $dim2,
+                               $dim = ($invoice->dimension_id != $customer['dimension_id'] ? $invoice->dimension_id : 
+                                       ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
+                               $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id : 
+                                       ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
+                               $total += add_gl_trans_customer(10, $invoice_no, $date_, $sales_account, $dim, $dim2,
                                        (-$line_taxfree_price * $invoice_line->qty_dispatched),
                                        $invoice->customer_id, "The sales price GL posting could not be inserted");
 
                                if ($invoice_line->discount_percent != 0) {
 
-                                       add_gl_trans_customer(10, $invoice_no, $date_,
+                                       $total += add_gl_trans_customer(10, $invoice_no, $date_,
                                                $branch_data["sales_discount_account"], $dim, $dim2,
                                                ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
                                                $invoice->customer_id, "The sales discount GL posting could not be inserted");
@@ -122,14 +124,14 @@ function write_sales_invoice(&$invoice)
                } /*quantity dispatched is more than 0 */
        } /*end of delivery_line loop */
 
-       /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
        if (($items_total + $charge_shipping) != 0) {
-               add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
+               $total += add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
                        ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
                        $invoice->customer_id, "The total debtor GL posting could not be inserted");
        }
+
        if ($charge_shipping != 0) {
-               add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
+               $total += add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
                        -$invoice->get_tax_free_shipping(), $invoice->customer_id,
                        "The freight GL posting could not be inserted");
        }
@@ -139,12 +141,15 @@ function write_sales_invoice(&$invoice)
                        add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
                                $taxitem['rate'], $invoice->tax_included, $taxitem['Value']);
 
-                       add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
+                       $total += add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
                                (-$taxitem['Value']), $invoice->customer_id,
                                "A tax GL posting could not be inserted");
                }
        }
 
+       /*Post a balance post if $total != 0 */
+       add_gl_balance(10, $invoice_no, $date_, -$total, payment_person_types::customer(), $invoice->customer_id);      
+
        add_comments(10, $invoice_no, $date_, $invoice->Comments);
 
        if ($trans_no == 0) {