Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding error "Total Summa med Skatt är inte lika med Total Summa Exklusive Moms, Total Skatt och Avrundning" #146

Closed
DanielMarklund opened this issue Apr 29, 2021 · 8 comments
Labels
bug Something isn't working
Milestone

Comments

@DanielMarklund
Copy link

DanielMarklund commented Apr 29, 2021

Customer reported that they were greeted with the following alert upon opening the checkout:

Total Summa med Skatt är inte lika med Total Summa Exklusive Moms, Total Skatt och Avrundning.Beräknat totalt: 49928 Tillhandahållet total:49929

I received a list of which products the customer had in the cart and I could reproduce it myself, as seen in the image below. Why is this happening? We have 2 decimals setup in WooCommerce.

FYI, there is a mix of 12% and 25% moms/VAT products in the cart.

rounding-error

@DanielMarklund
Copy link
Author

This is also easily reproduced (at least on our site) by adding 2 different 12% moms products and increasing the item amount to a total of 4. It works fine when there only is one 12% moms product in the cart, but as soon as there are two different it breaks.

image

@DanielMarklund
Copy link
Author

DanielMarklund commented May 5, 2021

Based on the last screenshot I posted, the calculation for the sums in the error message is the following.

  • Each product above costs 39 SEK inc VAT. VAT is 12%. Price ex VAT is 34,821428571. The total price ex VAT for 4 of the products is (39*4)/1,12 = 139,285714284 SEK.
  • Shipping cost is 29 SEK inc VAT. VAT is 25%. Price ex VAT is 23,2 SEK.
  • Invoice fee is 12 SEK inc VAT. VAT is 25%. Price ex VAT is 9,6 SEK.

Adding the price ex VAT for each above line:
139,285714284 + 23,2 + 9,6 = 172,085714284 SEK.

The "Beräknat totalt" is 172,08 in the error message, which means that it probably is rounded down via floor(). The "Tillhandahållet totalt" is using round().

So my theory is that Billmate is flooring the numbers on their end and the plugin is instead rounding them.

Edit: This ONLY happens when the third decimal is 5.

@DanielMarklund
Copy link
Author

DanielMarklund commented May 5, 2021

Final update with proposed solution

How to reproduce

  1. Make sure that you have a 12% MOMS/VAT rule set up in WooCommerce.
  2. Create 2 different products. They need to have 12% VAT and 39 SEK price inc VAT.
  3. Add both products to your cart. One of them needs 1 in quantity and the other one 3 in quantity.
  4. Go to checkout.
  5. You should now get an error that total sums are diffing by 0,01 SEK (it's presented in öre though).

Why is this happening?

In class-bco-cart-articles-helper.php, the function get_without_tax is not rounding up if the line_total is X.XX5. If the line_total would be 125.235, it would be rounded to 125.23. The Billmate API seems to round these numbers up instead, so it'd expect 125.24.

This ONLY happens if the third decimal of the order price sum without tax is 5.

Proposed fix

While searching around I found the answer in the top voted user contributed notes:
https://www.php.net/manual/en/function.round.php#114573

Basically we want to force always rounding up on the third decimal, so it can handle 0.005 floats.

/**
 * Get cart row total articles price excluding tax.
 *
 * @param array $cart_item cart item.
 * @return int $item_price Item price excluding tax.
 */
public static function get_without_tax( $cart_item ) {
  $items_subtotal = $cart_item['line_total'];
  $fig = (int) str_pad('1', 4, '0');
  return round((ceil($items_subtotal * $fig) / $fig) * 100);
}

The fix seems to work fine with all different type of carts:

  • Only 12% VAT products
  • Only 25% VAT products
  • Only 0% VAT products
  • A mix of all above
  • All above with % discount codes

Ping @NiklasHogefjord @andyjohansson

@DanielMarklund
Copy link
Author

DanielMarklund commented May 6, 2021

I've temporarily implemented the above fix in our production environment. No issues have been reported by customers so far and orders look correct. It has been live for approximately 12 hours. Usually we would've gotten around a lot of e-mails regarding this issue in that timeframe.

I do recommend that you test it yourselves too. I haven't tried it with 6% MOMS/VAT products, but it shouldn't in theory be any different.

Update: Still no issues as of 12:00 today.

@DanielMarklund
Copy link
Author

Update on proposed fix & more debugging

The above fix works fine when the third decimal is 5, but we've now run into cases where even later decimals are at 5 – so the proposed fix isn't working in those cases.

I've been discussing with Paul at Billmate regarding this and one of the things we wanted to try was updating classes/requests/helpers/order/class-bco-order-cart-helper.php row 49.

Original:

	public static function get_order_cart_total( $order ) {
		return array(
			'withouttax' => self::get_total_without_tax( $order ),
			'tax'        => self::get_total_tax( $order ),
			'rounding' => 0,
			'withtax'    => self::get_total_with_tax( $order ),
		);
	}

Modified:

	public static function get_order_cart_total( $order ) {
		return array(
			'withouttax' => self::get_total_without_tax( $order ),
			'tax'        => self::get_total_tax( $order ),
			'rounding' => self::get_total_with_tax( $order ) - self::get_total_without_tax( $order ) - self::get_total_tax( $order ),
			'withtax'    => self::get_total_with_tax( $order ),
		);
	}

Result:
Unfortunately the rounding value was 0 even after changing, so the rounding error happens earlier. Will update if I find the culprit.

@andyjohansson
Copy link

Could you take a look at this @NiklasHogefjord?

@andyjohansson andyjohansson added the bug Something isn't working label May 20, 2021
@andyjohansson andyjohansson added this to the 1.2.1 milestone May 20, 2021
@NiklasHogefjord
Copy link
Collaborator

@DanielMarklund, I have tried to recreate the issue but it is working without any problems for me.

I have followed your instructions (1 x 39 kr incl 12% VAT + 3 x 39 kr incl 12% VAT + shipping 29 kr incl 25% VAT + invoice fee 12 kr incl 25% VAT) and this is what is being sent in to Billmate:

{
	"credentials": {
		"id": "removed",
		"hash": "removed",
		"test": "false",
		"version": "2.2.2",
		"client": "WooCommerce_v2:1.1.0.1"
	},
	"data": {
		"Articles": [{
			"artnr": "test1234",
			"title": "Billmate rounding 2",
			"quantity": 1,
			"aprice": 3482,
			"withouttax": 3482,
			"taxrate": 12,
			"discount": 0
		}, {
			"artnr": "test12345",
			"title": "Billmate rounding 1",
			"quantity": 3,
			"aprice": 3482,
			"withouttax": 10446,
			"taxrate": 12,
			"discount": 0
		}],
		"Cart": {
			"Total": {
				"withouttax": 17208,
				"tax": 2492,
				"rounding": 0,
				"withtax": 19700
			},
			"Shipping": {
				"withouttax": 2320,
				"taxrate": 25
			},
			"Handling": {
				"withouttax": 960,
				"taxrate": 25
			}
		},
		"PaymentData": {
			"number": "109"
		}
	},
	"function": "updateCheckout"
},
"timeout": 10
}

What is your Rounding option set to in the --> WooCommerce --> Settings --> Tax tab? Mine looks like this:
image

@NiklasHogefjord
Copy link
Collaborator

Closing this since we haven't heard back from @DanielMarklund. Please feel free to re-open the issue it you still are experiencing issues with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants