From 992532e9efda62de7ae54b4807fae456ebac94e8 Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Wed, 10 Apr 2024 06:41:44 +0000 Subject: [PATCH] [FIX] account_edi[_ubl_cii]: company_id when searching retrieve_partner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: 1. Have at least 2 companies ("A" and "B") 2. Export an xml (Bis 3 for instance) for an invoice with customer "Azure Interior" 3. Set a company on "Azure Interior" (say: A) 4. Import the xml in multicompany mode, with current company = B The partner "Azure Interior" should be retrieved, but when writing it on the invoice, it will throw a UserError "odoo.exceptions.UserError: Incompatible companies on records: 'Draft Invoice (* 63) (INV/2024/00006)' belongs to company 'B' and 'Partner' (partner_id: 'Azure Interior') belongs to another company." Cause: We try to write a partner on an invoice belonging to another company. It only occors when we have several companies selected because there is the global rule `base.res_partner_rule` that will add `('company_id', 'in', company_ids + [False])` to any search domain on the partner (`company_ids` is replaced by `env.companies.ids`, see `_eval_context`). Fix: Ensure any search domain contains `env.company.id`: the `company_id` of the move being created. opw-3829223 closes odoo/odoo#162019 X-original-commit: 18119d446bdca9c04b7085bd1a1bd2f0e0b94ff3 Related: odoo/enterprise#60816 Signed-off-by: William André (wan) Signed-off-by: Julien Van Roy (juvr) --- addons/account/models/partner.py | 8 +++----- addons/account_edi_ubl_cii/models/account_edi_common.py | 4 +++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/addons/account/models/partner.py b/addons/account/models/partner.py index 9ea6f902f7f..8a7d758db37 100644 --- a/addons/account/models/partner.py +++ b/addons/account/models/partner.py @@ -880,12 +880,10 @@ class ResPartner(models.Model): return None return self.env['res.partner'].search(domain + extra_domain, limit=1) - company = company or self.env.company for search_method in (search_with_vat, search_with_domain, search_with_phone_mail, search_with_name): - for extra_domain in ([*self.env['res.partner']._check_company_domain(company), ('company_id', '!=', False)], []): - partner = search_method(extra_domain) - if partner and len(partner) == 1: - return partner + partner = search_method(self.env['res.partner']._check_company_domain(company or self.env.company)) + if partner and len(partner) == 1: + return partner return self.env['res.partner'] def _merge_method(self, destination, source): diff --git a/addons/account_edi_ubl_cii/models/account_edi_common.py b/addons/account_edi_ubl_cii/models/account_edi_common.py index b5352db4ee7..f4b2c3e35c0 100644 --- a/addons/account_edi_ubl_cii/models/account_edi_common.py +++ b/addons/account_edi_ubl_cii/models/account_edi_common.py @@ -352,7 +352,9 @@ class AccountEdiCommon(models.AbstractModel): def _import_retrieve_and_fill_partner(self, invoice, name, phone, mail, vat, country_code=False): """ Retrieve the partner, if no matching partner is found, create it (only if he has a vat and a name) """ - invoice.partner_id = self.env['res.partner']._retrieve_partner(name=name, phone=phone, mail=mail, vat=vat) + invoice.partner_id = self.env['res.partner'] \ + .with_company(invoice.company_id) \ + ._retrieve_partner(name=name, phone=phone, mail=mail, vat=vat) if not invoice.partner_id and name and vat: partner_vals = {'name': name, 'email': mail, 'phone': phone} country = self.env.ref(f'base.{country_code.lower()}', raise_if_not_found=False) if country_code else False