Skip to content

Commit

Permalink
Merge 406a880 into 63ffb43
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrixcosta committed Aug 22, 2016
2 parents 63ffb43 + 406a880 commit 416f433
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 72 deletions.
3 changes: 2 additions & 1 deletion l10n_br_account/models/res_company.py
Expand Up @@ -41,4 +41,5 @@ 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')
ipbt_token = fields.Char(string=u'IPBT Token')
ibpt_update_days = fields.Integer(string=u'IPBT Token')
6 changes: 5 additions & 1 deletion l10n_br_account/models/res_config.py
Expand Up @@ -10,6 +10,10 @@ class ResConfig(models.Model):
_inherit = 'account.config.settings'

ipbt_token = fields.Char(
string='IPBT Token',
string=u'IPBT Token',
related='company_id.ipbt_token'
)
ibpt_update_days = fields.Integer(
string=u'Quantidade de dias para Atualizar',
related='company_id.ibpt_update_days'
)
6 changes: 3 additions & 3 deletions l10n_br_account/sped/ibpt/deolhonoimposto.py
Expand Up @@ -12,8 +12,8 @@
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/?',
WS_SERVICOS: 'http://iws.ibpt.org.br/api/deolhonoimposto/Servicos/?',
WS_PRODUTOS: 'http://iws.ibpt.org.br/api/deolhonoimposto/Produtos/?',
}


Expand Down Expand Up @@ -41,7 +41,7 @@ def _request(req):
raise UserError(_('Error in the request: {0}'.format(e)))


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

