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)
20 if (is_array($credit_note->src_docs))
22 $docs = array_keys($credit_note->src_docs);
23 $credit_invoice = reset($docs);
26 $credit_invoice = $credit_note->src_docs;
28 $credit_date = $credit_note->document_date;
29 $tax_group_id = $credit_note->tax_group_id;
31 $trans_no = $credit_note->trans_no;
32 if (is_array($trans_no)) {
33 $trans_no = key($trans_no);
36 $credit_type = $write_off_acc == 0 ? 'Return' : 'WriteOff';
39 hook_db_prewrite($credit_note, ST_CUSTCREDIT);
41 $company_data = get_company_prefs();
42 $branch_data = get_branch_accounts($credit_note->Branch);
44 add_new_exchange_rate(get_customer_currency($credit_note->customer_id), $credit_date, $credit_note->ex_rate);
46 $credit_note_total = $credit_note->get_items_total_dispatch();
47 $freight_tax = $credit_note->get_shipping_tax();
49 $taxes = $credit_note->get_taxes();
52 foreach ($taxes as $taxitem) {
53 $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
54 $tax_total += $taxitem['Value'];
57 if ($credit_note->tax_included == 0) {
58 $items_added_tax = $tax_total-$freight_tax;
59 $freight_added_tax = $freight_tax;
62 $freight_added_tax = 0;
64 // If the Customer Branch AR Account is set to a Bank Account,
65 // the transaction will be settled at once.
66 if (is_bank_account($branch_data['receivables_account']))
67 $alloc = $credit_note_total + $items_added_tax + $credit_note->freight_cost + $freight_added_tax;
71 if (!isset($credit_note->order_no))
72 $credit_note->order_no = 0;
74 /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
75 // all amounts in debtor's currency
76 $credit_no = write_customer_trans(ST_CUSTCREDIT, $trans_no, $credit_note->customer_id,
77 $credit_note->Branch, $credit_date, $credit_note->reference,
78 $credit_note_total, 0, $items_added_tax,
79 $credit_note->freight_cost, $freight_added_tax,
80 $credit_note->sales_type, $credit_note->order_no, $credit_note->ship_via,
81 null, $alloc, 0, $credit_note->dimension_id, $credit_note->dimension2_id,
82 $credit_note->payment, $credit_note->tax_included);
85 $credit_note->trans_no = array($credit_no=>0);
87 delete_comments(ST_CUSTCREDIT, $credit_no);
88 void_cust_allocations(ST_CUSTCREDIT, $credit_no, $credit_date);
89 void_gl_trans(ST_CUSTCREDIT, $credit_no, true);
90 void_stock_move(ST_CUSTCREDIT, $credit_no);
91 void_trans_tax_details(ST_CUSTCREDIT, $credit_no);
93 if ($credit_invoice) {
94 update_customer_trans_version(get_parent_type(ST_CUSTCREDIT), $credit_note->src_docs );
96 $total = $credit_note_total + $credit_note->freight_cost + $items_added_tax
99 if (!credit_sales_invoice_allocate($credit_invoice, $credit_no, $total, $credit_date))
104 $credit_note->split_line_values();
105 foreach ($credit_note->line_items as $line) {
107 if ($credit_invoice && $line->qty_dispatched!=$line->qty_old ) {
108 update_parent_line(ST_CUSTCREDIT, $line->src_id,($line->qty_dispatched
112 $line_taxfree_price = $line->gl_amounts['Net'];
114 $line_tax = $line->gl_amounts['Net'];
116 $line->unit_cost = get_unit_cost($line->stock_id);
118 write_customer_trans_detail_item(ST_CUSTCREDIT, $credit_no, $line->stock_id,
119 $line->item_description, $line->qty_dispatched,
120 $line->line_price(), $line_tax, $line->discount_percent,
121 $line->unit_cost, $line->src_id, $trans_no==0 ? 0: $line->id);
123 if ($credit_type == 'Return')
124 add_credit_movements_item($credit_note, $line,
125 $credit_type, ($line_taxfree_price+$line_tax)*(1-$line->discount_percent), $credit_invoice);
127 $total += add_gl_trans_credit_costs($credit_note, $line, $credit_no,
128 $credit_date, $credit_type, $write_off_acc, $branch_data);
129 } /*end of credit_line loop */
131 /*Post credit note transaction to GL credit debtors,
132 debit freight re-charged and debit sales */
134 if (($credit_note_total + $credit_note->freight_cost) != 0) {
136 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
137 -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
138 $credit_note->customer_id);
141 $freight = get_item($credit_note->ship_via);
143 if ($credit_note->freight_cost != 0) {
144 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $freight["freight_act"], 0, 0,
145 $credit_note->get_tax_free_shipping(), $credit_note->customer_id);
148 foreach ($taxes as $taxitem) {
149 if ($taxitem['Net'] != 0) {
151 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($credit_note->customer_id), $credit_note->document_date);
152 add_trans_tax_details(ST_CUSTCREDIT, $credit_no, $taxitem['tax_type_id'],
153 $taxitem['rate'], $credit_note->tax_included, $taxitem['Value'],
154 $taxitem['Net'], $ex_rate,
155 $credit_note->document_date, $credit_note->reference, TR_OUTPUT, $credit_note->tax_group_id);
157 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
158 $taxitem['Value'], $credit_note->customer_id);
161 /*Post a balance post if $total != 0 */
162 add_gl_balance(ST_CUSTCREDIT, $credit_no, $credit_date, -$total, PT_CUSTOMER, $credit_note->customer_id);
164 add_comments(ST_CUSTCREDIT, $credit_no, $credit_date, $credit_note->Comments);
166 if ($trans_no == 0) {
167 $Refs->save(ST_CUSTCREDIT, $credit_no, $credit_note->reference);
169 hook_db_postwrite($credit_note, ST_CUSTCREDIT);
171 commit_transaction();
175 //----------------------------------------------------------------------------------------
176 // Insert a stock movement coming back in to show the credit note and
177 // a reversing stock movement to show the write off
179 function add_credit_movements_item(&$credit_note, &$credit_line,
180 $credit_type, $price, $credited_invoice=0)
182 //Chaitanya : Stamp current cost in stock moves $line does not fetch cost
183 $curr_std_cost = get_unit_cost($credit_line->stock_id);
185 $reference = _("Return");
186 if ($credited_invoice)
187 $reference .= ' '._("Ex Inv:").' ' . $credited_invoice;
189 add_stock_move(ST_CUSTCREDIT, $credit_line->stock_id,
190 key($credit_note->trans_no), $credit_note->Location,
191 $credit_note->document_date, $reference, $credit_line->qty_dispatched,
192 $curr_std_cost, $price);
195 //----------------------------------------------------------------------------------------
197 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
198 $credit_type, $write_off_gl_code, &$branch_data)
200 $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
201 $customer = get_customer($order->customer_id);
202 // If there is a Customer Dimension, then override with this,
203 // else take the Item Dimension (if any)
204 $dim = ($order->dimension_id != $customer['dimension_id'] ? $order->dimension_id :
205 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_codes["dimension_id"]));
206 $dim2 = ($order->dimension2_id != $customer['dimension2_id'] ? $order->dimension2_id :
207 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_codes["dimension2_id"]));
210 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
211 $unit_cost = get_unit_cost($order_line->stock_id);
212 if ($unit_cost != 0) {
213 /*first the cost of sales entry*/
215 $total += add_gl_trans_std_cost(ST_CUSTCREDIT, $credit_no, $date_, $stock_gl_codes["cogs_account"],
216 $dim, $dim2, "", -($unit_cost * $order_line->qty_dispatched),
217 PT_CUSTOMER, $order->customer_id);
219 /*now the stock entry*/
220 if ($credit_type == "WriteOff") {
221 $stock_entry_account = $write_off_gl_code;
223 $stock_gl_code = get_stock_gl_code($order_line->stock_id);
224 $stock_entry_account = $stock_gl_code["inventory_account"];
227 $total += add_gl_trans_std_cost(ST_CUSTCREDIT, $credit_no, $date_, $stock_entry_account, 0, 0,
228 "", ($unit_cost * $order_line->qty_dispatched),
229 PT_CUSTOMER, $order->customer_id);
231 } /* end of if GL and stock integrated and standard cost !=0 */
233 if ($order_line->line_price() != 0) {
235 $line_taxfree_price = $order_line->gl_amounts['Net'];
237 $line_tax = $order_line->gl_amounts['Tax'];
240 //Post sales transaction to GL credit sales
242 // If there is a Branch Sales Account, then override with this,
243 // else take the Item Sales Account
244 if ($branch_data['sales_account'] != "")
245 $sales_account = $branch_data['sales_account'];
247 $sales_account = $stock_gl_codes['sales_account'];
248 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $date_, $sales_account, $dim, $dim2,
249 ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id);
251 if ($order_line->discount_percent != 0) {
253 $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $date_, $branch_data["sales_discount_account"],
254 $dim, $dim2, -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
255 $order->customer_id);
257 } /*end of if discount !=0 */
258 } /*if line_price!=0 */