Skip to content

Commit

Permalink
Merge ab3bc3a into e157091
Browse files Browse the repository at this point in the history
  • Loading branch information
mileo committed Mar 23, 2016
2 parents e157091 + ab3bc3a commit 3966eed
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 16 deletions.
1 change: 1 addition & 0 deletions l10n_br_account/__openerp__.py
Expand Up @@ -43,6 +43,7 @@
'views/res_partner_view.xml',
'views/product_view.xml',
'views/res_company_view.xml',
'views/res_config_view.xml',
'security/ir.model.access.csv',
'security/l10n_br_account_security.xml',
'report/account_invoice_report_view.xml',
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account/models/__init__.py
Expand Up @@ -20,6 +20,7 @@
from . import l10n_br_account
from . import res_partner
from . import res_company
from . import res_config
from . import account
from . import account_invoice
from . import product
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account/models/res_company.py
Expand Up @@ -56,3 +56,4 @@ class ResCompany(models.Model):
fiscal_rule_parent_id = fields.Many2one(
'account.fiscal.position.rule', u'Conjunto de Regras Fiscais',
domain="[('parent_id', '=', False)]")
ipbt_token = fields.Char(string='IPBT Token')
31 changes: 31 additions & 0 deletions l10n_br_account/models/res_config.py
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2015 KMEE (http://www.kmee.com.br)
# @author Luis Felipe Miléo (mileo@kmee.com.br)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields


class ResConfig(models.Model):
_inherit = 'account.config.settings'

ipbt_token = fields.Char(
string='IPBT Token',
related='company_id.ipbt_token'
)
2 changes: 1 addition & 1 deletion l10n_br_account/security/ir.model.access.csv
Expand Up @@ -30,4 +30,4 @@
"l10n_br_account_document_event_user","l10n_br_account.document_event","model_l10n_br_account_document_event","account.group_account_invoice",1,0,0,0
"l10n_br_account_document_event_manager","l10n_br_account.document_event","model_l10n_br_account_document_event","account.group_account_manager",1,1,1,1
"l10n_br_account_invoice_cce_manager","l10n_br_account_invoice_cce","model_l10n_br_account_invoice_cce","account.group_account_manager",1,1,1,1

"access_account_config_settings","access_account_config_settings","model_account_config_settings","base.group_system",1,1,1,1
20 changes: 20 additions & 0 deletions l10n_br_account/sped/ibpt/__init__.py
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
###############################################################################
#
# Copyright (C) 2015 Luis Felipe Miléo - KMEE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################

from . import deolhonoimposto
92 changes: 92 additions & 0 deletions l10n_br_account/sped/ibpt/deolhonoimposto.py
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
###############################################################################
#
# Copyright (C) 2015 Luis Felipe Miléo - KMEE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################

import urllib
import urllib2
import json
from collections import namedtuple


WS_SERVICOS = 0
WS_PRODUTOS = 1

WS_IBPT = {
WS_SERVICOS: 'http://iws.ibpt.org.br/api/deolhonoimposto/servico/?',
WS_PRODUTOS: 'http://iws.ibpt.org.br/api/deolhonoimposto/produto/?',
}


DeOlhoNoImposto = namedtuple('Config', 'token cnpj uf')


def _convert(dictionary):
return namedtuple('Result', dictionary.keys())(**dictionary)


def _response_to_dict(response):
json_acceptable_string = response.replace("'", "\"").lower()
return json.loads(json_acceptable_string)


def _request(req):
try:
response = urllib2.urlopen(req)
data = _response_to_dict(response.read())
return _convert(data)

except urllib2.HTTPError, e:
from openerp import _
from openerp.exceptions import Warning as UserError
raise UserError(_('Error in the request: {0}'.format(e)))


def get_ibpt_product(config, ncm, ex, reference=None, description=None,
uom=None, amount=None, gtin=None):

data = urllib.urlencode({
'token': config.token,
'cnpj': config.cnpj,
'uf': config.uf,
'codigo': ncm,
'ex': ex,
'codigoInterno': reference,
'descricao': description,
'unidadeMedida': uom,
'valor': amount,
'gtin': gtin,
})

req = urllib2.Request(WS_IBPT[WS_PRODUTOS] + data)
return _request(req)


def get_ibpt_service(config, nbs, description=None, uom=None, amount=None):

data = urllib.urlencode({
'token': config.token,
'cnpj': config.cnpj,
'uf': config.uf,
'codigo': nbs,
'descricao': description,
'unidadeMedida': uom,
'valor': amount,
})

req = urllib2.Request(WS_IBPT[WS_SERVICOS] + data)
return _request(req)
28 changes: 28 additions & 0 deletions l10n_br_account/views/res_config_view.xml
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<openerp>
<data>
<record id="view_account_config_settings_l10n_br_account" model="ir.ui.view">
<field name="name">account.config.settings.l10n_br_account</field>
<field name="model">account.config.settings</field>
<field name="inherit_id" ref="account.view_account_config_settings" />
<field name="arch" type="xml">
<data>
<xpath expr="//group[@name='bank_cash']" position="after">
<group name="l10n_br_account" string="Contabilidade Brasil">
<label for="id" string="Configuration"/>
<div>
<div>
<label string="IPBT Token:"/>
</div>
<div>
<field name="ipbt_token" class="oe_inline" />
</div>
</div>
</group>
</xpath>
</data>
</field>
</record>

</data>
</openerp>
Expand Up @@ -24,6 +24,11 @@
L10nBrTaxDefinition,
L10nBrTaxDefinitionTemplate
)
from openerp.addons.l10n_br_account.sped.ibpt.deolhonoimposto import (
DeOlhoNoImposto,
get_ibpt_product
)
from openerp.addons.l10n_br_base.tools.misc import punctuation_rm


