Skip to content

Commit

Permalink
partner_import_helper: add support for industry and fiscal position (…
Browse files Browse the repository at this point in the history
…for FR)
  • Loading branch information
alexis-via committed Mar 25, 2024
1 parent 28bbf95 commit 2dccd33
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
6 changes: 4 additions & 2 deletions partner_import_helper/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ Here is some sample code:
'street2': row[2],
'zip': row[3],
'city': row[4],
'country_name': row[5],
'country_name': row[5], # name or ISO code
'vat': row[6],
'siret': row[7],
'iban': row[8],
'email': row[9],
'create_date': row[10], # in format %Y-%m-%d
'industry_name': row[10],
'create_date': row[11], # in format %Y-%m-%d
}
import_obj._create_partner(vals, speedy)
action = import_obj._result_action(speedy)
Expand All @@ -56,6 +57,7 @@ In the sample code above, ``vals`` is the dictionary that will be passed to ``cr

- it must contain a **'line'** key to indicate the Excel/CSV import ref in logs, which will be removed before calling ``create()``,
- it can contain a **'country_name'** key with the name of the country, that will be replaced by the native **'country_id'** key,
- it can contain an **'industry_name'** key that will be used to match an existing industry or create a new one,
- it can contain a **'title_code'** key with possible values 'madam', 'miss', 'mister', 'doctor' or 'prof' that will be replaced by the native **'title'** key,
- it can contain an **'iban'** key, that will be replaced by **'bank_ids': [(0, 0, {'acc_number': xxx})]** if the IBAN is valid,
- along with the 'iban' key, it can contain a **'bic'** key and a **'bank_name'** key that will be replaced by **'bank_ids': [(0, 0, {'acc_number': xxxx, 'bank_id': bank_id})]**. The bank will be created on the fly if the BIC is not already present in the Odoo database, unless ``create_bank=False`` is passed as argument of the method ``_create_partner()``,
Expand Down
65 changes: 62 additions & 3 deletions partner_import_helper/wizards/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def _prepare_speedy(self, aiengine='chatgpt'):
'prof': self.env.ref('base.res_partner_title_prof').id,
},
},
'industry_name2id': {},
'fiscal_position': {},
})
cyd = speedy['country']
code2to3 = {}
Expand All @@ -79,7 +81,33 @@ def _prepare_speedy(self, aiengine='chatgpt'):
for country in self.env['res.country'].with_context(lang=lang.code).search_read([], ['code', 'name']):
country_name_match = self._prepare_country_name_match(country['name'])
cyd['name2code'][country_name_match] = country['code']

for indus in self.env['res.partner.industry'].with_context(active_test=False).search_read([('name', '!=', False)], ['name']):
speedy['industry_name2id'][indus['name']] = indus['id']
if (
self.env.company.country_id.code == 'FR' and
hasattr(self.env['res.partner'], 'property_account_position_id') and
hasattr(self.env['account.fiscal.position'], 'fr_vat_type')):
fp_count = self.env['account.fiscal.position'].search_count([('company_id', '=', self.env.company.id)])
if fp_count:
speedy['fiscal_position']['id2name'] = {}
speedy['fiscal_position']['frvattype2id'] = {
'france': False,
'france_vendor_vat_on_payment': False, # not used for the moment
'intracom_b2b': False,
'intracom_b2c': False,
'extracom': False,
}
for fr_vat_type in speedy['fiscal_position']['frvattype2id'].keys():
fps = self.env['account.fiscal.position'].search_read([('company_id', '=', self.env.company.id), ('fr_vat_type', '=', fr_vat_type)], ['name'])
if not fps:
raise UserError(_("There are no fiscal position with fr_vat_type=%(fr_vat_type)s in company '%(company)s'.", fr_vat_type=fr_vat_type, company=self.env.company.display_name))
if len(fps) > 1:
logger.warning(
'There are %d fiscal positions with fr_vat_type=%s: %s',
len(fps), fr_vat_type, ' ,'.join([fp['name'] for fp in fps]))
fp = fps[0]
speedy['fiscal_position']['frvattype2id'][fr_vat_type] = fp['id']
speedy['fiscal_position']['id2name'][fp['id']] = fp['name']
return speedy

