diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 856a84e6fc7dfc72ccd38a106afe29dedab26f9d..7dfcc9d5ab64c5deed642581d5ddfff3f55a3194 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -85,7 +85,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 ecde9b1f08efc41a1c0f5825825fb8b84b1f2169..9fd95c4e5596c274a54083ddb46d78ce62eff626 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -616,3 +616,33 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon): 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")