From 988b4e9a7b110f596d0e8a25326e088c4572aa1a Mon Sep 17 00:00:00 2001 From: Yolann Sabaux 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 note fw 16: The record must be saved in order to trigger the compute in https://github.com/odoo/odoo/blob/5a256af35e5d612efed9ed8af1cf23fd62bd83f4/addons/account/models/account_move_line.py#L449-L457 in order to recompute the currency of the lines closes odoo/odoo#122075 X-original-commit: d0109dc5d7b0012ad1a4c45e5204934ae481779b Signed-off-by: Yolann Sabaux (yosa) --- addons/purchase/models/account_invoice.py | 2 +- addons/purchase/tests/test_purchase_invoice.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) 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")