Allowing modifying of Bank Payments/Deposits
[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
91         $trans_type = ST_BANKTRANSFER;
92
93         $currency = get_bank_account_currency($from_account);
94
95         $trans_no = get_next_trans_no($trans_type);
96
97         $from_gl_account = get_bank_gl_account($from_account);
98         $to_gl_account = get_bank_gl_account($to_account);
99         
100         $exchanged = false;
101         $total = 0;
102         // do the source account postings
103     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
104                 -($amount + $charge), $currency);
105
106     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
107                 $date_, -($amount + $charge),
108                 PT_MISC, "", $currency,
109                 "Cannot insert a source bank transaction");
110         if ($SysPrefs->auto_currency_revaluation())
111         {
112                 $trans_no1 = get_next_trans_no(ST_JOURNAL);
113                 if (add_exchange_variation($trans_no1, $date_, $from_account, $from_gl_account, $currency))
114                         $exchanged = true;
115         }
116         if ($charge != 0)
117         {
118                 /* Now Debit bank charge account with charges */
119                 $charge_act = get_company_pref('bank_charge_act');
120                 $total += add_gl_trans($trans_type, $trans_no, $date_,
121                         $charge_act, 0, 0, "", $charge, $currency);
122         }
123         // do the destination account postings
124         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
125                 $amount, $currency);
126                 
127         /*Post a balance post if $total != 0 */
128         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
129         
130         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
131                 $date_, $amount, PT_MISC, "",
132                 $currency, "Cannot insert a destination bank transaction");
133
134         if ($SysPrefs->auto_currency_revaluation())
135         {
136                 $currency = get_bank_account_currency($to_account);
137         
138                 if ($exchanged || add_exchange_variation($trans_no1, $date_, $to_account, $to_gl_account,       $currency))
139                 {
140                         $ref1 = $Refs->get_next(ST_JOURNAL);
141                         $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
142                         add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
143                 }       
144         }
145         add_comments($trans_type, $trans_no, $date_, $memo_);
146
147         $Refs->save($trans_type, $trans_no, $ref);
148         add_audit_trail($trans_type, $trans_no, $date_);
149
150         commit_transaction();
151
152         return $trans_no;
153 }
154
155 //----------------------------------------------------------------------------------
156 //      Add bank payment or deposit to database.
157 //
158 //      $from_account - bank account id
159 //  $item - transaction cart (line item's amounts in bank account's currency)
160 //  $person_type_id - defines type of $person_id identifiers
161 //  $person_id  - supplier/customer/other id
162 // $person_detail_id - customer branch id or not used
163 //
164 // returns an array of (inserted trans type, trans no)
165
166 function add_bank_transaction($trans_type, $from_account, $items, $date_,
167         $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
168 {
169         global $Refs, $SysPrefs;
170
171         // we can only handle type 1 (payment)and type 2 (deposit)
172         if ($trans_type != ST_BANKPAYMENT && $trans_type != ST_BANKDEPOSIT)
173                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
174
175         $do_exchange_variance = false;
176         $exchanged = false;
177         if ($use_transaction)
178                 begin_transaction();
179
180         $currency = get_bank_account_currency($from_account);
181         $bank_gl_account = get_bank_gl_account($from_account);
182
183         // the gl items are already inversed/negated for type 2 (deposit)
184         $total_amount = $items->gl_items_total();
185
186     if ($person_type_id == PT_CUSTOMER)
187     {
188         // we need to add a customer transaction record
189
190                 // convert to customer currency
191                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
192                 // we need to negate it too
193                 $cust_amount = -$cust_amount;
194
195                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
196                 $ref, $cust_amount);
197
198     }
199     elseif ($person_type_id == PT_SUPPLIER)
200     {
201         // we need to add a supplier transaction record
202                 // convert to supp currency
203                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
204
205                 // we need to negate it too
206                 $supp_amount = -$supp_amount;
207
208                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
209                 $ref, "", $supp_amount, 0, 0);
210
211     }
212     else
213     {
214         $trans_no = get_next_trans_no($trans_type);
215         $do_exchange_variance = $SysPrefs->auto_currency_revaluation();
216         if ($do_exchange_variance)
217                 $trans_no1 = get_next_trans_no(ST_JOURNAL);
218     }
219
220         // do the source account postings
221
222     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
223         $date_, -$total_amount,
224         $person_type_id, $person_id,
225         $currency,
226         "Cannot insert a source bank transaction");
227         $total = 0;
228         foreach ($items->gl_items as $gl_item)
229         {
230                 $is_bank_to = is_bank_account($gl_item->code_id);
231
232                 if ($trans_type == ST_BANKPAYMENT AND $is_bank_to)
233                 {
234                         // we don't allow payments to go to a bank account. use transfer for this !
235                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
236                 }
237
238         // do the destination account postings
239         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
240                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
241                 $gl_item->amount, $currency, $person_type_id, $person_id);
242
243         if ($is_bank_to)
244         {
245                 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
246                         $date_, $gl_item->amount,
247                         $person_type_id, $person_id, $currency,
248                         "Cannot insert a destination bank transaction");
249                 if ($do_exchange_variance)
250                 {
251                         add_exchange_variation($trans_no1, $date_, $is_bank_to, $gl_item->code_id, 
252                                 $currency, $person_type_id, $person_id);
253                 }               
254         }
255                 // store tax details if the gl account is a tax account
256
257                 $amount = $gl_item->amount;
258                 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
259                         
260                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, -$amount,
261                         $ex_rate, $date_, $memo_);
262         }
263
264         // do the source account postings
265     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
266         -$total, null, $person_type_id, $person_id);
267
268     if ($do_exchange_variance)
269     {
270         if ($exchanged || add_exchange_variation($trans_no1, $date_, $from_account, $bank_gl_account, 
271                 $currency, $person_type_id, $person_id))
272         {       
273                         $ref1 = $Refs->get_next(ST_JOURNAL);
274                         $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
275                         add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
276                 }       
277         }
278
279         add_comments($trans_type, $trans_no, $date_, $memo_);
280
281         $Refs->save($trans_type, $trans_no, $ref);
282         add_audit_trail($trans_type, $trans_no, $date_);
283
284         if ($use_transaction)
285                 commit_transaction();
286
287         return array($trans_type, $trans_no);
288 }
289
290 //----------------------------------------------------------------------------------------
291
292 //----------------------------------------------------------------------------------
293 //      Re-Insert bank payment or deposit to database using the transaction number passed.
294 //
295 //      $from_account - bank account id
296 //  $item - transaction cart (line item's amounts in bank account's currency)
297 //  $person_type_id - defines type of $person_id identifiers
298 //  $person_id  - supplier/customer/other id
299 //  $person_detail_id - customer branch id or not used
300 //
301 // returns an array of (inserted trans type, trans no)
302
303 function reinsert_bank_transaction($trans_type, $trans_no, $from_account, $items, $date_,
304         $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
305 {
306         global $Refs, $SysPrefs;
307
308         // we can only handle type 1 (payment)and type 2 (deposit)
309         if ($trans_type != ST_BANKPAYMENT && $trans_type != ST_BANKDEPOSIT)
310                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
311
312         $do_exchange_variance = false;
313         $exchanged = false;
314         if ($use_transaction)
315                 begin_transaction();
316
317         $currency = get_bank_account_currency($from_account);
318         $bank_gl_account = get_bank_gl_account($from_account);
319
320         // the gl items are already inversed/negated for type 2 (deposit)
321         $total_amount = $items->gl_items_total();
322
323     if ($person_type_id == PT_CUSTOMER)
324     {
325         // we need to add a customer transaction record
326
327                 // convert to customer currency
328                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
329                 // we need to negate it too
330                 $cust_amount = -$cust_amount;
331
332                 $trans_no = reinsert_customer_trans($trans_type, $trans_no, $person_id, $person_detail_id, $date_,
333                 $ref, $cust_amount);
334
335     }
336     elseif ($person_type_id == PT_SUPPLIER)
337     {
338         // we need to add a supplier transaction record
339                 // convert to supp currency
340                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
341
342                 // we need to negate it too
343                 $supp_amount = -$supp_amount;
344
345                 $trans_no = reinsert_supp_trans($trans_type, $trans_no, $person_id, $date_, '',
346                 $ref, "", $supp_amount, 0, 0);
347
348     }
349     else
350     {
351         $do_exchange_variance = $SysPrefs->auto_currency_revaluation();
352         if ($do_exchange_variance)
353                 $trans_no1 = get_next_trans_no(ST_JOURNAL);
354     }
355
356         // do the source account postings
357
358     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
359         $date_, -$total_amount,
360         $person_type_id, $person_id,
361         $currency,
362         "Cannot insert a source bank transaction");
363         $total = 0;
364         foreach ($items->gl_items as $gl_item)
365         {
366                 $is_bank_to = is_bank_account($gl_item->code_id);
367
368                 if ($trans_type == ST_BANKPAYMENT AND $is_bank_to)
369                 {
370                         // we don't allow payments to go to a bank account. use transfer for this !
371                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
372                 }
373
374         // do the destination account postings
375         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
376                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
377                 $gl_item->amount, $currency, $person_type_id, $person_id);
378
379         if ($is_bank_to)
380         {
381                 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
382                         $date_, $gl_item->amount,
383                         $person_type_id, $person_id, $currency,
384                         "Cannot insert a destination bank transaction");
385                 if ($do_exchange_variance)
386                 {
387                         add_exchange_variation($trans_no1, $date_, $is_bank_to, $gl_item->code_id, 
388                                 $currency, $person_type_id, $person_id);
389                 }               
390         }
391                 // store tax details if the gl account is a tax account
392
393                 $amount = $gl_item->amount;
394                 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
395                         
396                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, -$amount,
397                         $ex_rate, $date_, $memo_);
398         }
399         // do the source account postings
400     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
401         -$total, null, $person_type_id, $person_id);
402
403     if ($do_exchange_variance)
404     {
405         if ($exchanged || add_exchange_variation($trans_no1, $date_, $from_account, $bank_gl_account, 
406                 $currency, $person_type_id, $person_id))
407         {       
408                         $ref1 = $Refs->get_next(ST_JOURNAL);
409                         $Refs->save(ST_JOURNAL, $trans_no1, $ref1);
410                         add_audit_trail(ST_JOURNAL, $trans_no1, $date_);
411                 }       
412         }
413
414         add_comments($trans_type, $trans_no, $date_, $memo_);
415
416         $Refs->save($trans_type, $trans_no, $ref);
417         add_audit_trail($trans_type, $trans_no, $date_);
418
419         if ($use_transaction)
420                 commit_transaction();
421
422         return array($trans_type, $trans_no);
423 }
424
425 //----------------------------------------------------------------------------------------
426
427 ?>