Page 2 of 6 FirstFirst 1234 ... LastLast
Results 11 to 20 of 59
  1. #11
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,408
    Plugin Contributions
    88

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Try the following change to the currencies-class display_price function (both /includes/classes/currencies.php and /YOUR_ADMIN/includes/classes/currencies.php):
    Code:
      function display_price($products_price, $products_tax, $quantity = 1) {
    //-bof-20140729-lat9-Fix rounding error with tax calculation
    //      return $this->format(zen_add_tax($products_price, $products_tax) * $quantity);
        return $this->format (zen_add_tax($products_price * $quantity, $products_tax));
    //-eof-20140729-lat9-Fix rounding error ...
      }

  2. #12
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,408
    Plugin Contributions
    88

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Quote Originally Posted by lat9 View Post
    If I apply the calculations in the order that Zen Cart's "standard" processing would do:

    10 x 1.181 = 11.81 x 1.1 (10% tax) = 12.991 ... rounded is 12.99

    So I created a test product in a "stock" Zen Cart v1.5.1 store (using all default settings) that has a price of 1.181. Added 10 of them to my cart. Price displays as 11.80 (incorrect, should be 11.81). I then went to checkout and see the correct subtotal (11.81) with a 1.18 tax (10%) yielding a final price of 12.99.

    That said, I believe that the rounding error is on the shopping_cart page, but I'm not sure (yet) what the culprit is.
    Quote Originally Posted by lat9 View Post
    If you agree with my assertion that the rounding should occur after the per-product-price is multiplied by the cart quantity, then the following changes will move the processing on the shopping_cart page into line with the ot_subtotal calculation. All line numbers are relative to an unmodified v1.5.1 version of the specified file.

    First, /includes/classes/shopping_cart.php, starting at line #850:
    Code:
    //      $this->total += zen_round(zen_add_tax($productTotal, $products_tax), $decimalPlaces) * $qty;
          $this->total += zen_round(zen_add_tax($productTotal, $products_tax) * $qty, $decimalPlaces);  //-lat9-correct rounding issue
    Then, /includes/pages/shopping_cart/header_php.php starting at line #141
    Code:
    //-bof-lat9-fix rounding issue
    //  $ppe = zen_round(zen_add_tax($ppe, zen_get_tax_rate($products[$i]['tax_class_id'])), $currencies->get_decimal_places($_SESSION['currency']));
    //  $ppt = $ppe * $products[$i]['quantity'];
      $ppe = zen_add_tax($ppe, zen_get_tax_rate($products[$i]['tax_class_id']));
      $ppt = zen_round ($ppe * $products[$i]['quantity'], $currencies->get_decimal_places($_SESSION['currency']));
    //-eof-lat9
    I gave this a look again, based on the additional findings (posted above) in the currencies classes' calculations and now modify my suggestions for the above changes to read:
    Code:
    //      $this->total += zen_round(zen_add_tax($productTotal, $products_tax), $decimalPlaces) * $qty;
         $this->total += zen_round (zen_add_tax ($productTotal * $qty, $products_tax), $decimalPlaces);  //-lat9-correct rounding issue
    and
    Code:
    //-bof-lat9-fix rounding issue
    //  $ppe = zen_round(zen_add_tax($ppe, zen_get_tax_rate($products[$i]['tax_class_id'])), $currencies->get_decimal_places($_SESSION['currency']));
    //  $ppt = $ppe * $products[$i]['quantity'];
      $ppe = zen_add_tax ($ppe * $products[$i]['quantity'], zen_get_tax_rate($products[$i]['tax_class_id']));
      $ppt = zen_round ($ppe, $currencies->get_decimal_places($_SESSION['currency']));
    //-eof-lat9
    Each of these changes now enforces the calculation sequence of:

    total = zen_round ((unit_price * quantity) * tax)

  3. #13
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Based on your suggestions above I used this test product:

    (this is what the customer sees on the product info page and this price should be carried forward through all the calcs)

    Code:
    Test Candle (normal price) $5.95
    Test Candle (special price)  $5.36 
    
    Add 10 units to the cart, cart shows
    
    Test Candle 10     $53.55     $53.55
    Sub-Total: $53.55    
    Order completes and the invoice shows:
    
    Products         SKU         Tax     Price (ex)     Price (inc)     Total (ex)     Total (inc)
    10 x     Test Candle     test_candle     10%     $4.87         $5.36         $48.70         $53.60
    Sub-Total:     $53.55    (should be $53.60)
    Free Shipping Options (Free Shipping):     $0.00
    GST:     $4.87
    Total:     $53.55    (should be $53.60)
    Here we have a 5 cent difference in the total. Mathematically it is correct, but now divide $53.55 / 10 and we get a price of $5.355 each. In the real world there is no such thing as a fraction of a cent. Have not checked the DB entry but I bet that it shows 5.355. So, if a special price is calculated then that needs to be rounded to the nearest cent before it gets stored in the DB.

    That would be the next thing to fix.

    Cheers / Frank

  4. #14
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Looks like I finally got this issue sorted.

    After lots of experimenting, file hunting and tracing - and getting my maths brain into gear I made the changes below.

    Files affected:

    includes/classes/currencies.php

    at the end of the file I changed

    PHP Code:
      function display_price($products_price$products_tax$quantity 1) { 
        return 
    $this->format(zen_add_tax($products_price$products_tax) * $quantity); 
      } 
    to this

    PHP Code:
      function display_price($products_price$products_tax$quantity 1) { 
        
    //-bof-frank18-Fix rounding error with tax calculation 
        
    return $this->format(zen_round(zen_add_tax($products_price$products_tax), 2) * $quantity); 
        
    //-eof-frank18-Fix rounding error ... 
      


    includes/classes/order.php

    changed (from around line 526)

    PHP Code:
          /*********************************************
           * Calculate taxes for this product
           *********************************************/
          
    $shown_price = (zen_add_tax($this->products[$index]['final_price'] * $this->products[$index]['qty'], $this->products[$index]['tax']))
          + 
    zen_add_tax($this->products[$index]['onetime_charges'], $this->products[$index]['tax']);
          
    $this->info['subtotal'] += $shown_price;
          
    $this->notify('NOTIFIY_ORDER_CART_SUBTOTAL_CALCULATE', array('shown_price'=>$shown_price));
          
    // find product's tax rate and description
          
    $products_tax $this->products[$index]['tax'];
          
    $products_tax_description $this->products[$index]['tax_description']; 
    to this

    PHP Code:
          /*********************************************
           * Calculate taxes for this product
           *********************************************/
          //$shown_price = (zen_add_tax($this->products[$index]['final_price'] * $this->products[$index]['qty'], $this->products[$index]['tax'])) // standard ZC1.5.1 code  (frank18)
          // next line is replacement which works a treat  (frank18)
          
    $shown_price round((zen_add_tax($this->products[$index]['final_price'], $this->products[$index]['tax'])), 2) * $this->products[$index]['qty']   
          + 
    zen_add_tax($this->products[$index]['onetime_charges'], $this->products[$index]['tax']);
          
    $this->info['subtotal'] += $shown_price;
          
    $this->notify('NOTIFIY_ORDER_CART_SUBTOTAL_CALCULATE', array('shown_price'=>$shown_price));
          
    // find product's tax rate and description
          
    $products_tax $this->products[$index]['tax'];
          
    $products_tax_description $this->products[$index]['tax_description']; // - shows correct product totals incl tax (frank18) 
    includes/classes/shopping_cart.php

    changed line 851

    PHP Code:
          $this->total += zen_round(zen_add_tax($productTotal$products_tax), $decimalPlaces) * $qty
    to this

    PHP Code:
          // $this->total += zen_round(zen_add_tax($productTotal, $products_tax), $decimalPlaces) * $qty;
          
    $this->total += zen_round(zen_add_tax($productTotal$products_tax), 2) * $qty;  // -frank18- correct rounding issue 
    Even though $decimalPlaces are set to '2' they won't take (don't know why???) - but by actually specifying 2 decimal places in that line fixed it

    Finally in both of these files

    includes/functions/functions_prices.php
    admin/includes/functions/functions_prices.php


    There are 5 instances in each of the 2 files showing

    PHP Code:
    return number_format($blah_blah_variable4'.'''); 
    which I all changed to

    PHP Code:
    return number_format($blah_blah_variable2'.'''); 
    I tested this on 3 different local ZC1.5.1 installs with normal priced products, products priced by attributes, products with special prices and products with sale maker prices. All are now showing fine on the shopping cart page, checkout_shipping, checkout_payment, checkout_confirmation and in admin - including the invoice. The fixes are now active on my main live store.

    I would be grateful to see some comments on these changes - especially after seeing numerous reports on the same rounding issue on this forum and on the net - dating back to 2008.

    If we are lucky, Ajeh (the shipping and payment module specialist ) may take a look at this.

    Cheers / Frank
    Last edited by frank18; 5 Aug 2014 at 12:22 PM.

  5. #15
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    I may add that this is working fine on a 1.5.3 install.

  6. #16
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    BTW, I tested a scenario on a stock ZC 1.5.3 install, testing product paper forms at a cost of 0.00003511 each, minimum buy 100,000. The stock ZC 1.5.3 configuration returns $0.00 in the shopping cart (naturally).

    Then again, in a real World nobody sells paper forms at a cost of 0.00003511 each.... that sort of stuff is usually sold in packs of 1,000's or 10,000's - price charged per pack/ream/box, $3.51, $35.11 whatever.

    Also, I am yet to find a currency with more than 2 decimals. True, from a manufacturing point of view, the production costs are usually broken down to fractions of cents, but the stuff is never sold to the consumer as an individually produced item but only in minimum quantities.

    The code modification which I posted earlier works for me, it may not be suitable for others so use with caution and always backup your files before making changes.
    Last edited by frank18; 14 Dec 2014 at 01:23 PM.

  7. #17
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Sadly this rounding error has been an issue for ages, I even noted similar posts on the OsCommerce forums whilst Googling around so me thinks it's a legacy from that cart.

    Sure, my solution of having 2 decimals hardcoded is not ideal at all, but at least it eliminates my accounting system complaining that a transaction is not in balance after importing orders.

    Eventually someone will come up with a satisfactory solution.... (Ajeh ????)

    Thanks again

    Frank

  8. #18
    Join Date
    Nov 2007
    Location
    Sunny Coast, Australia
    Posts
    3,379
    Plugin Contributions
    9

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    Quote Originally Posted by frank18 View Post
    I may add that this is working fine on a 1.5.3 install.
    I can confirm that it also works on 1.5.4

  9. #19
    Join Date
    May 2004
    Location
    UK
    Posts
    478
    Plugin Contributions
    0

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    just wanted to post to say thank you - I don't see this error that often anymore but just had it in a live shop - 153 - would be really good if this could be looked at and built into the core code :)

  10. #20
    Join Date
    Feb 2007
    Location
    Ontario, Canada
    Posts
    232
    Plugin Contributions
    0

    Default Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error)

    I have a rounding problem here, and oddly enough I am a printing company that sells forms, as mentioned a few threads earlier, and we do sell in partial cents LOL.

    I'm going to use my product AF133 as a example at af-company.com. http://af-company.com/index.php?main...products_id=12

    I have it displaying both the unit price and the total price, since a unit is quite literally a sheet of paper, so it falls in the fraction cents, I find people get less confused.

    Currently my display **should** show two decimal places, unless the third number saved in the database is something other than "0", in which case it displays the additional 3rd digit ((see prices for 100 at .21 ea, vs 1000 for .174 ea)(although the very first quantity doesn't seem to work and have no idea why)). The calculation shown to the customer is the unit price multiplied by the quantity, as it should be. However when you add this amount to your shopping cart, it is calculating it to only 2 decimal places, so the price for 1000 at 0.174 ea is showing as $170.00. However when you continue on to check out to complete your order, the shopping cart then shows the price correctly at $174.00 for 1000 forms. I have no idea how to fix this discrepancy.

    You can use my test account test43770 (at) yourcompany dot com and the p/w is 43770test

    Any help is appreciated.
    Thanks, Colleen
    www.af-company.com
    To err is human, but to really foul things up requires a computer. Farmers' Almanac, 1978

 

 
Page 2 of 6 FirstFirst 1234 ... LastLast

Similar Threads

  1. Shopping Cart - Subtotal $0.00
    By dmagic in forum General Questions
    Replies: 1
    Last Post: 9 Apr 2011, 03:38 PM
  2. again tax/VAT being calculated on subtotal ignoring group discount
    By SarahL in forum Discounts/Coupons, Gift Certificates, Newsletters, Ads
    Replies: 8
    Last Post: 8 Apr 2008, 06:42 PM
  3. Shopping cart subtotal font color help
    By bigad21 in forum Templates, Stylesheets, Page Layout
    Replies: 3
    Last Post: 7 Aug 2006, 05:08 AM
  4. Shopping cart subtotal in header
    By businesstoweb in forum Templates, Stylesheets, Page Layout
    Replies: 1
    Last Post: 13 Jun 2006, 01:32 AM

Bookmarks

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
Zen-Cart, Internet Selling Services, Klamath Falls, OR