From 2f0035a0776c8ea837a5c864bd20469ca0390bac Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Thu, 21 Nov 2013 18:02:53 +0100 Subject: [PATCH] Applied VM code to support multiple tax rates in the cart --- rules_shipping_base.php | 76 ++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/rules_shipping_base.php b/rules_shipping_base.php index 6ec29c1..1bc990e 100644 --- a/rules_shipping_base.php +++ b/rules_shipping_base.php @@ -303,14 +303,21 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method) { + if (!class_exists ('calculationHelper')) { require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php'); } - - $calculator = calculationHelper::getInstance (); - $value = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice'); $_psType = ucfirst ($this->_psType); - $cart_prices[$this->_psType . 'Value'] = $value; + $calculator = calculationHelper::getInstance (); + + $cart_prices[$this->_psType . 'Value'] = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice'); + + if($this->_psType=='payment'){ + $cartTotalAmountOrig=$this->getCartAmount($cart_prices); + $cartTotalAmount=($cartTotalAmountOrig + $method->cost_per_transaction) / (1 -($method->cost_percent_total * 0.01)); + $cart_prices[$this->_psType . 'Value'] = $cartTotalAmount - $cartTotalAmountOrig; + } + $taxrules = array(); if(isset($method->tax_id) and (int)$method->tax_id === -1){ @@ -323,25 +330,48 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { $db->setQuery ($q); $taxrules = $db->loadAssocList (); } else { - //This construction makes trouble, if there are products with different vats in the cart - //on the other side, it is very unlikely to have different vats in the cart and simultan it is not possible to use a fixed tax rule for the shipment - if(!empty($calculator->_cartData['VatTax']) and count ($calculator->_cartData['VatTax']) == 1){ + + $taxrules = array(); + if(!empty($calculator->_cartData['VatTax']) ){ $taxrules = $calculator->_cartData['VatTax']; + + $denominator = 0; foreach($taxrules as &$rule){ - $rule['subTotal'] = $cart_prices[$this->_psType . 'Value']; + //$rule['numerator'] = $rule['calc_value']/100.0 * $rule['subTotal']; + $denominator += ($rule['subTotal']-$rule['taxAmount']); + $rule['subTotalOld'] = $rule['subTotal']; + $rule['subTotal'] = 0; + $rule['taxAmountOld'] = $rule['taxAmount']; + $rule['taxAmount'] = 0; + //$rule['subTotal'] = $cart_prices[$this->_psType . 'Value']; + } + if(empty($denominator)){ + $denominator = 1; } - } else { - $taxrules = $calculator->_cartData['taxRulesBill']; foreach($taxrules as &$rule){ - unset($rule['subTotal']); + $frac = ($rule['subTotalOld']-$rule['taxAmountOld'])/$denominator; + $rule['subTotal'] = $cart_prices[$this->_psType . 'Value'] * $frac; } + } else if(!empty($calculator->_cartData['taxRulesBill']) ){ + $taxrules = array_merge($taxrules,$calculator->_cartData['taxRulesBill']); } + } - - if (count ($taxrules) > 0) { + if(empty($method->cost_per_transaction)) $method->cost_per_transaction = 0.0; + if(empty($method->cost_percent_total)) $method->cost_percent_total = 0.0; + + //If the taxing via unpublished categories is used, then the rules use the subtotal which is now overriden here + /*if (count ($taxrules) == 1 and isset($taxrules[1]['subTotal'] )) { + $taxrules[1]['subTotal'] = $cart_prices[$this->_psType . 'Value']; + }/*/ + + if (count ($taxrules) > 0 ) { + + // BEGIN_RK_CHANGES if ($method->includes_tax) { + $cart_prices['salesPrice' . $_psType] = $calculator->roundInternal ($cart_prices[$this->_psType . 'Value'], 'salesPrice'); // Calculate the tax from the final sales price: $calculator->setRevert (true); @@ -349,20 +379,32 @@ class plgVmShipmentRules_Shipping_Base extends vmPSPlugin { $cart_prices[$this->_psType . 'Tax'] = $cart_prices['salesPrice' . $_psType] - $cart_prices[$this->_psType . 'Value']; $calculator->setRevert (false); } else { - $cart_prices['salesPrice' . $_psType] = $calculator->roundInternal ($calculator->executeCalculation ($taxrules, $cart_prices[$this->_psType . 'Value']), 'salesPrice'); - $cart_prices[$this->_psType . 'Tax'] = $calculator->roundInternal (($cart_prices['salesPrice' . $_psType] - $cart_prices[$this->_psType . 'Value']), 'salesPrice'); + // END_RK_CHANGES + $cart_prices['salesPrice' . $_psType] = $calculator->roundInternal ($calculator->executeCalculation ($taxrules, $cart_prices[$this->_psType . 'Value'],true,false), 'salesPrice'); + $cart_prices[$this->_psType . 'Tax'] = $calculator->roundInternal (($cart_prices['salesPrice' . $_psType] - $cart_prices[$this->_psType . 'Value']), 'salesPrice'); + // BEGIN_RK_CHANGES } + // END_RK_CHANGES reset($taxrules); $taxrule = current($taxrules); $cart_prices[$this->_psType . '_calc_id'] = $taxrule['virtuemart_calc_id']; + + foreach($taxrules as &$rule){ + if(isset($rule['subTotalOld'])) $rule['subTotal'] += $rule['subTotalOld']; + if(isset($rule['taxAmountOld'])) $rule['taxAmount'] += $rule['taxAmountOld']; + } + } else { - $cart_prices['salesPrice' . $_psType] = $value; + $cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value']; $cart_prices[$this->_psType . 'Tax'] = 0; $cart_prices[$this->_psType . '_calc_id'] = 0; } + + return $cart_prices['salesPrice' . $_psType]; + } - + protected function createMethodRule ($r, $countries, $tax) { return new ShippingRule($r, $countries, $tax); } -- GitLab