示例#1
0
 public function testformat_place_symbol()
 {
     $this->assertEquals('R 100', format_place_symbol(100, 'R', true));
     $this->assertEquals('R100', format_place_symbol(100, 'R', false));
     $this->assertEquals('100', format_place_symbol(100, '', false));
 }
示例#2
0
/**
 * Copy of format_number() from currency with fix for sugarpdf.
 * @return String formatted currency value
 * @see modules/Currencies/Currency.php
 */
function format_number_sugarpdf($amount, $round = null, $decimals = null, $params = array())
{
    global $app_strings, $current_user, $sugar_config, $locale;
    static $current_users_currency = null;
    static $last_override_currency = null;
    static $override_currency_id = null;
    static $currency;
    $seps = get_number_seperators();
    $num_grp_sep = $seps[0];
    $dec_sep = $seps[1];
    // cn: bug 8522 - sig digits not honored in pdfs
    if (is_null($decimals)) {
        $decimals = $locale->getPrecision();
    }
    if (is_null($round)) {
        $round = $locale->getPrecision();
    }
    // only create a currency object if we need it
    if (!empty($params['currency_symbol']) && $params['currency_symbol'] || !empty($params['convert']) && $params['convert'] || !empty($params['currency_id'])) {
        // if we have an override currency_id
        if (!empty($params['currency_id'])) {
            if ($override_currency_id != $params['currency_id']) {
                $override_currency_id = $params['currency_id'];
                $currency = new Currency();
                $currency->retrieve($override_currency_id);
                $last_override_currency = $currency;
            } else {
                $currency = $last_override_currency;
            }
        } elseif (!isset($current_users_currency)) {
            // else use current user's
            $current_users_currency = new Currency();
            if ($current_user->getPreference('currency')) {
                $current_users_currency->retrieve($current_user->getPreference('currency'));
            } else {
                $current_users_currency->retrieve('-99');
            }
            // use default if none set
            $currency = $current_users_currency;
        }
    }
    if (!empty($params['convert']) && $params['convert']) {
        $amount = $currency->convertFromDollar($amount, 6);
    }
    if (!empty($params['currency_symbol']) && $params['currency_symbol']) {
        if (!empty($params['symbol_override'])) {
            $symbol = $params['symbol_override'];
        } elseif (!empty($params['type']) && $params['type'] == 'sugarpdf') {
            $symbol = $currency->symbol;
            $symbol_space = false;
        } else {
            if (empty($currency->symbol)) {
                $symbol = $currency->getDefaultCurrencySymbol();
            } else {
                $symbol = $currency->symbol;
            }
            $symbol_space = true;
        }
    } else {
        $symbol = '';
    }
    if (isset($params['charset_convert'])) {
        $symbol = $locale->translateCharset($symbol, 'UTF-8', $locale->getExportCharset());
    }
    if (empty($params['human'])) {
        $amount = number_format(round($amount, $round), $decimals, $dec_sep, $num_grp_sep);
        $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
    } else {
        // If amount is more greater than a thousand(postiive or negative)
        if (strpos($amount, '.') > 0) {
            $checkAmount = strlen(substr($amount, 0, strpos($amount, '.')));
        }
        if ($checkAmount >= 1000 || $checkAmount <= -1000) {
            $amount = round($amount / 1000, 0);
            $amount = $amount . 'k';
            $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
        } else {
            $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
        }
    }
    if (!empty($params['percentage']) && $params['percentage']) {
        $amount .= $app_strings['LBL_PERCENTAGE_SYMBOL'];
    }
    return $amount;
}
/**
 * function format_number($amount, $round = 2, $decimals = 2, $params = array()) 
 * 
 * number formatting
 *
 * @param FLOAT $amount - # to be converted
 * @param INT $round - # of places to round (can be -)
 * @param INT $decimals - floating point precision
 * 
 * The following are passed in as an array of params:
 * @param BOOL $params['currency_symbol'] - true to display currency symbol
 * @param BOOL $params['convert'] - true to convert from USD dollar
 * @param BOOL $params['percentage'] - true to display % sign
 * @param BOOL $params['symbol_space'] - true to have space between currency symbol and amount
 * @param STRING $params['symbol_override'] - string to over default currency symbol
 * @param STRING $params['type'] - pass in 'pdf' for pdf currency symbol conversion
 * @param GUID $params['currency_id'] - currency_id to retreive, defaults to current user
 * 
 * @return STRING $amount - formatted number 
 */
function format_number($amount, $round = 2, $decimals = 2, $params = array())
{
    global $app_strings, $current_user, $sugar_config, $locale;
    static $current_users_currency = null;
    static $last_override_currency = null;
    static $override_currency_id = null;
    static $currency;
    $seps = get_number_seperators();
    $num_grp_sep = $seps[0];
    $dec_sep = $seps[1];
    // only create a currency object if we need it
    if (!empty($params['currency_symbol']) && $params['currency_symbol'] || !empty($params['convert']) && $params['convert'] || !empty($params['currency_id'])) {
        // if we have an override currency_id
        if (!empty($params['currency_id'])) {
            if ($override_currency_id != $params['currency_id']) {
                $override_currency_id = $params['currency_id'];
                $currency = new Currency();
                $currency->retrieve($override_currency_id);
                $last_override_currency = $currency;
            } else {
                $currency = $last_override_currency;
            }
        } elseif (!isset($current_users_currency)) {
            // else use current user's
            $current_users_currency = new Currency();
            if ($current_user->getPreference('currency')) {
                $current_users_currency->retrieve($current_user->getPreference('currency'));
            } else {
                $current_users_currency->retrieve('-99');
            }
            // use default if none set
            $currency = $current_users_currency;
        }
    }
    if (!empty($params['convert']) && $params['convert']) {
        $amount = $currency->convertFromDollar($amount, 6);
    }
    if (!empty($params['currency_symbol']) && $params['currency_symbol']) {
        if (!empty($params['symbol_override'])) {
            $symbol = $params['symbol_override'];
        } elseif (!empty($params['type']) && $params['type'] == 'pdf') {
            $symbol = $currency->getPdfCurrencySymbol();
            $symbol_space = false;
        } else {
            if (empty($currency->symbol)) {
                $symbol = $currency->getDefaultCurrencySymbol();
            } else {
                $symbol = $currency->symbol;
            }
            $symbol_space = true;
        }
    } else {
        $symbol = '';
    }
    if (isset($params['charset_convert'])) {
        $symbol = $locale->translateCharset($symbol, 'UTF-8', $locale->getExportCharset());
    }
    //TODO: display human readable - easy
    $human = false;
    if ($human == false) {
        $amount = number_format(round($amount, $round), $decimals, $dec_sep, $num_grp_sep);
        $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
    } else {
        //call for the switch
        if ($amount > 1000) {
            $amount = round($amount / 1000, 0);
            $amount = $amount . 'k';
            $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
        } else {
            $amount = format_place_symbol($amount, $symbol, empty($params['symbol_space']) ? false : true);
        }
        //end call for switch
    }
    if (!empty($params['percentage']) && $params['percentage']) {
        $amount .= $app_strings['LBL_PERCENTAGE_SYMBOL'];
    }
    return $amount;
}