Skip to content

Commit

Permalink
Merge 637b7b3 into 6c935b4
Browse files Browse the repository at this point in the history
  • Loading branch information
mbcosta committed Jul 12, 2019
2 parents 6c935b4 + 637b7b3 commit ecdf15a
Show file tree
Hide file tree
Showing 33 changed files with 548 additions and 1,129 deletions.
14 changes: 11 additions & 3 deletions l10n_br_zip/README.rst
Expand Up @@ -34,12 +34,12 @@ Brazilian Localisation ZIP Codes
Installation
============

Este módulo depende do l10n_br_base.
Este módulo depende do l10n_br_base e da biblioteca PyCEP-Correios.

Configuration
=============

Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou instalado o módulo l10n_br_zip_correios para a consulta de CEPs diretamente dos Correios.
Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou quando o CEP não for encontrado ele será consultado no serviço dos Correios utilizando a biblioteca PyCEP-Correios.

Usage
=====
Expand All @@ -49,7 +49,15 @@ Nos endereços de parceiro, empresa e prospectos será exibido ao lado do campo
Known issues / Roadmap
======================

* Utilizar a biblioteca pyhton pycep_correios e unir os módulos l10n_br_zip e l10n_br_correios


Changelog
=========

12.0.2.0.0 (2019-06-17)
~~~~~~~~~~~~~~~~~~~~~~~

* [REF] Incluida pesquisa e dependência da biblioteca PyCEP-Correios.

Bug Tracker
===========
Expand Down
5 changes: 4 additions & 1 deletion l10n_br_zip/__manifest__.py
Expand Up @@ -10,7 +10,7 @@
'Akretion, '
'Odoo Community Association (OCA)',
'website': 'http://odoo-brasil.org',
'version': '12.0.1.0.0',
'version': '12.0.2.0.0',
'depends': [
'l10n_br_base'
],
Expand All @@ -21,4 +21,7 @@
'security/ir.model.access.csv',
],
'installable': True,
'external_dependencies': {
'python': ['pycep_correios'],
}
}
173 changes: 110 additions & 63 deletions l10n_br_zip/models/l10n_br_zip.py
@@ -1,21 +1,30 @@
# Copyright (C) 2012 Renato Lima (Akretion) #
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import logging

from odoo import models, fields, api, _
from odoo.exceptions import UserError

from odoo.addons.l10n_br_base.tools import misc

_logger = logging.getLogger(__name__)

try:
import pycep_correios
except ImportError:
_logger.warning("Library PyCEP-Correios not installed !")


class L10nBrZip(models.Model):
""" Este objeto persiste todos os códigos postais que podem ser
utilizados para pesquisar e auxiliar o preenchimento dos endereços.
"""
_name = 'l10n_br.zip'
_description = 'CEP'
_rec_name = 'zip'
_rec_name = 'zip_code'

zip = fields.Char(
zip_code = fields.Char(
string='CEP',
size=8,
required=True)
Expand All @@ -24,6 +33,10 @@ class L10nBrZip(models.Model):
string='Street Type',
size=26)

zip_complement = fields.Char(
string='Range',
size=200)

