3 //----------------------------------------------------------------------------------
4 function add_exchange_variation($trans_type, $trans_no, $date_, $account,
5 $currency, $person_type_id=null, $person_id = "")
7 if (is_company_currency($currency))
11 $rate = get_exchange_rate_from_home_currency($currency, $date_);
12 $result = db_query("SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE
13 bank_act='$account' AND trans_date<='".date2sql($date_)."'",
14 "Transactions for account $account could not be calculated");
15 $row = db_fetch_row($result);
16 $foreign_amount = $row[0];
17 $amount = get_gl_trans_from_to("", $date_, $account);
18 $diff = $amount - (round2($foreign_amount * $rate, user_price_dec()));
21 if ($trans_type == null)
22 $trans_type = systypes::journal_entry();
23 if ($trans_no == null)
24 $trans_no = get_next_trans_no($trans_type);
25 if ($person_type_id == null)
26 $person_type_id = payment_person_types::misc();
27 add_gl_trans($trans_type, $trans_no, $date_, $account, 0, 0, _("Exchange Variance"),
28 -$diff, null, $person_type_id, $person_id);
29 add_gl_trans($trans_type, $trans_no, $date_, get_company_pref('exchange_diff_act'), 0, 0,
30 _("Exchange Variance"), $diff, null, $person_type_id, $person_id);
34 function add_exchange_variation_all()
36 $trans_no = get_next_trans_no(0);
37 $sql = "SELECT * FROM ".TB_PREF."bank_accounts";
38 $result = db_query($sql, "could not retreive bank accounts");
39 while ($myrow = db_fetch($result))
40 add_exchange_variation(0, $trans_no, null, $myrow['account_code'],
41 $myrow['currency_code']);
43 //----------------------------------------------------------------------------------
45 function add_bank_transfer($from_account, $to_account, $date_,
46 $amount, $payment_type, $ref, $memo_)
50 $trans_type = systypes::bank_transfer();
52 $currency = get_bank_account_currency($from_account);
54 $trans_no = get_next_trans_no($trans_type);
57 // do the source account postings
58 $total += add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
61 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
62 $date_, $payment_type, -$amount,
63 payment_person_types::misc(), "", $currency,
64 "Cannot insert a source bank transaction");
66 add_exchange_variation($trans_type, $trans_no, $date_, $from_account,
67 $currency, payment_person_types::misc(), "");
69 // do the destination account postings
70 $total += add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
73 /*Post a balance post if $total != 0 */
74 add_gl_balance($trans_type, $trans_no, $date_, -$total);
76 add_bank_trans($trans_type, $trans_no, $to_account, $ref,
77 $date_, $payment_type, $amount,
78 payment_person_types::misc(), "",
80 "Cannot insert a destination bank transaction");
82 add_exchange_variation($trans_type, $trans_no, $date_, $from_account,
83 $currency, payment_person_types::misc(), "");
85 add_comments($trans_type, $trans_no, $date_, $memo_);
87 references::save_last($ref, $trans_type);
94 //----------------------------------------------------------------------------------
96 // $amount is in $from_account's currency
98 // returns an array of (inserted trans type, trans no)
100 function add_bank_transaction($trans_type, $from_account, $items, $date_,
101 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
103 // we can only handle type 1 (payment)and type 2 (deposit)
104 if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
105 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
107 $do_exchange_variance = false;
111 $currency = get_bank_account_currency($from_account);
113 // the gl items are already inversed/negated for type 2 (deposit)
114 $total_amount = $items->gl_items_total();
116 if ($person_type_id == payment_person_types::customer())
118 // we need to add a customer transaction record
120 // convert to customer currency
121 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
122 // we need to negate it too
123 $cust_amount = -$cust_amount;
125 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
129 elseif ($person_type_id == payment_person_types::supplier())
131 // we need to add a supplier transaction record
132 // convert to supp currency
133 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
135 // we need to negate it too
136 $supp_amount = -$supp_amount;
138 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
139 $ref, "", $supp_amount, 0, 0);
144 $trans_no = get_next_trans_no($trans_type);
145 $do_exchange_variance = true;
148 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
149 $date_, $type, -$total_amount,
150 $person_type_id, $person_id,
152 "Cannot insert a source bank transaction");
154 foreach ($items->gl_items as $gl_item)
156 $is_bank_to = is_bank_account($gl_item->code_id);
158 if ($trans_type == 1 AND $is_bank_to)
160 // we don't allow payments to go to a bank account. use transfer for this !
161 display_db_error("invalid payment entered. Cannot pay to another bank account", "");
164 // do the destination account postings
165 $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
166 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
167 $gl_item->amount, $currency, $person_type_id, $person_id);
171 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
172 $date_, $type, $gl_item->amount,
173 $person_type_id, $person_id, $currency,
174 "Cannot insert a destination bank transaction");
175 if ($do_exchange_variance)
176 add_exchange_variation($trans_type, $trans_no, $date_, $gl_item->code_id,
177 $currency, $person_type_id, $person_id);
181 // do the source account postings
182 add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
183 -$total, null, $person_type_id, $person_id);
185 if ($do_exchange_variance)
186 add_exchange_variation($trans_type, $trans_no, $date_, $from_account,
187 $currency, $person_type_id, $person_id);
189 add_comments($trans_type, $trans_no, $date_, $memo_);
191 references::save_last($ref, $trans_type);
193 commit_transaction();
195 return array($trans_type, $trans_no);
198 //----------------------------------------------------------------------------------------