Shipping Rate Problems on VM3.0.8 to upcoming 3.0.10

If you are runnig Joomla 3.x and Virtuemart 3.x please post to this forum your questions or support tickets about One Page Checkout

Shipping Rate Problems on VM3.0.8 to upcoming 3.0.10

Postby admin » Tue Jul 21, 2015 3:11 pm

hello friends, in the very latest VM code, there has been a new caching introduced which may break lot's of calculation related features in Virtuemart's checkout with OPC and also without opc.

To disable any internal caches in VM's calculation, you may want to update this function in VM:

\administrator\components\com_virtuemart\plugins\vmpsplugin.php


Code: Select all
function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {

      static $c = array();
      $idN = 'virtuemart_'.$this->_psType.'method_id';

      $_psType = ucfirst ($this->_psType);
                /* stAn disabled caching:
      if(isset($c[$this->_psType][$method->$idN])){
         $cart_prices = array_merge($cart_prices,$c[$this->_psType][$method->$idN]);
         return $cart_prices['salesPrice' . $_psType];
      }
               */

      if (!class_exists ('calculationHelper')) {
         require(VMPATH_ADMIN . DS . 'helpers' . DS . 'calculationh.php');
      }

      $calculator = calculationHelper::getInstance ();

      $cart_prices[$this->_psType . 'Value'] = $calculator->roundInternal ($this->getCosts ($cart, $method, $cart_prices), 'salesPrice');
      if(!isset($cart_prices[$this->_psType . 'Value'])) $cart_prices[$this->_psType . 'Value'] = 0.0;
      if(!isset($cart_prices[$this->_psType . 'Tax'])) $cart_prices[$this->_psType . 'Tax'] = 0.0;

      if($this->_psType=='payment'){
         $cartTotalAmountOrig=$this->getCartAmount($cart_prices);

         if(!$progressive){
            //Simple
            $cartTotalAmount=($cartTotalAmountOrig + $method->cost_per_transaction) * (1 +($method->cost_percent_total * 0.01));
            //vmdebug('Simple $cartTotalAmount = ('.$cartTotalAmountOrig.' + '.$method->cost_per_transaction.') * (1 + ('.$method->cost_percent_total.' * 0.01)) = '.$cartTotalAmount );
            //vmdebug('Simple $cartTotalAmount = '.($cartTotalAmountOrig + $method->cost_per_transaction).' * '. (1 + $method->cost_percent_total * 0.01) .' = '.$cartTotalAmount );
         } else {
            //progressive
            $cartTotalAmount = ($cartTotalAmountOrig + $method->cost_per_transaction) / (1 -($method->cost_percent_total * 0.01));
            //vmdebug('Progressive $cartTotalAmount = ('.$cartTotalAmountOrig.' + '.$method->cost_per_transaction.') / (1 - ('.$method->cost_percent_total.' * 0.01)) = '.$cartTotalAmount );
            //vmdebug('Progressive $cartTotalAmount = '.($cartTotalAmountOrig + $method->cost_per_transaction) .' / '. (1 - $method->cost_percent_total * 0.01) .' = '.$cartTotalAmount );
         }

         $cart_prices[$this->_psType . 'Value'] = $cartTotalAmount - $cartTotalAmountOrig;
      }
                /* stAn original:
      if(!isset($cart_prices['salesPrice' . $_psType])) $cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value'];
                new: */
                $cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value'];
      
                $taxrules = array();
      if(isset($method->tax_id) and (int)$method->tax_id === -1){

      } else if (!empty($method->tax_id)) {
         $cart_prices[$this->_psType . '_calc_id'] = $method->tax_id;

         $db = JFactory::getDBO ();
         $q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $method->tax_id . '" ';
         $db->setQuery ($q);
         $taxrules = $db->loadAssocList ();

         if(!empty($taxrules) ){
            foreach($taxrules as &$rule){
               if(!isset($rule['subTotal'])) $rule['subTotal'] = 0;
               if(!isset($rule['taxAmount'])) $rule['taxAmount'] = 0;
               $rule['subTotalOld'] = $rule['subTotal'];
               $rule['taxAmountOld'] = $rule['taxAmount'];
               $rule['taxAmount'] = 0;
               $rule['subTotal'] = $cart_prices[$this->_psType . 'Value'];
               $cart_prices[$this->_psType . 'TaxPerID'][$rule['virtuemart_calc_id']] = $calculator->roundInternal($calculator->roundInternal($calculator->interpreteMathOp($rule, $rule['subTotal'])) - $rule['subTotal'], 'salesPrice');
               $cart_prices[$this->_psType . 'Tax'] += $cart_prices[$this->_psType . 'TaxPerID'][$rule['virtuemart_calc_id']];
            }
         }
      } else {

         $taxrules = array_merge($cart->cartData['VatTax'],$cart->cartData['taxRulesBill']);

         if(!empty($taxrules) ){
            $denominator = 0.0;
            foreach($taxrules as &$rule){
               //Quickn dirty
               if(!isset($rule['calc_kind'])) $rule = (array)VmModel::getModel('calc')->getCalc($rule['virtuemart_calc_id']);

               if(!isset($rule['subTotal'])) $rule['subTotal'] = 0;
               if(!isset($rule['taxAmount'])) $rule['taxAmount'] = 0;
               $denominator += ($rule['subTotal']-$rule['taxAmount']);
               $rule['subTotalOld'] = $rule['subTotal'];
               $rule['subTotal'] = 0;
               $rule['taxAmountOld'] = $rule['taxAmount'];
               $rule['taxAmount'] = 0;
            }
            if(empty($denominator)){
               $denominator = 1;
            }

            foreach($taxrules as &$rule){
               $frac = ($rule['subTotalOld']-$rule['taxAmountOld'])/$denominator;
               $rule['subTotal'] = $cart_prices[$this->_psType . 'Value'] * $frac;

               if(!isset($cart_prices[$this->_psType . 'Tax'])) $cart_prices[$this->_psType . 'Tax'] = 0.0;
               $cart_prices[$this->_psType . 'TaxPerID'][$rule['virtuemart_calc_id']] = $calculator->roundInternal($calculator->roundInternal($calculator->interpreteMathOp($rule, $rule['subTotal'])) - $rule['subTotal'], 'salesPrice');
               $cart_prices[$this->_psType . 'Tax'] += $cart_prices[$this->_psType . 'TaxPerID'][$rule['virtuemart_calc_id']];

            }
         }
      }

      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 (count ($taxrules) > 0 ) {

         $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');
         reset($taxrules);

         foreach($taxrules as &$rule){
            if(!isset($cart_prices[$this->_psType . '_calc_id']) or !is_array($cart_prices[$this->_psType . '_calc_id'])) $cart_prices[$this->_psType . '_calc_id'] = array();
            $cart_prices[$this->_psType . '_calc_id'][] = $rule['virtuemart_calc_id'];

            if(isset($rule['subTotalOld'])) $rule['subTotal'] += $rule['subTotalOld'];
            if(isset($rule['taxAmountOld'])) $rule['taxAmount'] += $rule['taxAmountOld'];
         }

      } else {

         $cart_prices['salesPrice' . $_psType] = $cart_prices[$this->_psType . 'Value'];
         $cart_prices[$this->_psType . 'Tax'] = 0;
         $cart_prices[$this->_psType . '_calc_id'] = 0;
      }
      /* stAn, disable cache:
                $c[$this->_psType][$method->$idN] = $cart_prices;
                */
      //if($_psType='Shipment')vmTrace('setCartPrices '.$cart_prices['salesPrice' . $_psType]);
      return $cart_prices['salesPrice' . $_psType];

   }


