New files from unstable branch
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index f0798af5f98cd9e1bd92482dc428eeeef89971ab..df56eb4d5d5d18ed2a534339f06dfc4cb1914f26 100644 (file)
@@ -25,6 +25,7 @@ function write_sales_invoice(&$invoice)
 
        begin_transaction();
 
+       hook_db_prewrite($invoice, ST_SALESINVOICE);
        $company_data = get_company_prefs();
 
        $branch_data = get_branch_accounts($invoice->Branch);
@@ -35,10 +36,6 @@ function write_sales_invoice(&$invoice)
        $items_total = $invoice->get_items_total_dispatch();
        $freight_tax = $invoice->get_shipping_tax();
 
-       $delivery_no = $invoice->src_docs;
-       if (is_array($delivery_no))
-               $delivery_no = 0;
-
        update_customer_trans_version(get_parent_type(ST_SALESINVOICE), $invoice->src_docs);
 
        $ov_gst = 0;
@@ -56,12 +53,6 @@ function write_sales_invoice(&$invoice)
            $items_added_tax = 0;
            $freight_added_tax = 0;
        }
-       // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
-       // the transaction will be settled at once.
-       if (is_bank_account($branch_data['receivables_account']))
-               $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
-       else
-               $alloc = 0;
 
        /* Insert/update the debtor_trans */
        $sales_order = $invoice->order_no;
@@ -71,13 +62,13 @@ function write_sales_invoice(&$invoice)
        $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, $delivery_no,
-               $invoice->ship_via, $invoice->due_date, $alloc, 0, $invoice->dimension_id, $invoice->dimension2_id); 
+               $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);
-               set_document_parent($invoice);
        } else {
                delete_comments(ST_SALESINVOICE, $invoice_no);
                void_gl_trans(ST_SALESINVOICE, $invoice_no, true);
@@ -98,7 +89,7 @@ function write_sales_invoice(&$invoice)
                write_customer_trans_detail_item(ST_SALESINVOICE, $invoice_no, $invoice_line->stock_id,
                        $invoice_line->item_description, $invoice_line->qty_dispatched,
                        $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
-                       $invoice_line->standard_cost,
+                       $invoice_line->standard_cost, $invoice_line->src_id,
                        $trans_no ? $invoice_line->id : 0);
 
                // Update delivery items for the quantity invoiced
@@ -168,7 +159,7 @@ function write_sales_invoice(&$invoice)
 
        if ($trans_no == 0) {
                $Refs->save(ST_SALESINVOICE, $invoice_no, $invoice->reference);
-               if ($invoice->cash) {
+               if ($invoice->payment_terms['cash_sale']) {
                        $amount = $items_total + $items_added_tax + $invoice->freight_cost 
                                + $freight_added_tax;
 
@@ -186,6 +177,7 @@ function write_sales_invoice(&$invoice)
                        update_debtor_trans_allocation(ST_CUSTPAYMENT, $pmtno, $amount);
                }
        }
+       hook_db_postwrite($invoice, ST_SALESINVOICE);
        commit_transaction();
 
        return $invoice_no;
@@ -197,19 +189,29 @@ function void_sales_invoice($type, $type_no)
 {
        begin_transaction();
 
+       hook_db_prevoid($type, $type_no);
        void_bank_trans($type, $type_no, true);
        void_gl_trans($type, $type_no, true);
 
        // reverse all the changes in parent document(s)
-       $items_result = get_customer_trans_details($type, $type_no);
 
-       $deliveries = get_parent_trans($type, $type_no);
+       $deliveries = get_sales_parent_numbers($type, $type_no);
 
        if ($deliveries !== 0) {
-               $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
-               while ($row = db_fetch($items_result)) {
-                       $src_line = db_fetch($srcdetails);
-                       update_parent_line($type, $src_line['id'], -$row['quantity']);
+               if (count($deliveries) == 1 && get_reference(ST_CUSTDELIVERY, $deliveries[0]) == "auto")
+               {
+                       void_sales_delivery(ST_CUSTDELIVERY, $deliveries[0], false);
+                       $date_ = Today();
+                       add_audit_trail(ST_CUSTDELIVERY, $deliveries[0], $date_, _("Voided."));
+                       add_voided_entry(ST_CUSTDELIVERY, $deliveries[0], $date_, "");
+               }
+               else
+               {
+                       
+                       $srcdetails = get_sales_parent_lines($type, $type_no);
+                       while ($row = db_fetch($src_details)) {
+                               update_parent_line($type, $row['id'], -$row['quantity']);
+                       }
                }
        }
        // clear details after they've been reversed in the sales order