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 hook_db_prewrite($invoice, ST_SALESINVOICE);
29 $company_data = get_company_prefs();
31 $branch_data = get_branch_accounts($invoice->Branch);
33 $customer = get_customer($invoice->customer_id);
35 add_new_exchange_rate($customer['curr_code'], $date_, $invoice->ex_rate);
37 // offer price values without freight costs
38 $items_total = $invoice->get_items_total_dispatch();
39 $freight_tax = $invoice->get_shipping_tax();
41 update_customer_trans_version(get_parent_type(ST_SALESINVOICE), $invoice->src_docs);
44 $taxes = $invoice->get_taxes(); // all taxes with freight_tax
46 foreach ($taxes as $taxitem) {
47 $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
48 $ov_gst += $taxitem['Value'];
51 if($invoice->tax_included==0) {
52 $items_added_tax = $ov_gst-$freight_tax;
53 $freight_added_tax = $freight_tax;
56 $freight_added_tax = 0;
59 /* Insert/update the debtor_trans */
60 $sales_order = $invoice->order_no;
61 if (is_array($sales_order))
62 $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
65 delete_comments(ST_SALESINVOICE, $trans_no);
66 void_gl_trans(ST_SALESINVOICE, $trans_no, true);
67 void_cust_allocations(ST_SALESINVOICE, $trans_no); // ?
68 void_trans_tax_details(ST_SALESINVOICE, $trans_no);
70 $invoice_no = write_customer_trans(ST_SALESINVOICE, $trans_no, $invoice->customer_id,
71 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
72 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
73 $invoice->sales_type, $sales_order, $invoice->ship_via,
74 $invoice->due_date, 0, 0, $invoice->dimension_id,
75 $invoice->dimension2_id, $invoice->payment);
76 // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
79 $invoice->trans_no = array($invoice_no=>0);
82 foreach ($invoice->line_items as $line_no => $invoice_line) {
83 $qty = $invoice_line->qty_dispatched;
84 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
85 $invoice_line->price * $qty, 0, $invoice->tax_included,
86 $invoice->tax_group_array);
88 $line_tax = get_full_price_for_item($invoice_line->stock_id,
89 $invoice_line->price * $qty, 0, $invoice->tax_included,
90 $invoice->tax_group_array) - $line_taxfree_price;
92 write_customer_trans_detail_item(ST_SALESINVOICE, $invoice_no, $invoice_line->stock_id,
93 $invoice_line->item_description, $invoice_line->qty_dispatched,
94 $invoice_line->line_price(), $qty ? $line_tax/$qty : 0, $invoice_line->discount_percent,
95 $invoice_line->standard_cost, $invoice_line->src_id,
96 $trans_no ? $invoice_line->id : 0);
98 // Update delivery items for the quantity invoiced
99 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
100 update_parent_line(ST_SALESINVOICE, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
102 if ($invoice_line->qty_dispatched != 0) {
103 $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
105 if ($invoice_line->line_price() != 0) {
106 //Post sales transaction to GL credit sales
108 // 2008-06-14. If there is a Branch Sales Account, then override with this,
109 // else take the Item Sales Account
110 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
111 // 2008-08-01. If there is a Customer Dimension, then override with this,
112 // else take the Item Dimension (if any)
113 $dim = ($invoice->dimension_id != $customer['dimension_id'] ? $invoice->dimension_id :
114 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
115 $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id :
116 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
117 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $sales_account, $dim, $dim2,
118 -$line_taxfree_price ,
119 $invoice->customer_id, "The sales price GL posting could not be inserted");
121 if ($invoice_line->discount_percent != 0) {
123 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_,
124 $branch_data["sales_discount_account"], $dim, $dim2,
125 ($line_taxfree_price * $invoice_line->discount_percent),
126 $invoice->customer_id, "The sales discount GL posting could not be inserted");
127 } /*end of if discount !=0 */
129 } /*quantity dispatched is more than 0 */
130 } /*end of delivery_line loop */
132 if (($items_total + $charge_shipping) != 0) {
133 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
134 ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
135 $invoice->customer_id, "The total debtor GL posting could not be inserted");
138 if ($charge_shipping != 0) {
139 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
140 -$invoice->get_tax_free_shipping(), $invoice->customer_id,
141 "The freight GL posting could not be inserted");
144 foreach ($taxes as $taxitem) {
146 if ($taxitem['Net'] != 0) {
147 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($invoice->customer_id), $date_);
148 add_trans_tax_details(ST_SALESINVOICE, $invoice_no, $taxitem['tax_type_id'],
149 $taxitem['rate'], $invoice->tax_included, $taxitem['Value'],
150 $taxitem['Net'], $ex_rate, $date_, $invoice->reference);
152 $total += add_gl_trans_customer(ST_SALESINVOICE, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
153 (-$taxitem['Value']), $invoice->customer_id,
154 "A tax GL posting could not be inserted");
158 /*Post a balance post if $total != 0 */
159 add_gl_balance(ST_SALESINVOICE, $invoice_no, $date_, -$total, PT_CUSTOMER, $invoice->customer_id);
161 add_comments(10, $invoice_no, $date_, $invoice->Comments);
163 if ($trans_no == 0) {
164 $Refs->save(ST_SALESINVOICE, $invoice_no, $invoice->reference);
165 if ($invoice->payment_terms['cash_sale'] && $invoice->pos['pos_account']) {
166 $amount = $items_total + $items_added_tax + $invoice->freight_cost
167 + $freight_added_tax;
169 // to use debtors.pmt_discount on cash sale:
170 // extend invoice entry page with final amount after discount
171 // and change line below.
172 $discount = 0; // $invoice->cash_discount*$amount;
173 $pmtno = write_customer_payment(0, $invoice->customer_id,
174 $invoice->Branch, $invoice->pos['pos_account'], $date_,
175 $Refs->get_next(ST_CUSTPAYMENT), $amount-$discount, $discount,
176 _('Cash invoice').' '.$invoice_no);
177 add_cust_allocation($amount, ST_CUSTPAYMENT, $pmtno, ST_SALESINVOICE, $invoice_no);
179 update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no, $amount);
180 update_debtor_trans_allocation(ST_CUSTPAYMENT, $pmtno, $amount);
183 hook_db_postwrite($invoice, ST_SALESINVOICE);
184 commit_transaction();
189 //--------------------------------------------------------------------------------------------------
191 function void_sales_invoice($type, $type_no)
195 hook_db_prevoid($type, $type_no);
196 void_bank_trans($type, $type_no, true);
197 void_gl_trans($type, $type_no, true);
199 // reverse all the changes in parent document(s)
201 $parents = get_sales_parent_numbers($type, $type_no);
203 if ($parents !== 0) {
204 if ($type == ST_SALESINVOICE && count($parents) == 1 && get_reference(ST_CUSTDELIVERY, $parents[0]) == "auto")
206 void_sales_delivery(ST_CUSTDELIVERY, $parents[0], false);
208 add_audit_trail(ST_CUSTDELIVERY, $parents[0], $date_, _("Voided."));
209 add_voided_entry(ST_CUSTDELIVERY, $parents[0], $date_, "");
213 $srcdetails = get_sales_parent_lines($type, $type_no);
214 while ($row = db_fetch($srcdetails)) {
215 update_parent_line($type, $row['id'], -$row['quantity']);
219 // clear details after they've been reversed in the sales order
220 void_customer_trans_details($type, $type_no);
222 void_stock_move($type, $type_no); // in case of credit note with return
224 void_trans_tax_details($type, $type_no);
226 void_cust_allocations($type, $type_no);
228 // do this last because other voidings can depend on it - especially voiding
229 // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
230 void_customer_trans($type, $type_no);
232 commit_transaction();