class AccountProductFiscalClassificationTemplate(models.Model):
Expand Down Expand Up @@ -79,8 +84,9 @@ def _compute_taxes(self):
compute='_compute_taxes')

tax_estimate_ids = fields.One2many(
'l10n_br_tax.estimate.template', 'fiscal_classification_id',
'Impostos Estimados')
comodel_name='l10n_br_tax.estimate.template',
inverse_name='fiscal_classification_id',
string=u'Impostos Estimados')

_sql_constraints = [
('account_fiscal_classfication_code_uniq', 'unique (code)',
Expand Down Expand Up @@ -209,13 +215,48 @@ def _compute_taxes(self):
compute='_compute_taxes', store=True)

tax_estimate_ids = fields.One2many(
'l10n_br_tax.estimate', 'fiscal_classification_id',
'Impostos Estimados')
comodel_name='l10n_br_tax.estimate',
inverse_name='fiscal_classification_id',
string=u'Impostos Estimados')

_sql_constraints = [
('account_fiscal_classfication_code_uniq', 'unique (code)',
u'Já existe um classificação fiscal com esse código!')]

@api.multi
def get_ibpt(self):
for item in self:
brazil = item.env['res.country'].search([('code', '=', 'BR')])
states = item.env['res.country.state'].search([('country_id', '=',
brazil.id)])
company = item.company_id or item.env.user.company_id
config = DeOlhoNoImposto(company.ipbt_token,
punctuation_rm(company.cnpj_cpf),
company.state_id.code)
tax_estimate = item.env['l10n_br_tax.estimate']
for state in states:
result = get_ibpt_product(
config,
punctuation_rm(item.code or ''),
ex='0')
update = tax_estimate.search([('state_id', '=', state.id),
('origin', '=', 'IBPT-WS'),
('fiscal_classification_id',
'=', item.id)])
vals = {
'fiscal_classification_id': item.id,
'origin': 'IBPT-WS',
'state_id': state.id,
'state_taxes': result.estadual,
'federal_taxes_national': result.nacional,
'federal_taxes_import': result.importado,
}
if update:
update.write(vals)
else:
tax_estimate.create(vals)
return True


class L10nBrTaxDefinitionModel(L10nBrTaxDefinition):
_name = 'l10n_br_tax.definition.model'
Expand Down Expand Up @@ -244,7 +285,7 @@ class L10nBrTaxEstimate(models.Model):
_inherit = 'l10n_br_tax.estimate.model'

fiscal_classification_id = fields.Many2one(
'account.product.fiscal.classification.template',
'account.product.fiscal.classification',
'Fiscal Classification', select=True)


Expand Down
Expand Up @@ -78,19 +78,41 @@
</group>
</page>
<page string="Impostos Estimados">
<div class="oe_right oe_button_box" name="buttons">
<button class="oe_inline oe_stat_button" type="object"
name="get_ibpt" icon="fa-search">
<div>Consultar<br/>IBPT</div>
</button>
</div>
<field name="tax_estimate_ids" nolabel="1">
<tree>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
<field name="key"/>
<field name="version"/>
<field name="origin"/>
<field name="federal_taxes_national"/>
<field name="federal_taxes_import"/>
<field name="state_taxes"/>
<field name="municipal_taxes"/>
<field name="origin"/>
</tree>
<form>
<group>
<group>
<field name="fiscal_classification_id"/>
<field name="origin"/>
<field name="version"/>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
</group>
<group>
<field name="active"/>
<field name="key"/>
<field name="federal_taxes_national"/>
<field name="federal_taxes_import"/>
<field name="state_taxes"/>
<field name="municipal_taxes"/>
</group>
</group>
</form>
</field>
</page>
</notebook>
Expand Down Expand Up @@ -174,19 +196,41 @@
</group>
</page>
<page string="Impostos Estimados">
<div class="oe_right oe_button_box" name="buttons">
<button class="oe_inline oe_stat_button" type="object"
name="get_ibpt" icon="fa-search">
<div>Consultar<br/>IBPT</div>
</button>
</div>
<field name="tax_estimate_ids" nolabel="1">
<tree>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
<field name="key"/>
<field name="version"/>
<field name="origin"/>
<field name="federal_taxes_national"/>
<field name="federal_taxes_import"/>
<field name="state_taxes"/>
<field name="municipal_taxes"/>
<field name="origin"/>
</tree>
<form>
<group>
<group>
<field name="fiscal_classification_id"/>
<field name="origin"/>
<field name="version"/>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
</group>
<group>
<field name="active"/>
<field name="key"/>
<field name="federal_taxes_national"/>
<field name="federal_taxes_import"/>
<field name="state_taxes"/>
<field name="municipal_taxes"/>
</group>
</group>
</form>
</field>
</page>
</notebook>
Expand Down

0 comments on commit 3966eed

Please sign in to comment.