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