+ $credit_no = write_customer_trans(11, $trans_no, $credit_note->customer_id,
+ $credit_note->Branch, $credit_date, $credit_note->reference,
+ $credit_note_total, 0, $items_added_tax,
+ $credit_note->freight_cost, $freight_added_tax,
+ $credit_note->sales_type,
+ $credit_note->order_no, $credit_invoice, $credit_note->ship_via,
+ null, $alloc); // 2008-06-14 extra $alloc
+
+ if ($trans_no==0) {
+ $credit_note->trans_no = array($credit_no=>0);
+ set_document_parent($credit_note);
+ } else {
+ delete_comments(11, $credit_no);
+ void_cust_allocations(11, $credit_no, $credit_date);
+ void_gl_trans(11, $credit_no, true);
+ void_stock_move(11, $credit_no);
+ void_customer_trans_tax_details(11, $credit_no);
+ }
+ if ($credit_invoice) {
+ $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $credit_invoice);
+ update_customer_trans_version(get_parent_type(11), $credit_note->src_docs );
+ if ($invoice_alloc_balance > 0) { //the invoice is not already fully allocated
+ $total = $credit_note_total + $credit_note->freight_cost +
+ $items_added_tax + $freight_added_tax;
+
+ $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
+ /*Now insert the allocation record if > 0 */
+ if ($allocate_amount != 0) {
+ update_debtor_trans_allocation(10, $credit_invoice, $allocate_amount);
+ update_debtor_trans_allocation(11, $credit_no, $allocate_amount); // ***
+ add_cust_allocation($allocate_amount, 11, $credit_no, 10, $credit_invoice);
+ // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
+
+ exchange_variation(11, $credit_no, 10, $credit_invoice, $credit_date,
+ $allocate_amount, payment_person_types::customer());
+
+ ///////////////////////////////////////////////////////////////////////////
+
+ }
+ }
+ }
+
+
+ foreach ($credit_note->line_items as $credit_line) {
+
+ if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
+ update_parent_line(11, $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(11, $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, $trans_no==0 ? 0: $credit_line->id);
+
+ add_credit_movements_item($credit_note, $credit_line,
+ $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
+
+ 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) {
+
+ add_gl_trans_customer(11, $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) {
+ add_gl_trans_customer(11, $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['Value'] != 0) {
+
+ add_customer_trans_tax_detail_item(11, $credit_no, $taxitem['tax_type_id'],
+ $taxitem['rate'], $credit_note->tax_included, $taxitem['Value']);
+
+ add_gl_trans_customer(11, $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");
+ }
+ }
+ add_comments(11, $credit_no, $credit_date, $credit_note->Comments);
+
+ if ($trans_no == 0) {
+ references::save_last($credit_note->reference, 11);