-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] Implement tax closing over a period and create a voucher to pay…
… the tax FIX Simples nacional calculation
- Loading branch information
1 parent
998f319
commit b864be0
Showing
25 changed files
with
621 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ | |
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from . import models | ||
from . import wizard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# © 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 | ||
from . import account_tax | ||
from . import account_voucher | ||
from . import account_account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# © 2018 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
|
||
from odoo import fields, models | ||
|
||
|
||
class AccountAccount(models.Model): | ||
_inherit = 'account.account' | ||
|
||
l10n_br_credit_account_id = fields.Many2one( | ||
'account.account', string="Credit Account") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# © 2018 Danimar Ribeiro <danimaribeiro@gmail.com>, Trustcode | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
|
||
from odoo import fields, models | ||
|
||
|
||
class AccountTaxTemplate(models.Model): | ||
_inherit = 'account.tax.template' | ||
|
||
domain = fields.Selection(selection_add=[('simples', 'Simples Nacional')]) | ||
|
||
|
||
class AccountTax(models.Model): | ||
_inherit = 'account.tax' | ||
|
||
domain = fields.Selection(selection_add=[('simples', 'Simples Nacional')]) | ||
l10n_br_tax_interval_ids = fields.One2many( | ||
'l10n_br.taxation.simples', 'tax_id', string="Tax Rules") | ||
l10n_br_revenue_account_ids = fields.Many2many( | ||
'account.account', string="Revenue Accounts") | ||
|
||
def aggregate_tax_to_pay(self, period): | ||
if self.domain == 'simples': | ||
return self._calculate_simples_nacional_tax() | ||
return self._calulate_amount_tax_period(period) | ||
|
||
def _calulate_amount_tax_period(self, period): | ||
if not self.account_id: | ||
return 0 | ||
acc_ids = [self.account_id.id] | ||
if self.account_id.l10n_br_credit_account_id: | ||
acc_ids += [self.account_id.l10n_br_credit_account_id.id] | ||
self.env.cr.execute( | ||
"select sum(credit-debit) from account_move_line \ | ||
where account_id in %s \ | ||
and date >= date_trunc('month', current_date - interval %s month)\ | ||
and date < date_trunc('month', current_date)", | ||
(tuple(acc_ids), str(period))) | ||
result = self.env.cr.fetchall() | ||
return result and result[0] and result[0][0] or 0.0 | ||
|
||
# Necessary because is not a matter of sum the taxes over the month | ||
def _calculate_simples_nacional_tax(self): | ||
acc_ids = self.l10n_br_revenue_account_ids.ids | ||
self.env.cr.execute( | ||
"select sum(debit+credit), account_id from account_move_line \ | ||
where account_id in %s \ | ||
and date >= date_trunc('month',current_date - interval '13' month)\ | ||
and date < date_trunc('month',current_date - interval '1' month)\ | ||
group by account_id;", (tuple(acc_ids),)) | ||
|
||
result = self.env.cr.fetchall() | ||
total_revenue = result and result[0] and result[0][0] or 0.0 | ||
if total_revenue <= 0: | ||
return 0.0 | ||
self.env.cr.execute( | ||
"select sum(debit+credit), account_id from account_move_line \ | ||
where account_id in %s \ | ||
and date >= date_trunc('month', current_date - interval '1' month)\ | ||
and date < date_trunc('month', current_date)\ | ||
group by account_id;", (tuple(acc_ids),)) | ||
|
||
data = self.env.cr.fetchall() | ||
for tax in self.l10n_br_tax_interval_ids: | ||
revenue = sum([x[0] for x in data if x[1] in acc_ids]) | ||
if tax.start_revenue <= total_revenue < tax.end_revenue: | ||
# Cálculo do simples Nacional | ||
fee = total_revenue * (tax.amount_tax / 100) | ||
fee = (fee - tax.amount_deduction) / total_revenue | ||
|
||
return fee * revenue | ||
return 0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
||
from datetime import date | ||
from calendar import monthrange | ||
from odoo import fields, models, _ | ||
from dateutil.relativedelta import relativedelta | ||
|
||
|
||
class AccountVoucher(models.Model): | ||
_inherit = 'account.voucher' | ||
|
||
l10n_br_tax_id = fields.Many2one( | ||
'account.tax', string="Tax to compute", | ||
help="If selected compute the voucher value \ | ||
according to the tax to pay") | ||
l10n_br_months_compute = fields.Integer( | ||
string="Period (months)", help="Period in months to compute", | ||
default=1) | ||
|
||
def prepare_voucher_values_to_copy(self, vals): | ||
vals = super(AccountVoucher, self).prepare_voucher_values_to_copy(vals) | ||
# Calculate first and last day of the period | ||
go_back = self.l10n_br_months_compute * -1 | ||
last_month = date.today() + relativedelta(months=-1) | ||
last_day = monthrange(last_month.year, last_month.month)[1] | ||
last_month = last_month.replace(day=last_day) | ||
start_month = date.today() + relativedelta(months=go_back) | ||
start_month = start_month.replace(day=1) | ||
if self.l10n_br_tax_id: | ||
description = _("%s - Period from %s to %s") % ( | ||
self.l10n_br_tax_id.name, start_month, last_month) | ||
vals.update({ | ||
'narration': description, | ||
'account_date': last_month | ||
}) | ||
return vals | ||
|
||
def calculate_amount_voucher_line(self, line): | ||
voucher = line.voucher_id | ||
if voucher.l10n_br_tax_id: | ||
return voucher.l10n_br_tax_id.aggregate_tax_to_pay( | ||
voucher.l10n_br_months_compute or 1) | ||
return super(AccountVoucher, self).calculate_amount_voucher_line(line) |
Oops, something went wrong.