bluedynamics / bda.plone.shop

Generic Shop Solution for Plone
Other
20 stars 15 forks source link

Incorrect total price of items in cart #65

Open andreesg opened 8 years ago

andreesg commented 8 years ago

The total price of items in cart is incorrect while using 21% of VAT. The image bellow shows an example of the issue (77.99€ instead of 78€) in a fresh Plone installation.

Example: jnx8rpb

The net price used for the above items was:

Any ideas what is causing this problem?

rnixx commented 8 years ago

At some places there is float calculation left instead of decimal calculation IIRC. That may cause the difference. Let me take a look

rnixx commented 8 years ago

This is where the data gets prepared for display.

https://github.com/bluedynamics/bda.plone.cart/blob/master/src/bda/plone/cart/__init__.py#L138

Here is the actual implementation of cart net and vat

https://github.com/bluedynamics/bda.plone.shop/blob/master/src/bda/plone/shop/cartdata.py#L75

i have no time right now to dig deeper, but I fear best would be to write some tests in order to figure out the cause now

andreesg commented 8 years ago

Thanks for the information.

Here's an example that might help: Product net price: 10.33€ Product gross price: 12.5€ Quantity: 8 VAT: 21%

Calculation of item_net:

item_net = Decimal(str(data.net)) * count
item_net = Decimal(’10.33') * 8
item_net = Decimal('82.64')

Calculation of item_vat:

item_vat = (item_net / Decimal(100)) * Decimal(str(data.vat)) * count
item_vat = (10.33 / Decimal(100)) * Decimal('21') * 8
item_vat = Decimal('17.3544')

Calculation of total price:

total = net + vat
total = Decimal('82.64') + Decimal('17.3544')
total = Decimal('99.9944’)

After using the ascur method: https://github.com/bluedynamics/bda.plone.cart/blob/master/src/bda/plone/cart/__init__.py#L48 The result is 99.99€ instead of 100€.

Do you have any insight where this can be solved? Let me know if I can be of any help.

ezvirtual commented 8 years ago

Hi there, We got a bit lost down the rabbit hole trying to figure out how to resolve this one at one point.

Following this we decided to change the validation in order to allow more decimal places to be included in decimal fields. (So you can include more decimals in the item price and avoid this error:

Item net price The entered value is not a valid decimal literal.

We patch the relevant locales in zope/i18n/locales/data/

Changing the decimal literal validator to allow more decimal places to be added to the net price:

<numbers>
+       <decimalFormats>
+           <decimalFormatLength >
+               <decimalFormat >
+                   <pattern>#,##0.#########;-#,##0.#########</pattern>
+               </decimalFormat>
+           </decimalFormatLength >
+       </decimalFormats>
        <currencies>

Definitely not the most eloquent of solutions but it does seem to be a workable workaround...