diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 0f39a58045b..8897f16e565 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -86,7 +86,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['res.currency'].browse(self.env.context.get("default_currency_id")) or self.currency_id ) diff --git a/addons/purchase/tests/test_purchase_invoice.py b/addons/purchase/tests/test_purchase_invoice.py index 11b103f12eb..92c2b8eaf69 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -835,6 +835,7 @@ class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon): """ 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 + or when and the `default_currency_id` is defined in the context """ vendor_a = self.env['res.partner'].create({ @@ -844,7 +845,8 @@ class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon): 'name': 'Vendor B with No Currency', }) - move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) + ctx = {'default_move_type': 'in_invoice'} + move_form = Form(self.env['account.move'].with_context(ctx)) 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: @@ -860,3 +862,17 @@ class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon): 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") + + ctx['default_currency_id'] = self.currency_data['currency'].id + move_form_currency_in_context = Form(self.env['account.move'].with_context(ctx)) + move_form_currency_in_context.currency_id = self.env.ref('base.EUR') + with move_form_currency_in_context.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_order + line_form.quantity = 1 + move_form_currency_in_context.save() + + move_form_currency_in_context.partner_id = vendor_a + bill = move_form_currency_in_context.save() + + self.assertEqual(bill.currency_id, self.currency_data['currency'], "The currency of the Bill should be the one of the context") + self.assertEqual(bill.invoice_line_ids.currency_id, self.currency_data['currency'], "The currency of the Bill lines should be the same as the currency of the Bill")