From 93fe7f9e6ac98f55204c69f1b5b7d2ef63b8253b Mon Sep 17 00:00:00 2001 From: "Andrea Grazioso (agr-odoo)" Date: Fri, 8 Dec 2023 16:57:26 +0100 Subject: [PATCH] [FIX] purchase_stock: traceback on empty date Configure a product having category set with: - Costing Method: Average Cost (AVCO) - Inventory Valuation: Manual Create a purchase order with the product Confirm Receive Products Create bill (do not set date) Confirm Traceback will appear: 'AssertionError: convert amount from unknown date' It occurs because of an attempted currency conversion without a date As the bill is without date it should raise an UserError instead opw-3628295 closes odoo/odoo#147367 X-original-commit: 03436a006f31f4bf42336fe31514161562c9b180 Signed-off-by: Adrien Widart (awt) Signed-off-by: Arnold Moyaux (arm) --- .../models/account_move_line.py | 8 +++-- .../tests/test_stockvaluation.py | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/addons/purchase_stock/models/account_move_line.py b/addons/purchase_stock/models/account_move_line.py index 7d8a2deded1..02c2a9425cb 100644 --- a/addons/purchase_stock/models/account_move_line.py +++ b/addons/purchase_stock/models/account_move_line.py @@ -203,20 +203,22 @@ class AccountMoveLine(models.Model): aml = self aml_gross_price_unit = aml._get_gross_unit_price() - aml_price_unit = aml.currency_id._convert(aml_gross_price_unit, aml.company_id.currency_id, aml.company_id, aml.move_id.invoice_date, round=False) + # convert from aml currency to company currency + aml_price_unit = aml_gross_price_unit / aml.currency_rate aml_price_unit = aml.product_uom_id._compute_price(aml_price_unit, product_uom) unit_valuation_difference = aml_price_unit - layer_price_unit # Generate the AML values for the already out quantities - unit_valuation_difference_curr = self.company_id.currency_id._convert(unit_valuation_difference, self.currency_id, self.company_id, self.move_id.invoice_date, round=False) + # convert from company currency to aml currency + unit_valuation_difference_curr = unit_valuation_difference * self.currency_rate unit_valuation_difference_curr = product_uom._compute_price(unit_valuation_difference_curr, self.product_uom_id) out_qty_to_invoice = product_uom._compute_quantity(out_qty_to_invoice, self.product_uom_id) if not float_is_zero(unit_valuation_difference_curr * out_qty_to_invoice, precision_rounding=self.currency_id.rounding): aml_vals_list += self._prepare_pdiff_aml_vals(out_qty_to_invoice, unit_valuation_difference_curr) # Generate the SVL values for the on hand quantities (and impact the parent layer) - po_pu_curr = po_line.currency_id._convert(po_line.price_unit, self.currency_id, self.company_id, self.move_id.invoice_date, round=False) + po_pu_curr = po_line.currency_id._convert(po_line.price_unit, self.currency_id, self.company_id, self.move_id.invoice_date or self.date or fields.Date.context_today(self), round=False) price_difference_curr = po_pu_curr - aml_gross_price_unit if not float_is_zero(unit_valuation_difference * qty_to_correct, precision_rounding=self.company_id.currency_id.rounding): svl_vals = self._prepare_pdiff_svl_vals(layer, sign * qty_to_correct, unit_valuation_difference, price_difference_curr) diff --git a/addons/purchase_stock/tests/test_stockvaluation.py b/addons/purchase_stock/tests/test_stockvaluation.py index 15360f49208..42c03015804 100644 --- a/addons/purchase_stock/tests/test_stockvaluation.py +++ b/addons/purchase_stock/tests/test_stockvaluation.py @@ -7,7 +7,7 @@ from freezegun import freeze_time from unittest.mock import patch import odoo -from odoo import fields +from odoo import fields, exceptions from odoo.tests import Form from odoo.tests.common import TransactionCase, tagged from odoo.addons.account.tests.common import AccountTestInvoicingCommon @@ -2806,3 +2806,34 @@ class TestStockValuationWithCOA(AccountTestInvoicingCommon): move2.picked = True picking2.button_validate() self.assertAlmostEqual(move2.stock_valuation_layer_ids.unit_cost, price_unit_USD) + + def test_pdiff_date_usererror(self): + """ + Test pdiff operations complete without errors in case we don't have + the bill date. A UserError is raised as usual. + """ + self.product1.product_tmpl_id.categ_id.property_cost_method = 'average' + + po = self.env['purchase.order'].create({ + 'partner_id': self.partner_id.id, + 'order_line': [ + (0, 0, { + 'name': self.product1.name, + 'product_id': self.product1.id, + 'product_qty': 1.0, + 'product_uom': self.product1.uom_po_id.id, + 'price_unit': 100.0, + 'taxes_id': False, + }), + ], + }) + po.button_confirm() + + receipt = po.picking_ids + receipt.move_ids.move_line_ids.quantity = 1.0 + receipt.button_validate() + + action = po.action_create_invoice() + bill = self.env["account.move"].browse(action["res_id"]) + with self.assertRaises(exceptions.UserError): + bill.action_post()