/** * Format and convert a number according to currency rules * * @param unknown_type $amount * @param unknown_type $currency * @return unknown_type */ public static function _($amount, $currency = '', $options = '') { // default to whatever is in config $config = DSC::getApp(); $options = (array) $options; $default_currencyid = $config->get('default_currencyid', '1'); $num_decimals = isset($options['num_decimals']) ? $options['num_decimals'] : $config->get('currency_num_decimals', '2'); $thousands = isset($options['thousands']) ? $options['thousands'] : $config->get('currency_thousands', ','); $decimal = isset($options['decimal']) ? $options['decimal'] : $config->get('currency_decimal', '.'); $pre = isset($options['pre']) ? $options['pre'] : $config->get('currency_symbol_pre', '$'); $post = isset($options['post']) ? $options['post'] : $config->get('currency_symbol_post', ''); // Now check the session variable to see if there is a currency setting there $session_currency = DSCHelper::getSessionVariable('currency_id', 0); if ($session_currency) { // Let the code below deal with currency loading $currency = $session_currency; } // if currency is an object, use it's properties if (is_object($currency)) { $table = $currency; $num_decimals = $table->currency_decimals; $thousands = $table->thousands_separator; $decimal = $table->decimal_separator; $pre = $table->symbol_left; $post = $table->symbol_right; if ($default_currencyid != $table->currency_id) { $convertTo = $table->currency_code; } } elseif (!empty($currency) && is_numeric($currency)) { // TODO if currency is an integer, load the object for its id JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sample' . DS . 'tables'); $table = JTable::getInstance('Currencies', 'DSCTable'); $table->load((int) $currency); if (!empty($table->currency_id)) { $num_decimals = $table->currency_decimals; $thousands = $table->thousands_separator; $decimal = $table->decimal_separator; $pre = $table->symbol_left; $post = $table->symbol_right; if ($default_currencyid != $currency) { $convertTo = $table->currency_code; } } } elseif (!empty($currency)) { // TODO if currency is a string (currency_code) load the object for its code JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sample' . DS . 'tables'); $table = JTable::getInstance('Currencies', 'DSCTable'); $keynames = array(); $keynames['currency_code'] = (string) $currency; $table->load($keynames); if (!empty($table->currency_id)) { $num_decimals = $table->currency_decimals; $thousands = $table->thousands_separator; $decimal = $table->decimal_separator; $pre = $table->symbol_left; $post = $table->symbol_right; if ($default_currencyid != $table->currency_id) { $convertTo = $table->currency_code; } } } // if the currency code we're using is diff from the store-wide currency, then we need to convert the amount if (!empty($convertTo)) { JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sample' . DS . 'tables'); $table = JTable::getInstance('Currencies', 'DSCTable'); $table->load((int) $default_currencyid); DSC::load('DSCHelperCurrency', 'helpers.currency'); $amount = DSCHelperCurrency::convert($table->currency_code, $convertTo, $amount); } $return = $pre . number_format($amount, $num_decimals, $decimal, $thousands) . $post; return $return; }