public static function convert($currency, $amount, $destCurrency = false, $date = false, $format = "%m") { $date or $date = date("Y-m-d"); $destCurrency or $destCurrency = config::get_config_item("currency"); $er = exchangeRate::get_er($currency, $destCurrency, $date); return page::money($destCurrency, $amount * $er, $format); }
function save() { // These need to be in here instead of validate(), because // validate is called after save() and we need these values set for save(). $this->get_value("currencyTypeID") or $this->set_value("currencyTypeID", config::get_config_item("currency")); $this->get_value("destCurrencyTypeID") or $this->set_value("destCurrencyTypeID", config::get_config_item("currency")); // The data prior to the save $old = $this->all_row_fields; if ($old["status"] != $this->get_value("status") && $this->get_value("status") == "approved") { $this->set_value("dateApproved", date("Y-m-d")); $field_changed = true; } else { if ($this->get_value("status") != "approved") { $this->set_value("dateApproved", ""); } } if ($old["currencyTypeID"] != $this->get_value("currencyTypeID")) { $field_changed = true; } if ($old["destCurrencyTypeID"] != $this->get_value("destCurrencyTypeID")) { $field_changed = true; } $db = new db_alloc(); // If there already is an exchange rate set for an approved // transaction, then there's no need to update the exchange rate if ($this->get_value("exchangeRate") && $this->get_value("dateApproved") && !$field_changed) { // Else update the transaction's exchange rate } else { $this->get_value("transactionCreatedTime") and $date = format_date("Y-m-d", $this->get_value("transactionCreatedTime")); $this->get_value("transactionModifiedTime") and $date = format_date("Y-m-d", $this->get_value("transactionModifiedTime")); $this->get_value("transactionDate") and $date = $this->get_value("transactionDate"); $this->get_value("dateApproved") and $date = $this->get_value("dateApproved"); $er = exchangeRate::get_er($this->get_value("currencyTypeID"), $this->get_value("destCurrencyTypeID"), $date); if (!$er) { alloc_error("Unable to determine exchange rate for " . $this->get_value("currencyTypeID") . " to " . $this->get_value("destCurrencyTypeID") . " for date: " . $date); } else { $this->set_value("exchangeRate", $er); } } return parent::save(); }