Xonu_RoundingErrorFix - Version 1.0.4

Version Notes

Fixes rounding error in Magento 1.7.x.x and PayPal.

Download this release

Release Info

Developer Pawel Kazakow
Extension Xonu_RoundingErrorFix
Version 1.0.4
Comparing to
See all releases


Code changes from version 1.0.2 to 1.0.4

app/code/community/Xonu/RoundingErrorFix/Model/Api/Standard.php CHANGED
@@ -15,4 +15,67 @@ class Xonu_RoundingErrorFix_Model_Api_Standard extends Mage_Paypal_Model_Api_Sta
15
  // return sprintf('%.2F', $value); // original line would round e. g. 30.605 to 30.60
16
  return sprintf('%.2F', round($value, 2)); // the modified line would round 30.605 to 30.61
17
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
15
  // return sprintf('%.2F', $value); // original line would round e. g. 30.605 to 30.60
16
  return sprintf('%.2F', round($value, 2)); // the modified line would round 30.605 to 30.61
17
  }
18
+
19
+
20
+ public function getStandardCheckoutRequest()
21
+ {
22
+ // calculate the rounded request total
23
+ $request = parent::getStandardCheckoutRequest();
24
+ $requestBaseGrandTotal = round($request['amount'] + $request['tax'] - $request['discount_amount'], 2);
25
+
26
+ // get the rounded order total
27
+ $orderObj = Mage::getSingleton('sales/order')
28
+ ->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
29
+ $orderBaseGrandTotal = round($orderObj->getBaseGrandTotal(), 2);
30
+
31
+ // get the rounded rounding error
32
+ $roundingError = round($orderBaseGrandTotal - $requestBaseGrandTotal, 2); // -0.01 or +0.01
33
+
34
+ // fix the rounding error
35
+ if($roundingError) {
36
+ $order = array(); // create an array from order data resembling the structure or the request array
37
+ $roundingDeltas = array(); // save the rounding error
38
+
39
+ $order['amount'] = round($orderObj->getBaseSubtotal() + $orderObj->getBaseShippingAmount(), 2);
40
+ $roundingDeltas['amount'] = ($orderObj->getBaseSubtotal() + $orderObj->getBaseShippingAmount()) - $order['amount'];
41
+
42
+ $order['tax'] = round($orderObj->getBaseTaxAmount(), 2);
43
+ $roundingDeltas['tax'] = $orderObj->getBaseTaxAmount() - $order['tax'];
44
+
45
+ $order['shipping'] = round($orderObj->getBaseShippingAmount(), 2);
46
+ $roundingDeltas['shipping'] = $orderObj->getBaseShippingAmount() - $order['shipping'];
47
+
48
+ // not contained in the request but useful to determine if there is a rounding error in shipping
49
+ $order['shipping_incl_tax'] = round($orderObj->getBaseShippingAmount() + $orderObj->getBaseShippingTaxAmount(), 2);
50
+ $roundingDeltas['shipping_incl_tax'] = ($orderObj->getBaseShippingAmount() + $orderObj->getBaseShippingTaxAmount()) - $order['shipping_incl_tax'];
51
+
52
+ $orderTotalItemCount = $orderObj->getTotalItemCount();
53
+
54
+ // hide rounding error in shipping
55
+ if($roundingDeltas['shipping_incl_tax'] && $order['shipping'] > 0) {
56
+ if(isset($request['amount_'.($orderTotalItemCount+1)])) { // ensure that the shipping item is there
57
+ $request['amount_'.($orderTotalItemCount+1)] += $roundingError;
58
+ }
59
+ $request['shipping'] += $roundingError;
60
+ $request['amount'] += $roundingError;
61
+
62
+ // hide rounding error in the last cart item
63
+ } elseif($roundingDeltas['amount'] && $order['amount'] > 0) {
64
+ if(isset($request['amount_'.($orderTotalItemCount+1)])) {
65
+ $request['amount_'.($orderTotalItemCount+1)] += $roundingError;
66
+ }
67
+ $request['amount'] += $roundingError;
68
+ } else {
69
+ // hide rounding error in tax
70
+ if($order['tax'] > 0) {
71
+ $request['tax'] += $roundingError;
72
+ $request['tax_cart'] += $roundingError;
73
+ } else {
74
+ // do not correct rounding error in this unexpected situation
75
+ }
76
+ }
77
+
78
+ }
79
+ return $request;
80
+ }
81
  }
