[FIX] *: avoid direct references to countries

Ensure the code works fine even if countries are deleted.

Also consider "re"-created countries after deletion, by only considering
the country code, not the data reference.

This should reduce support requests related to deleted countries, and
ease the resolution of such problems by the users themselves.

TASK ID - 2368842

closes odoo/odoo#60558

X-original-commit: 037012bc4e2935eb00a7f353529bb7ec55066733
Related: odoo/enterprise#14347
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
Victor Feyens
2020-10-22 16:17:26 +00:00
parent 0dd3b70690
commit 1b67affe7d
18 changed files with 45 additions and 47 deletions
@@ -11,7 +11,7 @@ class AccountChartTemplate(models.Model):
def _get_fp_vals(self, company, position):
res = super()._get_fp_vals(company, position)
if company.country_id == self.env.ref('base.ar'):
if company.country_id.code == "AR":
res['l10n_ar_afip_responsibility_type_ids'] = [
(6, False, position.l10n_ar_afip_responsibility_type_ids.ids)]
return res
@@ -19,7 +19,7 @@ class AccountChartTemplate(models.Model):
def _prepare_all_journals(self, acc_template_ref, company, journals_dict=None):
""" In case of an Argentinean CoA, we modify the default values of the sales journal to be a preprinted journal"""
res = super()._prepare_all_journals(acc_template_ref, company, journals_dict=journals_dict)
if company.country_id == self.env.ref('base.ar'):
if company.country_id.code == "AR":
for vals in res:
if vals['type'] == 'sale':
vals.update({
@@ -57,7 +57,7 @@ class AccountChartTemplate(models.Model):
company_responsibility = company.l10n_ar_afip_responsibility_type_id
company.write({
'l10n_ar_afip_responsibility_type_id': coa_responsibility.id,
'country_id': self.env.ref('base.ar').id,
'country_id': self.env['res.country'].search([('code', '=', 'AR')]).id,
'tax_calculation_rounding_method': 'round_globally',
})
# set CUIT identification type (which is the argentinean vat) in the created company partner instead of
@@ -15,7 +15,7 @@ class AccountFiscalPosition(models.Model):
def get_fiscal_position(self, partner_id, delivery_id=None):
""" Take into account the partner afip responsibility in order to auto-detect the fiscal position """
company = self.env.company
if company.country_id == self.env.ref('base.ar'):
if company.country_id.code == "AR":
PartnerObj = self.env['res.partner']
partner = PartnerObj.browse(partner_id)
@@ -39,7 +39,7 @@ class AccountFiscalPosition(models.Model):
@api.onchange('l10n_ar_afip_responsibility_type_ids', 'country_group_id', 'country_id', 'zip_from', 'zip_to')
def _onchange_afip_responsibility(self):
if self.company_id.country_id == self.env.ref('base.ar'):
if self.company_id.country_id.code == "AR":
if self.l10n_ar_afip_responsibility_type_ids and any(['country_group_id', 'country_id', 'zip_from', 'zip_to']):
return {'warning': {
'title': _("Warning"),
+2 -2
View File
@@ -107,7 +107,7 @@ class AccountJournal(models.Model):
'l10n_latam_use_documents')
def _check_afip_configurations(self):
""" Do not let the user update the journal if it already contains confirmed invoices """
journals = self.filtered(lambda x: x.company_id.country_id == self.env.ref('base.ar') and x.type in ['sale', 'purchase'])
journals = self.filtered(lambda x: x.company_id.country_id.code == "AR" and x.type in ['sale', 'purchase'])
invoices = self.env['account.move'].search([('journal_id', 'in', journals.ids), ('posted_before', '=', True)], limit=1)
if invoices:
raise ValidationError(
@@ -118,7 +118,7 @@ class AccountJournal(models.Model):
def _check_afip_pos_number(self):
to_review = self.filtered(
lambda x: x.type == 'sale' and x.l10n_latam_use_documents and
x.company_id.country_id == self.env.ref('base.ar'))
x.company_id.country_id.code == "AR")
if to_review.filtered(lambda x: x.l10n_ar_afip_pos_number == 0):
raise ValidationError(_('Please define an AFIP POS number'))
+8 -8
View File
@@ -38,7 +38,7 @@ class AccountMove(models.Model):
@api.constrains('move_type', 'journal_id')
def _check_moves_use_documents(self):
""" Do not let to create not invoices entries in journals that use documents """
not_invoices = self.filtered(lambda x: x.company_id.country_id == self.env.ref('base.ar') and x.journal_id.type in ['sale', 'purchase'] and x.l10n_latam_use_documents and not x.is_invoice())
not_invoices = self.filtered(lambda x: x.company_id.country_id.code == "AR" and x.journal_id.type in ['sale', 'purchase'] and x.l10n_latam_use_documents and not x.is_invoice())
if not_invoices:
raise ValidationError(_("The selected Journal can't be used in this transaction, please select one that doesn't use documents as these are just for Invoices."))
@@ -49,7 +49,7 @@ class AccountMove(models.Model):
@api.depends('invoice_line_ids', 'invoice_line_ids.product_id', 'invoice_line_ids.product_id.type', 'journal_id')
def _compute_l10n_ar_afip_concept(self):
recs_afip = self.filtered(lambda x: x.company_id.country_id == self.env.ref('base.ar') and x.l10n_latam_use_documents)
recs_afip = self.filtered(lambda x: x.company_id.country_id.code == "AR" and x.l10n_latam_use_documents)
for rec in recs_afip:
rec.l10n_ar_afip_concept = rec._get_concept()
remaining = self - recs_afip
@@ -77,7 +77,7 @@ class AccountMove(models.Model):
def _get_l10n_latam_documents_domain(self):
self.ensure_one()
domain = super()._get_l10n_latam_documents_domain()
if self.journal_id.company_id.country_id == self.env.ref('base.ar'):
if self.journal_id.company_id.country_id.code == "AR":
letters = self.journal_id._get_journal_letter(counterpart_partner=self.partner_id.commercial_partner_id)
domain += ['|', ('l10n_ar_letter', '=', False), ('l10n_ar_letter', 'in', letters)]
codes = self.journal_id._get_journal_codes()
@@ -113,7 +113,7 @@ class AccountMove(models.Model):
@api.onchange('partner_id')
def _onchange_afip_responsibility(self):
if self.company_id.country_id == self.env.ref('base.ar') and self.l10n_latam_use_documents and self.partner_id \
if self.company_id.country_id.code == 'AR' and self.l10n_latam_use_documents and self.partner_id \
and not self.partner_id.l10n_ar_afip_responsibility_type_id:
return {'warning': {
'title': _('Missing Partner Configuration'),
@@ -124,7 +124,7 @@ class AccountMove(models.Model):
def _onchange_partner_journal(self):
""" This method is used when the invoice is created from the sale or subscription """
expo_journals = ['FEERCEL', 'FEEWS', 'FEERCELP']
for rec in self.filtered(lambda x: x.company_id.country_id == self.env.ref('base.ar') and x.journal_id.type == 'sale'
for rec in self.filtered(lambda x: x.company_id.country_id.code == "AR" and x.journal_id.type == 'sale'
and x.l10n_latam_use_documents and x.partner_id.l10n_ar_afip_responsibility_type_id):
res_code = rec.partner_id.l10n_ar_afip_responsibility_type_id.code
domain = [('company_id', '=', rec.company_id.id), ('l10n_latam_use_documents', '=', True), ('type', '=', 'sale')]
@@ -146,7 +146,7 @@ class AccountMove(models.Model):
raise RedirectWarning(msg, action.id, _('Go to Journals'))
def _post(self, soft=True):
ar_invoices = self.filtered(lambda x: x.company_id.country_id == self.env.ref('base.ar') and x.l10n_latam_use_documents)
ar_invoices = self.filtered(lambda x: x.company_id.country_id.code == "AR" and x.l10n_latam_use_documents)
for rec in ar_invoices:
rec.l10n_ar_afip_responsibility_type_id = rec.commercial_partner_id.l10n_ar_afip_responsibility_type_id.id
if rec.company_id.currency_id == rec.currency_id:
@@ -201,14 +201,14 @@ class AccountMove(models.Model):
def _get_starting_sequence(self):
""" If use documents then will create a new starting sequence using the document type code prefix and the
journal document number with a 8 padding number """
if self.journal_id.l10n_latam_use_documents and self.env.company.country_id == self.env.ref('base.ar'):
if self.journal_id.l10n_latam_use_documents and self.env.company.country_id.code == "AR":
if self.l10n_latam_document_type_id:
return self._get_formatted_sequence()
return super()._get_starting_sequence()
def _get_last_sequence_domain(self, relaxed=False):
where_string, param = super(AccountMove, self)._get_last_sequence_domain(relaxed)
if self.company_id.country_id == self.env.ref('base.ar') and self.l10n_latam_use_documents:
if self.company_id.country_id.code == "AR" and self.l10n_latam_use_documents:
if not self.journal_id.l10n_ar_share_sequences:
where_string += " AND l10n_latam_document_type_id = %(l10n_latam_document_type_id)s"
param['l10n_latam_document_type_id'] = self.l10n_latam_document_type_id.id or 0
@@ -34,7 +34,7 @@ class L10nLatamDocumentType(models.Model):
def _filter_taxes_included(self, taxes):
""" In argentina we include taxes depending on document letter """
self.ensure_one()
if self.country_id == self.env.ref('base.ar') and self.l10n_ar_letter in ['B', 'C', 'X', 'R']:
if self.country_id.code == "AR" and self.l10n_ar_letter in ['B', 'C', 'X', 'R']:
return taxes.filtered(lambda x: x.tax_group_id.l10n_ar_vat_afip_code)
return super()._filter_taxes_included(taxes)
@@ -44,7 +44,7 @@ class L10nLatamDocumentType(models.Model):
* format the document_number against a pattern and return it
"""
self.ensure_one()
if self.country_id != self.env.ref('base.ar'):
if self.country_id.code != "AR":
return super()._format_document_number(document_number)
if not document_number:
+2 -2
View File
@@ -20,7 +20,7 @@ class ResCompany(models.Model):
@api.onchange('country_id')
def onchange_country(self):
""" Argentinean companies use round_globally as tax_calculation_rounding_method """
for rec in self.filtered(lambda x: x.country_id == self.env.ref('base.ar')):
for rec in self.filtered(lambda x: x.country_id.code == "AR"):
rec.tax_calculation_rounding_method = 'round_globally'
@api.depends('l10n_ar_afip_responsibility_type_id')
@@ -33,7 +33,7 @@ class ResCompany(models.Model):
def _localization_use_documents(self):
""" Argentinean localization use documents """
self.ensure_one()
return True if self.country_id == self.env.ref('base.ar') else super()._localization_use_documents()
return True if self.country_id.code == "AR" else super()._localization_use_documents()
@api.constrains('l10n_ar_afip_responsibility_type_id')
def _check_accounting_info(self):
@@ -12,6 +12,6 @@ class AccountChartTemplate(models.Model):
journal_data = super(AccountChartTemplate, self)._prepare_all_journals(
acc_template_ref, company, journals_dict)
for journal in journal_data:
if journal['type'] in ('sale', 'purchase') and company.country_id == self.env.ref('base.be'):
if journal['type'] in ('sale', 'purchase') and company.country_id.code == "BE":
journal.update({'refund_sequence': True})
return journal_data
+1 -2
View File
@@ -15,8 +15,7 @@ def init_settings(env):
'''If the company is localized in Switzerland, activate the cash rounding by default.
'''
# The cash rounding is activated by default only if the company is localized in Switzerland.
ch_country = env.ref('base.ch')
for company in env['res.company'].search([('partner_id.country_id', '=', ch_country.id)]):
for company in env['res.company'].search([('partner_id.country_id.code', '=', "CH")]):
res_config_id = env['res.config.settings'].create({
'company_id': company.id,
'group_cash_rounding': True
+10 -12
View File
@@ -18,10 +18,9 @@ class AccountMove(models.Model):
def _get_l10n_latam_documents_domain(self):
self.ensure_one()
domain = super()._get_l10n_latam_documents_domain()
if (self.journal_id.l10n_latam_use_documents and
self.journal_id.company_id.country_id == self.env.ref('base.cl')):
if self.journal_id.l10n_latam_use_documents and self.journal_id.company_id.country_id.code == "CL":
if self.journal_id.type == 'sale':
domain = [('country_id', '=', self.env.ref('base.cl').id), ('internal_type', '!=', 'invoice_in')]
domain = [('country_id.code', '=', "CL"), ('internal_type', '!=', 'invoice_in')]
if self.company_id.partner_id.l10n_cl_sii_taxpayer_type == '1':
domain += [('code', '!=', '71')] # Companies with VAT Affected doesn't have "Boleta de honorarios Electrónica"
document_type_ids = self.env['l10n_latam.document.type'].search(domain).ids
@@ -39,8 +38,7 @@ class AccountMove(models.Model):
partner_domain += [('code', 'in', ['70', '71', '56', '61'])]
elif self.partner_id.l10n_cl_sii_taxpayer_type == '3':
partner_domain += [('code', 'in', ['35', '38', '39', '41', '56', '61'])]
elif not self.partner_id.l10n_cl_sii_taxpayer_type or self.partner_id.country_id != self.env.ref(
'base.cl') or self.partner_id.l10n_cl_sii_taxpayer_type == '4':
elif not self.partner_id.l10n_cl_sii_taxpayer_type or self.partner_id.country_id.code != "CL" or self.partner_id.l10n_cl_sii_taxpayer_type == '4':
partner_domain += [('code', 'in', [])]
document_type_ids = self.env['l10n_latam.document.type'].search(partner_domain).ids
domain = expression.AND([domain, [('id', 'in', document_type_ids)]])
@@ -48,18 +46,18 @@ class AccountMove(models.Model):
def _check_document_types_post(self):
for rec in self.filtered(
lambda r: r.company_id.country_id == self.env.ref('base.cl') and
lambda r: r.company_id.country_id.code == "CL" and
r.journal_id.type in ['sale', 'purchase']):
tax_payer_type = rec.partner_id.l10n_cl_sii_taxpayer_type
vat = rec.partner_id.vat
country_id = rec.partner_id.country_id
latam_document_type_code = rec.l10n_latam_document_type_id.code
if (not tax_payer_type or not vat) and (country_id == self.env.ref('base.cl') and latam_document_type_code
if (not tax_payer_type or not vat) and (country_id.code == "CL" and latam_document_type_code
and latam_document_type_code not in ['35', '38', '39', '41']):
raise ValidationError(_('Tax payer type and vat number are mandatory for this type of '
'document. Please set the current tax payer type of this customer'))
if rec.journal_id.type == 'sale' and rec.journal_id.l10n_latam_use_documents:
if country_id != self.env.ref('base.cl'):
if country_id.code != "CL":
if not ((tax_payer_type == '4' and latam_document_type_code in ['110', '111', '112']) or (
tax_payer_type == '3' and latam_document_type_code in ['39', '41', '61', '56'])):
raise ValidationError(_(
@@ -70,7 +68,7 @@ class AccountMove(models.Model):
raise ValidationError(_('The DIN document is intended to be used only with RUT 60805000-0'
' (Tesorería General de La República)'))
if not tax_payer_type or not vat:
if country_id == self.env.ref('base.cl') and latam_document_type_code not in [
if country_id.code == "CL" and latam_document_type_code not in [
'35', '38', '39', '41']:
raise ValidationError(_('Tax payer type and vat number are mandatory for this type of '
'document. Please set the current tax payer type of this supplier'))
@@ -84,7 +82,7 @@ class AccountMove(models.Model):
if latam_document_type_code in ['110', '111', '112']:
raise ValidationError(_('The tax payer type of this supplier is not entitled to deliver '
'imports documents'))
if tax_payer_type == '4' or country_id != self.env.ref('base.cl'):
if tax_payer_type == '4' or country_id.code != "CL":
raise ValidationError(_('You need a journal without the use of documents for foreign '
'suppliers'))
if rec.journal_id.type == 'purchase' and not rec.journal_id.l10n_latam_use_documents:
@@ -106,14 +104,14 @@ class AccountMove(models.Model):
def _get_starting_sequence(self):
""" If use documents then will create a new starting sequence using the document type code prefix and the
journal document number with a 6 padding number """
if self.journal_id.l10n_latam_use_documents and self.env.company.country_id == self.env.ref('base.cl'):
if self.journal_id.l10n_latam_use_documents and self.env.company.country_id.code == "CL":
if self.l10n_latam_document_type_id:
return self._l10n_cl_get_formatted_sequence()
return super()._get_starting_sequence()
def _get_last_sequence_domain(self, relaxed=False):
where_string, param = super(AccountMove, self)._get_last_sequence_domain(relaxed)
if self.company_id.country_id == self.env.ref('base.cl') and self.l10n_latam_use_documents:
if self.company_id.country_id.code == "CL" and self.l10n_latam_use_documents:
where_string = where_string.replace('journal_id = %(journal_id)s AND', '')
where_string += ' AND l10n_latam_document_type_id = %(l10n_latam_document_type_id)s AND ' \
'company_id = %(company_id)s'
@@ -21,7 +21,7 @@ class L10nLatamDocumentType(models.Model):
* format the document_number against a pattern and return it
"""
self.ensure_one()
if self.country_id != self.env.ref('base.cl'):
if self.country_id.code != "CL":
return super()._format_document_number(document_number)
if not document_number:
+1 -1
View File
@@ -9,4 +9,4 @@ class ResCompany(models.Model):
def _localization_use_documents(self):
""" Chilean localization use documents """
self.ensure_one()
return self.country_id == self.env.ref('base.cl') or super()._localization_use_documents()
return self.country_id.code == "CL" or super()._localization_use_documents()
+5 -4
View File
@@ -26,10 +26,11 @@ class ResPartner(models.Model):
def _format_vat_cl(self, values):
identification_types = [self.env.ref('l10n_latam_base.it_vat').id, self.env.ref('l10n_cl.it_RUT').id,
self.env.ref('l10n_cl.it_RUN').id]
partner_country_is_chile = (values.get('country_id') == self.env.ref('base.cl').id) or (
values.get('l10n_latam_identification_type_id') and
self.env['l10n_latam.identification.type'].browse(
values.get('l10n_latam_identification_type_id')).country_id == self.env.ref('base.cl'))
country = self.env["res.country"].browse(values.get('country_id'))
identification_type = self.env['l10n_latam.identification.type'].browse(
values.get('l10n_latam_identification_type_id')
)
partner_country_is_chile = country.code == "CL" or identification_type.country_id.code == "CL"
if partner_country_is_chile and \
values.get('l10n_latam_identification_type_id') in identification_types and values.get('vat'):
return stdnum.util.get_cc_module('cl', 'vat').format(values['vat']).replace('.', '').replace(
+1 -1
View File
@@ -13,7 +13,7 @@ class ResPartner(models.Model):
# installed for Colombia.
if self.sudo().env.ref('base.module_base_vat').state == 'installed':
# don't check Colombian partners unless they have RUT (= Colombian VAT) set as document type
self = self.filtered(lambda partner: partner.country_id != self.env.ref('base.co') or\
self = self.filtered(lambda partner: partner.country_id.code != "CO" or\
partner.l10n_latam_identification_type_id.l10n_co_document_code == 'rut')
return super(ResPartner, self).check_vat()
else:
@@ -12,6 +12,6 @@ class AccountChartTemplate(models.Model):
journal_data = super(AccountChartTemplate, self)._prepare_all_journals(
acc_template_ref, company, journals_dict)
for journal in journal_data:
if journal['type'] in ('sale', 'purchase') and company.country_id == self.env.ref('base.fr'):
if journal['type'] in ('sale', 'purchase') and company.country_id.code == "FR":
journal.update({'refund_sequence': True})
return journal_data
+1 -1
View File
@@ -13,7 +13,7 @@ class ChartTemplate(models.Model):
def _prepare_all_journals(self, acc_template_ref, company, journals_dict=None):
journals = super(ChartTemplate, self)._prepare_all_journals(acc_template_ref, company, journals_dict)
if company.country_id == self.env.ref('base.fr'):
if company.country_id.code == "FR":
#For France, sale/purchase journals must have a dedicated sequence for refunds
for journal in journals:
if journal['type'] in ['sale', 'purchase']:
@@ -12,6 +12,6 @@ class AccountChartTemplate(models.Model):
journal_data = super(AccountChartTemplate, self)._prepare_all_journals(
acc_template_ref, company, journals_dict)
for journal in journal_data:
if journal['type'] in ('sale', 'purchase') and company.country_id == self.env.ref('base.lu'):
if journal['type'] in ('sale', 'purchase') and company.country_id.code == "LU":
journal.update({'refund_sequence': True})
return journal_data
+2 -2
View File
@@ -16,7 +16,7 @@ class AccountJournal(models.Model):
liquidity journal, set the correct tags for the mexican localization.
'''
res = super(AccountJournal, self)._prepare_liquidity_account(name, company, currency_id, type)
if company.country_id.id == self.env.ref('base.mx').id:
if company.country_id.code == "MX":
mx_tags = self.env['account.account'].mx_search_tags(res.get('code', ''))
if mx_tags:
res.update({
@@ -46,7 +46,7 @@ class AccountAccount(models.Model):
@api.onchange('code')
def _onchange_code(self):
if self.company_id.country_id.id == self.env.ref('base.mx').id and self.code:
if self.company_id.country_id.code == "MX" and self.code:
tags = self.mx_search_tags(self.code)
self.tag_ids = tags
+1 -1
View File
@@ -13,7 +13,7 @@ class ResCompany(models.Model):
@api.depends('vat')
def _compute_org_number(self):
for company in self:
if company.country_id == self.env.ref('base.se') and company.vat:
if company.country_id.code == "SE" and company.vat:
org_number = re.sub(r'\D', '', company.vat)[:-2]
org_number = org_number[:6] + '-' + org_number[6:]