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