Dimension is missing in Supplier Invoice View, GL Rows.
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
index c4e97e4c6dd84545f5585e26fda8105c00f997c3..cd80b2cf939830a4d441f1cbb3711ddabf89d62d 100644 (file)
@@ -25,20 +25,19 @@ 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);
 
        $customer = get_customer($invoice->customer_id);
 
+       add_new_exchange_rate($customer['curr_code'], $date_, $invoice->ex_rate);
+
        // offer price values without freight costs
        $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,49 +55,44 @@ 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;
        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, $delivery_no,
-               $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) {
+         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);
-               set_document_parent($invoice);
-       } 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 $invoice_line) {
-
+       foreach ($invoice->line_items as $line_no => $invoice_line) {
+               $qty = $invoice_line->qty_dispatched;
                $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
-                       $invoice_line->price, 0, $invoice->tax_included,
+                       $invoice_line->price * $qty, 0, $invoice->tax_included,
                        $invoice->tax_group_array);
 
                $line_tax = get_full_price_for_item($invoice_line->stock_id,
-                       $invoice_line->price, 0, $invoice->tax_included,
+                       $invoice_line->price * $qty, 0, $invoice->tax_included,
                        $invoice->tax_group_array) - $line_taxfree_price;
 
                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->line_price(), $qty ? $line_tax/$qty : 0, $invoice_line->discount_percent,
+                       $invoice_line->standard_cost, $invoice_line->src_id,
                        $trans_no ? $invoice_line->id : 0);
 
                // Update delivery items for the quantity invoiced
@@ -121,14 +115,14 @@ function write_sales_invoice(&$invoice)
                                $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(ST_SALESINVOICE, $invoice_no, $date_, $sales_account, $dim, $dim2,
-                                       (-$line_taxfree_price * $invoice_line->qty_dispatched),
+                                       -$line_taxfree_price ,
                                        $invoice->customer_id, "The sales price GL posting could not be inserted");
 
                                if ($invoice_line->discount_percent != 0) {
 
                                        $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_,
                                                $branch_data["sales_discount_account"], $dim, $dim2,
-                                               ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
+                                               ($line_taxfree_price * $invoice_line->discount_percent),
                                                $invoice->customer_id, "The sales discount GL posting could not be inserted");
                                } /*end of if discount !=0 */
                        }
@@ -168,7 +162,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'] && $invoice->pos['pos_account']) {
                        $amount = $items_total + $items_added_tax + $invoice->freight_cost 
                                + $freight_added_tax;
 
@@ -177,7 +171,7 @@ function write_sales_invoice(&$invoice)
                        // and change line below.
                        $discount = 0; // $invoice->cash_discount*$amount;
                        $pmtno = write_customer_payment(0, $invoice->customer_id, 
-                               $invoice->Branch, $invoice->cash_account, $date_,
+                               $invoice->Branch, $invoice->pos['pos_account'], $date_,
                                $Refs->get_next(ST_CUSTPAYMENT), $amount-$discount, $discount,
                                _('Cash invoice').' '.$invoice_no);
                        add_cust_allocation($amount, ST_CUSTPAYMENT, $pmtno, ST_SALESINVOICE, $invoice_no);
@@ -186,6 +180,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,24 +192,35 @@ 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);
+       $parents = 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 ($parents !== 0) {
+               if ($type == ST_SALESINVOICE && count($parents) == 1 && get_reference(ST_CUSTDELIVERY, $parents[0]) == "auto")
+               {
+                       void_sales_delivery(ST_CUSTDELIVERY, $parents[0], false);
+                       $date_ = Today();
+                       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']);
+                       }
                }
        }
        // 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);