Improved Customer and Supplier Balances Report. Option to choose last column as balance.
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index 558b2159aba0159fd9a1bad9c8e0a000ad275cfd..a1f6dd5cc62697364448f6b99d288d95d627a4d4 100644 (file)
@@ -59,21 +59,22 @@ function write_sales_invoice(&$invoice)
        if (is_array($sales_order))
                        $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
 
-       $invoice_no = write_customer_trans(ST_SALESINVOICE, $trans_no, $invoice->customer_id,
-               $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
-               $items_added_tax, $invoice->freight_cost, $freight_added_tax,
-               $invoice->sales_type, $sales_order, $invoice->ship_via, 
-               $invoice->due_date, 0, 0, $invoice->dimension_id, 
-               $invoice->dimension2_id, $invoice->payment); 
-               // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
+       if ($trans_no != 0) {
+         delete_comments(ST_SALESINVOICE, $trans_no);
+         void_gl_trans(ST_SALESINVOICE, $trans_no, true);
+         void_cust_allocations(ST_SALESINVOICE, $trans_no); // ?
+         void_trans_tax_details(ST_SALESINVOICE, $trans_no);
+    } 
+    $invoice_no = write_customer_trans(ST_SALESINVOICE, $trans_no, $invoice->customer_id,
+       $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
+       $items_added_tax, $invoice->freight_cost, $freight_added_tax,
+       $invoice->sales_type, $sales_order, $invoice->ship_via, 
+       $invoice->due_date, 0, 0, $invoice->dimension_id, 
+       $invoice->dimension2_id, $invoice->payment); 
+       // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
 
        if ($trans_no == 0) {
                $invoice->trans_no = array($invoice_no=>0);
-       } else {
-               delete_comments(ST_SALESINVOICE, $invoice_no);
-               void_gl_trans(ST_SALESINVOICE, $invoice_no, true);
-               void_cust_allocations(ST_SALESINVOICE, $invoice_no); // ?
-               void_trans_tax_details(ST_SALESINVOICE, $invoice_no);
        }
        $total = 0;
        foreach ($invoice->line_items as $line_no => $invoice_line) {
@@ -195,19 +196,18 @@ function void_sales_invoice($type, $type_no)
 
        // reverse all the changes in parent document(s)
 
-       $deliveries = get_sales_parent_numbers($type, $type_no);
+       $parents = get_sales_parent_numbers($type, $type_no);
 
-       if ($deliveries !== 0) {
-               if (count($deliveries) == 1 && get_reference(ST_CUSTDELIVERY, $deliveries[0]) == "auto")
+       if ($parents !== 0) {
+               if ($type == ST_SALESINVOICE && count($parents) == 1 && get_reference(ST_CUSTDELIVERY, $parents[0]) == "auto")
                {
-                       void_sales_delivery(ST_CUSTDELIVERY, $deliveries[0], false);
+                       void_sales_delivery(ST_CUSTDELIVERY, $parents[0], false);
                        $date_ = Today();
-                       add_audit_trail(ST_CUSTDELIVERY, $deliveries[0], $date_, _("Voided."));
-                       add_voided_entry(ST_CUSTDELIVERY, $deliveries[0], $date_, "");
+                       add_audit_trail(ST_CUSTDELIVERY, $parents[0], $date_, _("Voided."));
+                       add_voided_entry(ST_CUSTDELIVERY, $parents[0], $date_, "");
                }
                else
                {
-                       
                        $srcdetails = get_sales_parent_lines($type, $type_no);
                        while ($row = db_fetch($srcdetails)) {
                                update_parent_line($type, $row['id'], -$row['quantity']);
@@ -217,6 +217,8 @@ function void_sales_invoice($type, $type_no)
        // clear details after they've been reversed in the sales order
        void_customer_trans_details($type, $type_no);
 
+       void_stock_move($type, $type_no); // in case of credit note with return
+
        void_trans_tax_details($type, $type_no);
 
        void_cust_allocations($type, $type_no);