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

[12.0] Purchase triple discount error in runbot #834

Closed
manuelcalerosolis opened this issue Dec 24, 2019 · 4 comments
Closed

[12.0] Purchase triple discount error in runbot #834

manuelcalerosolis opened this issue Dec 24, 2019 · 4 comments
Labels
stale PR/Issue without recent activity, it'll be soon closed automatically.

Comments

@manuelcalerosolis
Copy link

Working with this module we have discovered an error that also occurs in the runbot.

We create a new purchase with the following data.

units: 48
Price: 0.33
discount: 5%
total order: 15,05

image

These calculations are correct.

We confirm the budget, and receive the products.

image

The price of the order is still good.

If we make the invoice, we see that the amount has changed.

image

We are trying to check the calculations to see where the error occurs.

Regards.

@manuelcalerosolis manuelcalerosolis changed the title Triple purchase discount error in runbot [12.0] Triple purchase discount error in runbot Dec 24, 2019
@manuelcalerosolis manuelcalerosolis changed the title [12.0] Triple purchase discount error in runbot [12.0] Purchase triple discount error in runbot Dec 24, 2019
@legalsylvain
Copy link
Contributor

Check rounding errors.
See :
48 * .33 * .95 = 15.05....
48 * round(.33 * .95, 2) = 14.88...

@manuelcalerosolis
Copy link
Author

If I uninstall the modules "tripe_discount" the calculations are correct.

image

Something is wrong with these modules.

@manuelcalerosolis
Copy link
Author

The only solution is to completely rewrite the method "_compute_price".

I know it is not a good idea, possible changes in this method will not be contemplated, and this is dangerous.

The ideal would be to propose a change so that the price of the product already with discounts is in a single method, and the code is not so coupled.

Do you think anyone would listen to this proposal?

class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"

discount2 = fields.Float(
    'Discount 2 (%)',
    digits=dp.get_precision('Discount'),
)
discount3 = fields.Float(
    'Discount 3 (%)',
    digits=dp.get_precision('Discount'),
)

@api.multi
@api.depends('discount2', 'discount3')
def _compute_price(self):
    currency = self.invoice_id and self.invoice_id.currency_id or None
    
    # to avoid coupling these calculations must be passed to a method 
    price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
    price *= (1 - (self.discount2 or 0.0) / 100.0)
    price *= (1 - (self.discount3 or 0.0) / 100.0)

    taxes = False
    if self.invoice_line_tax_ids:
        taxes = self.invoice_line_tax_ids.compute_all(price, currency, self.quantity, product=self.product_id,
                                                      partner=self.invoice_id.partner_id)
    self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price
    self.price_total = taxes['total_included'] if taxes else self.price_subtotal
    if self.invoice_id.currency_id and self.invoice_id.currency_id != self.invoice_id.company_id.currency_id:
        currency = self.invoice_id.currency_id
        date = self.invoice_id._get_currency_rate_date()
        price_subtotal_signed = currency._convert(price_subtotal_signed, self.invoice_id.company_id.currency_id,
                                                  self.company_id or self.env.user.company_id,
                                                  date or fields.Date.today())
    sign = self.invoice_id.type in ['in_refund', 'out_refund'] and -1 or 1
    self.price_subtotal_signed = price_subtotal_signed * sign`

@github-actions
Copy link

There hasn't been any activity on this issue in the past 6 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days.
If you want this issue to never become stale, please ask a PSC member to apply the "no stale" label.

@github-actions github-actions bot added the stale PR/Issue without recent activity, it'll be soon closed automatically. label Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale PR/Issue without recent activity, it'll be soon closed automatically.
Projects
None yet
Development

No branches or pull requests

2 participants