3 include_once($path_to_root . "/gl/includes/gl_db.inc");
5 //----------------------------------------------------------------------------------
7 function is_bank_account($account_code)
9 $sql= "SELECT account_code FROM ".TB_PREF."bank_accounts WHERE account_code='$account_code'";
10 $result = db_query($sql, "retreive bank account currency");
12 return (db_num_rows($result) > 0);
15 //----------------------------------------------------------------------------------
17 function is_company_currency($currency)
19 return (get_company_currency() == $currency);
22 //----------------------------------------------------------------------------------
24 function get_company_currency()
26 $sql= "SELECT curr_default FROM ".TB_PREF."company";
27 $result = db_query($sql, "retreive company currency");
29 if (db_num_rows($result) == 0)
30 display_db_error("Could not find the requested currency. Fatal.", $sql);
32 $myrow = db_fetch_row($result);
36 //----------------------------------------------------------------------------------
38 function get_bank_account_currency($bankAccount)
40 $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE account_code='$bankAccount'";
41 $result = db_query($sql, "retreive bank account currency");
43 $myrow = db_fetch_row($result);
47 //----------------------------------------------------------------------------------
49 function get_customer_currency($customer_id)
51 $sql = "SELECT curr_code FROM ".TB_PREF."debtors_master WHERE debtor_no = '$customer_id'";
53 $result = db_query($sql, "Retreive currency of customer $customer_id");
55 $myrow=db_fetch_row($result);
59 //----------------------------------------------------------------------------------
61 function get_supplier_currency($supplier_id)
63 $sql = "SELECT curr_code FROM ".TB_PREF."suppliers WHERE supplier_id = '$supplier_id'";
65 $result = db_query($sql, "Retreive currency of supplier $supplier_id");
67 $myrow=db_fetch_row($result);
71 //----------------------------------------------------------------------------------
73 function get_exchange_rate_from_home_currency($currency_code, $date_)
75 if ($currency_code == get_company_currency())
78 $date = date2sql($date_);
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";
83 $result = db_query($sql, "could not query exchange rates");
85 if (db_num_rows($result) == 0)
87 // no stored exchange rate, just return 1
91 $myrow = db_fetch_row($result);
95 //----------------------------------------------------------------------------------
97 function get_exchange_rate_to_home_currency($currency_code, $date_)
99 return 1 / get_exchange_rate_from_home_currency($currency_code, $date_);
102 //----------------------------------------------------------------------------------
104 function to_home_currency($amount, $currency_code, $date_)
106 $ex_rate = get_exchange_rate_to_home_currency($currency_code, $date_);
107 return round($amount / $ex_rate, user_price_dec());
110 //----------------------------------------------------------------------------------
112 function get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_)
114 // echo "converting from $from_curr_code to $to_curr_code <BR>";
115 if ($from_curr_code == $to_curr_code)
118 $home_currency = get_company_currency();
119 if ($to_curr_code == $home_currency)
121 return get_exchange_rate_to_home_currency($from_curr_code, $date_);
124 if ($from_curr_code == $home_currency)
126 return get_exchange_rate_from_home_currency($to_curr_code, $date_);
129 // neither from or to are the home currency
130 return get_exchange_rate_to_home_currency($from_curr_code, $date_) / get_exchange_rate_to_home_currency($to_curr_code, $date_);
133 //--------------------------------------------------------------------------------
135 function exchange_from_to($amount, $from_curr_code, $to_curr_code, $date_)
137 $ex_rate = get_exchange_rate_from_to($from_curr_code, $to_curr_code, $date_);
138 return $amount / $ex_rate;
141 //--------------------------------------------------------------------------------