From fdd4af3e2abc2ef92280a29cfcbde91d0c823cbf Mon Sep 17 00:00:00 2001 From: Adrien Widart Date: Tue, 3 Aug 2021 12:12:29 +0000 Subject: [PATCH] [FIX] purchase: use Product Price precision in bills MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the Product Price precision is greater than the currency precision, it can lead to incorrect data To reproduce the error: (Enable debug mode) 1. Settings > Technical > Database Structure > Decimal Accuracy, edit Product Price: - Digits: 3 2. Create a PO - Add a product: - Quantity: 12 - Unit Price: 0.001 3. Save, Confirm, Edit the PO: - Qty Received: 12 - (Note that the total is $0.01) 4. Create a bill: - Add the PO to the field "Auto-Complete" Error: The unit price is $0.000 and so does the total The rounding of the unit price should be based on the Product Price precision, not the currency precision. OPW-2601867 closes odoo/odoo#74813 X-original-commit: 1123856c77cce4b69059b63c2bcbb382c7f0719e Signed-off-by: Rémy Voet Signed-off-by: Adrien Widart --- addons/purchase/models/purchase.py | 2 +- .../purchase/tests/test_purchase_invoice.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 59ef2f525e4..03d4368559b 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -1260,7 +1260,7 @@ class PurchaseOrderLine(models.Model): 'product_id': self.product_id.id, 'product_uom_id': self.product_uom.id, 'quantity': self.qty_to_invoice, - 'price_unit': self.currency_id._convert(self.price_unit, aml_currency, self.company_id, date), + 'price_unit': self.currency_id._convert(self.price_unit, aml_currency, self.company_id, date, round=False), 'tax_ids': [(6, 0, self.taxes_id.ids)], 'analytic_account_id': self.account_analytic_id.id, 'analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)], diff --git a/addons/purchase/tests/test_purchase_invoice.py b/addons/purchase/tests/test_purchase_invoice.py index 81cc1abc1e1..3c67d2ebc20 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -275,3 +275,27 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon): self.assertEqual(len(amls), 2) self.assertEqual(amls[0].amount_currency, 1000) self.assertEqual(amls[1].amount_currency, 500) + + def test_product_price_decimal_accuracy(self): + self.env['ir.model.data'].xmlid_to_object('product.decimal_price').digits = 3 + self.env.company.currency_id.rounding = 0.01 + + po = self.env['purchase.order'].with_context(tracking_disable=True).create({ + 'partner_id': self.partner_a.id, + 'order_line': [(0, 0, { + 'name': self.product_a.name, + 'product_id': self.product_a.id, + 'product_qty': 12, + 'product_uom': self.product_a.uom_id.id, + 'price_unit': 0.001, + 'taxes_id': False, + })] + }) + po.button_confirm() + po.order_line.qty_received = 12 + + move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) + move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-po.id) + move = move_form.save() + + self.assertEqual(move.amount_total, 0.01)