+ /*
+ Write transaction GL postings, creating tax records and updating AP/AR/bank ledger if needed.
+ */
+ function write_gl($check_balance = true)
+ {
+ $delta = $this->gl_items_total();
+ if ($check_balance && floatcmp($delta, 0) !=0)
+ {
+ $this->add_gl_item(get_company_pref($delta>0 ? 'rounding_db_act' : 'rounding_cr_act'),
+ 0, 0, -$delta, '');
+ error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $delta, $this->trans_type, $this->order_id));
+ }
+
+ $bank_trans = $supp_trans = $cust_trans = array();
+ $total_gl = 0;
+ foreach($this->gl_items as $gl)
+ {
+ if (!isset($gl->date))
+ $gl->date = $this->tran_date;
+
+ $total_gl += add_gl_trans($this->trans_type, $this->order_id, $gl->date, $gl->code_id, $gl->dimension_id, $gl->dimension2_id,
+ $gl->reference, $gl->amount, $this->currency, $gl->person_type_id, $gl->person_id, "", $this->rate);
+
+ // post to first found bank account using given gl acount code.
+ $is_bank_to = is_bank_account($gl->code_id);
+ if ($is_bank_to && (get_bank_account_currency($is_bank_to) == $this->currency)) // do not register exchange variations in bank trans
+ {
+ if (!isset($bank_trans[$is_bank_to]))
+ $bank_trans[$is_bank_to] = 0;
+
+ $bank_trans[$is_bank_to] += $gl->amount;
+ } elseif ($gl->person_id)
+ {
+ $home_currency = get_company_currency();
+ // collect per counterparty amounts (in case more than one posting was done to the account),
+ // do not post exchange variations to AR/AP (journal in not customer/supplier currency)
+ if ($gl->person_type_id==PT_SUPPLIER && (get_supplier_currency($gl->person_id) == $this->currency || $this->currency != $home_currency))
+ $supp_trans[$gl->person_id] = @$supp_trans[$gl->person_id] + $gl->amount;
+ elseif ($gl->person_type_id==PT_CUSTOMER && (get_customer_currency(null, $gl->branch_id) == $this->currency || $this->currency != $home_currency))
+ $cust_trans[$gl->branch_id] = @$cust_trans[$gl->branch_id] + $gl->amount;
+ }
+
+ }
+ // post currency roundings if any
+ if ($check_balance && floatcmp($total_gl, 0))
+ add_gl_trans($this->trans_type, $this->order_id, $this->tran_date,
+ get_company_pref($total_gl>0 ? 'rounding_db_act' : 'rounding_cr_act'), 0, 0, _('Exchange rate roundings'), -$total_gl);
+
+ // update bank ledger if used
+ foreach($bank_trans as $bank_id => $amount)
+ add_bank_trans($this->trans_type, $this->order_id, $bank_id, $this->reference,
+ $this->tran_date, $amount, 0, "", $this->currency,
+ "Cannot insert a destination bank transaction");
+
+ // add AP/AR for journal transaction
+ if ($this->trans_type == ST_JOURNAL)
+ {
+ // update AR
+ foreach($cust_trans as $branch_id => $amount)
+ if (floatcmp($amount, 0))
+ write_cust_journal($this->trans_type, $this->order_id, $branch_id, $this->tran_date,
+ $this->reference, $amount, $this->rate);
+ // update AP
+ foreach($supp_trans as $supp_id => $amount)
+ if (floatcmp($amount, 0))
+ write_supp_journal($this->trans_type, $this->order_id, $supp_id, $this->tran_date,
+ $this->reference, -$amount, $this->rate, $this->source_ref);
+ }