app/code/community/Xonu/RoundingErrorFix/etc/config.xml CHANGED
@@ -6,7 +6,7 @@
6
  <config>
7
  <modules>
8
  <Xonu_RoundingErrorFix>
9
- <version>1.0.2</version>
10
  </Xonu_RoundingErrorFix>
11
  </modules>
12
  <global>
6
  <config>
7
  <modules>
8
  <Xonu_RoundingErrorFix>
9
+ <version>1.0.4</version>
10
  </Xonu_RoundingErrorFix>
11
  </modules>
12
  <global>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Xonu_RoundingErrorFix</name>
4
- <version>1.0.2</version>
5
  <stability>stable</stability>
6
  <license uri="https://xonu.de/license">xonu.de EULA</license>
7
  <channel>community</channel>
@@ -10,9 +10,9 @@
10
  <description>There is a rounding error in Magento 1.7. Example: If a product costs 9.99 incl. tax. 19% and shipping is 5.00 these values add up to 15.00 in the cart. The correct value would be 14.99.</description>
11
  <notes>Fixes rounding error in Magento 1.7.x.x and PayPal.</notes>
12
  <authors><author><name>Pawel Kazakow</name><user>xonu</user><email>support@xonu.de</email></author></authors>
13
- <date>2013-10-20</date>
14
- <time>11:45:29</time>
15
- <contents><target name="magecommunity"><dir name="Xonu"><dir name="RoundingErrorFix"><dir name="Model"><dir name="Api"><file name="Standard.php" hash="7b20a014c8b9444796535e899287799f"/></dir><dir name="Sales"><dir name="Order"><file name="Payment.php" hash="d5ec636bc978992d177977d52d9d17eb"/></dir></dir><file name="Store.php" hash="49d8a77771e445ef100860cb65a7cd54"/></dir><dir name="etc"><file name="config.xml" hash="2fca1499d83c2c24db94bd591dc11aaa"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Xonu_RoundingErrorFix.xml" hash="fc60ed34d31316a139635137222c8442"/></dir></target></contents>
16
  <compatible/>
17
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Xonu_RoundingErrorFix</name>
4
+ <version>1.0.4</version>
5
  <stability>stable</stability>
6
  <license uri="https://xonu.de/license">xonu.de EULA</license>
7
  <channel>community</channel>
10
  <description>There is a rounding error in Magento 1.7. Example: If a product costs 9.99 incl. tax. 19% and shipping is 5.00 these values add up to 15.00 in the cart. The correct value would be 14.99.</description>
11
  <notes>Fixes rounding error in Magento 1.7.x.x and PayPal.</notes>
12
  <authors><author><name>Pawel Kazakow</name><user>xonu</user><email>support@xonu.de</email></author></authors>
13
+ <date>2013-10-26</date>
14
+ <time>12:10:03</time>
15
+ <contents><target name="magecommunity"><dir name="Xonu"><dir name="RoundingErrorFix"><dir name="Model"><dir name="Api"><file name="Standard.php" hash="ddfd309b49000ce2c92f9e502dbd98ed"/></dir><dir name="Sales"><dir name="Order"><file name="Payment.php" hash="d5ec636bc978992d177977d52d9d17eb"/></dir></dir><file name="Store.php" hash="49d8a77771e445ef100860cb65a7cd54"/></dir><dir name="etc"><file name="config.xml" hash="5bce12318435bd4d01a826d9444f689f"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Xonu_RoundingErrorFix.xml" hash="fc60ed34d31316a139635137222c8442"/></dir></target></contents>
16
  <compatible/>
17
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
  </package>