Final rerun :)
[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_type, $trans_no, $date_, $acc_id, $account,
13     $currency, $person_type_id=null, $person_id = "")
14 {
15         if (is_company_currency($currency))
16                 return;
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_type == null)
41                         $trans_type = ST_JOURNAL;
42                 if ($trans_no == null)
43                         $trans_no = get_next_trans_no($trans_type);
44                 if ($person_type_id == null)
45                         $person_type_id = PT_MISC;
46                 add_gl_trans($trans_type, $trans_no, $date_, $account, 0, 0, _("Exchange Variance"),
47                 -$diff, null, $person_type_id, $person_id);
48         add_gl_trans($trans_type, $trans_no, $date_, get_company_pref('exchange_diff_act'), 0, 0, 
49                 _("Exchange Variance"), $diff, null, $person_type_id, $person_id);
50         }       
51 }
52
53 function add_exchange_variation_all()
54 {
55         $trans_no = get_next_trans_no(ST_JOURNAL);
56         $sql = "SELECT * FROM ".TB_PREF."bank_accounts";
57         $result = db_query($sql, "could not retreive bank accounts");
58         while ($myrow = db_fetch($result))
59                 add_exchange_variation(ST_JOURNAL, $trans_no, null, $myrow['id'], $myrow['account_code'],
60                         $myrow['currency_code']);
61 }
62 //----------------------------------------------------------------------------------
63 //      Add bank tranfer to database.
64 //
65 //      $from_account - source bank account id
66 //      $to_account   - target bank account id
67 //      
68
69 function add_bank_transfer($from_account, $to_account, $date_,
70         $amount, $ref, $memo_, $charge=0)
71 {
72         global $Refs;
73         
74         begin_transaction();
75
76         $trans_type = ST_BANKTRANSFER;
77
78         $currency = get_bank_account_currency($from_account);
79
80         $trans_no = get_next_trans_no($trans_type);
81
82         $from_gl_account = get_bank_gl_account($from_account);
83         $to_gl_account = get_bank_gl_account($to_account);
84
85         $total = 0;
86         // do the source account postings
87     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
88                 -($amount + $charge), $currency);
89
90     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
91                 $date_, -($amount + $charge),
92                 PT_MISC, "", $currency,
93                 "Cannot insert a source bank transaction");
94
95         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, $from_gl_account, 
96                 $currency, PT_MISC, "");
97
98         if ($charge != 0)
99         {
100                 /* Now Debit bank charge account with charges */
101                 $charge_act = get_company_pref('bank_charge_act');
102                 $total += add_gl_trans($trans_type, $trans_no, $date_,
103                         $charge_act, 0, 0, "", $charge, $currency);
104         }
105         // do the destination account postings
106         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
107                 $amount, $currency);
108                 
109         /*Post a balance post if $total != 0 */
110         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
111         
112         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
113                 $date_, $amount, PT_MISC, "",
114                 $currency, "Cannot insert a destination bank transaction");
115
116         $currency = get_bank_account_currency($to_account);
117         
118         add_exchange_variation($trans_type, $trans_no, $date_, $to_account, $to_gl_account, 
119                 $currency, PT_MISC, "");
120         
121         add_comments($trans_type, $trans_no, $date_, $memo_);
122
123         $Refs->save($trans_type, $trans_no, $ref);
124         add_audit_trail($trans_type, $trans_no, $date_);
125
126         commit_transaction();
127
128         return $trans_no;
129 }
130
131 //----------------------------------------------------------------------------------
132 //      Add bank payment or deposit to database.
133 //
134 //      $from_account - bank account id
135 //  $item - transaction cart (line item's amounts in bank account's currency)
136 //  $person_type_id - defines type of $person_id identifiers
137 //  $person_id  - supplier/customer/other id
138 // $person_detail_id - customer branch id or not used
139 //
140 // returns an array of (inserted trans type, trans no)
141
142 function add_bank_transaction($trans_type, $from_account, $items, $date_,
143         $person_type_id, $person_id, $person_detail_id, $ref, $memo_, $use_transaction=true)
144 {
145         global $Refs;
146
147         // we can only handle type 1 (payment)and type 2 (deposit)
148         if ($trans_type != ST_BANKPAYMENT && $trans_type != ST_BANKDEPOSIT)
149                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
150
151         $do_exchange_variance = false;
152         
153         if ($use_transaction)
154                 begin_transaction();
155
156         $currency = get_bank_account_currency($from_account);
157         $bank_gl_account = get_bank_gl_account($from_account);
158
159         // the gl items are already inversed/negated for type 2 (deposit)
160         $total_amount = $items->gl_items_total();
161
162     if ($person_type_id == PT_CUSTOMER)
163     {
164         // we need to add a customer transaction record
165
166                 // convert to customer currency
167                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
168                 // we need to negate it too
169                 $cust_amount = -$cust_amount;
170
171                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
172                 $ref, $cust_amount);
173
174     }
175     elseif ($person_type_id == PT_SUPPLIER)
176     {
177         // we need to add a supplier transaction record
178                 // convert to supp currency
179                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
180
181                 // we need to negate it too
182                 $supp_amount = -$supp_amount;
183
184                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
185                 $ref, "", $supp_amount, 0, 0);
186
187     }
188     else
189     {
190         $trans_no = get_next_trans_no($trans_type);
191         $do_exchange_variance = true;
192     }
193
194         // do the source account postings
195
196     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
197         $date_, -$total_amount,
198         $person_type_id, $person_id,
199         $currency,
200         "Cannot insert a source bank transaction");
201         $total = 0;
202         foreach ($items->gl_items as $gl_item)
203         {
204                 $is_bank_to = is_bank_account($gl_item->code_id);
205
206                 if ($trans_type == ST_BANKPAYMENT AND $is_bank_to)
207                 {
208                         // we don't allow payments to go to a bank account. use transfer for this !
209                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
210                 }
211
212         // do the destination account postings
213         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
214                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
215                 $gl_item->amount, $currency, $person_type_id, $person_id);
216
217         if ($is_bank_to)
218         {
219                 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
220                         $date_, $gl_item->amount,
221                         $person_type_id, $person_id, $currency,
222                         "Cannot insert a destination bank transaction");
223                 if ($do_exchange_variance)
224                         add_exchange_variation($trans_type, $trans_no, $date_, $is_bank_to, $gl_item->code_id, 
225                                 $currency, $person_type_id, $person_id);
226         }
227                 // store tax details if the gl account is a tax account
228
229                 $amount = $gl_item->amount;
230                 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
231                         
232                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, -$amount,
233                         $ex_rate, $date_, $memo_);
234         }
235
236         // do the source account postings
237     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
238         -$total, null, $person_type_id, $person_id);
239
240     if ($do_exchange_variance)
241         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, $bank_gl_account, 
242                 $currency, $person_type_id, $person_id);
243
244         add_comments($trans_type, $trans_no, $date_, $memo_);
245
246         $Refs->save($trans_type, $trans_no, $ref);
247         add_audit_trail($trans_type, $trans_no, $date_);
248
249         if ($use_transaction)
250                 commit_transaction();
251
252         return array($trans_type, $trans_no);
253 }
254
255 //----------------------------------------------------------------------------------------
256
257 ?>