Editable sales terms in sales orders and invoices.
[fa-stable.git] / sales / includes / db / sales_credit_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 // if ($writeoff_acc==0) return goods into $cart->Location
14 // if src_docs!=0 => credit invoice else credit note
15 //
16 function write_credit_note($credit_note, $write_off_acc)
17 {
18         global $Refs;
19
20         $credit_invoice =       is_array($credit_note->src_docs) ?
21                  reset(array_keys($credit_note->src_docs)) : $credit_note->src_docs;
22
23         $credit_date = $credit_note->document_date;
24         $tax_group_id = $credit_note->tax_group_id;
25
26         $trans_no = $credit_note->trans_no;
27         if (is_array($trans_no)) {
28                 $trans_no = key($trans_no);
29         }
30
31         $credit_type = $write_off_acc == 0 ? 'Return' : 'WriteOff';
32
33         begin_transaction();
34
35         $company_data = get_company_prefs();
36         $branch_data = get_branch_accounts($credit_note->Branch);
37
38         $credit_note_total = $credit_note->get_items_total_dispatch();
39         $freight_tax = $credit_note->get_shipping_tax();
40
41         $taxes = $credit_note->get_taxes();
42
43         $tax_total = 0;
44         foreach ($taxes as $taxitem) {
45                 $taxitem['Value'] =  round2($taxitem['Value'], user_price_dec());
46                 $tax_total += $taxitem['Value'];
47         }
48
49         if ($credit_note->tax_included == 0) {
50             $items_added_tax = $tax_total-$freight_tax;
51             $freight_added_tax = $freight_tax;
52         } else {
53             $items_added_tax = 0;
54             $freight_added_tax = 0;
55         }
56         // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
57         // the transaction will be settled at once.
58         if (is_bank_account($branch_data['receivables_account']))
59                 $alloc = $credit_note_total + $items_added_tax + $credit_note->freight_cost + $freight_added_tax;
60         else
61                 $alloc = 0;
62
63 //      $sales_order=$invoice->order_no;        //?
64 //    if (is_array($sales_order)) $sales_order = $sales_order[0]; //?
65         if (!isset($credit_note->order_no))
66                 $credit_note->order_no = 0;
67
68         /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
69         // all amounts in debtor's currency
70         $credit_no = write_customer_trans(ST_CUSTCREDIT, $trans_no, $credit_note->customer_id,
71                 $credit_note->Branch, $credit_date, $credit_note->reference,
72                 $credit_note_total, 0, $items_added_tax,
73                 $credit_note->freight_cost, $freight_added_tax,
74                 $credit_note->sales_type,
75                 $credit_note->order_no, $credit_invoice, $credit_note->ship_via,
76                 null, $alloc, 0, $credit_note->dimension_id, $credit_note->dimension2_id,
77                 $credit_note->payment); 
78                 // 2008-06-14 extra $alloc, 2008-11-12 dimension_id Joe Hunt
79
80         if ($trans_no==0) {
81                 $credit_note->trans_no = array($credit_no=>0);
82                 set_document_parent($credit_note);
83         } else {
84                 delete_comments(ST_CUSTCREDIT, $credit_no);
85                 void_cust_allocations(ST_CUSTCREDIT, $credit_no, $credit_date);
86                 void_gl_trans(ST_CUSTCREDIT, $credit_no, true);
87                 void_stock_move(ST_CUSTCREDIT, $credit_no);
88                 void_trans_tax_details(ST_CUSTCREDIT, $credit_no);
89         }
90         if ($credit_invoice) {
91                 $invoice_alloc_balance = get_DebtorTrans_allocation_balance(ST_SALESINVOICE, $credit_invoice);
92                 update_customer_trans_version(get_parent_type(ST_CUSTCREDIT), $credit_note->src_docs );
93                 if ($invoice_alloc_balance > 0) {       //the invoice is not already fully allocated 
94                         $total = $credit_note_total + $credit_note->freight_cost +
95                             $items_added_tax + $freight_added_tax;
96
97                         $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
98                         /*Now insert the allocation record if > 0 */
99                         if ($allocate_amount != 0) {
100                                 update_debtor_trans_allocation(ST_SALESINVOICE, $credit_invoice, $allocate_amount);
101                                 update_debtor_trans_allocation(ST_CUSTCREDIT, $credit_no, $allocate_amount); // ***
102                                 add_cust_allocation($allocate_amount, ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $credit_invoice);
103                                 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
104
105                                 exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $credit_invoice, $credit_date,
106                                         $allocate_amount, PT_CUSTOMER);
107
108                                 ///////////////////////////////////////////////////////////////////////////
109
110                         }
111                 }
112         }
113
114         $total = 0;
115         foreach ($credit_note->line_items as $credit_line) {
116
117                 if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
118                         update_parent_line(11, $credit_line->src_id,($credit_line->qty_dispatched
119                                         -$credit_line->qty_old));
120                 }
121
122                 $line_taxfree_price = get_tax_free_price_for_item($credit_line->stock_id, $credit_line->price,
123                   0, $credit_note->tax_included, $credit_note->tax_group_array);
124
125                 $line_tax = get_full_price_for_item($credit_line->stock_id, $credit_line->price,
126                   0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
127
128                 write_customer_trans_detail_item(ST_CUSTCREDIT, $credit_no, $credit_line->stock_id,
129                         $credit_line->item_description, $credit_line->qty_dispatched,
130                         $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
131                         $credit_line->standard_cost, $trans_no==0 ? 0:  $credit_line->id);
132
133                 add_credit_movements_item($credit_note, $credit_line,
134                         $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
135
136                 $total += add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
137                     $credit_date, $credit_type, $write_off_acc, $branch_data);
138         } /*end of credit_line loop */
139
140         /*Post credit note transaction to GL credit debtors,
141         debit freight re-charged and debit sales */
142
143         if (($credit_note_total + $credit_note->freight_cost) != 0) {
144
145                 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
146                         -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
147                         $credit_note->customer_id,
148                         "The total debtor GL posting for the credit note could not be inserted");
149         }
150
151         if ($credit_note->freight_cost != 0) {
152                 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
153                         $credit_note->get_tax_free_shipping(), $credit_note->customer_id,
154                         "The freight GL posting for this credit note could not be inserted");
155         }
156
157         foreach ($taxes as $taxitem) {
158                 if ($taxitem['Net'] != 0) {
159
160                         $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($credit_note->customer_id), $credit_note->document_date);
161                         add_trans_tax_details(ST_CUSTCREDIT, $credit_no, $taxitem['tax_type_id'],
162                                 $taxitem['rate'], $credit_note->tax_included, $taxitem['Value'],
163                                 $taxitem['Net'], $ex_rate,
164                                 $credit_note->document_date, $credit_note->reference);
165
166                         $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
167                                 $taxitem['Value'], $credit_note->customer_id,
168                                 "A tax GL posting for this credit note could not be inserted");
169                 }
170         }
171         /*Post a balance post if $total != 0 */
172         add_gl_balance(ST_CUSTCREDIT, $credit_no, $credit_date, -$total, PT_CUSTOMER, $credit_note->customer_id);       
173
174         add_comments(ST_CUSTCREDIT, $credit_no, $credit_date, $credit_note->Comments);
175
176         if ($trans_no == 0) {
177                 $Refs->save(ST_CUSTCREDIT, $credit_no, $credit_note->reference);
178         }
179
180         commit_transaction();
181
182         return $credit_no;
183 }
184
185 //----------------------------------------------------------------------------------------
186 // Insert a stock movement coming back in to show the credit note and
187 //      a reversing stock movement to show the write off
188 //
189 function add_credit_movements_item(&$credit_note, &$credit_line,
190         $credit_type, $price, $credited_invoice=0)
191 {
192
193         if ($credit_type == "Return") {
194
195                 $reference = "Return ";
196                 if ($credited_invoice) {
197                         $reference .= "Ex Inv: " . $credited_invoice;
198                 }
199
200         } elseif ($credit_type == "WriteOff") {
201
202                 $reference = "WriteOff ";
203                 if ($credited_invoice)
204                         $reference .= "Ex Inv: " . $credited_invoice;
205
206         add_stock_move_customer(ST_CUSTCREDIT,  $credit_line->stock_id,
207                 key($credit_note->trans_no), $credit_note->Location,
208                 $credit_note->document_date, $reference, -$credit_line->qty_dispatched,
209                 $credit_line->standard_cost,  0, $price,
210                 $credit_line->discount_percent);
211
212                 }
213         add_stock_move_customer(ST_CUSTCREDIT,  $credit_line->stock_id,
214                 key($credit_note->trans_no), $credit_note->Location,
215                 $credit_note->document_date, $reference, $credit_line->qty_dispatched,
216                 $credit_line->standard_cost,  0, $price,
217                 $credit_line->discount_percent);
218
219         }
220
221 //----------------------------------------------------------------------------------------
222
223 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
224         $credit_type, $write_off_gl_code, &$branch_data)
225 {
226         $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
227         $customer = get_customer($order->customer_id);
228         // 2008-08-01. If there is a Customer Dimension, then override with this,
229         // else take the Item Dimension (if any)
230         $dim = ($order->dimension_id != $customer['dimension_id'] ? $order->dimension_id : 
231                 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_codes["dimension_id"]));
232         $dim2 = ($order->dimension2_id != $customer['dimension2_id'] ? $order->dimension2_id : 
233                 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_codes["dimension2_id"]));
234
235         $total = 0;
236         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
237         $standard_cost = get_standard_cost($order_line->stock_id);
238         if ($standard_cost != 0) {
239                 /*first the cost of sales entry*/
240
241                 $total += add_gl_trans_std_cost(ST_CUSTCREDIT, $credit_no, $date_, $stock_gl_codes["cogs_account"],
242                         $dim, $dim2, "", -($standard_cost * $order_line->qty_dispatched),
243                         PT_CUSTOMER, $order->customer_id,
244                         "The cost of sales GL posting could not be inserted");
245
246                 /*now the stock entry*/
247                 if ($credit_type == "WriteOff") {
248                         $stock_entry_account = $write_off_gl_code;
249                 } else {
250                         $stock_gl_code = get_stock_gl_code($order_line->stock_id);
251                         $stock_entry_account = $stock_gl_code["inventory_account"];
252                 }
253
254                 $total += add_gl_trans_std_cost(ST_CUSTCREDIT, $credit_no, $date_, $stock_entry_account, 0, 0,
255                         "", ($standard_cost * $order_line->qty_dispatched),
256                         PT_CUSTOMER, $order->customer_id,
257                         "The stock side (or write off) of the cost of sales GL posting could not be inserted");
258
259         } /* end of if GL and stock integrated and standard cost !=0 */
260
261         if ($order_line->line_price() != 0) {
262
263                 $line_taxfree_price =
264                 get_tax_free_price_for_item($order_line->stock_id, $order_line->price,
265                   0, $order->tax_included, $order->tax_group_array);
266
267                 $line_tax = get_full_price_for_item($order_line->stock_id, $order_line->price,
268                   0, $order->tax_included, $order->tax_group_array) - $line_taxfree_price;
269
270
271                 //Post sales transaction to GL credit sales
272
273                 // 2008-06-14. If there is a Branch Sales Account, then override with this,
274                 // else take the Item Sales Account
275                 if ($branch_data['sales_account'] != "")
276                         $sales_account = $branch_data['sales_account'];
277                 else
278                         $sales_account = $stock_gl_codes['sales_account'];
279                 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $date_, $sales_account, $dim, $dim2,
280                         ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
281                         "The credit note GL posting could not be inserted");
282
283                 if ($order_line->discount_percent != 0) {
284
285                         $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $date_, $branch_data["sales_discount_account"],
286                                 $dim, $dim2, -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
287                                 $order->customer_id,
288                                 "The credit note discount GL posting could not be inserted");
289
290                 } /*end of if discount !=0 */
291         } /*if line_price!=0 */
292         return $total;
293 }
294
295 ?>