street = fields.Char(
string='Logradouro',
size=72)
Expand Down Expand Up @@ -53,7 +66,7 @@ def _set_domain(self, country_id=False, state_id=False,
domain = []
if zip_code:
new_zip = misc.punctuation_rm(zip_code or '')
domain.append(('zip', '=', new_zip))
domain.append(('zip_code', '=', new_zip))
else:
if not state_id or not city_id or len(street or '') == 0:
raise UserError(
Expand All @@ -72,25 +85,20 @@ def _set_domain(self, country_id=False, state_id=False,

return domain

def set_result(self, zip_obj=None):
if zip_obj:
zip_code = zip_obj.zip
if len(zip_code) == 8:
zip_code = '%s-%s' % (zip_code[0:5], zip_code[5:8])
result = {
'country_id': zip_obj.country_id.id,
'state_id': zip_obj.state_id.id,
'city_id': zip_obj.city_id.id,
'city': zip_obj.city_id.name,
'district': zip_obj.district,
'street': ((zip_obj.street_type or '') +
' ' + (zip_obj.street or '')) if
zip_obj.street_type else (zip_obj.street or ''),
'zip': zip_code,
}
else:
result = {}
return result
@api.multi
def set_result(self):
self.ensure_one()
return {
'country_id': self.country_id.id,
'state_id': self.state_id.id,
'city_id': self.city_id.id,
'city': self.city_id.name,
'district': self.district,
'street': ((self.street_type or '') +
' ' + (self.street or '')) if
self.street_type else (self.street or ''),
'zip': misc.format_zipcode(
self.zip_code, self.country_id.code)}

@api.model
def zip_search(self, obj):
Expand All @@ -107,58 +115,97 @@ def zip_search(self, obj):
raise UserError(
_('Erro a Carregar Atributo: ') + str(e))

zip_ids = self.search(domain)
zips = self.search(domain)

if len(zip_ids) == 1:
result = self.set_result(zip_ids[0])
obj.write(result)
# One ZIP was found
if len(zips) == 1:
obj.write(zips[0].set_result())
return True
else:
if len(zip_ids) > 1:
obj_zip_result = self.env['l10n_br.zip.result']
zip_ids = obj_zip_result.map_to_zip_result(
zip_ids, obj._name, obj.id)

return self.create_wizard(
obj._name,
obj.id,
country_id=obj.country_id.id,
state_id=obj.state_id.id,
city_id=obj.city_id.id,
district=obj.district,
street=obj.street,
zip_code=obj.zip,
zip_ids=[zip.id for zip in zip_ids]
)
else:
raise UserError(_('Nenhum registro encontrado'))

def create_wizard(self, object_name, address_id, country_id=False,
state_id=False, city_id=False,
district=False, street=False, zip_code=False,
zip_ids=False):

# More than one ZIP was found
elif len(zips) > 1:

return self.create_wizard(obj, zips)

# 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,
}

# Create zip object
zip = self.create(values)
obj.write(zip.set_result())
return True

def create_wizard(self, obj, zips):

context = dict(self.env.context)
context.update({
'zip': zip_code,
'street': street,
'district': district,
'country_id': country_id,
'state_id': state_id,
'city_id': city_id,
'zip_ids': zip_ids,
'address_id': address_id,
'object_name': object_name})

result = {
'address_id': obj.id,
'object_name': obj._name,
})

wizard = self.env['l10n_br.zip.search'].create({
'zip': obj.zip,
'street': obj.street,
'district': obj.district,
'country_id': obj.country_id.id,
'state_id': obj.state_id.id,
'city_id': obj.city_id.id,
'zip_ids': [[6, 0, [zip.id for zip in zips]]],
'address_id': obj.id,
'object_name': obj._name
})

return {
'name': 'Zip Search',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'l10n_br.zip.search',
'view_id': False,
'context': context,
'type': 'ir.actions.act_window',
'target': 'new',
'nodestroy': True,
'res_id': wizard.id,
'context': context,
}

return result
@api.multi
def zip_select(self):
self.ensure_one()
address_id = self._context.get('address_id')
object_name = self._context.get('object_name')
if address_id and object_name:
obj = self.env[object_name].browse(address_id)
obj.write(self.set_result())
return True
2 changes: 1 addition & 1 deletion l10n_br_zip/readme/CONFIGURE.rst
@@ -1 +1 @@
Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou instalado o módulo l10n_br_zip_correios para a consulta de CEPs diretamente dos Correios.
Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou quando o CEP não for encontrado ele será consultado no serviço dos Correios utilizando a biblioteca PyCEP-Correios.
4 changes: 4 additions & 0 deletions l10n_br_zip/readme/HISTORY.rst
@@ -0,0 +1,4 @@
12.0.2.0.0 (2019-06-17)
~~~~~~~~~~~~~~~~~~~~~~~

* [REF] Incluida pesquisa e dependência da biblioteca PyCEP-Correios.
2 changes: 1 addition & 1 deletion l10n_br_zip/readme/INSTALL.rst
@@ -1 +1 @@
Este módulo depende do l10n_br_base.
Este módulo depende do l10n_br_base e da biblioteca PyCEP-Correios.
2 changes: 1 addition & 1 deletion l10n_br_zip/readme/ROADMAP.rst
@@ -1 +1 @@
* Utilizar a biblioteca pyhton pycep_correios e unir os módulos l10n_br_zip e l10n_br_correios

54 changes: 33 additions & 21 deletions l10n_br_zip/static/description/index.html
Expand Up @@ -371,63 +371,75 @@ <h1 class="title">Brazilian Localisation ZIP Codes</h1>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#installation" id="id1">Installation</a></li>
<li><a class="reference internal" href="#configuration" id="id2">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="id3">Usage</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="id4">Known issues / Roadmap</a></li>
<li><a class="reference internal" href="#bug-tracker" id="id5">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id6">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id7">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id8">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id9">Maintainers</a></li>
<li><a class="reference internal" href="#installation" id="id2">Installation</a></li>
<li><a class="reference internal" href="#configuration" id="id3">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="id4">Usage</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="id5">Known issues / Roadmap</a></li>
<li><a class="reference internal" href="#changelog" id="id6">Changelog</a><ul>
<li><a class="reference internal" href="#id1" id="id7">12.0.2.0.0 (2019-06-17)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bug-tracker" id="id8">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id9">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id10">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id11">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id12">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="installation">
<h1><a class="toc-backref" href="#id1">Installation</a></h1>
<p>Este módulo depende do l10n_br_base.</p>
<h1><a class="toc-backref" href="#id2">Installation</a></h1>
<p>Este módulo depende do l10n_br_base e da biblioteca PyCEP-Correios.</p>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#id2">Configuration</a></h1>
<p>Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou instalado o módulo l10n_br_zip_correios para a consulta de CEPs diretamente dos Correios.</p>
<h1><a class="toc-backref" href="#id3">Configuration</a></h1>
<p>Ao ser instalado o módulo de CEP deve ser populado a tabela l10n_br.zip ou quando o CEP não for encontrado ele será consultado no serviço dos Correios utilizando a biblioteca PyCEP-Correios.</p>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#id3">Usage</a></h1>
<h1><a class="toc-backref" href="#id4">Usage</a></h1>
<p>Nos endereços de parceiro, empresa e prospectos será exibido ao lado do campo CEP um botão para pesquisa de CEP.</p>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#id4">Known issues / Roadmap</a></h1>
<h1><a class="toc-backref" href="#id5">Known issues / Roadmap</a></h1>
</div>
<div class="section" id="changelog">
<h1><a class="toc-backref" href="#id6">Changelog</a></h1>
<div class="section" id="id1">
<h2><a class="toc-backref" href="#id7">12.0.2.0.0 (2019-06-17)</a></h2>
<blockquote>
<ul class="simple">
<li>Utilizar a biblioteca pyhton pycep_correios e unir os módulos l10n_br_zip e l10n_br_correios</li>
<li>[REF] Incluida pesquisa e dependência da biblioteca PyCEP-Correios.</li>
</ul>
</blockquote>
</div>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id5">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#id8">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/l10n-brazil/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/l10n-brazil/issues/new?body=module:%20l10n_br_zip%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#id6">Credits</a></h1>
<h1><a class="toc-backref" href="#id9">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#id7">Authors</a></h2>
<h2><a class="toc-backref" href="#id10">Authors</a></h2>
<ul class="simple">
<li>Akretion</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#id8">Contributors</a></h2>
<h2><a class="toc-backref" href="#id11">Contributors</a></h2>
<ul class="simple">
<li>Renato Lima &lt;<a class="reference external" href="mailto:rento.lima&#64;akretion.com.br">rento.lima&#64;akretion.com.br</a>&gt;</li>
<li>Magno Costa &lt;<a class="reference external" href="mailto:magno.costa&#64;akretion.com.br">magno.costa&#64;akretion.com.br</a>&gt;</li>
<li>Hendrix Costa &lt;<a class="reference external" href="mailto:hendrix.costa&#64;kmee.com.br">hendrix.costa&#64;kmee.com.br</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#id9">Maintainers</a></h2>
<h2><a class="toc-backref" href="#id12">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
Expand Down
3 changes: 2 additions & 1 deletion l10n_br_zip/tests/__init__.py
@@ -1 +1,2 @@
from . import test_l10n_br_zip
from . import test_l10n_br_zip_res_company
from . import test_l10n_br_zip_res_partner

0 comments on commit ecdf15a

Please sign in to comment.