Eliminated non-static method calls and some more fixes to avoid log warnings on php4&5
[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='$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         $sql= "SELECT curr_default FROM ".TB_PREF."company";
45         $result = db_query($sql, "retreive company currency");
46
47         if (db_num_rows($result) == 0)
48                 display_db_error("Could not find the requested currency. Fatal.", $sql);
49
50         $myrow = db_fetch_row($result);
51         return $myrow[0];
52 }
53
54 //----------------------------------------------------------------------------------
55
56 function get_bank_account_currency($id)
57 {
58         $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE id='$id'";
59         $result = db_query($sql, "retreive bank account currency");
60
61         $myrow = db_fetch_row($result);
62         return $myrow[0];
63 }
64
65 //----------------------------------------------------------------------------------
66
67 function get_customer_currency($customer_id)
68 {
69     $sql = "SELECT curr_code FROM ".TB_PREF."debtors_master WHERE debtor_no = '$customer_id'";
70
71         $result = db_query($sql, "Retreive currency of customer $customer_id");
72
73         $myrow=db_fetch_row($result);
74         return $myrow[0];
75 }
76
77 //----------------------------------------------------------------------------------
78
79 function get_supplier_currency($supplier_id)
80 {
81     $sql = "SELECT curr_code FROM ".TB_PREF."suppliers WHERE supplier_id = '$supplier_id'";
82
83         $result = db_query($sql, "Retreive currency of supplier $supplier_id");
84
85         $myrow=db_fetch_row($result);
86         return $myrow[0];
87 }
88
89 //----------------------------------------------------------------------------------
90
91 function get_exchange_rate_from_home_currency($currency_code, $date_)
92 {
93         if ($currency_code == get_company_currency())
94                 return 1.0000;
95
96         $date = date2sql($date_);
97
98         $sql = "SELECT rate_buy, max(date_) as date_ FROM ".TB_PREF."exchange_rates WHERE curr_code = '$currency_code'
99                                 AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
100
101         $result = db_query($sql, "could not query exchange rates");
102
103         if (db_num_rows($result) == 0)
104         {
105                 // no stored exchange rate, just return 1
106                 display_error(
107                         sprintf(_("Cannot retrieve exchange rate for currency %s as of %s. Please add exchange rate manually on Exchange Rates page."),
108                                  $currency_code, $date_));
109                 return 1.000;
110         }
111
112         $myrow = db_fetch_row($result);
113         return $myrow[0];
114 }
115
116 //----------------------------------------------------------------------------------
117
118 function get_exchange_rate_to_home_currency($currency_code, $date_)
119 {
120         return 1 / get_exchange_rate_from_home_currency($currency_code, $date_);
121 }
122
123 //----------------------------------------------------------------------------------
124
125 function to_home_currency($amount, $currency_code, $date_)
126 {
127         $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
128         return round2($amount / $ex_rate,  user_price_dec());
129 }
130
131 //----------------------------------------------------------------------------------
132
133 function get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_)
134 {
135 //      echo "converting from $from_curr_code to $to_curr_code <BR>";
136         if ($from_curr_code == $to_curr_code)
137                 return 1.0000;
138
139         $home_currency = get_company_currency();
140         if ($to_curr_code == $home_currency)
141         {
142                 return get_exchange_rate_to_home_currency($from_curr_code, $date_);
143         }
144
145         if ($from_curr_code == $home_currency)
146         {
147                 return get_exchange_rate_from_home_currency($to_curr_code, $date_);
148         }
149
150         // neither from or to are the home currency
151          return get_exchange_rate_to_home_currency($from_curr_code, $date_) / get_exchange_rate_to_home_currency($to_curr_code, $date_);
152 }
153
154 //--------------------------------------------------------------------------------
155
156 function exchange_from_to($amount, $from_curr_code, $to_curr_code, $date_)
157 {
158         $ex_rate = get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_);
159         return $amount / $ex_rate;
160 }
161
162 //--------------------------------------------------------------------------------
163 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
164
165 function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $amount, $person_type, $neg=false)
166 {
167         global $systypes_array;
168         
169         if ($person_type == PT_CUSTOMER)
170         {
171                 $trans = get_customer_trans($trans_no, $type);
172                 $pyt_trans = get_customer_trans($pyt_no, $pyt_type);
173                 $ar_ap_act = $trans['receivables_account'];
174                 $person_id = $trans['debtor_no'];
175                 $curr = $trans['curr_code'];
176                 $date = sql2date($trans['tran_date']);
177         }
178         else
179         {
180                 $trans = get_supp_trans($trans_no, $type);
181                 $pyt_trans = get_supp_trans($pyt_no, $pyt_type);
182                 $supp_accs = get_supplier_accounts($trans['supplier_id']);
183                 $ar_ap_act = $supp_accs['payable_account'];
184                 $person_id = $trans['supplier_id'];
185                 $curr = $trans['SupplierCurrCode'];
186                 $date = sql2date($trans['tran_date']);
187         }
188         if (is_company_currency($curr))
189                 return;
190         $inv_amt = round2($amount * $trans['rate'], user_price_dec()); 
191         $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec());
192         if ($inv_amt != $pay_amt)
193         {
194                 $diff = $inv_amt - $pay_amt;
195                 if ($person_type == PT_SUPPLIER)
196                         $diff = -$diff;
197                 if ($neg)
198                         $diff = -$diff;
199                 $exc_var_act = get_company_pref('exchange_diff_act');
200                 if (date1_greater_date2($date, $pyt_date))
201                 {
202                         $memo = $systypes_array[$pyt_type]." ".$pyt_no;
203                         add_gl_trans($type, $trans_no, $date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
204                         add_gl_trans($type, $trans_no, $date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
205                 }
206                 else
207                 {
208                         $memo = $systypes_array[$type]." ".$trans_no;
209                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
210                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
211                 }
212         }
213 }
214
215 ?>