the code was suggested towards vm3.0.10 of 21.7.2015 and OPC2.0.293

we are trying to find a solution to the calculation problems without this core VM modification needed.

best regards, stan, rupostel.com
admin
Site Admin
 
Posts: 2708
Joined: Wed Jan 06, 2010 11:43 pm

Re: Shipping Rate Problems on VM3.0.8 to upcoming 3.0.10

Postby admin » Tue Jul 21, 2015 3:12 pm

to see the problem in core VM:
- alatak USPS calcultion or other multi shipment's method's prices do not update

to see the problem in OPC and VM:
- use a coupon code (awo coupon or VM coupon)
- shipping is always zero when using a coupon

best regards, stan
admin
Site Admin
 
Posts: 2708
Joined: Wed Jan 06, 2010 11:43 pm

Re: Shipping Rate Problems on VM3.0.8 to upcoming 3.0.10

Postby admin » Thu Jul 23, 2015 3:36 pm

OPC294 automatically disables this VM cache upon clicking save in OPC config.

It changes:
Code: Select all
static $c = array();


to

Code: Select all
$c = array();


so that the cache is not stored upon repeated calls of the function, since the value of the cache is incorrect.

this page is linked from OPC notice. VM versions affected are 3.0.9.x and possibly a 3.0.10 until the VM developer understands the problem with this cache.

best regards, stan, rupostel.com
admin
Site Admin
 
Posts: 2708
Joined: Wed Jan 06, 2010 11:43 pm


Return to One Page Checkout for Virtuemart 3 on Joomla 3.x