Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Rounding error in coupon calculations

    Add a single product to the cart (value $155.00). This product is within a category that is restricted for the coupon named "TESTME". Store uses a 6% sales tax, which adds $9.30 to the order's total. Go to checkout. On the checkout_payment page, apply the TESTME coupon. This results in a warning message "You must spend at least $0.00 to redeem this coupon.".

    It turns out that the ot_coupon's get_order_total function introduces a slight rounding error when calculating the overall order total, resulting in a total of -1.7763568394003E-15 when the product value plus tax is removed. I made the following update to that function to receive the proper message: "This coupon code is not valid for any product currently in your cart. TESTME".
    Code:
      function get_order_total($couponCode)
      {
        global $order;
        $orderTaxGroups = $order->info['tax_groups'];
        $orderTotalTax = $order->info['tax'];
        $orderTotal = $order->info['total'];
        $products = $_SESSION['cart']->get_products();
        for ($i=0; $i<sizeof($products); $i++) {
          if (!is_product_valid($products[$i]['id'], $couponCode)) {
            $products_tax = zen_get_tax_rate($products[$i]['tax_class_id']);
            $productsTaxAmount = (zen_calculate_tax($products[$i]['final_price'], $products_tax))   * $products[$i]['quantity'];
            $orderTotal -= $products[$i]['final_price'] * $products[$i]['quantity'];
            if ($this->include_tax == 'true') {
             $orderTotal -= $productsTaxAmount;
            }
            if (DISPLAY_PRICE_WITH_TAX == 'true')
            {
              $orderTotal -= $productsTaxAmount;
            }
            $orderTaxGroups[zen_get_tax_description($products[$i]['tax_class_id'])] -= $productsTaxAmount;
            $orderTotalTax -= (zen_calculate_tax($products[$i]['final_price'], zen_get_tax_rate($products[$i]['tax_class_id'])))   * $products[$i]['quantity'];
          }
        }
        if ($this->include_shipping != 'true')
        {
          $orderTotal -= $order->info['shipping_cost'];
          if (isset($_SESSION['shipping_tax_description']) && $_SESSION['shipping_tax_description'] != '') 
          {
             $orderTaxGroups[$_SESSION['shipping_tax_description']] -= $order->info['shipping_tax']; 
             $orderTotalTax -= $order->info['shipping_tax']; 
          }
        }
        if (DISPLAY_PRICE_WITH_TAX != 'true')
        {
          $orderTotal -= $order->info['tax'];
        }
    //-bof-20140724-lat9-Fix for negative-going orderTotal value, due to teeny rounding errors during the tax calculations
        if ($orderTotal < 0) {
          $orderTotal = 0;
          
        }
    //-eof-20140724-lat9
        return array('orderTotal'=>$orderTotal, 'orderTaxGroups'=>$orderTaxGroups, 'orderTax'=>$orderTotalTax, 'shipping'=>$order->info['shipping_cost'], 'shippingTax'=>$order->info['shipping_tax']);
      }

  2. #2
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default Re: Rounding error in coupon calculations

    I believe that I fixed this in v1.5.3 ... to clean up the messages and such ...

    Do you have specifics on the Discount Coupon setup that you used so that I can recreate it and test this?

    Somewhere, I believe I posted the fix for this in v1.5.1 ... hmm ... now where did I put that ...
    Linda McGrath
    If you have to think ... you haven't been zenned ...

    Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!

    Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
    Officially PayPal-Certified! Just click here

    Try our Zen Cart Recommended Services - Hosting, Payment and more ...
    Signup for our Announcements Forums to stay up to date on important changes and updates!

  3. #3
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default Re: Rounding error in coupon calculations

    Linda McGrath
    If you have to think ... you haven't been zenned ...

    Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!

    Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
    Officially PayPal-Certified! Just click here

    Try our Zen Cart Recommended Services - Hosting, Payment and more ...
    Signup for our Announcements Forums to stay up to date on important changes and updates!

  4. #4
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Re: Rounding error in coupon calculations

    Ajeh, I installed the v1.5.3 version of ot_coupon.php in this test store and received the same "You must spend $0.00 ..." message.

  5. #5
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default Re: Rounding error in coupon calculations

    Could you post the exact settings on the Discount Coupon so that I can recreate this scenario?
    Linda McGrath
    If you have to think ... you haven't been zenned ...

    Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!

    Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
    Officially PayPal-Certified! Just click here

    Try our Zen Cart Recommended Services - Hosting, Payment and more ...
    Signup for our Announcements Forums to stay up to date on important changes and updates!

  6. #6
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Re: Rounding error in coupon calculations

    Sure, the ot_coupon settings are
    Code:
    Include Shipping: false
    Include Tax: false
    Re-calculate Tax: Standard
    Tax Class: Taxable Goods
    The coupon settings are
    Code:
    Coupon Name ::  Test Me
    Coupon Amount ::  19.0000%
    Start Date ::  07/08/2014
    End Date ::  07/31/2014
    Uses per Coupon ::  Unlimited
    Uses per Customer ::  Unlimited
    Valid Product List ::  --none--
    Valid Categories List ::  Restrictions Apply
    Date Created ::  07/08/2014
    Date Modified ::  07/08/2014
    I'm guessing that the 19% is the contributing factor!

  7. #7
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default Re: Rounding error in coupon calculations

    Heck ... fixed that in v1.6 not v1.5.3 ...

    Unfortunately, I added a few new things to Discount Coupons as well ... so trying to piece the code together to work in v1.5.3 or v1.5.0/1 is not a lot of fun ...

    If I get time, I will see if I can hack it into something workable but it probably won't be pretty ...
    Linda McGrath
    If you have to think ... you haven't been zenned ...

    Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!

    Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
    Officially PayPal-Certified! Just click here

    Try our Zen Cart Recommended Services - Hosting, Payment and more ...
    Signup for our Announcements Forums to stay up to date on important changes and updates!

 

 

Similar Threads

  1. Replies: 4
    Last Post: 17 Apr 2016, 09:04 PM
  2. v138a Error in Discount Calculations
    By AdaLovelace in forum General Questions
    Replies: 2
    Last Post: 14 Feb 2012, 02:54 AM
  3. Rounding up and rounding down how to stop this?
    By Kazz in forum Currencies & Sales Taxes, VAT, GST, etc.
    Replies: 9
    Last Post: 24 Jul 2008, 06:40 PM
  4. Creating a 10% coupon screws my VAT calculations
    By Pauls in forum General Questions
    Replies: 2
    Last Post: 22 Aug 2007, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg