[FIX] account_edi_ubl_cii: fix partner creation during import

If a partner in the imported invoice does not exist, we create one on the fly. However, the partner is created without a country and `_run_vat_test` doesn't check it properly in 15.0, and fails in later versions, where we try to get the country code from the country we pass from `_import_retrieve_and_fill_partner`, which is `False`.

This commit retrieves the country from the imported invoice to create the partner and passes the object instead of the `country_code` string to `_run_vat_test`

closes odoo/odoo#122410

X-original-commit: fc1f9b15e54794b1029daffa3719b2f30873ee0a
Signed-off-by: Laurent Smet <las@odoo.com>
Signed-off-by: Aliya Tastemirova (alta) <alta@odoo.com>
This commit is contained in:
aliya
2023-05-25 21:28:51 +02:00
parent c207b40b51
commit dd7df8122f
2 changed files with 9 additions and 5 deletions
@@ -347,14 +347,17 @@ class AccountEdiCommon(models.AbstractModel):
return True
def _import_retrieve_and_fill_partner(self, invoice, name, phone, mail, vat):
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)
if not invoice.partner_id and name and vat:
invoice.partner_id = self.env['res.partner'].create({'name': name, 'email': mail, 'phone': phone})
country_code = invoice.partner_id.commercial_partner_id.country_code
if vat and self.env['res.partner']._run_vat_test(vat, country_code, invoice.partner_id.is_company):
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
if country:
partner_vals['country_id'] = country.id
invoice.partner_id = self.env['res.partner'].create(partner_vals)
if vat and self.env['res.partner']._run_vat_test(vat, country, invoice.partner_id.is_company):
invoice.partner_id.vat = vat
def _import_fill_invoice_allowance_charge(self, tree, invoice, qty_factor):
@@ -485,7 +485,8 @@ class AccountEdiXmlUBL20(models.AbstractModel):
phone = _find_value(f'//cac:Accounting{role}Party/cac:Party//cbc:Telephone')
mail = _find_value(f'//cac:Accounting{role}Party/cac:Party//cbc:ElectronicMail')
name = _find_value(f'//cac:Accounting{role}Party/cac:Party//cbc:Name')
self._import_retrieve_and_fill_partner(invoice, name=name, phone=phone, mail=mail, vat=vat)
country_code = _find_value(f'//cac:Accounting{role}Party/cac:Party//cac:Country//cbc:IdentificationCode')
self._import_retrieve_and_fill_partner(invoice, name=name, phone=phone, mail=mail, vat=vat, country_code=country_code)
# ==== currency_id ====