diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 2237a985de3ed0e72e67495e3e0fa26e05822e02..482d177b19c6e648a99b6eef3862c6dcfbccfd88 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -1246,7 +1246,7 @@ class PurchaseOrderLine(models.Model): line.taxes_id, line.company_id, ) - price_unit = line.product_id.currency_id._convert( + price_unit = line.product_id.cost_currency_id._convert( price_unit, line.currency_id, line.company_id, diff --git a/addons/purchase/tests/test_purchase.py b/addons/purchase/tests/test_purchase.py index 1977e2423bf83be3e10d5477978a16f56e27b4db..5215dc9bfbcc836bbf91c2b33424c4cb28535b54 100644 --- a/addons/purchase/tests/test_purchase.py +++ b/addons/purchase/tests/test_purchase.py @@ -291,9 +291,12 @@ class TestPurchase(AccountTestInvoicingCommon): pol.product_qty += 1 self.assertEqual(pol.name, "New custom description") - def test_unit_price_precision_multicurrency(self): + def test_purchase_multicurrency(self): """ Purchase order lines should keep unit price precision of products + Also the products having prices in different currencies should be + correctly handled when creating a purchase order i-e product having a price of 100 usd + and when purchasing in EUR company the correct conversion should be applied """ self.env['decimal.precision'].search([ ('name', '=', 'Product Price'), @@ -334,6 +337,49 @@ class TestPurchase(AccountTestInvoicingCommon): purchase_order_coco = po_form.save() self.assertEqual(purchase_order_coco.order_line.price_unit, currency_rate.rate * product.standard_price, "Value shouldn't be rounded ðŸ«") + #check if the correct currency is set on the purchase order by comparing the expected price and actual price + + company_a = self.company_data['company'] + company_b = self.company_data_2['company'] + + company_b.currency_id = currency + + self.env['res.currency.rate'].create({ + 'name': '2023-01-01', + 'rate': 2, + 'currency_id': currency.id, + 'company_id': company_b.id, + }) + + product_b = self.env['product.product'].with_company(company_a).create({ + 'name': 'product_2', + 'uom_id': self.env.ref('uom.product_uom_unit').id, + 'standard_price': 0.0, + }) + + self.assertEqual(product_b.cost_currency_id, company_a.currency_id, 'The cost currency should be the one set on' + ' the company') + + product_b = product_b.with_company(company_b) + + self.assertEqual(product_b.cost_currency_id, currency, 'The cost currency should be the one set on the company,' + ' as the product is now opened in another company') + + product_b.supplier_taxes_id = False + product_b.update({'standard_price': 10.0}) + + #create a purchase order with the product from company B + order_b = self.env['purchase.order'].with_company(company_b).create({ + 'partner_id': self.partner_a.id, + 'order_line': [(0, 0, { + 'product_id': product_b.id, + 'product_qty': 1, + 'product_uom': self.env.ref('uom.product_uom_unit').id, + })], + }) + + self.assertEqual(order_b.order_line.price_unit, 10.0, 'The price unit should be 10.0') + def test_purchase_not_creating_useless_product_vendor(self): """ This test ensures that the product vendor is not created when the product is not set on the purchase order line.