Antonio Espinosa
9 years ago
committed by
Andrii Skrypka
11 changed files with 150 additions and 154 deletions
-
33base_location_nuts/README.rst
-
4base_location_nuts/i18n/es.po
-
18base_location_nuts/models/res_country.py
-
139base_location_nuts/models/res_partner.py
-
6base_location_nuts/models/res_partner_nuts.py
-
10base_location_nuts/views/res_country_view.xml
-
3base_location_nuts/views/res_partner_nuts_view.xml
-
71base_location_nuts/views/res_partner_view.xml
-
6base_location_nuts/wizard/__init__.py
-
11base_location_nuts/wizard/nuts_import.py
-
3base_location_nuts/wizard/nuts_import_view.xml
@ -1,73 +1,122 @@ |
|||
# -*- coding: utf-8 -*- |
|||
############################################################################## |
|||
# For copyright and license notices, see __openerp__.py file in root directory |
|||
############################################################################## |
|||
# © 2015 Antiun Ingeniería S.L. - Antonio Espinosa |
|||
# © 2015 Antiun Ingeniería S.L. - Jairo Llopis |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from openerp import models, fields, api |
|||
from openerp.tools.translate import _ |
|||
|
|||
|
|||
class ResPartner(models.Model): |
|||
_inherit = 'res.partner' |
|||
|
|||
region_id = fields.Many2one( |
|||
'res.partner.nuts', |
|||
"Region", |
|||
oldname="region") |
|||
substate_id = fields.Many2one( |
|||
'res.partner.nuts', |
|||
"Substate", |
|||
oldname="substate") |
|||
lbl_region = fields.Char( |
|||
default=_("Region"), |
|||
compute='_labels_get') |
|||
lbl_substate = fields.Char( |
|||
default=_("Substate"), |
|||
compute='_labels_get') |
|||
nuts1_id = fields.Many2one( |
|||
comodel_name='res.partner.nuts', domain=[('level', '=', 1)], |
|||
string="NUTS L1") |
|||
nuts2_id = fields.Many2one( |
|||
comodel_name='res.partner.nuts', domain=[('level', '=', 2)], |
|||
string="NUTS L2", oldname="region") |
|||
nuts3_id = fields.Many2one( |
|||
comodel_name='res.partner.nuts', domain=[('level', '=', 3)], |
|||
string="NUTS L3", oldname="substate") |
|||
nuts4_id = fields.Many2one( |
|||
comodel_name='res.partner.nuts', domain=[('level', '=', 4)], |
|||
string="NUTS L4") |
|||
|
|||
@api.multi |
|||
@api.depends('country_id') |
|||
def _labels_get(self): |
|||
for s in self: |
|||
s.lbl_region = s.country_id.region_label or _('Region') |
|||
s.lbl_substate = s.country_id.substate_label or _('Substate') |
|||
def _onchange_nuts(self, field): |
|||
country_id = self[field].country_id.id |
|||
state_id = self[field].state_id.id |
|||
if country_id and self.country_id.id != country_id: |
|||
self.country_id = country_id |
|||
if state_id and self.state_id.id != state_id: |
|||
self.state_id = state_id |
|||
level = int(field[:5][-1]) |
|||
if (level - 1) > 0: |
|||
parent_id = self[field].parent_id.id |
|||
if parent_id: |
|||
parent_field = 'nuts%d_id' % (level - 1) |
|||
if self[parent_field].id != parent_id: |
|||
self[parent_field] = parent_id |
|||
result = dict() |
|||
if country_id and level < 4: |
|||
result['domain'] = dict() |
|||
while level < 4: |
|||
parent_field = 'nuts%d_id' % level |
|||
domain_field = 'nuts%d_id' % (level + 1) |
|||
parent_id = self[parent_field].id |
|||
if parent_id: |
|||
result['domain'][domain_field] = [ |
|||
('parent_id', '=', parent_id), |
|||
] |
|||
level += 1 |
|||
return result |
|||
|
|||
@api.multi |
|||
@api.onchange("substate_id") |
|||
def _onchange_substate_id(self): |
|||
if self.substate_id.country_id: |
|||
self.country_id = self.substate_id.country_id |
|||
return dict() |
|||
@api.onchange('nuts4_id') |
|||
def _onchange_nuts4_id(self): |
|||
return self._onchange_nuts('nuts4_id') |
|||
|
|||
@api.multi |
|||
@api.onchange("region_id") |
|||
def _onchange_region_id(self): |
|||
if self.region_id.country_id: |
|||
self.country_id = self.region_id.country_id |
|||
return dict() |
|||
@api.onchange('nuts3_id') |
|||
def _onchange_nuts3_id(self): |
|||
return self._onchange_nuts('nuts3_id') |
|||
|
|||
@api.multi |
|||
@api.onchange("country_id") |
|||
@api.onchange('nuts2_id') |
|||
def _onchange_nuts2_id(self): |
|||
return self._onchange_nuts('nuts2_id') |
|||
|
|||
@api.multi |
|||
@api.onchange('nuts1_id') |
|||
def _onchange_nuts1_id(self): |
|||
return self._onchange_nuts('nuts1_id') |
|||
|
|||
@api.multi |
|||
@api.onchange('country_id') |
|||
def _onchange_country_id(self): |
|||
"""Sensible values and domains for related fields.""" |
|||
fields = {"state", "region", "substate"} |
|||
country_domain = ([("country_id", "=", self.country_id.id)] |
|||
fields = ['state_id', 'nuts1_id', 'nuts2_id', 'nuts3_id', 'nuts4_id'] |
|||
country_domain = ([('country_id', '=', self.country_id.id)] |
|||
if self.country_id else []) |
|||
|
|||
domain = dict() |
|||
for field in fields: |
|||
field += "_id" |
|||
if self.country_id and self[field].country_id != self.country_id: |
|||
self[field] = False |
|||
domain[field] = list(country_domain) # Using list() to copy |
|||
|
|||
fields.remove("state") |
|||
fields.remove('state_id') |
|||
for field in fields: |
|||
level = self.country_id["%s_level" % field] |
|||
field += "_id" |
|||
level = int(field[:5][-1]) |
|||
if level: |
|||
domain[field].append(("level", "=", level)) |
|||
|
|||
domain[field].append(('level', '=', level)) |
|||
if self.country_id: |
|||
nuts1 = self.env['res.partner.nuts'].search([ |
|||
('level', '=', 1), |
|||
('country_id', '=', self.country_id.id), |
|||
], limit=1) |
|||
if self.nuts1_id.id != nuts1.id: |
|||
self.nuts1_id = nuts1.id |
|||
return { |
|||
"domain": domain, |
|||
'domain': domain, |
|||
} |
|||
|
|||
@api.multi |
|||
def onchange_state(self, state_id): |
|||
result = super(ResPartner, self).onchange_state(state_id) |
|||
state = self.env['res.country.state'].browse(state_id) |
|||
if state.country_id.state_level: |
|||
nuts_state = self.env['res.partner.nuts'].search([ |
|||
('level', '=', state.country_id.state_level), |
|||
('state_id', '=', state.id) |
|||
], limit=1) |
|||
if nuts_state: |
|||
field = 'nuts%d_id' % state.country_id.state_level |
|||
result.setdefault("value", dict()) |
|||
result['value'][field] = nuts_state.id |
|||
return result |
|||
|
|||
@api.model |
|||
def _address_fields(self): |
|||
fields = super(ResPartner, self)._address_fields() |
|||
if fields: |
|||
fields += ['nuts1_id', 'nuts2_id', 'nuts3_id', 'nuts4_id'] |
|||
return fields |
@ -1,6 +1,6 @@ |
|||
# -*- coding: utf-8 -*- |
|||
############################################################################## |
|||
# For copyright and license notices, see __openerp__.py file in root directory |
|||
############################################################################## |
|||
# © 2015 Antiun Ingeniería S.L. - Antonio Espinosa |
|||
# © 2015 Antiun Ingeniería S.L. - Jairo Llopis |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from . import nuts_import |
Write
Preview
Loading…
Cancel
Save
Reference in new issue