+ foreach ($taxes as $taxitem) {
+ $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
+ $ov_gst += $taxitem['Value'];
+ }
+
+ if($invoice->tax_included==0) {
+ $items_added_tax = $ov_gst-$freight_tax;
+ $freight_added_tax = $freight_tax;
+ } else {
+ $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, $invoice->ship_via,
+ $invoice->due_date, $alloc, 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) {
+
+ $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
+ $invoice_line->price, 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->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->src_id,
+ $trans_no ? $invoice_line->id : 0);
+
+ // Update delivery items for the quantity invoiced
+ if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
+ update_parent_line(ST_SALESINVOICE, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
+
+ if ($invoice_line->qty_dispatched != 0) {
+ $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
+
+ if ($invoice_line->line_price() != 0) {