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");
14 //----------------------------------------------------------------------------------
16 function is_company_currency($currency)
18 return (get_company_currency() == $currency);
21 //----------------------------------------------------------------------------------
23 function get_company_currency()
25 return get_company_pref('curr_default');
28 //----------------------------------------------------------------------------------
30 function get_exchange_rate_from_home_currency($currency_code, $date_)
32 if ($currency_code == get_company_currency() || $currency_code == null)
36 $rate = get_last_exchange_rate($currency_code, $date_);
40 // no stored exchange rate, just return 1
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_));
47 return $rate['rate_buy'];
50 //----------------------------------------------------------------------------------
52 function get_exchange_rate_to_home_currency($currency_code, $date_)
54 return 1 / get_exchange_rate_from_home_currency($currency_code, $date_);
57 //----------------------------------------------------------------------------------
59 function to_home_currency($amount, $currency_code, $date_)
61 $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
62 return round2($amount / $ex_rate, user_price_dec());
65 //----------------------------------------------------------------------------------
67 function get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_)
69 // echo "converting from $from_curr_code to $to_curr_code <BR>";
70 if ($from_curr_code == $to_curr_code)
73 $home_currency = get_company_currency();
74 if ($to_curr_code == $home_currency)
76 return get_exchange_rate_to_home_currency($from_curr_code, $date_);
79 if ($from_curr_code == $home_currency)
81 return get_exchange_rate_from_home_currency($to_curr_code, $date_);
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_);
88 //--------------------------------------------------------------------------------
90 function exchange_from_to($amount, $from_curr_code, $to_curr_code, $date_)
92 $ex_rate = get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_);
93 return $amount / $ex_rate;
96 //--------------------------------------------------------------------------------
97 // Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
99 function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $amount, $person_type, $neg=false)
101 global $systypes_array;
103 if ($person_type == PT_CUSTOMER)
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']);
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']);
123 if (is_company_currency($curr))
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)
129 $diff = $inv_amt - $pay_amt;
130 if ($person_type == PT_SUPPLIER)
134 $exc_var_act = get_company_pref('exchange_diff_act');
135 if (date1_greater_date2($date, $pyt_date))
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);
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);