Merged changes up to version 2.3.4 into unstable.
[fa-stable.git] / gl / includes / db / gl_db_banking.inc
1 <?php
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 = "")
14 {
15         if (is_company_currency($currency))
16                 return false;
17         if ($date_ == null)
18                 $date_ = Today();
19         $for_amount = 0;
20
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))
30         {
31                 if ($row['for_amount'] == 0)
32                         continue;
33                 $rate = get_exchange_rate_from_home_currency($row['bank_curr_code'], $date_);
34                 $for_amount += round2($row['for_amount'] * $rate, user_price_dec());
35         }       
36         $amount = get_gl_trans_from_to("", $date_, $account);
37         $diff = $amount - $for_amount;
38         if ($diff != 0)
39         {
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);
48         }
49         return ($diff != 0);
50 }
51
52 function add_exchange_variation_all($date=null, $ref="", $memo)
53 {
54         global $Refs;
55         begin_transaction();
56         $exchanged = false;
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))
61         {
62                 if (add_exchange_variation($trans_no, $date, $myrow['id'], $myrow['account_code'],
63                         $myrow['bank_curr_code']))
64                         $exchanged = true;
65         }
66         if ($exchanged)
67         {
68                 add_comments(ST_JOURNAL, $trans_no, $date, $memo);
69                 if ($ref == "")
70                         $ref = $Refs->get_next(ST_JOURNAL);
71                 $Refs->save(ST_JOURNAL, $trans_no, $ref);
72                 add_audit_trail(ST_JOURNAL, $trans_no, $date);
73         }       
74         commit_transaction();
75         return ($exchanged ? $trans_no : 0);
76 }
77 //----------------------------------------------------------------------------------
78 //      Add bank tranfer to database.
79 //
80 //      $from_account - source bank account id
81 //      $to_account   - target bank account id
82 //      
83
84 function add_bank_transfer($from_account, $to_account, $date_,
85         $amount, $ref, $memo_, $charge=0)
86 {
87         global $Refs, $SysPrefs;
88         
89         begin_transaction();
90         $args = func_get_args(); if (count($args) < 7) $args[] = 0;
91         $args = (object)array_combine(array('from_account', 'to_account', 'date_', 'amount',
92                 'ref', 'memo_', 'charge'), $args);
93         $args->trans_no = 0;
94         hook_db_prewrite($args, ST_BANKTRANSFER);
95
96         $trans_type = ST_BANKTRANSFER;
97
98         $currency = get_bank_account_currency($from_account);
99
100         $trans_no = get_next_trans_no($trans_type);
101         
102     $fromact = get_bank_account($from_account);
103     $toact = get_bank_account($to_account);
104     $person_id = _("From")." ".$fromact['bank_account_name']." "._("To")." ".$toact['bank_account_name'];
105
106         $from_gl_account = get_bank_gl_account($from_account);
107         $to_gl_account = get_bank_gl_account($to_account);
108         
109         $exchanged = false;
110         $total = 0;
111         // do the source account postings
112     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, $person_id,
113                 -($amount + $charge), $currency);
114
115     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
116                 $date_, -($amount + $charge),
117                 PT_MISC, $person_id, $currency,
118                 "Cannot insert a source bank transaction");
119         if ($SysPrefs->auto_currency_revaluation())
120         {
121                 $trans_no1 = get_next_trans_no(ST_JOURNAL);
122                 if (add_exchange_variation($trans_no1, $date_, $from_account, $from_gl_account, 
123                         $currency, PT_MISC, $person_id))
124                         $exchanged = true;
125         }
126         if ($charge != 0)
127         {
128                 /* Now Debit bank charge account with charges */
129                 $charge_act = get_company_pref('bank_charge_act');
130                 $total += add_gl_trans($trans_type, $trans_no, $date_,
131                         $charge_act, 0, 0, $person_id, $charge, $currency);
132         }
133         // do the destination account postings
134         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, $person_id,
135                 $amount, $currency);
136                 
137         /*Post a balance post if $total != 0 */
138         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
139         
140         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
141                 $date_, $amount, PT_MISC, $person_id,
142                 $currency, "Cannot insert a destination bank transaction");
143
144         if ($SysPrefs->auto_currency_revaluation())
145         {
146                 $currency = get_bank_account_currency($to_account);
147         
148                 if ($exchanged || add_exchange_variation($trans_no1, $date_, $to_account, $to_gl_account,
149                         $currency, PT_MISC, $person_id))
150                         $exchanged = true;
151         }
152         if ($exchanged == true)
153         {
154                 $ref1 = $Refs->get_next(ST_JOURNAL);
155                 $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
156                 add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
157         }
158         add_comments($trans_type, $trans_no, $date_, $memo_);
159
160         $Refs->save($trans_type, $trans_no, $ref);
161         add_audit_trail($trans_type, $trans_no, $date_);
162
163         $args->trans_no = $trans_no;
164         hook_db_postwrite($args, ST_BANKTRANSFER);
165         commit_transaction();
166
167         return $trans_no;
168 }
169 //----------------------------------------------------------------------------------
170 //      Add bank payment or deposit to database.
171 //
172 //      $from_account - bank account id
173 //  $item - transaction cart (line item's amounts in bank account's currency)
174 //  $person_type_id - defines type of $person_id identifiers
175 //  $person_id  - supplier/customer/other id
176 // $person_detail_id - customer branch id or not used
177 //
178 // returns an array of (inserted trans type, trans no)
179 //
180 // FIXME -revise code for update case
181 //
182 function write_bank_transaction($trans_type, $trans_no, $from_account, $items, $date_,
183         $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
184 {
185         global $Refs, $SysPrefs;
186
187         // we can only handle type 1 (payment)and type 2 (deposit)
188         if ($trans_type != ST_BANKPAYMENT && $trans_type != ST_BANKDEPOSIT)
189                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
190
191         $do_exchange_variance = false;
192         $exchanged = false;
193         if ($use_transaction)
194                 begin_transaction();
195
196         $args = func_get_args(); if (count($args) < 11) $args[] = true;
197         $args = (object)array_combine(array('trans_type', 'trans_no', 'from_account', 'items', 'date_',
198                 'person_type_id', 'person_id', 'person_detail_id', 'ref', 'memo_', 'use_transaction'),
199                 $args);
200         hook_db_prewrite($args, $trans_type);
201
202         if ($trans_no)
203                 clear_bank_transaction($_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id);
204
205         $currency = get_bank_account_currency($from_account);
206         $bank_gl_account = get_bank_gl_account($from_account);
207
208         // the gl items are already inversed/negated for type 2 (deposit)
209         $total_amount = $items->gl_items_total();
210
211     if ($person_type_id == PT_CUSTOMER)
212     {
213         // we need to add a customer transaction record
214
215                 // convert to customer currency
216                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
217                 // we need to negate it too
218                 $cust_amount = -$cust_amount;
219
220                 $trans_no = write_customer_trans($trans_type, $trans_no, $person_id, $person_detail_id, $date_,
221                 $ref, $cust_amount);
222
223     }
224     elseif ($person_type_id == PT_SUPPLIER)
225     {
226         // we need to add a supplier transaction record
227                 // convert to supp currency
228                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
229
230                 // we need to negate it too
231                 $supp_amount = -$supp_amount;
232
233                 $trans_no = write_supp_trans($trans_type, $trans_no, $person_id, $date_, '',
234                         $ref, "", $supp_amount, 0, 0);
235
236     }
237     else
238     {
239         if (!$trans_no)
240                 $trans_no = get_next_trans_no($trans_type);
241         $do_exchange_variance = $SysPrefs->auto_currency_revaluation();
242         if ($do_exchange_variance)
243                 $trans_no1 = get_next_trans_no(ST_JOURNAL);
244     }
245
246         // do the source account postings
247
248     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
249         $date_, -$total_amount,
250         $person_type_id, $person_id,
251         $currency,
252         "Cannot insert a source bank transaction");
253         $total = 0;
254         foreach ($items->gl_items as $gl_item)
255         {
256                 $is_bank_to = is_bank_account($gl_item->code_id);
257
258                 if ($trans_type == ST_BANKPAYMENT AND $is_bank_to)
259                 {
260                         // we don't allow payments to go to a bank account. use transfer for this !
261                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
262                 }
263
264         // do the destination account postings
265         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
266                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
267                 $gl_item->amount, $currency, $person_type_id, $person_id);
268
269         if ($is_bank_to)
270         {
271                 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
272                         $date_, $gl_item->amount,
273                         $person_type_id, $person_id, $currency,
274                         "Cannot insert a destination bank transaction");
275                 if ($do_exchange_variance)
276                 {
277                         add_exchange_variation($trans_no1, $date_, $is_bank_to, $gl_item->code_id, 
278                                 $currency, $person_type_id, $person_id);
279                 }               
280         }
281                 // store tax details if the gl account is a tax account
282
283                 $amount = $gl_item->amount;
284                 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
285                         
286                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, -$amount,
287                         $ex_rate, $date_, $memo_);
288         }
289
290         // do the source account postings
291     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
292         -$total, null, $person_type_id, $person_id);
293
294     if ($do_exchange_variance)
295     {
296         if ($exchanged || add_exchange_variation($trans_no1, $date_, $from_account, $bank_gl_account, 
297                 $currency, $person_type_id, $person_id))
298         {       
299                         $ref1 = $Refs->get_next(ST_JOURNAL);
300                         $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
301                         add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
302                 }       
303         }
304
305         add_comments($trans_type, $trans_no, $date_, $memo_);
306
307         $Refs->save($trans_type, $trans_no, $ref);
308         add_audit_trail($trans_type, $trans_no, $date_);
309
310         $args->trans_no = $trans_no;
311         hook_db_postwrite($args, $trans_type);
312         if ($use_transaction)
313                 commit_transaction();
314
315         return array($trans_type, $trans_no);
316 }
317 //----------------------------------------------------------------------------------------
318
319 function clear_bank_transaction($type, $type_no)
320 {
321
322         hook_db_prevoid($type, $type_no);
323
324         $sql = "DELETE FROM ".TB_PREF."bank_trans 
325                 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
326
327         $result = db_query($sql, "could not clear bank transactions for type=$type and trans_no=$type_no");
328
329         clear_gl_trans($type, $type_no, true);
330
331         // in case it's a customer trans - probably better to check first
332         void_cust_allocations($type, $type_no);
333
334         // in case it's a supplier trans - probably better to check first
335         void_supp_allocations($type, $type_no);
336         clear_supp_trans($type, $type_no);
337
338         clear_trans_tax_details($type, $type_no);
339
340         delete_comments($type, $type_no);
341 }
342
343 ?>