Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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
16 function is_company_currency($currency)
17 {
18         return (get_company_currency() == $currency);
19 }
20
21 //----------------------------------------------------------------------------------
22
23 function get_company_currency()
24 {
25         return get_company_pref('curr_default');
26 }
27
28 //----------------------------------------------------------------------------------
29
30 function get_exchange_rate_from_home_currency($currency_code, $date_)
31 {
32         if ($currency_code == get_company_currency() || $currency_code == null)
33                 return 1.0000;
34
35
36         $rate = get_last_exchange_rate($currency_code, $date_);
37
38         if (!$rate)
39         {
40                 // no stored exchange rate, just return 1
41                 display_error(
42                         sprintf(_("Cannot retrieve exchange rate for currency %s as of %s. Please add exchange rate manually on Exchange Rates page."),
43                                  $currency_code, $date_));
44                 return 1.000;
45         }
46
47         return $rate['rate_buy'];
48 }
49
50 //----------------------------------------------------------------------------------
51
52 function get_exchange_rate_to_home_currency($currency_code, $date_)
53 {
54         return 1 / get_exchange_rate_from_home_currency($currency_code, $date_);
55 }
56
57 //----------------------------------------------------------------------------------
58
59 function to_home_currency($amount, $currency_code, $date_)
60 {
61         $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
62         return round2($amount / $ex_rate,  user_price_dec());
63 }
64
65 //----------------------------------------------------------------------------------
66
67 function get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_)
68 {
69 //      echo "converting from $from_curr_code to $to_curr_code <BR>";
70         if ($from_curr_code == $to_curr_code)
71                 return 1.0000;
72
73         $home_currency = get_company_currency();
74         if ($to_curr_code == $home_currency)
75         {
76                 return get_exchange_rate_to_home_currency($from_curr_code, $date_);
77         }
78
79         if ($from_curr_code == $home_currency)
80         {
81                 return get_exchange_rate_from_home_currency($to_curr_code, $date_);
82         }
83
84         // neither from or to are the home currency
85          return get_exchange_rate_to_home_currency($from_curr_code, $date_) / get_exchange_rate_to_home_currency($to_curr_code, $date_);
86 }
87
88 //--------------------------------------------------------------------------------
89
90 function exchange_from_to($amount, $from_curr_code, $to_curr_code, $date_)
91 {
92         $ex_rate = get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_);
93         return $amount / $ex_rate;
94 }
95
96 //--------------------------------------------------------------------------------
97 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
98
99 function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $amount, $person_type, $neg=false)
100 {
101         global $systypes_array;
102         
103         if ($person_type == PT_CUSTOMER)
104         {
105                 $trans = get_customer_trans($trans_no, $type);
106                 $pyt_trans = get_customer_trans($pyt_no, $pyt_type);
107                 $cust_accs = get_branch_accounts($trans['branch_code']);
108                 $ar_ap_act = $cust_accs['receivables_account'];
109                 $person_id = $trans['debtor_no'];
110                 $curr = $trans['curr_code'];
111                 $date = sql2date($trans['tran_date']);
112         }
113         else
114         {
115                 $trans = get_supp_trans($trans_no, $type);
116                 $pyt_trans = get_supp_trans($pyt_no, $pyt_type);
117                 $supp_accs = get_supplier_accounts($trans['supplier_id']);
118                 $ar_ap_act = $supp_accs['payable_account'];
119                 $person_id = $trans['supplier_id'];
120                 $curr = $trans['curr_code'];
121                 $date = sql2date($trans['tran_date']);
122         }
123         if (is_company_currency($curr))
124                 return;
125         $inv_amt = round2($amount * $trans['rate'], user_price_dec()); 
126         $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec());
127         if ($inv_amt != $pay_amt)
128         {
129                 $diff = $inv_amt - $pay_amt;
130                 if ($person_type == PT_SUPPLIER)
131                         $diff = -$diff;
132                 if ($neg)
133                         $diff = -$diff;
134                 $exc_var_act = get_company_pref('exchange_diff_act');
135                 if (date1_greater_date2($date, $pyt_date))
136                 {
137                         $memo = $systypes_array[$pyt_type]." ".$pyt_no;
138                         add_gl_trans($type, $trans_no, $date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
139                         add_gl_trans($type, $trans_no, $date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
140                 }
141                 else
142                 {
143                         $memo = $systypes_array[$type]." ".$trans_no;
144                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
145                         add_gl_trans($pyt_type, $pyt_no, $pyt_date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
146                 }
147         }
148 }
149