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