From 647e289c06e591a9d8fdbd33129df9cbe0acfc1d Mon Sep 17 00:00:00 2001
From: Yolann Sabaux <yosa@odoo.com>
Date: Wed, 12 Apr 2023 10:28:24 +0000
Subject: [PATCH] [FIX] purchase: prevent unnecesary change of currency

Steps to reproduce:
- put the currency of the bill to eur
- change the partner with a partner with no purchase currency set

Issue:
The bill is re-set to usd

Note:
addendum to https://github.com/odoo/odoo/pull/116852

opw-3233527

closes odoo/odoo#118350

Signed-off-by: Cedric Snauwaert <csn@odoo.com>
---
 addons/purchase/models/account_invoice.py     |  2 +-
 .../purchase/tests/test_purchase_invoice.py   | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py
index 856a84e6fc7d..7dfcc9d5ab64 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 ecde9b1f08ef..9fd95c4e5596 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")
-- 
GitLab