Back to PhocacartCartCalculation class

Method correctSubTotal

public
correctSubTotal
(mixed &$item, mixed &$total)

Method correctSubTotal - Source code

// Correct rounding errors
// When we count the tax and quantity, we need to do without rounding because of VAT
// This is why we run $price->getPriceItems without rounding Output
// Ok, now rounding, this means, we can get difference e.g. 0.01 so such we need to correct
// - in total, even on the row
// This can happen when using Exclusive Tax - this is common problem with exclusive tax counting
public function correctSubTotal(&$item, &$total)
{
    // Fix when discount is larger than final netto (this can happen due to rounding, so it can happen that netto is e.g. - 0.01
    if ($total['netto'] < 0) {
        $item['finaldiscount'] += $total['netto'];
        $total['netto'] = 0;
    }
    // Fixed VAT
    if ($item['taxcalctype'] == 2) {
        //return;
    }
    $price = new PhocacartPrice();
    $quantityCorrect = $item['quantity'] > 0 ? $item['quantity'] : 1;
    $nettoNotRounded = $item['netto'] * $quantityCorrect;
    $taxNotRounded = $item['tax'] * $quantityCorrect;
    $nettoRounded = $price->roundPrice($item['netto'] * $quantityCorrect);
    $bruttoRounded = $price->roundPrice($item['brutto'] * $quantityCorrect);
    $taxRounded = $price->roundPrice($item['tax'] * $quantityCorrect);
    //- $nettoRounded 		= $item['netto'] * $quantityCorrect;
    //- $bruttoRounded 		= $item['brutto'] * $quantityCorrect;
    //- $taxRounded 		= $item['tax'] * $quantityCorrect;
    $nettoRoundedCorrected = $bruttoRounded - $taxRounded;
    if ($nettoNotRounded > 0) {
        if (abs(($nettoRoundedCorrected - $nettoNotRounded) / $nettoNotRounded) < 1.0E-5) {
            // the floats are the same
        } else {
            $item['netto'] = $nettoRoundedCorrected / $quantityCorrect;
            $total['netto'] = $total['netto'] + $nettoRoundedCorrected / $item['quantity'] - $nettoNotRounded / $quantityCorrect;
            if (!empty($total['tax'])) {
                foreach ($total['tax'] as $kT => $vT) {
                    if ($kT == $item['taxkey']) {
                        $total['tax'][$kT]['netto'] = $vT['netto'] + $nettoRoundedCorrected / $item['quantity'] - $nettoNotRounded / $quantityCorrect;
                        $total['tax'][$kT]['tax'] = $vT['tax'];
                        $total['tax'][$kT]['brutto'] = $total['tax'][$kT]['tax'] + $total['tax'][$kT]['netto'];
                    }
                }
            }
        }
    }
}