From da6724e333c4b4998e1e0b182d16f7c9e6e161ee Mon Sep 17 00:00:00 2001 From: Yolann Sabaux <yosa@odoo.com> Date: Wed, 17 May 2023 11:40:42 +0000 Subject: [PATCH] [FIX] purchase: prevent traceback onchange partner Steps to reproduce: In an account move, if the partner_id is changed to one that does not have a value assigned in the property_purchase_currency_id field and with a value in the context for default_currency_id, when passing through the _onchange_partner_id function of the purchase module, Cause: the variable currency_id will take the value in the context as second option causing an error when trying to get the value in currency_id.id because currency_id will be an integer and not a record. issue-121232 closes odoo/odoo#121921 X-original-commit: e04d4a0b4d604927252a44a8472fcade300b5f6f Signed-off-by: Yolann Sabaux (yosa) <yosa@odoo.com> Signed-off-by: Adrien Widart (awt) <awt@odoo.com> --- addons/purchase/models/account_invoice.py | 2 +- addons/purchase/tests/test_purchase_invoice.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 7dfcc9d5ab64..eaf50a5297c9 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -84,7 +84,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 ab1ce88609d2..5cde59616498 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -665,6 +665,7 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon): """ 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({ @@ -674,7 +675,8 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon): '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: @@ -690,3 +692,15 @@ class TestPurchaseToInvoice(AccountTestInvoicingCommon): 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.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") -- GitLab