Added bank charge in bank transfer
[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='$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 = systypes::journal_entry();
31                 if ($trans_no == null)
32                         $trans_no = get_next_trans_no($trans_type);
33                 if ($person_type_id == null)
34                         $person_type_id = payment_person_types::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(0);
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(0, $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         begin_transaction();
62
63         $trans_type = systypes::bank_transfer();
64
65         $currency = get_bank_account_currency($from_account);
66
67         $trans_no = get_next_trans_no($trans_type);
68
69         $from_gl_account = get_bank_gl_account($from_account);
70         $to_gl_account = get_bank_gl_account($to_account);
71
72         $total = 0;
73         // do the source account postings
74     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_gl_account, 0, 0, "",
75                 -($amount + $charge), $currency);
76
77     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
78                 $date_, -($amount + $charge),
79                 payment_person_types::misc(), "", $currency,
80                 "Cannot insert a source bank transaction");
81
82         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, $from_gl_account, 
83                 $currency, payment_person_types::misc(), "");
84
85         if ($charge != 0)
86         {
87                 /* Now Debit bank charge account with charges */
88                 $charge_act = get_company_pref('bank_charge_act');
89                 $total += add_gl_trans($trans_type, $trans_no, $date_,
90                         $charge_act, 0, 0, "", $charge, $currency);
91         }
92         // do the destination account postings
93         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_gl_account, 0, 0, "",
94                 $amount, $currency);
95                 
96         /*Post a balance post if $total != 0 */
97         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
98         
99         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
100                 $date_, $amount, payment_person_types::misc(), "",
101                 $currency, "Cannot insert a destination bank transaction");
102
103         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, $from_gl_account, 
104                 $currency, payment_person_types::misc(), "");
105         
106         add_comments($trans_type, $trans_no, $date_, $memo_);
107
108         references::save($trans_type, $trans_no, $ref);
109         add_audit_trail($trans_type, $trans_no, $date_);
110
111         commit_transaction();
112
113         return $trans_no;
114 }
115
116 //----------------------------------------------------------------------------------
117 //      Add bank payment or deposit to database.
118 //
119 //      $from_account - bank account id
120 //  $item - transaction cart (line item's amounts in bank account's currency)
121 //  $person_type_id - defines type of $person_id identifiers
122 //  $person_id  - supplier/customer/other id
123 // $person_detail_id - customer branch id or not used
124 //
125 // returns an array of (inserted trans type, trans no)
126
127 function add_bank_transaction($trans_type, $from_account, $items, $date_,
128         $person_type_id, $person_id, $person_detail_id, $ref, $memo_)
129 {
130         // we can only handle type 1 (payment)and type 2 (deposit)
131         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
132                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
133
134         $do_exchange_variance = false;
135         
136         begin_transaction();
137
138         $currency = get_bank_account_currency($from_account);
139         $bank_gl_account = get_bank_gl_account($from_account);
140
141         // the gl items are already inversed/negated for type 2 (deposit)
142         $total_amount = $items->gl_items_total();
143
144     if ($person_type_id == payment_person_types::customer())
145     {
146         // we need to add a customer transaction record
147
148                 // convert to customer currency
149                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
150                 // we need to negate it too
151                 $cust_amount = -$cust_amount;
152
153                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
154                 $ref, $cust_amount);
155
156     }
157     elseif ($person_type_id == payment_person_types::supplier())
158     {
159         // we need to add a supplier transaction record
160                 // convert to supp currency
161                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
162
163                 // we need to negate it too
164                 $supp_amount = -$supp_amount;
165
166                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
167                 $ref, "", $supp_amount, 0, 0);
168
169     }
170     else
171     {
172         $trans_no = get_next_trans_no($trans_type);
173         $do_exchange_variance = true;
174     }
175
176         // do the source account postings
177
178     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
179         $date_, -$total_amount,
180         $person_type_id, $person_id,
181         $currency,
182         "Cannot insert a source bank transaction");
183         $total = 0;
184         foreach ($items->gl_items as $gl_item)
185         {
186                 $is_bank_to = is_bank_account($gl_item->code_id);
187
188                 if ($trans_type == 1 AND $is_bank_to)
189                 {
190                         // we don't allow payments to go to a bank account. use transfer for this !
191                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
192                 }
193
194         // do the destination account postings
195         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
196                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
197                 $gl_item->amount, $currency, $person_type_id, $person_id);
198
199         if ($is_bank_to)
200         {
201                 add_bank_trans($trans_type, $trans_no, $is_bank_to, $ref,
202                         $date_, $gl_item->amount,
203                         $person_type_id, $person_id, $currency,
204                         "Cannot insert a destination bank transaction");
205                 if ($do_exchange_variance)
206                         add_exchange_variation($trans_type, $trans_no, $date_, $is_bank_to, $gl_item->code_id, 
207                                 $currency, $person_type_id, $person_id);
208         }
209                 // store tax details if the gl account is a tax account
210
211                 $amount = $gl_item->amount;
212                 $ex_rate = get_exchange_rate_from_home_currency($currency, $date_);
213                         
214                 add_gl_tax_details($gl_item->code_id, $trans_type, $trans_no, $amount,
215                         $ex_rate, $date_, $memo_);
216         }
217
218         // do the source account postings
219     add_gl_trans($trans_type, $trans_no, $date_, $bank_gl_account, 0, 0, $memo_,
220         -$total, null, $person_type_id, $person_id);
221
222     if ($do_exchange_variance)
223         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, $bank_gl_account, 
224                 $currency, $person_type_id, $person_id);
225
226         add_comments($trans_type, $trans_no, $date_, $memo_);
227
228         references::save($trans_type, $trans_no, $ref);
229         add_audit_trail($trans_type, $trans_no, $date_);
230
231         commit_transaction();
232
233         return array($trans_type, $trans_no);
234 }
235
236 //----------------------------------------------------------------------------------------
237
238 ?>