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
16 function write_credit_note($credit_note, $write_off_acc)
18 $credit_invoice = is_array($credit_note->src_docs) ?
19 reset(array_keys($credit_note->src_docs)) : $credit_note->src_docs;
21 $credit_date = $credit_note->document_date;
22 $tax_group_id = $credit_note->tax_group_id;
24 $trans_no = $credit_note->trans_no;
25 if (is_array($trans_no)) {
26 $trans_no = key($trans_no);
29 $credit_type = $write_off_acc == 0 ? 'Return' : 'WriteOff';
33 $company_data = get_company_prefs();
34 $branch_data = get_branch_accounts($credit_note->Branch);
36 $credit_note_total = $credit_note->get_items_total_dispatch();
37 $freight_tax = $credit_note->get_shipping_tax();
39 $taxes = $credit_note->get_taxes();
42 foreach ($taxes as $taxitem) {
43 $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
44 $tax_total += $taxitem['Value'];
47 if ($credit_note->tax_included == 0) {
48 $items_added_tax = $tax_total-$freight_tax;
49 $freight_added_tax = $freight_tax;
52 $freight_added_tax = 0;
54 // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
55 // the transaction will be settled at once.
56 if (is_bank_account($branch_data['receivables_account']))
57 $alloc = $credit_note_total + $items_added_tax + $credit_note->freight_cost + $freight_added_tax;
61 // $sales_order=$invoice->order_no; //?
62 // if (is_array($sales_order)) $sales_order = $sales_order[0]; //?
63 if (!isset($credit_note->order_no))
64 $credit_note->order_no = 0;
66 /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
67 // all amounts in debtor's currency
68 $credit_no = write_customer_trans(11, $trans_no, $credit_note->customer_id,
69 $credit_note->Branch, $credit_date, $credit_note->reference,
70 $credit_note_total, 0, $items_added_tax,
71 $credit_note->freight_cost, $freight_added_tax,
72 $credit_note->sales_type,
73 $credit_note->order_no, $credit_invoice, $credit_note->ship_via,
74 null, $alloc, 0, $credit_note->dimension_id, $credit_note->dimension2_id);
75 // 2008-06-14 extra $alloc, 2008-11-12 dimension_id Joe Hunt
78 $credit_note->trans_no = array($credit_no=>0);
79 set_document_parent($credit_note);
81 delete_comments(11, $credit_no);
82 void_cust_allocations(11, $credit_no, $credit_date);
83 void_gl_trans(11, $credit_no, true);
84 void_stock_move(11, $credit_no);
85 void_trans_tax_details(11, $credit_no);
87 if ($credit_invoice) {
88 $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $credit_invoice);
89 update_customer_trans_version(get_parent_type(11), $credit_note->src_docs );
90 if ($invoice_alloc_balance > 0) { //the invoice is not already fully allocated
91 $total = $credit_note_total + $credit_note->freight_cost +
92 $items_added_tax + $freight_added_tax;
94 $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
95 /*Now insert the allocation record if > 0 */
96 if ($allocate_amount != 0) {
97 update_debtor_trans_allocation(10, $credit_invoice, $allocate_amount);
98 update_debtor_trans_allocation(11, $credit_no, $allocate_amount); // ***
99 add_cust_allocation($allocate_amount, 11, $credit_no, 10, $credit_invoice);
100 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
102 exchange_variation(11, $credit_no, 10, $credit_invoice, $credit_date,
103 $allocate_amount, payment_person_types::customer());
105 ///////////////////////////////////////////////////////////////////////////
112 foreach ($credit_note->line_items as $credit_line) {
114 if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
115 update_parent_line(11, $credit_line->src_id,($credit_line->qty_dispatched
116 -$credit_line->qty_old));
119 $line_taxfree_price = get_tax_free_price_for_item($credit_line->stock_id, $credit_line->price,
120 0, $credit_note->tax_included, $credit_note->tax_group_array);
122 $line_tax = get_full_price_for_item($credit_line->stock_id, $credit_line->price,
123 0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
125 write_customer_trans_detail_item(11, $credit_no, $credit_line->stock_id,
126 $credit_line->item_description, $credit_line->qty_dispatched,
127 $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
128 $credit_line->standard_cost, $trans_no==0 ? 0: $credit_line->id);
130 add_credit_movements_item($credit_note, $credit_line,
131 $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
133 $total += add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
134 $credit_date, $credit_type, $write_off_acc, $branch_data);
135 } /*end of credit_line loop */
137 /*Post credit note transaction to GL credit debtors,
138 debit freight re-charged and debit sales */
140 if (($credit_note_total + $credit_note->freight_cost) != 0) {
142 $total += add_gl_trans_customer(11, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
143 -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
144 $credit_note->customer_id,
145 "The total debtor GL posting for the credit note could not be inserted");
148 if ($credit_note->freight_cost !=0) {
149 $total += add_gl_trans_customer(11, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
150 $credit_note->get_tax_free_shipping(), $credit_note->customer_id,
151 "The freight GL posting for this credit note could not be inserted");
154 foreach ($taxes as $taxitem) {
155 if ($taxitem['Net'] != 0) {
157 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($credit_note->customer_id), $credit_note->document_date);
158 add_trans_tax_details(11, $credit_no, $taxitem['tax_type_id'],
159 $taxitem['rate'], $credit_note->tax_included, $taxitem['Value'],
160 $taxitem['Net'], $ex_rate,
161 $credit_note->document_date, $credit_note->reference);
163 $total += add_gl_trans_customer(11, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
164 $taxitem['Value'], $credit_note->customer_id,
165 "A tax GL posting for this credit note could not be inserted");
168 /*Post a balance post if $total != 0 */
169 add_gl_balance(11, $credit_no, $credit_date, -$total, payment_person_types::customer(), $credit_note->customer_id);
171 add_comments(11, $credit_no, $credit_date, $credit_note->Comments);
173 if ($trans_no == 0) {
174 references::save_last($credit_note->reference, 11);
177 commit_transaction();
182 //----------------------------------------------------------------------------------------
183 // Insert a stock movement coming back in to show the credit note and
184 // a reversing stock movement to show the write off
186 function add_credit_movements_item(&$credit_note, &$credit_line,
187 $credit_type, $price, $credited_invoice=0)
190 if ($credit_type == "Return") {
192 $reference = "Return ";
193 if ($credited_invoice) {
194 $reference .= "Ex Inv: " . $credited_invoice;
197 } elseif ($credit_type == "WriteOff") {
199 $reference = "WriteOff ";
200 if ($credited_invoice)
201 $reference .= "Ex Inv: " . $credited_invoice;
203 add_stock_move_customer(11, $credit_line->stock_id,
204 key($credit_note->trans_no), $credit_note->Location,
205 $credit_note->document_date, $reference, -$credit_line->qty_dispatched,
206 $credit_line->standard_cost, 0, $price,
207 $credit_line->discount_percent);
210 add_stock_move_customer(11, $credit_line->stock_id,
211 key($credit_note->trans_no), $credit_note->Location,
212 $credit_note->document_date, $reference, $credit_line->qty_dispatched,
213 $credit_line->standard_cost, 0, $price,
214 $credit_line->discount_percent);
218 //----------------------------------------------------------------------------------------
220 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
221 $credit_type, $write_off_gl_code, &$branch_data)
223 $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
224 $customer = get_customer($order->customer_id);
225 // 2008-08-01. If there is a Customer Dimension, then override with this,
226 // else take the Item Dimension (if any)
227 $dim = ($order->dimension_id != $customer['dimension_id'] ? $order->dimension_id :
228 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_codes["dimension_id"]));
229 $dim2 = ($order->dimension2_id != $customer['dimension2_id'] ? $order->dimension2_id :
230 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_codes["dimension2_id"]));
233 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
234 $standard_cost = get_standard_cost($order_line->stock_id);
235 if ($standard_cost != 0) {
236 /*first the cost of sales entry*/
238 $total += add_gl_trans_std_cost(11, $credit_no, $date_, $stock_gl_codes["cogs_account"],
239 $dim, $dim2, "", -($standard_cost * $order_line->qty_dispatched),
240 payment_person_types::customer(), $order->customer_id,
241 "The cost of sales GL posting could not be inserted");
243 /*now the stock entry*/
244 if ($credit_type == "WriteOff") {
245 $stock_entry_account = $write_off_gl_code;
247 $stock_gl_code = get_stock_gl_code($order_line->stock_id);
248 $stock_entry_account = $stock_gl_code["inventory_account"];
251 $total += add_gl_trans_std_cost(11, $credit_no, $date_, $stock_entry_account, 0, 0,
252 "", ($standard_cost * $order_line->qty_dispatched),
253 payment_person_types::customer(), $order->customer_id,
254 "The stock side (or write off) of the cost of sales GL posting could not be inserted");
256 } /* end of if GL and stock integrated and standard cost !=0 */
258 if ($order_line->line_price() != 0) {
260 $line_taxfree_price =
261 get_tax_free_price_for_item($order_line->stock_id, $order_line->price,
262 0, $order->tax_included, $order->tax_group_array);
264 $line_tax = get_full_price_for_item($order_line->stock_id, $order_line->price,
265 0, $order->tax_included, $order->tax_group_array) - $line_taxfree_price;
268 //Post sales transaction to GL credit sales
270 // 2008-06-14. If there is a Branch Sales Account, then override with this,
271 // else take the Item Sales Account
272 if ($branch_data['sales_account'] != "")
273 $sales_account = $branch_data['sales_account'];
275 $sales_account = $stock_gl_codes['sales_account'];
276 $total += add_gl_trans_customer(11, $credit_no, $date_, $sales_account, $dim, $dim2,
277 ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
278 "The credit note GL posting could not be inserted");
280 if ($order_line->discount_percent != 0) {
282 $total += add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"],
283 $dim, $dim2, -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
285 "The credit note discount GL posting could not be inserted");
287 } /*end of if discount !=0 */
288 } /*if line_price!=0 */