Tax calculation issue for a Prestashop Plugin - math

I am building a Prestashop plugin, and i have stumbled on to this tax calculation issue.
The program flow is as follows:
A person places an order at the eCommerce website, the order is accepted, the order details such as items, prices, taxes, discounts etc is send to an external invoice API service.
An example of an order could be:
(These prices include taxes)
1x T-shirt 20.64
1x Shipping 125
1x Discount -18.58 (In this particular test case, the discount is 90% off the product, the math is simple 20.64 * 0.90 = 18.58)
Order total after tax is applied: 127.06
This is all fine, but, when i send the details off to the API, the order is saved as the following:
(These prices are saved without tax applied, the tax rate being 25%)
1x T-shirt 16.51
1x Shipping 100
1x Discount -14
Order total after tax is applied: 128.14
As you see, the order totals do not mach, the difference is 1.08, i am thinking this a discount & tax issue.
Its worth noting that the external site does its own math on the values sent, e.g products, tax etc. I cannot influence the way the external invoicing site does its calculations. I think the issue occurs in the order of which the operations are performed on the different platforms.
Its also worth mentioning that the code works perfectly fine when there is no discount present.
What can i do prior to sending the values, so that i end up with the same values both places.

The total they compute is indeed (100 + 16.51 - 14) x 1.25 = 128.1375
However their computation of taxless discount is wrong, 18.58 / 1.25 = 14.864 not 14.
Is there a rule that discount should be rounded (or floored or ceiled) to an integer value ?
discount may be non-integer, implying different tax rate
If not that means they divide the discount by 1.32714285714, thus considering there is 32.71...% taxes on discounts. This would be strange and unlikely, since when adding the discount again they use the normal 25%
Check your prestashop documentation, local laws or whatever to find out if this is real, and whether the value is for example 33% or 32.7% Since I reverse-engineered the value from the tax value they stored, it could be distorted by the rounding to 2 decimals.
Then, to get the correct result, replace the discount by discount * 1.32714285714 / 1.25, that is, replace your expected tax rate by the one they apply.
discount is rounded to integer
If the taxless discount does have to be an integer, we can modify it so that it is on our side as well, since I guess what matters most is the final price. So use this algorithm to send in a discount that will finally be an integer:
% get taxless prices
price_notax = price / 1.25
discount_notax = discount / 1.25
% transfer decimal part of discount into price reduction
discount_int = floor(discount_notax)
price_compensated = price_notax - (discount_int - discount_notax)
% re-add tax to get values to send
discount_send = discount_int * 1.25
price_send = price_compensated * 1.25
The sum of values should be the same : price - discount == price_send - discount_send
check if it's not a bug in Prestashop
Update to the latest version, look up their bug tracker or contact them. File a report if there isn't one.
One of the workarounds above will work for now, but if it's a bug that gets fixed and you use the first one, you will be billing wrong amounts again.

Related

Woocommerce - Get WC_Order_Item_Product line discount

i've been trying this for while with no success, i'm trying to get the WC_Order_Item_Product applied discount, OR even better, the line subtotal without tax BUT with discount once the order is already created in the backend, example:
(Backend)
Order #xxxx with 20% discount:
Item - Price - Amount - Total
Pen - 10$ - 2 - 16$ (4$ discount)
Jar - 30$ - 1 - 24$ (6$ discount)
Yup expensive jar... so i need either the subtotal (16$, 24$) or the discount so i can make the maths (4$, 6$) and also without taxes (as i said, subtotal).
Tried: $wc_order_item_product->get_subtotal() but it returns the full price without the discount, i need what the customer really paid (with the discount).
Thanks in advance guys, really appreciate it.
If anyone is breaking is head already i've found the solution, the method "get_total()" from WC_Order_Item_Product DOESN'T give the total (sum of price+discount+taxes) instead, it gives the total without taxes (price+discount) =D

Two different prices for the one product on the checkout page (bug)

The correct price for the product is €38 and VAT is included to the price #23%
In the products page the price 30.89 plus 23% = 30.894*1.23 = 37.99962
But when I add a product and go tho the cart, the total price and the cart prices are out by a cent
the cart is €38 but the check out is €38.01
I tried every combination possible to get both prices correct and I can get €37.99 and €40.01 but I can't get €40 in both carts.
I'm not sure how they could be different I thought they would be going by the same calculation but obviously not.
It looks like a rounding issue. Try increasing the number of decimals in WooCommerce > Settings > General

Add flat rate shipping fees in Woocommerce?

Hi , I am working in Woocommerce and I want to add shipping fees $7 on
the total order that less than $50 .
Could you please help me on this issue ?
Advanced Costs option under WC settings
The Cost field allows you to charge a flat rate per item, a percentage based cost or a minimum fee.
Available placeholders:
[qty] – Number of products in the cart
[fee] – An additional fee. This fee has two optional arguments.
percent – A percentage based on total order cost.
min_fee – A minimum amount. Useful when using percentages.
Examples
10 + ( 2 * [qty] ) – A base shipping cost of $10 plus $2 for each item in the cart.
20 + [fee percent="10" min_fee="4"] – A base shipping cost of $20 plus 10% of the order total, which is at least $4.
Check here

WooCommerce - Tax Calculated Incorrectly?

I am running the latest WordPress with the latest WooCommerce, as of this writing.
I have one standard tax rule setup for 20%. This is UK VAT.
I have a product setup at £350. 20% of 350 should be 280.
20 x 350 / 100 = 70
However, on my product page AND checkout basket etc, the amount comes to: 291.67 (ex Tax). This should be 280. This is really wrong! So I'm not sure it's related to how I am coding the output of the product page, since I haven't touched the checkout template at all.
I am not sure if I have set something up incorrectly? Why am I getting such an odd number.
Some rules I have setup:
Yes, I will enter prices inclusive of tax.
Display prices during cart/checkout: Excluding tax.
Shipping tax class: Shipping tax class based on cart items.
I really cannot work this one out.
The tax setup:
The tax on the checkout:
Any help greatly appreciated!
Thanks,
Mikey
I think its correct, its not:
ExVAT = £350 - ((£350 / 100) * 20) = £280
as that's calculating off the price that already includes the VAT, rather its;
20% / 100 = 0.2
0.2 + 1 = 1.2
£350 / 1.2 = £291.67

Drupal and Ubercart - buy products including and excluding tax

I am developing an e-commerce site for use in the UK, and have been required to let the user buy products either with or without tax included.
I have slightly modified the AAC module to show prices either including or excluding VAT depending on a cookie which is set via a "view prices including/excluding" link, but of course whenever a product is added to cart, the price is that from the database - i.e. including VAT.
Does anyone know of any neat tricks or modules that would let me do this?
We've decided to go for just viewing prices excluding the vat, and not being able to buy them at the excluding vat price.
In case anyone needs it, here is the code/formula used to get the full price and work out the lower vat-free price:
(line 217-222 of uc_aac.module in my version - and you will need a div with the class "excluding_VAT").
//Get the updated price, minus VAT
new_val = parseFloat(updated_price.substr(1));
new_val = (100/115) * new_val;
new_val = Math.round(new_val*Math.pow(10,2))/Math.pow(10,2);
$(".excluding_VAT").html("£"+new_val+" exc VAT");

Resources