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