diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/l10n_latam_base/models | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/l10n_latam_base/models')
| -rw-r--r-- | addons/l10n_latam_base/models/__init__.py | 4 | ||||
| -rw-r--r-- | addons/l10n_latam_base/models/l10n_latam_identification_type.py | 22 | ||||
| -rw-r--r-- | addons/l10n_latam_base/models/res_company.py | 18 | ||||
| -rw-r--r-- | addons/l10n_latam_base/models/res_partner.py | 31 |
4 files changed, 75 insertions, 0 deletions
diff --git a/addons/l10n_latam_base/models/__init__.py b/addons/l10n_latam_base/models/__init__.py new file mode 100644 index 00000000..3111eda0 --- /dev/null +++ b/addons/l10n_latam_base/models/__init__.py @@ -0,0 +1,4 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import l10n_latam_identification_type +from . import res_partner +from . import res_company diff --git a/addons/l10n_latam_base/models/l10n_latam_identification_type.py b/addons/l10n_latam_base/models/l10n_latam_identification_type.py new file mode 100644 index 00000000..68af4f24 --- /dev/null +++ b/addons/l10n_latam_base/models/l10n_latam_identification_type.py @@ -0,0 +1,22 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields, api +from odoo.osv import expression + + +class L10nLatamIdentificationType(models.Model): + _name = 'l10n_latam.identification.type' + _description = "Identification Types" + _order = 'sequence' + + sequence = fields.Integer(default=10) + name = fields.Char(translate=True, required=True,) + description = fields.Char() + active = fields.Boolean(default=True) + is_vat = fields.Boolean() + country_id = fields.Many2one('res.country') + + def name_get(self): + multi_localization = len(self.search([]).mapped('country_id')) > 1 + return [(rec.id, '%s%s' % ( + rec.name, multi_localization and rec.country_id and ' (%s)' % rec.country_id.code or '')) for rec in self] diff --git a/addons/l10n_latam_base/models/res_company.py b/addons/l10n_latam_base/models/res_company.py new file mode 100644 index 00000000..165d0afb --- /dev/null +++ b/addons/l10n_latam_base/models/res_company.py @@ -0,0 +1,18 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields, api + + +class ResCompany(models.Model): + _inherit = 'res.company' + + @api.model + def create(self, vals): + """ If exists, use specific vat identification.type for the country of the company """ + country_id = vals.get('country_id') + if country_id: + country_vat_type = self.env['l10n_latam.identification.type'].search( + [('is_vat', '=', True), ('country_id', '=', country_id)], limit=1) + if country_vat_type: + self = self.with_context(default_l10n_latam_identification_type_id=country_vat_type.id) + return super().create(vals) diff --git a/addons/l10n_latam_base/models/res_partner.py b/addons/l10n_latam_base/models/res_partner.py new file mode 100644 index 00000000..10396dbb --- /dev/null +++ b/addons/l10n_latam_base/models/res_partner.py @@ -0,0 +1,31 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields, api + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + l10n_latam_identification_type_id = fields.Many2one('l10n_latam.identification.type', + string="Identification Type", index=True, auto_join=True, + default=lambda self: self.env.ref('l10n_latam_base.it_vat', raise_if_not_found=False), + help="The type of identification") + vat = fields.Char(string='Identification Number', help="Identification Number for selected type") + + @api.model + def _commercial_fields(self): + return super()._commercial_fields() + ['l10n_latam_identification_type_id'] + + @api.constrains('vat', 'l10n_latam_identification_type_id') + def check_vat(self): + with_vat = self.filtered(lambda x: x.l10n_latam_identification_type_id.is_vat) + return super(ResPartner, with_vat).check_vat() + + @api.onchange('country_id') + def _onchange_country(self): + country = self.country_id or self.company_id.country_id or self.env.company.country_id + identification_type = self.l10n_latam_identification_type_id + if not identification_type or (identification_type.country_id != country): + self.l10n_latam_identification_type_id = self.env['l10n_latam.identification.type'].search( + [('country_id', '=', country.id), ('is_vat', '=', True)], limit=1) or self.env.ref( + 'l10n_latam_base.it_vat', raise_if_not_found=False) |
