3 //----------------------------------------------------------------------------------------
5 function add_credit_note($credit_items, $credit_date, $credit_type, $tax_group_id,
6 $charge_freight, $sales_type, $shipper, $reference, $memo_, $write_off_gl_code)
10 $branch_data = get_branch_accounts($credit_items->Branch);
12 $credit_items_total = $credit_items->get_items_total($tax_group_id);
15 $taxes = $credit_items->get_taxes($tax_group_id, $charge_freight);
16 foreach ($taxes as $taxitem)
17 $tax_total += $taxitem['Value'];
19 $credit_no = add_customer_trans(11, $credit_items->customer_id, $credit_items->Branch,
20 $credit_date, $reference, -($credit_items_total), 0, -$tax_total, -$charge_freight,
21 $sales_type, 0, $credit_items->ship_via);
23 foreach ($credit_items->line_items as $credit_line)
26 if ($credit_line->quantity > 0)
28 // the functions use qty_dispatched - so until that's fixed make sure it's set
30 $credit_line->qty_dispatched = $credit_line->quantity;
32 add_gl_trans_credit_order_item($credit_items, $credit_line, $credit_no, $credit_date,
33 $tax_group_id, $credit_type);
35 add_gl_trans_credit_costs($credit_items, $credit_line, $credit_no, $credit_date,
36 $tax_group_id, $credit_type, $write_off_gl_code, $branch_data);
38 } /*quantity credited is more than 0 */
39 } /*end of credit_line loop */
41 /*Post credit note transaction to GL credit debtors,
42 debit freight re-charged and debit sales */
44 add_g_trans_credit_common($credit_items, $credit_no, $credit_date,
45 $credit_items_total, $charge_freight, $taxes, $tax_total, $branch_data);
47 add_comments(11, $credit_no, $credit_date, $memo_);
49 add_forms_for_sys_type(11, $credit_no, $credit_items->Location);
51 references::save_last($reference, 11);
58 //----------------------------------------------------------------------------------------
60 function credit_invoice($credit_items, $invoice_no, $order_no,
61 $default_dispatch_date, $credit_type, $tax_group_id,
62 $charge_freight, $reference, $memo_, $write_off_gl_code)
66 $branch_data = get_branch_accounts($credit_items->Branch);
68 $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $invoice_no);
70 $invoice_items_total = $credit_items->get_items_total_dispatch($tax_group_id);
73 $taxes = $credit_items->get_taxes($tax_group_id, $charge_freight);
74 foreach ($taxes as $taxitem)
75 $tax_total += $taxitem['Value'];
79 if ($invoice_alloc_balance > 0)
80 { /*the invoice is not already fully allocated */
82 if ($invoice_alloc_balance > ($invoice_items_total + $charge_freight + $tax_total))
85 $allocate_amount = $invoice_items_total + $charge_freight + $tax_total;
88 { /*the balance left to allocate is less than the credit note value */
89 $allocate_amount = $invoice_alloc_balance;
92 update_debtor_trans_allocation(10, $invoice_no, $allocate_amount);
95 /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
96 // all amounts in debtor's currency
98 $credit_no = add_customer_trans(11, $credit_items->customer_id, $credit_items->Branch,
99 $default_dispatch_date, $reference, -($invoice_items_total), 0, -$tax_total,
100 -$charge_freight, $credit_items->default_sales_type, $order_no,
101 $credit_items->ship_via, "", $allocate_amount);
103 /*Now insert the allocation record if > 0 */
104 if ($allocate_amount != 0)
106 add_cust_allocation($allocate_amount, 11, $credit_no, 10, $invoice_no);
109 /* Update sales order details quantity invoiced less this credit quantity. */
110 foreach ($credit_items->line_items as $order_line)
113 if ($order_line->qty_dispatched >0)
116 // always deduct the credited/returned quantities from the sales order
117 dispatch_sales_order_item($order_no, $order_line->id, -$order_line->qty_dispatched);
119 //if (($_POST['credit_type']=="Return") OR ($_POST['ItemsAction']==1))
121 $sql = "UPDATE ".TB_PREF."sales_order_details
122 SET qty_invoiced = qty_invoiced - " . $order_line->qty_dispatched . "
123 WHERE order_no = " . $order_no . "
124 AND id = '" . $order_line->id . "'";
125 $result = db_query($sql,"The sales order detail record could not be updated for the reduced quantity invoiced");
128 /*Now update sales_order_details for the quantity invoiced and the actual dispatch dates. */
130 add_gl_trans_credit_order_item($credit_items, $order_line, $credit_no,
131 $default_dispatch_date, $tax_group_id, $credit_type, $invoice_no);
133 add_gl_trans_credit_costs($credit_items, $order_line, $credit_no, $default_dispatch_date,
134 $tax_group_id, $credit_type, $write_off_gl_code, $branch_data);
136 } /*quantity dispatched is more than 0 */
137 } /*end of order_line loop */
139 add_g_trans_credit_common($credit_items, $credit_no, $default_dispatch_date,
140 $invoice_items_total, $charge_freight, $taxes, $tax_total, $branch_data);
142 add_comments(11, $credit_no, $default_dispatch_date, $memo_);
144 add_forms_for_sys_type(11, $credit_no);
146 references::save_last($reference, 11);
148 commit_transaction();
153 //----------------------------------------------------------------------------------------
155 function add_gl_trans_credit_order_item(&$order, &$order_line, $credit_no, $date_, $tax_group_id,
156 $credit_type, $credited_invoice=0)
158 $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
159 $line_tax = $order_line->full_price() - $line_taxfree_price;
161 if ($credit_type == "Return")
164 /* Insert stock movements for the stock coming back in - with unit cost */
167 if ($credited_invoice)
168 $reference .= "Ex Inv: " . $credited_invoice;
170 add_customer_trans_detail_item(11, $credit_no, $order_line->stock_id,
171 $order_line->item_description, $order->Location, $date_,
172 $order_line->qty_dispatched, $line_taxfree_price, $line_tax,
173 $order_line->discount_percent, $reference, $order_line->standard_cost);
176 elseif ($credit_type == "WriteOff")
178 /*Insert a stock movement coming back in to show the credit note and
179 a reversing stock movement to show the write off
180 no mods to location stock records*/
183 if ($credited_invoice)
184 $reference .= "Ex Inv: " . $credited_invoice;
186 add_customer_trans_detail_item_writeoff(11, $credit_no, $order_line->stock_id,
187 $order_line->item_description, $order->Location, $date_,
188 $order_line->qty_dispatched, $line_taxfree_price, $line_tax,
189 $order_line->discount_percent, $reference, $order_line->standard_cost);
193 //----------------------------------------------------------------------------------------
195 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_, $tax_group_id,
196 $credit_type, $write_off_gl_code, &$branch_data)
198 $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
200 /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
201 if ($order_line->standard_cost != 0)
204 /*first the cost of sales entry*/
206 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_gl_codes["cogs_account"],
207 $stock_gl_codes["dimension_id"], $stock_gl_codes["dimension2_id"], "",
208 -($order_line->standard_cost * $order_line->qty_dispatched),
209 payment_person_types::customer(), $order->customer_id,
210 "The cost of sales GL posting could not be inserted");
212 /*now the stock entry*/
213 if ($credit_type == "WriteOff")
215 $stock_entry_account = $write_off_gl_code;
219 $stock_gl_code = get_stock_gl_code($order_line->stock_id);
220 $stock_entry_account = $stock_gl_code["inventory_account"];
223 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_entry_account, 0, 0,
224 "", ($order_line->standard_cost * $order_line->qty_dispatched),
225 payment_person_types::customer(), $order->customer_id,
226 "The stock side (or write off) of the cost of sales GL posting could not be inserted");
228 } /* end of if GL and stock integrated and standard cost !=0 */
230 if ($order_line->price != 0)
233 $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
234 $line_tax = $order_line->full_price() - $line_taxfree_price;
236 //Post sales transaction to GL credit sales
238 add_gl_trans_customer(11, $credit_no, $date_, $stock_gl_codes["sales_account"], 0, 0,
239 ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
240 "The credit note GL posting could not be inserted");
242 if ($order_line->discount_percent != 0)
245 add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"], 0, 0,
246 -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
248 "The credit note discount GL posting could not be inserted");
250 } /*end of if discount !=0 */
251 } /*end of if sales integrated with debtors */
254 //----------------------------------------------------------------------------------------
256 function add_g_trans_credit_common($order, $credit_no, $date_,
257 $credit_items_total, $freight, $taxes, $tax_total, &$branch_data)
259 $company_data = get_company_prefs();
261 /*Post credit note transaction to GL credit debtors,
262 debit freight re-charged and debit sales */
264 if (($credit_items_total + $freight + $tax_total) != 0)
267 add_gl_trans_customer(11, $credit_no, $date_, $branch_data["receivables_account"], 0, 0,
268 -($credit_items_total + $freight + $tax_total), $order->customer_id,
269 "The total debtor GL posting for the credit note could not be inserted");
274 add_gl_trans_customer(11, $credit_no, $date_, $company_data["freight_act"], 0, 0,
275 $freight, $order->customer_id,
276 "The freight GL posting for this credit note could not be inserted");
279 foreach ($taxes as $taxitem)
281 if ($taxitem['Value'] != 0)
284 add_customer_trans_tax_detail_item(11, $credit_no, $taxitem['tax_type_id'],
285 $taxitem['rate'], $taxitem['included_in_price'], $taxitem['Value']);
287 add_gl_trans_customer(11, $credit_no, $date_, $taxitem['sales_gl_code'], 0, 0,
288 $taxitem['Value'], $order->customer_id,
289 "A tax GL posting for this credit note could not be inserted");
294 //----------------------------------------------------------------------------------------