Git Product home page Git Product logo

Comments (8)

DanielMarklund avatar DanielMarklund commented on June 26, 2024

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

from billmate-checkout-for-woocommerce.

DanielMarklund avatar DanielMarklund commented on June 26, 2024

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.

from billmate-checkout-for-woocommerce.

DanielMarklund avatar DanielMarklund commented on June 26, 2024

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

from billmate-checkout-for-woocommerce.

DanielMarklund avatar DanielMarklund commented on June 26, 2024

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.

from billmate-checkout-for-woocommerce.

DanielMarklund avatar DanielMarklund commented on June 26, 2024

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.

from billmate-checkout-for-woocommerce.

andyjohansson avatar andyjohansson commented on June 26, 2024

Could you take a look at this @NiklasHogefjord?

from billmate-checkout-for-woocommerce.

NiklasHogefjord avatar NiklasHogefjord commented on June 26, 2024

@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

from billmate-checkout-for-woocommerce.

NiklasHogefjord avatar NiklasHogefjord commented on June 26, 2024

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.

from billmate-checkout-for-woocommerce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.