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