From d7ce6d9136ee02394b560aceffa80b9a28601f2b Mon Sep 17 00:00:00 2001 From: Yolann Sabaux Date: Wed, 12 Apr 2023 10:28:24 +0000 Subject: [PATCH] [FIX] purchase: prevent unnecesary change of currency Steps to reproduce: - put the currency of the bill to eur - change the partner with a partner with no purchase currency set Issue: The bill is re-set to usd Note: addendum to https://github.com/odoo/odoo/pull/116852 opw-3233527 closes odoo/odoo#119753 X-original-commit: 51ff9c6026b8f9079aba7c9e26891f62c782761d Signed-off-by: Cedric Snauwaert Signed-off-by: Yolann Sabaux (yosa) --- addons/purchase/models/account_invoice.py | 2 +- .../purchase/tests/test_purchase_invoice.py | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 24970089aeb..65c3a13e1ad 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -87,7 +87,7 @@ class AccountMove(models.Model): currency_id = ( self.partner_id.property_purchase_currency_id or self.env.context.get("default_currency_id") - or self.env.company.currency_id + or self.currency_id ) if self.partner_id and self.move_type in ['in_invoice', 'in_refund'] and self.currency_id != currency_id: diff --git a/addons/purchase/tests/test_purchase_invoice.py b/addons/purchase/tests/test_purchase_invoice.py index 222000e68a6..11b103f12eb 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -830,3 +830,33 @@ class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon): self.assertEqual(bill.currency_id, self.env.company.currency_id, "The currency of the Bill should be the same as the currency of the company") self.assertEqual(bill.invoice_line_ids.currency_id, self.env.company.currency_id, "The currency of the Bill lines should be the same as the currency of the company") + + def test_onchange_partner_no_currency(self): + """ + Test that the currency of the Bill is correctly set when the partner is changed + as well as the currency of the Bill lines even if the partner has no property_purchase_currency_id set + """ + + vendor_a = self.env['res.partner'].create({ + 'name': 'Vendor A with No Currency', + }) + vendor_b = self.env['res.partner'].create({ + 'name': 'Vendor B with No Currency', + }) + + move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) + move_form.partner_id = vendor_a + move_form.currency_id = self.env.ref('base.EUR') + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_order + line_form.quantity = 1 + bill = move_form.save() + + self.assertEqual(bill.currency_id, self.env.ref('base.EUR'), "The currency of the Bill should be the one set on the Bill") + self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.EUR'), "The currency of the Bill lines should be the same as the currency of the Bill") + + move_form.partner_id = vendor_b + bill = move_form.save() + + self.assertEqual(bill.currency_id, self.env.ref('base.EUR'), "The currency of the Bill should be the one set on the Bill") + self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.EUR'), "The currency of the Bill lines should be the same as the currency of the Bill")