3 //-----------------------------------------------------------------------------
4 // Add or update Sales Invoice
6 function write_sales_invoice(&$invoice)
9 $trans_no = $invoice->trans_no;
10 if (is_array($trans_no))
11 $trans_no = key($trans_no);
13 $date_ = $invoice->document_date;
14 $charge_shipping =$invoice->freight_cost;
18 $company_data = get_company_prefs();
20 $branch_data = get_branch_accounts($invoice->Branch);
22 // offer price values without freight costs
23 $items_total = $invoice->get_items_total_dispatch();
24 $freight_tax = $invoice->get_shipping_tax();
26 $delivery_no = $invoice->src_docs;
27 if (is_array($delivery_no))
30 update_customer_trans_version(get_parent_type(10), $invoice->src_docs);
33 $taxes = $invoice->get_taxes(); // all taxes with freight_tax
35 foreach ($taxes as $taxitem) {
36 $ov_gst += $taxitem['Value'];
39 if($invoice->tax_included==0) {
40 $items_added_tax = $ov_gst-$freight_tax;
41 $freight_added_tax = $freight_tax;
44 $freight_added_tax = 0;
46 // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
47 // the transaction will be settled at once.
48 if (is_bank_account($branch_data['receivables_account']))
49 $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
53 /* Insert/update the debtor_trans */
54 $sales_order = $invoice->order_no;
55 if (is_array($sales_order))
56 $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
58 $invoice_no = write_customer_trans(10, $trans_no, $invoice->customer_id,
59 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
60 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
61 $invoice->sales_type, $sales_order, $delivery_no,
62 $invoice->ship_via, $invoice->due_date, $alloc); // 2008-06-14 extra $alloc
65 $invoice->trans_no = array($invoice_no=>0);
66 set_document_parent($invoice);
68 delete_comments(10, $invoice_no);
69 void_gl_trans(10, $invoice_no, true);
70 void_cust_allocations(10, $invoice_no); // ?
71 void_customer_trans_tax_details(10, $invoice_no);
74 foreach ($invoice->line_items as $invoice_line) {
76 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
77 $invoice_line->price, 0, $invoice->tax_included,
78 $invoice->tax_group_array);
80 $line_tax = get_full_price_for_item($invoice_line->stock_id,
81 $invoice_line->price, 0, $invoice->tax_included,
82 $invoice->tax_group_array) - $line_taxfree_price;
84 write_customer_trans_detail_item(10, $invoice_no, $invoice_line->stock_id,
85 $invoice_line->item_description, $invoice_line->qty_dispatched,
86 $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
87 $invoice_line->standard_cost,
88 $trans_no ? $invoice_line->id : 0);
90 // Update delivery items for the quantity invoiced
91 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
92 update_parent_line(10, $invoice_line->id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
94 if ($invoice_line->qty_dispatched != 0) {
95 $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
96 // 2008-06-14. If there is a Branch Sales Account, then override with this,
97 // else take the Item Sales Account
98 if ($branch_data['sales_account'] != "")
99 $sales_account = $branch_data['sales_account'];
101 $sales_account = $stock_gl_code['sales_account'];
103 if ($invoice_line->line_price() != 0) {
104 //Post sales transaction to GL credit sales
106 add_gl_trans_customer(10, $invoice_no, $date_, $sales_account,
107 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
108 (-$line_taxfree_price * $invoice_line->qty_dispatched),
109 $invoice->customer_id, "The sales price GL posting could not be inserted");
111 if ($invoice_line->discount_percent != 0) {
113 add_gl_trans_customer(10, $invoice_no, $date_,
114 $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
115 $stock_gl_code["dimension2_id"],
116 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
117 $invoice->customer_id, "The sales discount GL posting could not be inserted");
118 } /*end of if discount !=0 */
120 } /*quantity dispatched is more than 0 */
121 } /*end of delivery_line loop */
123 /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
124 if (($items_total + $charge_shipping) != 0) {
125 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
126 ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
127 $invoice->customer_id, "The total debtor GL posting could not be inserted");
129 if ($charge_shipping != 0) {
130 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
131 -$invoice->get_tax_free_shipping(), $invoice->customer_id,
132 "The freight GL posting could not be inserted");
135 foreach ($taxes as $taxitem) {
136 if ($taxitem['Value'] != 0) {
137 add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
138 $taxitem['rate'], $invoice->tax_included, $taxitem['Value']);
140 add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
141 (-$taxitem['Value']), $invoice->customer_id,
142 "A tax GL posting could not be inserted");
146 add_comments(10, $invoice_no, $date_, $invoice->Comments);
148 if ($trans_no == 0) {
149 references::save_last($invoice->reference, 10);
152 commit_transaction();
157 //--------------------------------------------------------------------------------------------------
159 function void_sales_invoice($type, $type_no)
163 void_bank_trans($type, $type_no, true);
164 void_gl_trans($type, $type_no, true);
166 // reverse all the changes in parent document(s)
167 $items_result = get_customer_trans_details($type, $type_no);
169 $deliveries = get_parent_trans($type, $type_no);
171 if (count($deliveries)) {
172 $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
173 while ($row = db_fetch($items_result)) {
174 $src_line = db_fetch($srcdetails);
175 update_parent_line(10, $src_line['id'], -$row['quantity']);
178 // clear details after they've been reversed in the sales order
179 void_customer_trans_details($type, $type_no);
181 void_customer_trans_tax_details($type, $type_no);
183 void_cust_allocations($type, $type_no);
185 // do this last because other voidings can depend on it - especially voiding
186 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
187 void_customer_trans($type, $type_no);
189 commit_transaction();