Cleanup: various random sql queries found in UI files moved to database interface...
[fa-stable.git] / gl / includes / db / gl_db_rates.inc
index 70ab230a630b3e2758ed64080b3fe675ca8cb8f8..9a64fcfa9e698d600466ecac6623e90b25891ae0 100644 (file)
@@ -33,6 +33,25 @@ function get_date_exchange_rate($curr_code, $date_)
                return $row[0];
 }
 
+/*
+       Return last exchange rate of $currency not later than $date.
+*/
+function get_last_exchange_rate($curr_code, $date_)
+{
+       $date = date2sql($date_);
+
+       $sql = "SELECT rate_buy, max(date_) as date_ FROM ".TB_PREF."exchange_rates WHERE curr_code = ".db_escape($curr_code)."
+                               AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
+
+       $result = db_query($sql, "could not query exchange rates");
+
+
+       if (db_num_rows($result) == 0)
+               return false;
+
+       return db_fetch_row($result);
+}
+
 //---------------------------------------------------------------------------------------------
 
 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
@@ -218,11 +237,11 @@ function get_extern_rate($curr_b, $provider = 'ECB', $date)
 
 //-----------------------------------------------------------------------------
 
-function get_sql_for_exchange_rates()
+function get_sql_for_exchange_rates($curr)
 {
        $sql = "SELECT date_, rate_buy, id FROM "
                .TB_PREF."exchange_rates "
-               ."WHERE curr_code=".db_escape($_POST['curr_abrev'])."
+               ."WHERE curr_code=".db_escape($curr)."
                 ORDER BY date_ DESC";
        return $sql;     
 }