Back to PhocacartCartCalculation class

Method taxRecapitulation

public static
taxRecapitulation
(mixed &$total, mixed $shippingCosts, mixed $paymentCosts)

Method taxRecapitulation - Source code

/*
    public function resetVariables(&$fullItems, $levels = array()) {
        if (!empty($levels)) {
            foreach($levels as $k => $v) {
                if (!empty($fullItems[$k])) {
                    foreach($fullItems[$k] as $k => $v) {
                        $fullItems[$k]['discountproduct'] 		= 0;
                        $fullItems[$k]['discountcartfixedid'] 	= 0;
                        $fullItems[$k]['discounttitle'] 		= '';
                    }
                }
            }
        }
    }*/
public static function taxRecapitulation(&$total, $shippingCosts, $paymentCosts)
{
    if (empty($total)) {
        return;
    }
    //$app						= JFactory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    //$dynamic_tax_rate			= $paramsC->get( 'dynamic_tax_rate', 0 );
    $tax_calculation = $paramsC->get('tax_calculation', 0);
    $tax_calculation_shipping = $paramsC->get('tax_calculation_shipping', 0);
    $tax_calculation_payment = $paramsC->get('tax_calculation_payment', 0);
    // 0 ... don't fix anything
    // 1 ... fix TAX RECAPITULATION - when total brutto will change, change the total brutto and rounding for CART CALCULATION
    // 2 ... fix TAX RECAPITULATION - fix taxes in CART CALCULATION - total brutto and rounding will change even
    $tax_recapitulation = $paramsC->get('tax_recapitulation', 0);
    $round = 1;
    $currencyRate = PhocacartCurrency::getCurrentCurrencyRateIfNotDefault();
    $price = new PhocacartPrice();
    $total['taxrecapitulation'] = array();
    $total['taxrecapitulation']['brutto'] = 0;
    $total['taxrecapitulation']['netto'] = 0;
    $total['taxrecapitulation']['netto_incl_sp'] = 0;
    // netto including shipping and payment costs
    $total['taxrecapitulation']['tax'] = 0;
    $total['taxrecapitulation']['rounding'] = 0;
    $total['taxrecapitulation']['items'] = array();
    //	$total['taxrecapitulation']['diffbrutto']		= 0;// is cart calculation brutto different to tax recapitulation
    //	$total['taxrecapitulation']['corrected']		= 0;
    //$total['taxrecapitulation']['corrected_currency']= 0;
    // Currency
    $total['taxrecapitulation']['currency_rate'] = $currencyRate;
    $total['taxrecapitulation']['brutto_currency'] = 0;
    $total['taxrecapitulation']['rounding_currency'] = 0;
    $total['taxrecapitulation']['brutto_incl_rounding'] = 0;
    $total['taxrecapitulation']['brutto_currency_incl_rounding'] = 0;
    $total['taxsum'] = 0;
    // Sum all taxes in total so we can compare them because of rounding with $total['taxrecapitulation']['tax']
    // We compare $total['taxrecapitulation']['brutto'] and $total['brutto'] at the end BUT
    // there are 3 SETTINGS: Tax for products, Tax for Shipping and Tax for Payment $total['brutto'] can include amount of products which don't not belong to tax
    // $total['brutto_tax'] is brutt but only for taxable items (products, shipping, payment)
    $total['brutto_tax'] = $total['brutto'];
    $total['brutto_currency_tax'] = $total['brutto_currency'];
    if ($tax_calculation == 0) {
        $total['brutto_tax'] -= $total['netto'];
        $total['brutto_currency_tax'] -= $price->roundPrice($total['netto'] * $currencyRate);
    }
    if (!empty($total['tax']) && $tax_calculation > 0) {
        foreach ($total['tax'] as $k => $v) {
            $total['taxsum'] += $v['tax'];
            $netto = $v['netto'];
            $tax = $price->roundPrice($v['tax']);
            $brutto = $v['brutto'];
            $rate = $price->roundPrice($v['rate']);
            //$bruttoCurrency = $price->roundPrice(($v['brutto'] * $currencyRate));
            if ($tax_recapitulation > 0) {
                // Tax
                // - Tax changed by country/region is set
                // - $tax_calculation: no/inclusive/exclusive (set in options)
                // - $v['type']: fixed/percentage (set in product)
                // NO TAX
                if ($tax_calculation == 0) {
                    $tax = 0;
                    // EXCLUSIVE TAX
                } else {
                    if ($tax_calculation == 1) {
                        if ($v['type'] == 2) {
                            // FIX
                            $brutto = $netto + $tax;
                        } else {
                            // Percentage
                            $tax = $netto * ($rate / 100);
                            if ($round == 1) {
                                $tax = $price->roundPrice($tax);
                            }
                            $brutto = $netto + $tax;
                        }
                        // INCLUSIVE TAX
                    } else {
                        if ($tax_calculation == 2) {
                            if ($v['type'] == 2) {
                                // FIX
                                $netto = $brutto - $tax;
                            } else {
                                // Percentage
                                $tax = $brutto - $brutto / ($rate / 100 + 1);
                                if ($round == 1) {
                                    $tax = $price->roundPrice($tax);
                                }
                                $netto = $brutto - $tax;
                            }
                        }
                    }
                }
            }
            if ($round == 1) {
                $netto = $price->roundPrice($netto);
                $brutto = $price->roundPrice($brutto);
                $tax = $price->roundPrice($tax);
            }
            $total['taxrecapitulation']['items'][$k]['title'] = $v['title'];
            $total['taxrecapitulation']['items'][$k]['title_lang'] = $v['title_lang'];
            $total['taxrecapitulation']['items'][$k]['title_lang_suffix2'] = $v['title_lang_suffix2'];
            $total['taxrecapitulation']['items'][$k]['netto'] = $netto;
            $total['taxrecapitulation']['items'][$k]['tax'] = $tax;
            $total['taxrecapitulation']['items'][$k]['brutto'] = $brutto;
            // Currency
            //	$bruttoCurrency = $price->roundPrice($netto * $currencyRate) + $price->roundPrice($tax * $currencyRate);
            //	$total['taxrecapitulation']['items'][$k]['brutto_currency'] = $bruttoCurrency;
            $total['taxrecapitulation']['brutto'] += $brutto;
            // changed when tax inclusive
            $total['taxrecapitulation']['netto'] += $netto;
            // changed when tax exclusive
            $total['taxrecapitulation']['tax'] += $tax;
            // changed when tax exclusive
            // Currency
            //$total['taxrecapitulation']['brutto_currency'] 		+= $bruttoCurrency;
            if ($tax_recapitulation == 2) {
                $total['tax'][$k]['netto'] = $netto;
                $total['tax'][$k]['tax'] = $tax;
                $total['tax'][$k]['brutto'] = $brutto;
                //$total['tax'][$k]['brutto_currency']	= $bruttoCurrency;
            }
            if ($tax_recapitulation == 2) {
                //$total['taxrecapitulation']['rounding'] += ($price->roundPrice($total['tax'][$k]['tax']) - $price->roundPrice($total['taxrecapitulation']['items'][$k]['tax']));
            }
        }
        if ($tax_recapitulation == 2) {
            //$total['taxrecapitulation']['rounding'] += ($price->roundPrice($total['netto']) - $price->roundPrice($total['taxrecapitulation']['netto']));
        }
    }
    $total['taxrecapitulation']['netto_incl_sp'] = $total['taxrecapitulation']['netto'];
    // Shipping
    if ($tax_calculation_shipping == 0 && isset($shippingCosts['brutto'])) {
        $total['brutto_tax'] -= $shippingCosts['brutto'];
        $total['brutto_currency_tax'] -= $price->roundPrice($shippingCosts['brutto'] * $currencyRate);
    }
    if (isset($shippingCosts['taxkey']) && (int) $shippingCosts['taxkey']) {
        $netto = $shippingCosts['netto'];
        $tax = $shippingCosts['tax'];
        $brutto = $shippingCosts['brutto'];
        $taxkey = $shippingCosts['taxkey'];
        // Nothing to fix
        /*if ($correct_tax_recapitulation == 1) {
        
                        if ($v['type'] == 1) {
                            $tax 	= $price->roundPrice($netto * $shippingCosts['taxrate'] / 100);
                            $brutto = $shippingCosts['netto'] + $tax;
                        } else {
                            $brutto = $shippingCosts['netto'] + $tax;
                        }
                    }*/
        if (isset($total['taxrecapitulation']['items'][$taxkey]['netto'])) {
            $total['taxrecapitulation']['items'][$taxkey]['netto'] += $netto;
            $total['taxrecapitulation']['netto_incl_sp'] += $netto;
            //	$total['taxrecapitulation']['netto'] 					+= $netto;
        } else {
            $total['taxrecapitulation']['items'][$taxkey]['title'] = $shippingCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang'] = $shippingCosts['tax_title_lang'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang_suffix'] = $shippingCosts['tax_title_suffix'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang_suffix2'] = $shippingCosts['tax_title_suffix2'] != '' ? '(' . $shippingCosts['tax_title_suffix2'] . ')' : '';
            $total['taxrecapitulation']['items'][$taxkey]['netto'] = $netto;
            $total['taxrecapitulation']['netto_incl_sp'] = $netto;
            //	$total['taxrecapitulation']['netto'] 					= $netto;
        }
        if (isset($total['taxrecapitulation']['items'][$taxkey]['tax'])) {
            $total['taxrecapitulation']['items'][$taxkey]['tax'] += $tax;
            $total['taxrecapitulation']['tax'] += $tax;
        } else {
            //$total['taxrecapitulation']['items'][$taxkey]['title'] 	= $shippingCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['tax'] = $tax;
            $total['taxrecapitulation']['tax'] += $tax;
        }
        if (isset($total['taxrecapitulation']['items'][$taxkey]['brutto'])) {
            $total['taxrecapitulation']['items'][$taxkey]['brutto'] += $brutto;
            //	$total['taxrecapitulation']['items'][$taxkey]['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
            $total['taxrecapitulation']['brutto'] += $brutto;
            //	$total['taxrecapitulation']['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
        } else {
            //$total['taxrecapitulation']['items'][$taxkey]['title'] 	= $shippingCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['brutto'] = $brutto;
            //	$total['taxrecapitulation']['items'][$taxkey]['brutto_currency'] 	= $price->roundPrice($brutto * $currencyRate);
            $total['taxrecapitulation']['brutto'] += $brutto;
            //	$total['taxrecapitulation']['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
        }
    }
    // Payment
    if ($tax_calculation_payment == 0 && isset($paymentCosts['brutto'])) {
        $total['brutto_tax'] -= $paymentCosts['brutto'];
        $total['brutto_currency_tax'] -= $price->roundPrice($paymentCosts['brutto'] * $currencyRate);
    }
    if (isset($paymentCosts['taxkey']) && (int) $paymentCosts['taxkey']) {
        $netto = $paymentCosts['netto'];
        $tax = $paymentCosts['tax'];
        $brutto = $paymentCosts['brutto'];
        $taxkey = $paymentCosts['taxkey'];
        /*if ($correct_tax_recapitulation == 1) {
        
                        if ($v['type'] == 1) {
                            $tax 	= $price->roundPrice($netto * $paymentCosts['taxrate'] / 100);
                            $brutto = $paymentCosts['netto'] + $tax;
                        } else {
                            $brutto = $paymentCosts['netto'] + $tax;
                        }
                    }*/
        if (isset($total['taxrecapitulation']['items'][$taxkey]['netto'])) {
            $total['taxrecapitulation']['items'][$taxkey]['netto'] += $netto;
            $total['taxrecapitulation']['netto_incl_sp'] += $netto;
            //$total['taxrecapitulation']['netto_incl_sp'] 			+= $netto;
        } else {
            $total['taxrecapitulation']['items'][$taxkey]['title'] = $paymentCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang'] = $paymentCosts['tax_title_lang'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang_suffix'] = $paymentCosts['tax_title_suffix'];
            $total['taxrecapitulation']['items'][$taxkey]['title_lang_suffix2'] = $paymentCosts['tax_title_suffix2'] != '' ? '(' . $paymentCosts['tax_title_suffix2'] . ')' : '';
            $total['taxrecapitulation']['items'][$taxkey]['netto'] = $netto;
            $total['taxrecapitulation']['netto_incl_sp'] = $netto;
            //$total['taxrecapitulation']['netto'] 					= $netto;
        }
        if (isset($total['taxrecapitulation']['items'][$taxkey]['tax'])) {
            $total['taxrecapitulation']['items'][$taxkey]['tax'] += $tax;
            $total['taxrecapitulation']['tax'] += $tax;
        } else {
            //$total['taxrecapitulation']['items'][$taxkey]['title'] 	= $paymentCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['tax'] = $tax;
            $total['taxrecapitulation']['tax'] += $tax;
        }
        if (isset($total['taxrecapitulation']['items'][$taxkey]['brutto'])) {
            $total['taxrecapitulation']['items'][$taxkey]['brutto'] += $brutto;
            //	$total['taxrecapitulation']['items'][$taxkey]['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
            $total['taxrecapitulation']['brutto'] += $brutto;
            //	$total['taxrecapitulation']['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
        } else {
            //$total['taxrecapitulation']['items'][$taxkey]['title'] 	= $paymentCosts['taxtxt'];
            $total['taxrecapitulation']['items'][$taxkey]['brutto'] = $brutto;
            //$total['taxrecapitulation']['items'][$taxkey]['brutto_currency'] 	= $price->roundPrice($brutto * $currencyRate);
            $total['taxrecapitulation']['brutto'] += $brutto;
            //	$total['taxrecapitulation']['brutto_currency'] 	+= $price->roundPrice($brutto * $currencyRate);
        }
    }
    // Currency is counted with shipping and payment
    if (!empty($total['taxrecapitulation']['items'])) {
        foreach ($total['taxrecapitulation']['items'] as $k => $v) {
            $bruttoCurrency = $price->roundPrice($v['netto'] * $currencyRate) + $price->roundPrice($v['tax'] * $currencyRate);
            $total['taxrecapitulation']['items'][$k]['brutto_currency'] = $bruttoCurrency;
            $total['taxrecapitulation']['brutto_currency'] += $bruttoCurrency;
        }
    }
    // MUST BE RUN AFTER PAYMENT AND SHIPPING - BRUTTO INCLUDES Payment and Shipping methods
    if ($tax_recapitulation > 0) {
        // There are two options
        // 1) tax_recapitulation == 1: Fix tax recapitulation but not change cart calculation
        // 2) tax_recapitulation == 2: Fix tax recapitulation and change cart calculation (fixing means that tax is countent from sum not from each product items - different rounding
        //
        // In case of 1) when TOTAL BRUTTO is smaller than FIXED TOTAL BRUTTO - we need to add rounding
        // In case of 2) when TOTAL BRUTTO is smaller than FIXED TOTAL BRUTTO - we don't need to do rounding as we fixed whole CART CALCULATION - so no need to fix as both are same
        // BRUTTO MUST BE ALWAYS THE SAME - so when fixed brutto in tax recapitulation is larger, we need to change the cart calculation brutto
        //                                  but not when the BRUTTO is larger than tax recapitulation brutto
        if ($tax_recapitulation > 0) {
            // BRUTTO STANDARD
            if ($total['brutto_tax'] > $total['taxrecapitulation']['brutto']) {
                $total['taxrecapitulation']['rounding'] += $price->roundPrice($total['brutto_tax']) - $price->roundPrice($total['taxrecapitulation']['brutto']);
            }
            /* else if ($total['brutto'] < $total['taxrecapitulation']['brutto']) {
            					if ($tax_recapitulation == 1) {
            						//	$total['taxrecapitulation']['rounding'] 	+= $price->roundPrice($total['taxrecapitulation']['brutto']) - $price->roundPrice($total['brutto']);
            					}
            					// Can be set for $tax_recapitulation == 2
            					$total['brutto'] = $price->roundPrice($total['taxrecapitulation']['brutto']);
            					$total['brutto_tax'] = $price->roundPrice($total['taxrecapitulation']['brutto']);
            
            				}*/
            if ($total['brutto'] < $total['taxrecapitulation']['brutto']) {
                $total['brutto'] = $price->roundPrice($total['taxrecapitulation']['brutto']);
            }
            if ($total['brutto_tax'] < $total['taxrecapitulation']['brutto']) {
                $total['brutto_tax'] = $price->roundPrice($total['taxrecapitulation']['brutto']);
            }
        }
        // Currency
        if ($tax_recapitulation > 0) {
            // BRUTTO STANDARD
            if ($total['brutto_currency_tax'] > $total['taxrecapitulation']['brutto_currency']) {
                $total['taxrecapitulation']['rounding_currency'] += $price->roundPrice($total['brutto_currency_tax']) - $price->roundPrice($total['taxrecapitulation']['brutto_currency']);
            }
            /*else if ($total['brutto_currency'] < $total['taxrecapitulation']['brutto_currency']) {
            			if ($tax_recapitulation == 1) {
            				//	$total['taxrecapitulation']['rounding_currency'] 	+= $price->roundPrice($total['taxrecapitulation']['brutto_currency']) - $price->roundPrice($total['brutto_currency']);
            			}
            			$total['brutto_currency'] = $price->roundPrice($total['taxrecapitulation']['brutto_currency']);
            			$total['brutto_currency_tax'] = $price->roundPrice($total['taxrecapitulation']['brutto_currency']);
            		}*/
            if ($total['brutto_currency'] < $total['taxrecapitulation']['brutto_currency']) {
                $total['brutto_currency'] = $price->roundPrice($total['taxrecapitulation']['brutto_currency']);
            }
            if ($total['brutto_currency_tax'] < $total['taxrecapitulation']['brutto_currency']) {
                $total['brutto_currency_tax'] = $price->roundPrice($total['taxrecapitulation']['brutto_currency']);
            }
        }
    }
}