VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: Enrique_Barrajon on March 08, 2015, 22:36:56 PM

Title: Wrong calculation percentage in VM 3.0.2
Post by: Enrique_Barrajon on March 08, 2015, 22:36:56 PM
Hi, i am new in the forum, but i have a problem that i cant resolve and i would like to get your help.


VirtueMart 3.0.2 produces a miscalculation when a payment method (Plugin standard) with increased rate in percent is selected.

For example, if a product has a final price, including taxes, of € 100 and the payment method selected represents an increase of 3% of the total, virtuemart shows a final price of € 103.09 instead of display € 103.

After some research, I have deduced that the operation does is the following:
€ 100 * 1.03 = 103 €
This should be normal, but then Virtuemart calculates the following operation:
€ 103 * 1.03 = € 106.09
And from this result Virtuemart makes subtraction with the above result, leading to the following:
€ 106.09 € -103 = € 3.09
and then to this value of € 3.09, Virtuemart finally sum € 100 of product price, as showing the panel frontral € 103.09

(http://www.fidelitysound.es/pantallazo%20web.jpg)
http://www.fidelitysound.es/pantallazo%20web.jpg (http://www.fidelitysound.es/pantallazo%20web.jpg)

Does anyone know how to resolve this error?
What its due this error?
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: GJC Web Design on March 08, 2015, 22:56:28 PM
So effectively the 3% is calc. twice?

The calc. is done in the function setCartPrices(VirtueMartCart $cart, &$cart_prices, $method, $progressive = true)

in administrator\components\com_virtuemart\plugins\vmpsplugin.php ~ line 933

don't know if u can find anything there?
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Enrique_Barrajon on March 09, 2015, 00:49:07 AM
Hi, thanks for your reply.
I'll try to look into that file.
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Enrique_Barrajon on March 09, 2015, 01:27:58 AM
Hello again, I've been reviewing the file and I find no error.
The code is as follows, the equations are simple and seems fine.


function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {

      if (!class_exists ('calculationHelper')) {
         require(VMPATH_ADMIN . DS . 'helpers' . DS . 'calculationh.php');
      }
      $_psType = ucfirst ($this->_psType);
      $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;
      }

Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Enrique_Barrajon on March 09, 2015, 01:55:49 AM
 :) :D ;D

I've already solved !!!!!!!!!!!!
I changed the order of calculation, simply by progressive and perfect !!!!
That I'm happy !!!

Thank you so much for the guidance !!! many many thank you very much, my friend !!!!
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: GJC Web Design on March 09, 2015, 09:24:23 AM
Please explain better what you did as a help for others

Thanks
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Milbo on March 10, 2015, 12:34:00 PM
The payment plugin can implement an option for that
function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {

progressive considers that you have to loan the fee itself
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: alatak on March 10, 2015, 12:39:26 PM
Hello
Yes, i will add this option in paypal.
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Milbo on March 10, 2015, 19:38:11 PM
Yeh the VirtueMart flower is by far not showing all of its possibilities.
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Enrique_Barrajon on May 21, 2015, 22:43:27 PM
Hello again, not turned to look at the forum but now I had to go back to solve the problem by upgrading to Virtuemart 3.0.9 and i will explain the simple modification to the calculation of percentages is correct.

I canceled one of the processes of calculation on line 959 to simply stay the progressive calculation:

(http://www.fidelitysound.es/images/vmpspluginmod1.jpg)
Title: Re: Wrong calculation percentage in VM 3.0.12
Post by: mosdata on February 01, 2016, 22:14:18 PM
Hi, I'm using the VM version 3.0.12 and I have the same problem.

If the payment method I put 10% or -10% miscalculate the percentage.

I am not programmed experienced. Could include this fix in the next version or help me fix it?

Regards,
Hernan
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Milbo on February 08, 2016, 08:54:18 AM
Mosdata, please read closely again
Quote from: Milbo on March 10, 2015, 12:34:00 PM
The payment plugin can implement an option for that
function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {

progressive considers that you have to loan the fee itself

Your payment is using the progressive option
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Robert_ITMan on July 11, 2021, 19:22:33 PM
I can't find setCartPrices function in my customized payment plugin ... can I add this function ... if so, what is the full function?

function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {

FYI - Plugin developer provided solution on 3.8.9 10473 with hack to core /administrator/components/com_virtuemart/plugins/virtuemart/vmpsplugin.php
... but I'm also need to find immediate solution for 3.2.12 for LIVE site which is now taking orders and over charging payment fees ... this will cost me weeks of manual work to re-calulate and re-imburse everyone !!
Title: Re: Wrong calculation percentage in VM 3.0.2
Post by: Milbo on October 25, 2021, 13:08:26 PM
You just add the function in your plugin and overwrite the core function. Or as proxy

function setCartPrices (VirtueMartCart $cart, &$cart_prices, $method, $progressive = true) {
parent::setCartPrices (VirtueMartCart $cart, $cart_prices, $method, false);
}