diff --git a/l10n_br_zip/__manifest__.py b/l10n_br_zip/__manifest__.py index ecb96a69b2b6..4e42c7f39ff0 100644 --- a/l10n_br_zip/__manifest__.py +++ b/l10n_br_zip/__manifest__.py @@ -17,6 +17,7 @@ 'data': [ 'views/l10n_br_zip_view.xml', 'views/res_partner_address_view.xml', + 'views/res_config_settings_view.xml', 'wizard/l10n_br_zip_search_view.xml', 'security/ir.model.access.csv', ], diff --git a/l10n_br_zip/models/__init__.py b/l10n_br_zip/models/__init__.py index ce07fca2f89f..13e129e31728 100644 --- a/l10n_br_zip/models/__init__.py +++ b/l10n_br_zip/models/__init__.py @@ -1,5 +1,6 @@ # Copyright (C) 2015 Renato Lima - Akretion # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html +from . import res_config_settings from . import l10n_br_zip from . import format_address_mixin diff --git a/l10n_br_zip/models/l10n_br_zip.py b/l10n_br_zip/models/l10n_br_zip.py index 198e9aa05f50..a89bc4f13186 100644 --- a/l10n_br_zip/models/l10n_br_zip.py +++ b/l10n_br_zip/models/l10n_br_zip.py @@ -85,9 +85,23 @@ def _set_domain(self, country_id=False, state_id=False, return domain + @api.multi + def _zip_update(self): + self.ensure_one() + cep_update_days = int( + self.env['ir.config_parameter'].sudo().get_param( + 'l10n_br_zip.cep_update_days', default=365)) + date_delta = fields.Datetime.today() - self.write_date + if date_delta.days >= cep_update_days: + cep_values = self._consultar_cep(self.zip_code) + if cep_values: + # Update zip object + self.write(cep_values) + @api.multi def set_result(self): self.ensure_one() + self._zip_update() return { 'country_id': self.country_id.id, 'state_id': self.state_id.id, @@ -100,6 +114,41 @@ def set_result(self): 'zip': misc.format_zipcode( self.zip_code, self.country_id.code)} + def _consultar_cep(self, zip_code): + zip_str = misc.punctuation_rm(zip_code) + try: + cep = pycep_correios.consultar_cep(zip_str) + except Exception as e: + raise UserError( + _('Erro no PyCEP-Correios : ') + str(e)) + + values = {} + if cep: + # Search Brazil id + country = self.env['res.country'].search( + [('code', '=', 'BR')], limit=1) + + # Search state with state_code and country id + state = self.env['res.country.state'].search([ + ('code', '=', cep['uf']), + ('country_id', '=', country.id)], limit=1) + + # search city with name and state + city = self.env['res.city'].search([ + ('name', '=', cep['cidade']), + ('state_id.id', '=', state.id)], limit=1) + + values = { + 'zip_code': zip_str, + 'street': cep['end'], + 'zip_complement': cep['complemento2'], + 'district': cep['bairro'], + 'city_id': city.id or False, + 'state_id': state.id or False, + 'country_id': country.id or False, + } + return values + @api.model def zip_search(self, obj): @@ -130,40 +179,11 @@ def zip_search(self, obj): # Address not found in local DB, search by PyCEP-Correios elif not zips and obj.zip: - zip_str = misc.punctuation_rm(obj.zip) - try: - result = pycep_correios.consultar_cep(zip_str) - except Exception as e: - raise UserError( - _('Erro no PyCEP-Correios : ') + str(e)) - - if result: - # Search Brazil id - country = self.env['res.country'].search( - [('code', '=', 'BR')], limit=1) - - # Search state with state_code and country id - state = self.env['res.country.state'].search([ - ('code', '=', result['uf']), - ('country_id', '=', country.id)], limit=1) - - # search city with name and state - city = self.env['res.city'].search([ - ('name', '=', result['cidade']), - ('state_id.id', '=', state.id)], limit=1) - - values = { - 'zip_code': zip_str, - 'street': result['end'], - 'zip_complement': result['complemento2'], - 'district': result['bairro'], - 'city_id': city.id or False, - 'state_id': state.id or False, - 'country_id': country.id or False, - } + cep_values = self._consultar_cep(obj.zip) + if cep_values: # Create zip object - zip = self.create(values) + zip = self.create(cep_values) obj.write(zip.set_result()) return True diff --git a/l10n_br_zip/models/res_config_settings.py b/l10n_br_zip/models/res_config_settings.py new file mode 100644 index 000000000000..767cc251884b --- /dev/null +++ b/l10n_br_zip/models/res_config_settings.py @@ -0,0 +1,13 @@ +# Copyright (C) 2019 Renato Lima (Akretion) # +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from odoo import models, fields + + +class ResConfigSettings(models.TransientModel): + _inherit = 'res.config.settings' + + cep_update_days = fields.Integer( + config_parameter='l10n_br_zip.cep_update_days', + default=365, + string='Days to Update CEP') diff --git a/l10n_br_zip/views/res_config_settings_view.xml b/l10n_br_zip/views/res_config_settings_view.xml new file mode 100644 index 000000000000..105a0ea72162 --- /dev/null +++ b/l10n_br_zip/views/res_config_settings_view.xml @@ -0,0 +1,20 @@ + + + + + res.config.settings.form + res.config.settings + + + +
+
+
+
+
+
+
+ +