Back to PhocacartCartCalculation class

Method roundTotalAmount

public
roundTotalAmount
(mixed &$total)

Method roundTotalAmount - Source code

public function roundTotalAmount(&$total)
{
    $price = new PhocacartPrice();
    $paramsC = PhocacartUtils::getComponentParameters();
    $rounding_calculation = $paramsC->get('rounding_calculation', 1);
    $rounding_calculation_total = $paramsC->get('rounding_calculation_total', 2);
    $currencyRate = PhocacartCurrency::getCurrentCurrencyRateIfNotDefault();
    if (!isset($total['brutto'])) {
        return false;
    }
    // ------------------------
    // 2) ROUNDING
    // ------------------------
    // !Important
    // Each currency has own total rounding and brutto
    if ($rounding_calculation_total > -1) {
        if ($currencyRate > 1 || $currencyRate < 1) {
            /*$totalBruttoCurrency 		= $total['brutto_currency'];//$total['brutto'] * $currencyRate;
                            $totalBruttoCurrencyRound	= round($totalBruttoCurrency , (int)$rounding_calculation_total, $rounding_calculation);
                            $bruttoCurrency 			= round($total['brutto_currency'], 2, $rounding_calculation);
                            if ($totalBruttoCurrency != $bruttoCurrency) {
            
                                $total['rounding_currency']	+= ($totalBruttoCurrencyRound - $bruttoCurrency);
                            }
                            $total['brutto_currency'] 	= $price->roundPrice($totalBruttoCurrencyRound);*/
            // 2a) ROUNDING ORDER CURRENCY
            $bruttoCurrency = round($total['brutto_currency'], (int) $rounding_calculation_total, $rounding_calculation);
            $diff = $price->roundPrice($bruttoCurrency) - $price->roundPrice($total['brutto_currency']);
            if (!($price->roundPrice($diff) > -0.01 && $price->roundPrice($diff) < 0.01)) {
                $total['rounding_currency'] += $diff;
                //	$total['taxrecapitulation']['rounding_currency']	+= $diff;
            }
            $total['brutto_currency'] = $price->roundPrice($bruttoCurrency);
            //	$total['taxrecapitulation']['brutto_currency'] 			= $price->roundPrice($bruttoCurrency);
            $bruttoCurrencyTax = round($total['brutto_currency_tax'], (int) $rounding_calculation_total, $rounding_calculation);
            $diffTax = $price->roundPrice($bruttoCurrencyTax) - $price->roundPrice($total['brutto_currency_tax']);
            if (!($price->roundPrice($diffTax) > -0.01 && $price->roundPrice($diffTax) < 0.01)) {
                //$total['rounding_currency']							+= $diff;
                $total['taxrecapitulation']['rounding_currency'] += $diffTax;
            }
            $total['brutto_currency_tax'] = $price->roundPrice($bruttoCurrencyTax);
            $total['taxrecapitulation']['brutto_currency'] = $price->roundPrice($bruttoCurrencyTax);
        }
        // We store default to database, so run it always
        // 2b) ROUNDING DEFAULT CURRENCY
        $brutto = round($total['brutto'], (int) $rounding_calculation_total, $rounding_calculation);
        $diff = $price->roundPrice($brutto) - $price->roundPrice($total['brutto']);
        if (!($price->roundPrice($diff) > -0.01 && $price->roundPrice($diff) < 0.01)) {
            $total['rounding'] += $diff;
            //$total['taxrecapitulation']['rounding']		+= $diff;
        }
        $total['brutto'] = $price->roundPrice($brutto);
        //$total['taxrecapitulation']['brutto']			= $price->roundPrice($brutto);
        $bruttoTax = round($total['brutto_tax'], (int) $rounding_calculation_total, $rounding_calculation);
        $diffTax = $price->roundPrice($bruttoTax) - $price->roundPrice($total['brutto_tax']);
        if (!($price->roundPrice($diffTax) > -0.01 && $price->roundPrice($diffTax) < 0.01)) {
            //$total['rounding']							+= $diff;
            $total['taxrecapitulation']['rounding'] += $diffTax;
        }
        $total['brutto_tax'] = $price->roundPrice($bruttoTax);
        $total['taxrecapitulation']['brutto'] = $price->roundPrice($bruttoTax);
    }
    // Final correction
    // Correct float, so we can compare to zero
    if (isset($total['rounding'])) {
        if ($price->roundPrice($total['rounding']) > -0.01 && $price->roundPrice($total['rounding']) < 0.01) {
            $total['rounding'] = (int) 0;
        }
    }
    // Correct float, so we can compare to zero
    if (isset($total['rounding_currency'])) {
        if ($price->roundPrice($total['rounding_currency']) > -0.01 && $price->roundPrice($total['rounding_currency']) < 0.01) {
            $total['rounding_currency'] = (int) 0;
        }
    }
    // Correct float, so we can compare to zero
    if (isset($total['brutto'])) {
        if ($price->roundPrice($total['brutto']) > -0.01 && $price->roundPrice($total['brutto']) < 0.01) {
            $total['brutto'] = (int) 0;
        }
    }
    // Correct float, so we can compare to zero
    if (isset($total['brutto_currency'])) {
        if ($price->roundPrice($total['brutto_currency']) > -0.01 && $price->roundPrice($total['brutto_currency']) < 0.01) {
            $total['brutto_currency'] = (int) 0;
        }
    }
    // Correct float, so we can compare to zero
    if (isset($total['taxrecapitulation']['rounding'])) {
        if ($price->roundPrice($total['taxrecapitulation']['rounding']) > -0.01 && $price->roundPrice($total['taxrecapitulation']['rounding']) < 0.01) {
            $total['taxrecapitulation']['rounding'] = (int) 0;
        }
    }
    // Correct float, so we can compare to zero
    if (isset($total['taxrecapitulation']['rounding_currency'])) {
        if ($price->roundPrice($total['taxrecapitulation']['rounding_currency']) > -0.01 && $price->roundPrice($total['taxrecapitulation']['rounding_currency']) < 0.01) {
            $total['taxrecapitulation']['rounding_currency'] = (int) 0;
        }
    }
    $total['taxrecapitulation']['brutto_incl_rounding'] = $total['taxrecapitulation']['brutto'];
    $total['taxrecapitulation']['brutto_currency_incl_rounding'] = $total['taxrecapitulation']['brutto_currency'];
    if ($rounding_calculation_total == -1) {
        // If not rounded here: e.g. 0.87 -> 1, 0.93 -> 1
        // We need to count standard rounding 0.8666 -> 0.87 (if rounded here, such will be included)
        if ($price->roundPrice($total['taxrecapitulation']['rounding']) > 0) {
            $total['taxrecapitulation']['brutto_incl_rounding'] = $total['taxrecapitulation']['brutto'] + $price->roundPrice($total['taxrecapitulation']['rounding']);
        }
        if ($price->roundPrice($total['taxrecapitulation']['rounding_currency']) > 0) {
            $total['taxrecapitulation']['brutto_currency_incl_rounding'] = $total['taxrecapitulation']['brutto_currency'] + $price->roundPrice($total['taxrecapitulation']['rounding_currency']);
        }
    }
    return true;
}