2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-----------------------------------------------------------------------------
13 // Add or update Sales Invoice
15 function write_sales_invoice(&$invoice)
19 $trans_no = $invoice->trans_no;
20 if (is_array($trans_no))
21 $trans_no = key($trans_no);
23 $date_ = $invoice->document_date;
24 $charge_shipping =$invoice->freight_cost;
28 $company_data = get_company_prefs();
30 $branch_data = get_branch_accounts($invoice->Branch);
32 $customer = get_customer($invoice->customer_id);
34 // offer price values without freight costs
35 $items_total = $invoice->get_items_total_dispatch();
36 $freight_tax = $invoice->get_shipping_tax();
38 $delivery_no = $invoice->src_docs;
39 if (is_array($delivery_no))
42 update_customer_trans_version(get_parent_type(ST_SALESINVOICE), $invoice->src_docs);
45 $taxes = $invoice->get_taxes(); // all taxes with freight_tax
47 foreach ($taxes as $taxitem) {
48 $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
49 $ov_gst += $taxitem['Value'];
52 if($invoice->tax_included==0) {
53 $items_added_tax = $ov_gst-$freight_tax;
54 $freight_added_tax = $freight_tax;
57 $freight_added_tax = 0;
59 // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
60 // the transaction will be settled at once.
61 if (is_bank_account($branch_data['receivables_account']))
62 $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
66 /* Insert/update the debtor_trans */
67 $sales_order = $invoice->order_no;
68 if (is_array($sales_order))
69 $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
71 $invoice_no = write_customer_trans(ST_SALESINVOICE, $trans_no, $invoice->customer_id,
72 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
73 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
74 $invoice->sales_type, $sales_order, $delivery_no,
75 $invoice->ship_via, $invoice->due_date, $alloc, 0, $invoice->dimension_id,
76 $invoice->dimension2_id, $invoice->payment);
77 // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
80 $invoice->trans_no = array($invoice_no=>0);
81 set_document_parent($invoice);
83 delete_comments(ST_SALESINVOICE, $invoice_no);
84 void_gl_trans(ST_SALESINVOICE, $invoice_no, true);
85 void_cust_allocations(ST_SALESINVOICE, $invoice_no); // ?
86 void_trans_tax_details(ST_SALESINVOICE, $invoice_no);
89 foreach ($invoice->line_items as $line_no => $invoice_line) {
91 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
92 $invoice_line->price, 0, $invoice->tax_included,
93 $invoice->tax_group_array);
95 $line_tax = get_full_price_for_item($invoice_line->stock_id,
96 $invoice_line->price, 0, $invoice->tax_included,
97 $invoice->tax_group_array) - $line_taxfree_price;
99 write_customer_trans_detail_item(ST_SALESINVOICE, $invoice_no, $invoice_line->stock_id,
100 $invoice_line->item_description, $invoice_line->qty_dispatched,
101 $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
102 $invoice_line->standard_cost,
103 $trans_no ? $invoice_line->id : 0);
105 // Update delivery items for the quantity invoiced
106 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
107 update_parent_line(ST_SALESINVOICE, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
109 if ($invoice_line->qty_dispatched != 0) {
110 $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
112 if ($invoice_line->line_price() != 0) {
113 //Post sales transaction to GL credit sales
115 // 2008-06-14. If there is a Branch Sales Account, then override with this,
116 // else take the Item Sales Account
117 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
118 // 2008-08-01. If there is a Customer Dimension, then override with this,
119 // else take the Item Dimension (if any)
120 $dim = ($invoice->dimension_id != $customer['dimension_id'] ? $invoice->dimension_id :
121 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
122 $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id :
123 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
124 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $sales_account, $dim, $dim2,
125 (-$line_taxfree_price * $invoice_line->qty_dispatched),
126 $invoice->customer_id, "The sales price GL posting could not be inserted");
128 if ($invoice_line->discount_percent != 0) {
130 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_,
131 $branch_data["sales_discount_account"], $dim, $dim2,
132 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
133 $invoice->customer_id, "The sales discount GL posting could not be inserted");
134 } /*end of if discount !=0 */
136 } /*quantity dispatched is more than 0 */
137 } /*end of delivery_line loop */
139 if (($items_total + $charge_shipping) != 0) {
140 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
141 ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
142 $invoice->customer_id, "The total debtor GL posting could not be inserted");
145 if ($charge_shipping != 0) {
146 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
147 -$invoice->get_tax_free_shipping(), $invoice->customer_id,
148 "The freight GL posting could not be inserted");
151 foreach ($taxes as $taxitem) {
153 if ($taxitem['Net'] != 0) {
154 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($invoice->customer_id), $date_);
155 add_trans_tax_details(ST_SALESINVOICE, $invoice_no, $taxitem['tax_type_id'],
156 $taxitem['rate'], $invoice->tax_included, $taxitem['Value'],
157 $taxitem['Net'], $ex_rate, $date_, $invoice->reference);
159 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
160 (-$taxitem['Value']), $invoice->customer_id,
161 "A tax GL posting could not be inserted");
165 /*Post a balance post if $total != 0 */
166 add_gl_balance(ST_SALESINVOICE, $invoice_no, $date_, -$total, PT_CUSTOMER, $invoice->customer_id);
168 add_comments(10, $invoice_no, $date_, $invoice->Comments);
170 if ($trans_no == 0) {
171 $Refs->save(ST_SALESINVOICE, $invoice_no, $invoice->reference);
172 if ($invoice->cash) {
173 $amount = $items_total + $items_added_tax + $invoice->freight_cost
174 + $freight_added_tax;
176 // to use debtors.pmt_discount on cash sale:
177 // extend invoice entry page with final amount after discount
178 // and change line below.
179 $discount = 0; // $invoice->cash_discount*$amount;
180 $pmtno = write_customer_payment(0, $invoice->customer_id,
181 $invoice->Branch, $invoice->cash_account, $date_,
182 $Refs->get_next(ST_CUSTPAYMENT), $amount-$discount, $discount,
183 _('Cash invoice').' '.$invoice_no);
184 add_cust_allocation($amount, ST_CUSTPAYMENT, $pmtno, ST_SALESINVOICE, $invoice_no);
186 update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no, $amount);
187 update_debtor_trans_allocation(ST_CUSTPAYMENT, $pmtno, $amount);
190 commit_transaction();
195 //--------------------------------------------------------------------------------------------------
197 function void_sales_invoice($type, $type_no)
201 void_bank_trans($type, $type_no, true);
202 void_gl_trans($type, $type_no, true);
204 // reverse all the changes in parent document(s)
205 $items_result = get_customer_trans_details($type, $type_no);
207 $deliveries = get_parent_trans($type, $type_no);
209 if ($deliveries !== 0) {
210 $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
211 while ($row = db_fetch($items_result)) {
212 $src_line = db_fetch($srcdetails);
213 update_parent_line($type, $src_line['id'], -$row['quantity']);
216 // clear details after they've been reversed in the sales order
217 void_customer_trans_details($type, $type_no);
219 void_trans_tax_details($type, $type_no);
221 void_cust_allocations($type, $type_no);
223 // do this last because other voidings can depend on it - especially voiding
224 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
225 void_customer_trans($type, $type_no);
227 commit_transaction();