+ if (!credit_sales_invoice_allocate($credit_invoice, $credit_no, $total, $credit_date))
+ return false;
+ }
+
+ $total = 0;
+ foreach ($credit_note->line_items as $credit_line) {
+
+ if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
+ update_parent_line(ST_CUSTCREDIT, $credit_line->src_id,($credit_line->qty_dispatched
+ -$credit_line->qty_old));
+ }
+
+ $line_taxfree_price = get_tax_free_price_for_item($credit_line->stock_id, $credit_line->price,
+ 0, $credit_note->tax_included, $credit_note->tax_group_array);
+
+ $line_tax = get_full_price_for_item($credit_line->stock_id, $credit_line->price,
+ 0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
+
+ write_customer_trans_detail_item(ST_CUSTCREDIT, $credit_no, $credit_line->stock_id,
+ $credit_line->item_description, $credit_line->qty_dispatched,
+ $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
+ $credit_line->standard_cost, $credit_line->src_id, $trans_no==0 ? 0: $credit_line->id);
+
+ add_credit_movements_item($credit_note, $credit_line,
+ $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
+
+ $total += add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
+ $credit_date, $credit_type, $write_off_acc, $branch_data);
+ } /*end of credit_line loop */
+
+ /*Post credit note transaction to GL credit debtors,
+ debit freight re-charged and debit sales */
+
+ if (($credit_note_total + $credit_note->freight_cost) != 0) {
+
+ $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
+ -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
+ $credit_note->customer_id,
+ "The total debtor GL posting for the credit note could not be inserted");
+ }
+
+ if ($credit_note->freight_cost != 0) {
+ $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
+ $credit_note->get_tax_free_shipping(), $credit_note->customer_id,
+ "The freight GL posting for this credit note could not be inserted");
+ }
+
+ foreach ($taxes as $taxitem) {
+ if ($taxitem['Net'] != 0) {
+
+ $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($credit_note->customer_id), $credit_note->document_date);
+ add_trans_tax_details(ST_CUSTCREDIT, $credit_no, $taxitem['tax_type_id'],
+ $taxitem['rate'], $credit_note->tax_included, $taxitem['Value'],
+ $taxitem['Net'], $ex_rate,
+ $credit_note->document_date, $credit_note->reference);
+
+ $total += add_gl_trans_customer(ST_CUSTCREDIT, $credit_no, $credit_date, $taxitem['sales_gl_code'], 0, 0,
+ $taxitem['Value'], $credit_note->customer_id,
+ "A tax GL posting for this credit note could not be inserted");
+ }
+ }
+ /*Post a balance post if $total != 0 */
+ add_gl_balance(ST_CUSTCREDIT, $credit_no, $credit_date, -$total, PT_CUSTOMER, $credit_note->customer_id);
+
+ add_comments(ST_CUSTCREDIT, $credit_no, $credit_date, $credit_note->Comments);
+
+ if ($trans_no == 0) {
+ $Refs->save(ST_CUSTCREDIT, $credit_no, $credit_note->reference);
+ }
+ hook_db_postwrite($credit_note, ST_CUSTCREDIT);