3 //----------------------------------------------------------------------------------------
4 // if ($writeoff_acc==0) return goods into $cart->Location
5 // if src_docs!=0 => credit invoice else credit note
7 function write_credit_note($credit_note, $write_off_acc)
9 $credit_invoice = is_array($credit_note->src_docs) ?
10 reset(array_keys($credit_note->src_docs)) : $credit_note->src_docs;
12 $credit_date = $credit_note->document_date;
13 $tax_group_id = $credit_note->tax_group_id;
15 $trans_no = $credit_note->trans_no;
16 if (is_array($trans_no)) {
17 $trans_no = key($trans_no);
20 $credit_type = $write_off_acc == 0 ? 'Return' : 'WriteOff';
24 $company_data = get_company_prefs();
25 $branch_data = get_branch_accounts($credit_note->Branch);
27 $credit_note_total = $credit_note->get_items_total_dispatch();
28 $freight_tax = $credit_note->get_shipping_tax();
30 $taxes = $credit_note->get_taxes();
33 foreach ($taxes as $taxitem) {
34 $tax_total += $taxitem['Value'];
37 if ($credit_note->tax_included == 0) {
38 $items_added_tax = $tax_total-$freight_tax;
39 $freight_added_tax = $freight_tax;
42 $freight_added_tax = 0;
44 // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
45 // the transaction will be settled at once.
46 if (is_bank_account($branch_data['receivables_account']))
47 $alloc = $credit_note_total + $items_added_tax + $credit_note->freight_cost + $freight_added_tax;
51 // $sales_order=$invoice->order_no; //?
52 // if (is_array($sales_order)) $sales_order = $sales_order[0]; //?
53 if (!isset($credit_note->order_no))
54 $credit_note->order_no = 0;
56 /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
57 // all amounts in debtor's currency
58 $credit_no = write_customer_trans(11, $trans_no, $credit_note->customer_id,
59 $credit_note->Branch, $credit_date, $credit_note->reference,
60 $credit_note_total, 0, $items_added_tax,
61 $credit_note->freight_cost, $freight_added_tax,
62 $credit_note->sales_type,
63 $credit_note->order_no, $credit_invoice, $credit_note->ship_via,
64 null, $alloc); // 2008-06-14 extra $alloc
67 $credit_note->trans_no = array($credit_no=>0);
68 set_document_parent($credit_note);
70 delete_comments(11, $credit_no);
71 void_cust_allocations(11, $credit_no, $credit_date);
72 void_gl_trans(11, $credit_no, true);
73 void_stock_move(11, $credit_no);
74 void_customer_trans_tax_details(11, $credit_no);
76 if ($credit_invoice) {
77 $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $credit_invoice);
78 update_customer_trans_version(get_parent_type(11), $credit_note->src_docs );
79 if ($invoice_alloc_balance > 0) { //the invoice is not already fully allocated
80 $total = $credit_note_total + $credit_note->freight_cost +
81 $items_added_tax + $freight_added_tax;
83 $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
84 /*Now insert the allocation record if > 0 */
85 if ($allocate_amount != 0) {
86 update_debtor_trans_allocation(10, $credit_invoice, $allocate_amount);
87 update_debtor_trans_allocation(11, $credit_no, $allocate_amount); // ***
88 add_cust_allocation($allocate_amount, 11, $credit_no, 10, $credit_invoice);
89 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
91 exchange_variation(11, $credit_no, 10, $credit_invoice, $credit_date,
92 $allocate_amount, payment_person_types::customer());
94 ///////////////////////////////////////////////////////////////////////////
101 foreach ($credit_note->line_items as $credit_line) {
103 if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
104 update_parent_line(11, $credit_line->src_id,($credit_line->qty_dispatched
105 -$credit_line->qty_old));
108 $line_taxfree_price = get_tax_free_price_for_item($credit_line->stock_id, $credit_line->price,
109 0, $credit_note->tax_included, $credit_note->tax_group_array);
111 $line_tax = get_full_price_for_item($credit_line->stock_id, $credit_line->price,
112 0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
114 write_customer_trans_detail_item(11, $credit_no, $credit_line->stock_id,
115 $credit_line->item_description, $credit_line->qty_dispatched,
116 $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
117 $credit_line->standard_cost, $trans_no==0 ? 0: $credit_line->id);
119 add_credit_movements_item($credit_note, $credit_line,
120 $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
122 add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
123 $credit_date, $credit_type, $write_off_acc, $branch_data);
124 } /*end of credit_line loop */
126 /*Post credit note transaction to GL credit debtors,
127 debit freight re-charged and debit sales */
129 if (($credit_note_total + $credit_note->freight_cost) != 0) {
131 add_gl_trans_customer(11, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
132 -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
133 $credit_note->customer_id,
134 "The total debtor GL posting for the credit note could not be inserted");
137 if ($credit_note->freight_cost !=0) {
138 add_gl_trans_customer(11, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
139 $credit_note->get_tax_free_shipping(), $credit_note->customer_id,
140 "The freight GL posting for this credit note could not be inserted");
143 foreach ($taxes as $taxitem) {
144 if ($taxitem['Value'] != 0) {
146 add_customer_trans_tax_detail_item(11, $credit_no, $taxitem['tax_type_id'],
147 $taxitem['rate'], $credit_note->tax_included, $taxitem['Value']);
149 add_gl_trans_customer(11, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
150 $taxitem['Value'], $credit_note->customer_id,
151 "A tax GL posting for this credit note could not be inserted");
154 add_comments(11, $credit_no, $credit_date, $credit_note->Comments);
156 if ($trans_no == 0) {
157 references::save_last($credit_note->reference, 11);
160 commit_transaction();
165 //----------------------------------------------------------------------------------------
166 // Insert a stock movement coming back in to show the credit note and
167 // a reversing stock movement to show the write off
169 function add_credit_movements_item(&$credit_note, &$credit_line,
170 $credit_type, $price, $credited_invoice=0)
173 if ($credit_type == "Return") {
175 $reference = "Return ";
176 if ($credited_invoice) {
177 $reference .= "Ex Inv: " . $credited_invoice;
180 } elseif ($credit_type == "WriteOff") {
182 $reference = "WriteOff ";
183 if ($credited_invoice)
184 $reference .= "Ex Inv: " . $credited_invoice;
186 add_stock_move_customer(11, $credit_line->stock_id,
187 key($credit_note->trans_no), $credit_note->Location,
188 $credit_note->document_date, $reference, -$credit_line->qty_dispatched,
189 $credit_line->standard_cost, 0, $price,
190 $credit_line->discount_percent);
193 add_stock_move_customer(11, $credit_line->stock_id,
194 key($credit_note->trans_no), $credit_note->Location,
195 $credit_note->document_date, $reference, $credit_line->qty_dispatched,
196 $credit_line->standard_cost, 0, $price,
197 $credit_line->discount_percent);
201 //----------------------------------------------------------------------------------------
203 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
204 $credit_type, $write_off_gl_code, &$branch_data)
206 $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
207 $customer = get_customer($order->customer_id);
208 // 2008-08-01. If there is a Customer Dimension, then override with this,
209 // else take the Item Dimension (if any)
210 $dim = ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_codes["dimension_id"]);
211 $dim2 = ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_codes["dimension2_id"]);
213 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
214 $standard_cost = get_standard_cost($order_line->stock_id);
215 if ($standard_cost != 0) {
216 /*first the cost of sales entry*/
218 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_gl_codes["cogs_account"],
219 $dim, $dim2, "", -($standard_cost * $order_line->qty_dispatched),
220 payment_person_types::customer(), $order->customer_id,
221 "The cost of sales GL posting could not be inserted");
223 /*now the stock entry*/
224 if ($credit_type == "WriteOff") {
225 $stock_entry_account = $write_off_gl_code;
227 $stock_gl_code = get_stock_gl_code($order_line->stock_id);
228 $stock_entry_account = $stock_gl_code["inventory_account"];
231 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_entry_account, 0, 0,
232 "", ($standard_cost * $order_line->qty_dispatched),
233 payment_person_types::customer(), $order->customer_id,
234 "The stock side (or write off) of the cost of sales GL posting could not be inserted");
236 } /* end of if GL and stock integrated and standard cost !=0 */
238 if ($order_line->line_price() != 0) {
240 $line_taxfree_price =
241 get_tax_free_price_for_item($order_line->stock_id, $order_line->price,
242 0, $order->tax_included, $order->tax_group_array);
244 $line_tax = get_full_price_for_item($order_line->stock_id, $order_line->price,
245 0, $order->tax_included, $order->tax_group_array) - $line_taxfree_price;
248 //Post sales transaction to GL credit sales
250 // 2008-06-14. If there is a Branch Sales Account, then override with this,
251 // else take the Item Sales Account
252 if ($branch_data['sales_account'] != "")
253 $sales_account = $branch_data['sales_account'];
255 $sales_account = $stock_gl_codes['sales_account'];
256 add_gl_trans_customer(11, $credit_no, $date_, $sales_account, $dim, $dim2,
257 ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
258 "The credit note GL posting could not be inserted");
260 if ($order_line->discount_percent != 0) {
262 add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"],
263 $dim, $dim2, -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
265 "The credit note discount GL posting could not be inserted");
267 } /*end of if discount !=0 */
268 } /*if line_price!=0 */