Skip to content

Commit

Permalink
[ADD] Added settings parameter to update cep in database
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonlima committed Aug 30, 2019
1 parent 1f37e00 commit 48ed002
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 32 deletions.
1 change: 1 addition & 0 deletions l10n_br_zip/__manifest__.py
Expand Up @@ -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',
],
Expand Down
1 change: 1 addition & 0 deletions 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
84 changes: 52 additions & 32 deletions l10n_br_zip/models/l10n_br_zip.py
Expand Up @@ -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,
Expand All @@ -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):

Expand Down Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions 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')
20 changes: 20 additions & 0 deletions l10n_br_zip/views/res_config_settings_view.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="l10n_br_base_res_config_settings_form" model="ir.ui.view">
<field name="name">res.config.settings.form</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@name='l10n_br_zip_api']/div[2]" position="inside">
<div attrs="{'invisible': [('module_l10n_br_zip', '=', False)]}">
<div class="content-group mt16">
<label for="cep_update_days" class="o_light_label"/>
<field name="cep_update_days" attrs="{'required': [('module_l10n_br_zip', '=', True)]}"/> days
</div>
</div>
</xpath>
</field>
</record>

</odoo>

0 comments on commit 48ed002

Please sign in to comment.