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