Back to PhocacartCartCalculation class

Method roundFixedAmountDiscount

public
roundFixedAmountDiscount
(mixed &$total)

Method roundFixedAmountDiscount - Source code

/*
    public function correctSubTotalAll(&$fullItems, &$total) {

        foreach($fullItems as $k => $v) {
            $price				= new PhocacartPrice();
            $quantityCorrect	= $v['quantity'] > 0 ? $v['quantity'] : 1;
            $nettoNotRounded	= $v['netto'] * $quantityCorrect;
            $taxNotRounded		= $v['tax'] * $quantityCorrect;
            $nettoRounded 		= $price->roundPrice($v['netto'] * $quantityCorrect);
            $bruttoRounded 		= $price->roundPrice($v['brutto'] * $quantityCorrect);
            $taxRounded 		= $price->roundPrice($v['tax'] * $quantityCorrect);

            $nettoRoundedCorrected = $bruttoRounded - $taxRounded;

            if ($nettoNotRounded > 0) {
                if (abs(($nettoRoundedCorrected - $nettoNotRounded)/$nettoNotRounded) < 0.00001) {
                    // the floats are the same
                } else {

                    $fullItems[$k]['netto'] = $nettoRoundedCorrected / $quantityCorrect;
                    $total['netto'] = $total['netto'] + ($nettoRoundedCorrected / $v['quantity']) - ($nettoNotRounded / $quantityCorrect);

                    if (!empty($total['tax'])) {
                        foreach($total['tax'] as $kT => $vT) {
                            if ($kT == $v['taxkey']) {
                                $total['tax'][$kT]['tax'] = $vT['tax'] + ($taxRounded / $quantityCorrect) - ($taxNotRounded / $quantityCorrect);
                            }
                        }
                    }
                }
            }
        }
    }*/
public function roundFixedAmountDiscount(&$total)
{
    $app = Factory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    $rounding_calculation_fad = $paramsC->get('rounding_calculation_fixed_amount_discount', -1);
    if ($rounding_calculation_fad < 0) {
        return;
    }
    $discount = 0;
    if (!empty($total['discountcartfixedamount'])) {
        foreach ($total['discountcartfixedamount'] as $k => $v) {
            if (isset($v['discount'])) {
                if (isset($v['netto']) && $v['netto'] < $v['discount']) {
                    $discount = $v['netto'];
                    // more pieces of product - so the cart discount is divided to more pieces of products, but max to cart discount fixed amount
                } else {
                    $discount = $v['discount'];
                    // one product - whole cart discount goes to one product
                }
            }
        }
    }
    if (isset($total['dnetto']) && $total['dnetto'] > 0 && $discount > 0) {
        $dif = $discount - $total['dnetto'];
        if ($dif > 0) {
            $total['rounding'] += $dif;
            $total['dnetto'] = $discount;
        } else {
            if ($dif < 0) {
                $total['rounding'] -= $dif;
                $total['dnetto'] = $discount;
                $total['netto'] += $dif;
            }
        }
    }
    return;
}