diff --git a/purchase_stock_ux/models/stock_rule.py b/purchase_stock_ux/models/stock_rule.py index d69ab9a1..daf2e487 100644 --- a/purchase_stock_ux/models/stock_rule.py +++ b/purchase_stock_ux/models/stock_rule.py @@ -24,7 +24,7 @@ def _prepare_purchase_order_line(self, product_id, product_qty, product_uom, com # if price was not computed (not seller or seller price = 0.0), then # use standar price if not res["price_unit"]: - price_unit = product_id.with_context(force_company=company_id.id).standard_price + price_unit = product_id.with_company(company_id).standard_price company_currency = company_id.currency_id if price_unit and po.currency_id != company_currency: price_unit = company_currency._convert( @@ -42,14 +42,14 @@ def _update_purchase_order_line(self, product_id, product_qty, product_uom, comp # if price was not computed (not seller or seller price = 0.0), then # use standar price if not res["price_unit"]: - price_unit = product_id.with_context(force_company=company_id.id).standard_price + price_unit = product_id.with_company(company_id).standard_price company_currency = company_id.currency_id if price_unit and line.order_id.currency_id != company_currency: price_unit = company_currency._convert( price_unit, line.order_id.currency_id, company_id, line.order_id.date_order or fields.Date.today() ) - if price_unit and line.product_uom and product_id.uom_id != line.product_uom: - price_unit = product_id.uom_id._compute_price(price_unit, line.product_uom) + if price_unit and line.product_uom_id and product_id.uom_id != line.product_uom_id: + price_unit = product_id.uom_id._compute_price(price_unit, line.product_uom_id) res["price_unit"] = price_unit return res diff --git a/purchase_stock_ux/tests/__init__.py b/purchase_stock_ux/tests/__init__.py index 76f30cf0..99e464fd 100644 --- a/purchase_stock_ux/tests/__init__.py +++ b/purchase_stock_ux/tests/__init__.py @@ -6,3 +6,4 @@ from . import test_purchase_order from . import test_stock_orderpoint from . import test_qty_returned +from . import test_stock_rule diff --git a/purchase_stock_ux/tests/test_stock_rule.py b/purchase_stock_ux/tests/test_stock_rule.py new file mode 100644 index 00000000..34dccf38 --- /dev/null +++ b/purchase_stock_ux/tests/test_stock_rule.py @@ -0,0 +1,45 @@ +from odoo import Command +from odoo.tests import TransactionCase, tagged + + +@tagged("post_install", "-at_install") +class TestStockRule(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env["res.partner"].create({"name": "Vendor"}) + cls.warehouse = cls.env["stock.warehouse"].search([("company_id", "=", cls.env.company.id)], limit=1) + cls.product = cls.env["product.product"].create( + { + "name": "Product without vendor price", + "is_storable": True, + "standard_price": 100.0, + "route_ids": [Command.link(cls.env.ref("purchase_stock.route_warehouse0_buy").id)], + "seller_ids": [Command.create({"partner_id": cls.partner.id, "price": 0.0})], + } + ) + + def _run_buy_procurement(self, qty): + self.env["stock.rule"].run( + [ + self.env["stock.rule"].Procurement( + self.product, + qty, + self.product.uom_id, + self.warehouse.lot_stock_id, + "Test", + "Test", + self.env.company, + {"warehouse_id": self.warehouse}, + ) + ] + ) + + def test_merge_po_line_falls_back_to_standard_price(self): + """Merging a procurement into an existing PO line uses the product cost when the vendor has no price.""" + self._run_buy_procurement(1) + self._run_buy_procurement(1) + line = self.env["purchase.order.line"].search([("product_id", "=", self.product.id)]) + self.assertEqual(len(line), 1) + self.assertEqual(line.product_qty, 2) + self.assertEqual(line.price_unit, 100.0)