Fixed problems with excel report generation.
[fa-stable.git] / includes / 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 include_once($path_to_root . "/gl/includes/gl_db.inc");
13
14 //----------------------------------------------------------------------------------
15 //      Check if given account is used by any bank_account. 
16 //      Returns id of first bank_account using account_code, null otherwise.
17 //
18 //      Keep in mind that direct posting to bank account is depreciated
19 //      because we have no way to select right bank account if 
20 //      there is more than one using given gl account.
21 //
22 function is_bank_account($account_code)
23 {
24         $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code=".db_escape($account_code);
25         $result = db_query($sql, "checking account is bank account");
26         if (db_num_rows($result) > 0) {
27                 $acct = db_fetch($result);
28                 return $acct['id'];
29         } else
30                 return false;
31 }
32
33 //----------------------------------------------------------------------------------
34
35 function is_company_currency($currency)
36 {
37         return (get_company_currency() == $currency);
38 }
39
40 //----------------------------------------------------------------------------------
41
42 function get_company_currency()
43 {
44         return get_company_pref('curr_default');
45 }
46
47 //----------------------------------------------------------------------------------
48
49 function get_bank_account_currency($id)
50 {
51         $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE id=".db_escape($id);
52         $result = db_query($sql, "retreive bank account currency");
53
54         $myrow = db_fetch_row($result);
55         return $myrow[0];
56 }
57
58 //----------------------------------------------------------------------------------
59
60 function get_customer_currency($customer_id)
61 {
62     $sql = "SELECT curr_code FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id);
63
64         $result = db_query($sql, "Retreive currency of customer $customer_id");
65
66         $myrow=db_fetch_row($result);
67         return $myrow[0];
68 }
69
70 //----------------------------------------------------------------------------------
71
72 function get_supplier_currency($supplier_id)
73 {
74     $sql = "SELECT curr_code FROM ".TB_PREF."suppliers WHERE supplier_id = ".db_escape($supplier_id);
75
76         $result = db_query($sql, "Retreive currency of supplier $supplier_id");
77
78         $myrow=db_fetch_row($result);
79         return $myrow[0];
80 }
81
82 //----------------------------------------------------------------------------------
83
84 function get_exchange_rate_from_home_currency($currency_code, $date_)
85 {
86         if ($currency_code == get_company_currency() || $currency_code == null)
87                 return 1.0000;
88
89         $date = date2sql($date_);
90
91         $sql = "SELECT rate_buy, max(date_) as date_ FROM ".TB_PREF."exchange_rates WHERE curr_code = ".db_escape($currency_code)."
92                                 AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
93
94         $result = db_query($sql, "could not query exchange rates");
95
96         if (db_num_rows($result) == 0)
97         {
98                 // no stored exchange rate, just return 1
99                 display_error(
100                         sprintf(_("Cannot retrieve exchange rate for currency %s as of %s. Please add exchange rate manually on Exchange Rates page."),
101                                  $currency_code, $date_));
102                 return 1.000;
103         }
104
105         $myrow = db_fetch_row($result);
106         return $myrow[0];
107 }
108
109 //----------------------------------------------------------------------------------
110
111 function get_exchange_rate_to_home_currency($currency_code, $date_)
112 {
113         return 1 / get_exchange_rate_from_home_currency($currency_code, $date_);
114 }
115
116 //----------------------------------------------------------------------------------
117
118 function to_home_currency($amount, $currency_code, $date_)
119 {
120         $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
121         return round2($amount / $ex_rate,  user_price_dec());
122 }
123
124 //----------------------------------------------------------------------------------
125
126 function get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_)
127 {
128 //      echo "converting from $from_curr_code to $to_curr_code <BR>";
129         if ($from_curr_code == $to_curr_code)
130                 return 1.0000;
131
132         $home_currency = get_company_currency();
133         if ($to_curr_code == $home_currency)
134         {
135                 return get_exchange_rate_to_home_currency($from_curr_code, $date_);
136         }
137
138         if ($from_curr_code == $home_currency)
139         {
140                 return get_exchange_rate_from_home_currency($to_curr_code, $date_);
141         }
142
143         // neither from or to are the home currency
144          return get_exchange_rate_to_home_currency($from_curr_code, $date_) / get_exchange_rate_to_home_currency($to_curr_code, $date_);
145 }
146
147 //--------------------------------------------------------------------------------
148
149 function exchange_from_to($amount, $from_curr_code, $to_curr_code, $date_)
150 {
151         $ex_rate = get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_);
152         return $amount / $ex_rate;
153 }
154
155 //--------------------------------------------------------------------------------
156 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
157
158 function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $amount, $person_type, $neg=false)
159 {
160         global $systypes_array;
161         
162         if ($person_type == PT_CUSTOMER)
163         {
164                 $trans = get_customer_trans($trans_no, $type);
165                 $pyt_trans = get_customer_trans($pyt_no, $pyt_type);
166                 $cust_accs = get_branch_accounts($trans['branch_code']);
167                 $ar_ap_act = $cust_accs['receivables_account'];
168                 $person_id = $trans['debtor_no'];
169                 $curr = $trans['curr_code'];
170                 $date = sql2date($trans['tran_date']);
171         }
172         else
173         {
174                 $trans = get_supp_trans($trans_no, $type);
175                 $pyt_trans = get_supp_trans($pyt_no, $pyt_type);
176                 $supp_accs = get_supplier_accounts($trans['supplier_id']);
177                 $ar_ap_act = $supp_accs['payable_account'];
178                 $person_id = $trans['supplier_id'];
179                 $curr = $trans['curr_code'];
180                 $date = sql2date($trans['tran_date']);
181         }
182         if (is_company_currency($curr))
183                 return;
184         $inv_amt = round2($amount * $trans['rate'], user_price_dec()); 
185         $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec());
186         if ($inv_amt != $pay_amt)
187         {
188                 $diff = $inv_amt - $pay_amt;
189                 if ($person_type == PT_SUPPLIER)
190                         $diff = -$diff;
191                 if ($neg)
192                         $diff = -$diff;
193                 $exc_var_act = get_company_pref('exchange_diff_act');
194                 if (date1_greater_date2($date, $pyt_date))
195                 {
196                         $memo = $systypes_array[$pyt_type]." ".$pyt_no;
197                         add_gl_trans($type, $trans_no, $date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
198                         add_gl_trans($type, $trans_no, $date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
199                 }
200                 else
201                 {
202                         $memo = $systypes_array[$type]." ".$trans_no;
203                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
204                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
205                 }
206         }
207 }
208
209 ?>