data = urllib.urlencode({
Expand Down
6 changes: 6 additions & 0 deletions l10n_br_account/views/res_config_view.xml
Expand Up @@ -17,6 +17,12 @@
<div>
<field name="ipbt_token" class="oe_inline" />
</div>
<div>
<label string="Quantidade de dias para Atualizar:"/>
</div>
<div>
<field name="ibpt_update_days"/>
</div>
</div>
</group>
</xpath>
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account_product/__openerp__.py
Expand Up @@ -21,6 +21,7 @@
'data/l10n_br_account.fiscal.document.csv',
'data/l10n_br_account_data.xml',
'data/l10n_br_account_product_data.xml',
'data/ir_cron.xml',
'views/l10n_br_account_product_view.xml',
'views/l10n_br_account_view.xml',
'views/l10n_br_account_product_view.xml',
Expand Down
17 changes: 17 additions & 0 deletions l10n_br_account_product/data/ir_cron.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record forcecreate="True" id="ir_cron_update_due_ncm_action" model="ir.cron">
<field name="name">Update NCM Estimate Taxes</field>
<field eval="True" name="active"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="'account.product.fiscal.classification'" name="model"/>
<field eval="'update_due_ncm'" name="function"/>
<field eval="" name="args"/>
</record>
</data>
</openerp>
30 changes: 3 additions & 27 deletions l10n_br_account_product/models/account.py
Expand Up @@ -2,8 +2,6 @@
# Copyright (C) 2013 Renato Lima - Akretion #
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from datetime import datetime

from openerp import models, fields, api


Expand Down Expand Up @@ -216,31 +214,9 @@ def compute_all(self, cr, uid, taxes, price_unit, quantity,

# Estimate Taxes
if fiscal_position and fiscal_position.asset_operation:
obj_tax_estimate = self.pool.get('l10n_br_tax.estimate')
date = datetime.now().strftime('%Y-%m-%d')
tax_estimate_ids = obj_tax_estimate.search(
cr, uid, [('fiscal_classification_id', '=',
product.fiscal_classification_id.id),
'|', ('date_start', '=', False),
('date_start', '<=', date),
'|', ('date_end', '=', False),
('date_end', '>=', date),
('active', '=', True)])

if tax_estimate_ids:
tax_estimate = obj_tax_estimate.browse(
cr, uid, tax_estimate_ids)[0]
tax_estimate_percent = 0.00
if product.origin in ('1', '2', '6', '7'):
tax_estimate_percent += tax_estimate.federal_taxes_import
else:
tax_estimate_percent += tax_estimate.federal_taxes_national

tax_estimate_percent += tax_estimate.state_taxes
tax_estimate_percent /= 100
total_taxes = ((result['total_included'] - totaldc) *
tax_estimate_percent)
result['total_taxes'] = round(total_taxes, precision)
total_taxes = ((result['total_included'] - totaldc) *
product.product_estimated_taxes_percent/100)
result['total_taxes'] = round(total_taxes, precision)

return {
'total': result['total'],
Expand Down
Expand Up @@ -2,6 +2,8 @@
# Copyright (C) 2012 Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from datetime import timedelta, date

from openerp import models, fields, api
from openerp.addons import decimal_precision as dp

Expand Down Expand Up @@ -138,16 +140,19 @@ class L10nBrTaxEstimateModel(models.AbstractModel):
'Impostos Municipais Nacional', default=0.00,
digits_compute=dp.get_precision('Account'))

date_start = fields.Date('Data Inicial')

date_end = fields.Date('Data Final')
create_date = fields.Datetime(
u'Data de Criação', readonly=True)

key = fields.Char('Chave', size=32)

version = fields.Char(u'Versão', size=32)

origin = fields.Char('Fonte', size=32)

company_id = fields.Many2one(
comodel_name='res.company'
)


class L10nBrTaxEstimateTemplate(models.Model):
_name = 'l10n_br_tax.estimate.template'
Expand Down Expand Up @@ -211,7 +216,7 @@ def _compute_taxes(self):
tax_estimate_ids = fields.One2many(
comodel_name='l10n_br_tax.estimate',
inverse_name='fiscal_classification_id',
string=u'Impostos Estimados')
string=u'Impostos Estimados', readonly=True)

cest = fields.Char(
string='CEST',
Expand All @@ -223,38 +228,84 @@ def _compute_taxes(self):

@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)
for fiscal_classification in self:

company = (
fiscal_classification.env.user.company_id or
fiscal_classification.company_id)

config = DeOlhoNoImposto(
company.ipbt_token, punctuation_rm(company.cnpj_cpf),
company.state_id.code)

result = get_ibpt_product(
config,
punctuation_rm(fiscal_classification.code or ''),
)

vals = {
'fiscal_classification_id': fiscal_classification.id,
'origin': 'IBPT-WS',
'state_id': company.state_id.id,
'state_taxes': result.estadual,
'federal_taxes_national': result.nacional,
'federal_taxes_import': result.importado,
'company_id': company.id,
}

tax_estimate = fiscal_classification.env[
'l10n_br_tax.estimate']

tax_estimate.create(vals)

return True

@api.model
def update_due_ncm(self):

config_date = self.env['account.config.settings'].browse(
[1]).ibpt_update_days
today = date.today()
data_max = today - timedelta(days=config_date)

all_ncm = self.env[
'account.product.fiscal.classification'].search([])

not_estimated = all_ncm.filtered(
lambda r: r.product_tmpl_qty > 0 and not r.tax_estimate_ids
)

query = (
"WITH ncm_max_date AS ("
" SELECT "
" fiscal_classification_id, "
" max(create_date) "
" FROM "
" l10n_br_tax_estimate "
" GROUP BY "
" fiscal_classification_id"
") SELECT fiscal_classification_id "
"FROM "
" ncm_max_date "
"WHERE "
" max < %(create_date)s "
)
query_params = {'create_date': data_max.strftime('%Y-%m-%d')}

self._cr.execute(self._cr.mogrify(query, query_params))
past_estimated = self._cr.fetchall()

ids = [estimate[0] for estimate in past_estimated]

ncm_past_estimated = self.env[
'account.product.fiscal.classification'].browse(ids)

for ncm in not_estimated + ncm_past_estimated:
try:
ncm.get_ibpt()
except:
pass


class L10nBrTaxDefinitionModel(L10nBrTaxDefinition):
_name = 'l10n_br_tax.definition.model'
Expand Down
26 changes: 25 additions & 1 deletion l10n_br_account_product/models/product.py
Expand Up @@ -2,7 +2,7 @@
# Copyright (C) 2013 Renato Lima - Akretion #
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import models, fields
from openerp import models, fields, api

from .l10n_br_account_product import (
PRODUCT_FISCAL_TYPE,
Expand Down Expand Up @@ -33,6 +33,25 @@
class ProductTemplate(models.Model):
_inherit = 'product.template'

@api.multi
@api.depends('origin',
'fiscal_classification_id',
'fiscal_classification_id.tax_estimate_ids')
def _compute_product_estimated_taxes_percent(self):
for template in self:

t_ids = template.fiscal_classification_id.tax_estimate_ids.ids
last_estimated = self.env['l10n_br_tax.estimate'].search(
[('id', 'in', t_ids)], order='create_date DESC', limit=1)

tax_estimate_percent = 0.00
if template.origin in ('1', '2', '6', '7'):
tax_estimate_percent += last_estimated.federal_taxes_import
else:
tax_estimate_percent += last_estimated.federal_taxes_national
tax_estimate_percent += last_estimated.state_taxes
template.product_estimated_taxes_percent = tax_estimate_percent

fiscal_type = fields.Selection(
selection_add=PRODUCT_FISCAL_TYPE,
default=PRODUCT_FISCAL_TYPE_DEFAULT)
Expand All @@ -43,3 +62,8 @@ class ProductTemplate(models.Model):

service_type_id = fields.Many2one(
'l10n_br_account.service.type', u'Tipo de Serviço')

product_estimated_taxes_percent = fields.Float(
string=u'Estimated Taxes(%)',
compute='_compute_product_estimated_taxes_percent',
)
Expand Up @@ -38,6 +38,13 @@
<field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
</record>

<record id="l10n_br_tax_estimate_rule" model="ir.rule">
<field name="name">Tax Estimate multi-company</field>
<field name="model_id" ref="model_l10n_br_tax_estimate"/>
<field eval="True" name="global"/>
<field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
</record>

</data>

</openerp>
Expand Up @@ -99,8 +99,7 @@
<field name="origin"/>
<field name="version"/>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
<field name="create_date"/>
</group>
<group>
<field name="active"/>
Expand Down Expand Up @@ -146,7 +145,7 @@
</tree>
</field>
</record>

<record id="l10n_br_tax_definition_purchase_form" model="ir.ui.view">
<field name="name">l10n_br_tax.definition.purchase.form</field>
<field name="model">l10n_br_tax.definition.purchase</field>
Expand Down Expand Up @@ -224,8 +223,7 @@
<field name="origin"/>
<field name="version"/>
<field name="state_id"/>
<field name="date_start"/>
<field name="date_end"/>
<field name="create_date"/>
</group>
<group>
<field name="active"/>
Expand Down
1 change: 1 addition & 0 deletions l10n_br_account_product/views/product_view.xml
Expand Up @@ -18,6 +18,7 @@
<group>
<field name="fiscal_type" required="1"/>
<field name="origin" attrs="{'required': [('fiscal_type', '=', 'product')]}" />
<field name="product_estimated_taxes_percent"/>
</group>
<group>
<field name="fiscal_classification_id" attrs="{'required': [('fiscal_type', '=', 'product')]}" domain="[('type', '=', 'normal')]"/>
Expand Down

0 comments on commit 416f433

Please sign in to comment.