Skip to content

Commit

Permalink
[ADD] Implement simples calculation of tax
Browse files Browse the repository at this point in the history
  • Loading branch information
danimaribeiro committed Sep 15, 2018
1 parent d5fb11d commit 998f319
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 3 deletions.
2 changes: 1 addition & 1 deletion br_account_close/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# © 2017 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from . import models
from . import wizard
1 change: 1 addition & 0 deletions br_account_close/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'account_voucher', 'br_account'
],
'data': [
'views/simples.xml',
'wizard/account_close.xml',
],
'application': True,
Expand Down
5 changes: 5 additions & 0 deletions br_account_close/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# © 2018 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import res_company
from . import simples
44 changes: 44 additions & 0 deletions br_account_close/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# © 2018 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import datetime
from odoo import api, fields, models


class ResCompany(models.Model):
_inherit = 'res.company'

def get_gross_revenue_last_year(self):
gross_revenues = {}
date_max = datetime.date.today()
date_min = date_max - datetime.timedelta(days=365)
for simples_nacional_id in self.taxes_ids:
account_lines = self.env['account.move.line'].search([
('account_id', '=', simples_nacional_id.account_id),
('create_date', '>=', date_min),
('create_date', '<=', date_max)])
gross_revenue = 0
for line in account_lines:
gross_revenue += line.credit
gross_revenues.update({simples_nacional_id: gross_revenue})
return gross_revenues

def compute_new_taxes_simples_nacional(self):
gross_revenues = self.get_gross_revenue_last_year()
taxes = {}
for simples_nacional_id in gross_revenues.keys():
default_tax = simples_nacional_id.tax
pd = simples_nacional_id.deducao
gross_revenue = gross_revenues[simples_nacional_id]
tax = (default_tax*gross_revenue - pd)/gross_revenue
taxes.update({simples_nacional_id: tax})
return taxes

def compute_icms_credit_simples_nacional(self):
icms = {}
taxes = self.compute_new_taxes_simples_nacional()
for simples_nacional_id in taxes.keys():
icms_credit = simples_nacional_id.icms_percent*taxes[
simples_nacional_id]
icms.update({simples_nacional_id: icms_credit})
return icms
15 changes: 15 additions & 0 deletions br_account_close/models/simples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# © 2018 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class l10nBrTaxationSimples(models.Model):
_name = 'l10n_br.taxation.simples'

start_revenue = fields.Float(string="Start Revenue")
end_revenue = fields.Float(string="End Revenue")
amount_tax = fields.Float(string="Tax")
amount_deduction = fields.Float(string="Amount to deduct")
account_ids = fields.Many2many('account.account', string="Accounts")
company_id = fields.Many2one('res.company')
14 changes: 14 additions & 0 deletions br_account_close/views/simples.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="account_payment_mode_action" model="ir.actions.act_window">
<field name="name">Tabelas do simples</field>
<field name="res_model">l10n_br.taxation.simples</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem id="account_payment_mode_menu"
action="account_payment_mode_action"
parent="account.account_management_menu" />

</odoo>
87 changes: 85 additions & 2 deletions br_account_close/wizard/account_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# © 2017 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from datetime import datetime
from odoo import api, fields, models


Expand All @@ -11,7 +11,90 @@ class AccountClose(models.TransientModel):

start_date = fields.Date(string="Start Date")
end_date = fields.Date(string="End Date")
partner_id = fields.Many2one(
string="Parceiro",
comodel_name="res.partner",
help="Parceiro para o qual serão geradas as contas a pagar.",
)
payment_date = fields.Date(
string="Data do vencimento",
help="Data do vencimento das contas a pagar geradas.",
)
account_id = fields.Many2one(
string="Conta",
comodel_name="account.account",
help="Conta de lançamento dos impostos.",
)
journal_id = fields.Many2one(
string="Diário",
comodel_name="account.journal",
help="Diário no qual a conta será lançada.",
)
account_payment_id = fields.Many2one(
string="Conta de pagamento dos impostos.",
comodel_name="account.account",
help="Conta para pagamento dos impostos lançados.",
)

@api.multi
def action_close_period(self):
pass

self.env.cr.execute(
"select sum(debit+credit), account_id from account_move_line \
where account_id in (select account_account_id from \
account_account_l10n_br_taxation_simples_rel) \
and date >= date_trunc('month',current_date - interval '13' month)\
and date < date_trunc('month',current_date - interval '1' month)\
group by account_id;")

total_revenue = self.env.cr.fetchall()[0][0]
self.env.cr.execute(
"select sum(debit+credit), account_id from account_move_line \
where account_id in (select account_account_id from \
account_account_l10n_br_taxation_simples_rel) \
and date >= date_trunc('month', current_date - interval '1' month)\
and date < date_trunc('month', current_date)\
group by account_id;")

data = self.env.cr.fetchall()
taxes = self.env['l10n_br.taxation.simples'].search([])
for tax in taxes:
acc_ids = tax.account_ids.ids

revenue = sum([x[0] for x in data if x[1] in acc_ids])
if tax.start_revenue < total_revenue < tax.end_revenue:

fee = total_revenue * (tax.amount_tax / 100)
fee = (fee - tax.amount_deduction) / total_revenue

self.create_account_vouchers_simples_nacional(fee * revenue)

def prepare_account_voucher(self):
vals = dict(
partner_id=self.partner_id.id,
pay_now='pay_later',
date=datetime.strftime(datetime.now(), '%Y-%m-%d'),
date_due=self.payment_date,
account_date=self.payment_date,
account_id=self.account_id.id,
journal_id=self.journal_id.id,
voucher_type='purchase',
line_ids=[],
)
return vals

def prepare_account_line_voucher(self, domain, price_unit):
vals = dict(
name=domain,
account_id=self.account_payment_id.id,
quantity=1,
price_unit=price_unit,
)
return [0, 0, vals]

def create_account_vouchers_simples_nacional(self, price):
account_voucher = self.prepare_account_voucher()
voucher_line = [self.prepare_account_line_voucher(
'Simples Nacional', price)]
account_voucher['line_ids'] = voucher_line
self.env['account.voucher'].create(account_voucher)
5 changes: 5 additions & 0 deletions br_account_close/wizard/account_close.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<group name='dates'>
<field name="start_date"/>
<field name="end_date"/>
<field name="partner_id"/>
<field name="payment_date"/>
<field name="account_id" />
<field name="journal_id" />
<field name="account_payment_id" domain="([('account_type', '=', 'expense')])"/>
</group>
</group>
<footer>
Expand Down

0 comments on commit 998f319

Please sign in to comment.