@api.model
Expand Down Expand Up @@ -321,6 +349,8 @@ def _prepare_partner_vals(self, vals, speedy, email_check_deliverability=True, c
# SIREN_OR_SIRET
if vals.get('siren_or_siret') and hasattr(self.env['res.partner'], 'siret'):
siren_or_siret = vals['siren_or_siret']
if isinstance(siren_or_siret, int):
siren_or_siret = str(siren_or_siret)
siren_or_siret = ''.join(re.findall(r'[0-9]+', siren_or_siret))
if siren_or_siret:
if len(siren_or_siret) == 14:
Expand Down Expand Up @@ -432,6 +462,14 @@ def _prepare_partner_vals(self, vals, speedy, email_check_deliverability=True, c
vals['siret'] = False
else:
vals.pop('siren')
# INDUSTRY
if vals.get('industry_name'):
if vals['industry_name'] not in speedy['industry_name2id']:
indus = self.env['res.partner.industry'].create(self._prepare_industry(vals, speedy))
speedy['industry_name2id'][vals['industry_name']] = indus.id
vals['industry_id'] = speedy['industry_name2id'][vals['industry_name']]
if 'industry_name' in vals:
vals.pop('industry_name')
if country_id:
country_code = speedy['country']['id2code'][country_id]
# TODO Northern Ireland doesn't pass this check
Expand All @@ -455,8 +493,26 @@ def _prepare_partner_vals(self, vals, speedy, email_check_deliverability=True, c
'vals': vals,
'field': 'res.partner.bank,acc_number',
})
if hasattr(self.env['res.partner'], 'property_account_position_id'):
print('') # TODO add support for fiscal position
# FISCAL POSITION for France
if (
hasattr(self.env['res.partner'], 'property_account_position_id') and
speedy['fiscal_position'].get('frvattype2id') and country_id):
if country_id == speedy['fr_country_id']:
vals['property_account_position_id'] = speedy['fiscal_position']['frvattype2id']['france']
elif country_id in speedy['eu_country_ids']:
if vals.get('is_company'):
vals['property_account_position_id'] = speedy['fiscal_position']['frvattype2id']['intracom_b2b']
if not vals.get('vat'):
speedy['logs']['res.partner'].append({
'msg': "The fiscal position Intra-EU B2B has been set but the partner has no VAT.",
'value': speedy['fiscal_position']['id2name'][vals['property_account_position_id']],
'vals': vals,
'field': 'res.partner,property_account_position_id',
})
else:
vals['property_account_position_id'] = speedy['fiscal_position']['frvattype2id']['intracom_b2c']
else:
vals['property_account_position_id'] = speedy['fiscal_position']['frvattype2id']['extracom']
# vals will keep the original keys
# rvals will be used for create(), so we need to remove all the keys are don't exist on res.partner
rvals = dict(vals)
Expand All @@ -469,6 +525,9 @@ def _prepare_partner_vals(self, vals, speedy, email_check_deliverability=True, c
rvals.pop('siret')
return rvals

def _prepare_industry(self, vals, speedy):
return {'name': vals['industry_name']}

def _phone_number_clean(self, number, country_code, phone_field, vals, speedy):
try:
clean_number = phone_validation.phone_format(
Expand Down
9 changes: 0 additions & 9 deletions product_import_helper/wizards/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
class ImportHelper(models.TransientModel):
_inherit = "import.helper"

# fullname = fields.Char(compute="_compute_fullname", store=True)

# @api.depends(
# "product_tmpl_id.name", "default_code", "product_template_attribute_value_ids"
# )
# def _compute_fullname(self):
# for record in self.with_context(display_default_code=True, partner_id=False):
# record.fullname = record.display_name

@api.model
def _prepare_speedy(self, aiengine='chatgpt'):
speedy = super()._prepare_speedy(aiengine=aiengine)
Expand Down

0 comments on commit 2dccd33

Please sign in to comment.