From 57542c6ee3fde2faffb197ee41d3c0260a97e73d Mon Sep 17 00:00:00 2001 From: Yolann Sabaux Date: Tue, 28 Mar 2023 13:26:10 +0000 Subject: [PATCH] [FIX] purchase: enable correct currency on onchange partner_id Steps to reproduce: - company currency = USD - set a partner P with a `property_purchase_currency_id` in EUR - create a bill with Azure partner and set an bill line - change to partner P issue -> the currency of the line has not been change - change to Azure issue -> no change about the currency Cause: - We update the move.currency_id but not the line_ids.currency_id - after setting Partner P, we try to set a partner that no `property_purchase_currency_id`, we do not enter in the condition opw-3233527 X-original-commit: 213e22c63f6259e2e69193b7d6a7022d8b6eab20 Part-of: odoo/odoo#119753 --- addons/purchase/models/account_invoice.py | 21 +++++----- .../purchase/tests/test_purchase_invoice.py | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 2f6bdd4f81e..24970089aeb 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -83,23 +83,26 @@ class AccountMove(models.Model): @api.onchange('partner_id', 'company_id') def _onchange_partner_id(self): res = super(AccountMove, self)._onchange_partner_id() - if self.partner_id and\ - self.move_type in ['in_invoice', 'in_refund'] and\ - self.currency_id != self.partner_id.property_purchase_currency_id and\ - self.partner_id.property_purchase_currency_id.id: + + currency_id = ( + self.partner_id.property_purchase_currency_id + or self.env.context.get("default_currency_id") + or self.env.company.currency_id + ) + + if self.partner_id and self.move_type in ['in_invoice', 'in_refund'] and self.currency_id != currency_id: if not self.env.context.get('default_journal_id'): journal_domain = [ ('type', '=', 'purchase'), ('company_id', '=', self.company_id.id), - ('currency_id', '=', self.partner_id.property_purchase_currency_id.id), + ('currency_id', '=', currency_id.id), ] default_journal_id = self.env['account.journal'].search(journal_domain, limit=1) if default_journal_id: self.journal_id = default_journal_id - if self.env.context.get('default_currency_id'): - self.currency_id = self.env.context['default_currency_id'] - if self.partner_id.property_purchase_currency_id: - self.currency_id = self.partner_id.property_purchase_currency_id + + self.currency_id = currency_id + return res @api.depends('line_ids.purchase_line_id') diff --git a/addons/purchase/tests/test_purchase_invoice.py b/addons/purchase/tests/test_purchase_invoice.py index 1dd9e0a2b5c..222000e68a6 100644 --- a/addons/purchase/tests/test_purchase_invoice.py +++ b/addons/purchase/tests/test_purchase_invoice.py @@ -790,3 +790,43 @@ class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon): ['other_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False) self.assertTrue(invoice.id not in po.invoice_ids.ids) + + def test_onchange_partner_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 + """ + + vendor_eur = self.env['res.partner'].create({ + 'name': 'Vendor EUR', + 'property_purchase_currency_id': self.env.ref('base.EUR').id, + }) + vendor_us = self.env['res.partner'].create({ + 'name': 'Vendor USD', + 'property_purchase_currency_id': self.env.ref('base.USD').id, + }) + vendor_no_currency = self.env['res.partner'].create({ + 'name': 'Vendor No Currency', + }) + + move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) + move_form.partner_id = vendor_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 same as the currency of the partner") + 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 partner") + + move_form.partner_id = vendor_us + bill = move_form.save() + + self.assertEqual(bill.currency_id, self.env.ref('base.USD'), "The currency of the Bill should be the same as the currency of the partner") + self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.USD'), "The currency of the Bill lines should be the same as the currency of the partner") + + move_form.partner_id = vendor_no_currency + bill = move_form.save() + + 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")