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")