2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function add_exchange_variation($trans_no, $date_, $acc_id, $account,
13 $currency, $person_type_id=null, $person_id = "")
15 if (is_company_currency($currency))
21 // We have to calculate all the currency accounts belonging to the GL account
22 // upto $date_ and calculate with the exchange rates. And then compare with the GL account balance.
23 // 2010-02-23 Joe Hunt with help of Ary Wibowo
24 $sql = "SELECT SUM(bt.amount) AS for_amount, ba.bank_curr_code
25 FROM ".TB_PREF."bank_trans bt, ".TB_PREF."bank_accounts ba
26 WHERE ba.id = bt.bank_act AND ba.account_code = ".db_escape($account)." AND bt.trans_date<='".date2sql($date_)."'
27 GROUP BY ba.bank_curr_code";
28 $result = db_query($sql, "Transactions for bank account $acc_id could not be calculated");
29 while ($row = db_fetch($result))
31 if ($row['for_amount'] == 0)
33 $rate = get_exchange_rate_from_home_currency($row['bank_curr_code'], $date_);
34 $for_amount += round2($row['for_amount'] * $rate, user_price_dec());
36 $amount = get_gl_trans_from_to("", $date_, $account);
37 $diff = $amount - $for_amount;
40 if ($trans_no == null)
41 $trans_no = get_next_trans_no(ST_JOURNAL);
42 if ($person_type_id == null)
43 $person_type_id = PT_MISC;
44 add_gl_trans(ST_JOURNAL, $trans_no, $date_, $account, 0, 0, _("Exchange Variance"),
45 -$diff, null, $person_type_id, $person_id);
46 add_gl_trans(ST_JOURNAL, $trans_no, $date_, get_company_pref('exchange_diff_act'), 0, 0,
47 _("Exchange Variance"), $diff, null, $person_type_id, $person_id);
52 function add_exchange_variation_all($date=null, $ref="", $memo)
57 $trans_no = get_next_trans_no(ST_JOURNAL);
58 $sql = "SELECT * FROM ".TB_PREF."bank_accounts";
59 $result = db_query($sql, "could not retreive bank accounts");
60 while ($myrow = db_fetch($result))
62 if (add_exchange_variation($trans_no, $date, $myrow['id'], $myrow['account_code'],
63 $myrow['bank_curr_code']))
68 add_comments(ST_JOURNAL, $trans_no, $date, $memo);
70 $ref = $Refs->get_next(ST_JOURNAL);
71 $Refs->save(ST_JOURNAL, $trans_no, $ref);
72 add_audit_trail(ST_JOURNAL, $trans_no, $date);
75 return ($exchanged ? $trans_no : 0);
77 //----------------------------------------------------------------------------------
78 // Add bank tranfer to database.
80 // $from_account - source bank account id
81 // $to_account - target bank account id
84 function add_bank_transfer($from_account, $to_account, $date_,
85 $amount, $ref, $memo_, $charge=0)
87 global $Refs, $SysPrefs;
91 $trans_type = ST_BANKTRANSFER;
93 $currency = get_bank_account_currency($from_account);
95 $trans_no = get_next_trans_no($trans_type);
97 $fromact = get_bank_account($from_account);
98 $toact = get_bank_account($to_account);
99 $person_id = _("From")." ".$fromact['bank_account_name']." "._("To")." ".$toact['bank_account_name'];
101 $from_gl_account = get_bank_gl_account($from_account);
102 $to_gl_account = get_bank_gl_account($to_account);
106 // do the source account postings
107 $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, $person_id,
108 -($amount + $charge), $currency);
110 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
111 $date_, -($amount + $charge),
112 PT_MISC, $person_id, $currency,
113 "Cannot insert a source bank transaction");
114 if ($SysPrefs->auto_currency_revaluation())
116 $trans_no1 = get_next_trans_no(ST_JOURNAL);
117 if (add_exchange_variation($trans_no1, $date_, $from_account, $from_gl_account,
118 $currency, PT_MISC, $person_id))
123 /* Now Debit bank charge account with charges */
124 $charge_act = get_company_pref('bank_charge_act');
125 $total += add_gl_trans($trans_type, $trans_no, $date_,
126 $charge_act, 0, 0, $person_id, $charge, $currency);
128 // do the destination account postings
129 $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, $person_id,
132 /*Post a balance post if $total != 0 */
133 add_gl_balance($trans_type, $trans_no, $date_, -$total);
135 add_bank_trans($trans_type, $trans_no, $to_account, $ref,
136 $date_, $amount, PT_MISC, $person_id,
137 $currency, "Cannot insert a destination bank transaction");
139 if ($SysPrefs->auto_currency_revaluation())
141 $currency = get_bank_account_currency($to_account);
143 if ($exchanged || add_exchange_variation($trans_no1, $date_, $to_account, $to_gl_account,
144 $currency, PT_MISC, $person_id))
146 $ref1 = $Refs->get_next(ST_JOURNAL);
147 $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
148 add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
151 add_comments($trans_type, $trans_no, $date_, $memo_);
153 $Refs->save($trans_type, $trans_no, $ref);
154 add_audit_trail($trans_type, $trans_no, $date_);
156 commit_transaction();
160 //----------------------------------------------------------------------------------
161 // Add bank payment or deposit to database.
163 // $from_account - bank account id
164 // $item - transaction cart (line item's amounts in bank account's currency)
165 // $person_type_id - defines type of $person_id identifiers
166 // $person_id - supplier/customer/other id
167 // $person_detail_id - customer branch id or not used
169 // returns an array of (inserted trans type, trans no)
171 // FIXME -revise code for update case
173 function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $date_,
174 $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
176 global $Refs, $SysPrefs;
178 // we can only handle type 1 (payment)and type 2 (deposit)
179 if ($trans_type != ST_BANKPAYMENT && $trans_type != ST_BANKDEPOSIT)
180 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
182 $do_exchange_variance = false;
184 if ($use_transaction)
188 clear_bank_transaction($_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, true);
190 $currency = get_bank_account_currency($from_account);
191 $bank_gl_account = get_bank_gl_account($from_account);
193 // the gl items are already inversed/negated for type 2 (deposit)
194 $total_amount = $items->gl_items_total();
196 if ($person_type_id == PT_CUSTOMER)
198 // we need to add a customer transaction record
200 // convert to customer currency
201 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
202 // we need to negate it too
203 $cust_amount = -$cust_amount;
205 $trans_no = write_customer_trans($trans_type, $trans_no, $person_id, $person_detail_id, $date_,
209 elseif ($person_type_id == PT_SUPPLIER)
211 // we need to add a supplier transaction record
212 // convert to supp currency
213 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
215 // we need to negate it too
216 $supp_amount = -$supp_amount;
218 $trans_no = write_supp_trans($trans_type, $trans_no, $person_id, $date_, '',
219 $ref, "", $supp_amount, 0, 0);
225 $trans_no = get_next_trans_no($trans_type);
226 $do_exchange_variance = $SysPrefs->auto_currency_revaluation();
227 if ($do_exchange_variance)
228 $trans_no1 = get_next_trans_no(ST_JOURNAL);
231 // do the source account postings
233 add_bank_trans($trans_type, $trans_no, $from_account, $ref,
234 $date_, -$total_amount,
235 $person_type_id, $person_id,
237 "Cannot insert a source bank transaction");
239 foreach ($items->gl_items as $gl_item)
241 $is_bank_to = is_bank_account($gl_item->code_id);
243 if ($trans_type == ST_BANKPAYMENT AND $is_bank_to)
245 // we don't allow payments to go to a bank account. use transfer for this !
246 display_db_error("invalid payment entered. Cannot pay to another bank account", "");
249 // do the destination account postings
250 $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
251 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
252 $gl_item->amount, $currency, $person_type_id, $person_id);
256 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
257 $date_, $gl_item->amount,
258 $person_type_id, $person_id, $currency,
259 "Cannot insert a destination bank transaction");
260 if ($do_exchange_variance)
262 add_exchange_variation($trans_no1, $date_, $is_bank_to, $gl_item->code_id,
263 $currency, $person_type_id, $person_id);
266 // store tax details if the gl account is a tax account
268 $amount = $gl_item->amount;
269 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
271 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, -$amount,
272 $ex_rate, $date_, $memo_);
275 // do the source account postings
276 add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
277 -$total, null, $person_type_id, $person_id);
279 if ($do_exchange_variance)
281 if ($exchanged || add_exchange_variation($trans_no1, $date_, $from_account, $bank_gl_account,
282 $currency, $person_type_id, $person_id))
284 $ref1 = $Refs->get_next(ST_JOURNAL);
285 $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
286 add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
290 add_comments($trans_type, $trans_no, $date_, $memo_);
292 $Refs->save($trans_type, $trans_no, $ref);
293 add_audit_trail($trans_type, $trans_no, $date_);
295 if ($use_transaction)
296 commit_transaction();
298 return array($trans_type, $trans_no);
300 //----------------------------------------------------------------------------------------
302 function clear_bank_transaction($type, $type_no, $nested=false)
309 $sql = "DELETE FROM ".TB_PREF."bank_trans
310 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
312 $result = db_query($sql, "could not clear bank transactions for type=$type and trans_no=$type_no");
314 clear_gl_trans($type, $type_no, true);
316 // in case it's a customer trans - probably better to check first
317 void_cust_allocations($type, $type_no);
318 clear_customer_trans($type, $type_no);
320 // in case it's a supplier trans - probably better to check first
321 void_supp_allocations($type, $type_no);
322 clear_supp_trans($type, $type_no);
324 clear_trans_tax_details($type, $type_no);
326 delete_comments($type, $type_no);
328 commit_transaction();