Skip to content

Commit

Permalink
REF account_vat_period_end_statement (#332)
Browse files Browse the repository at this point in the history
move _get_tax_codes_amounts to l10n_it_account to make it reusable
  • Loading branch information
eLBati committed Jul 24, 2017
1 parent 19399d3 commit 5c20a72
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 99 deletions.
100 changes: 2 additions & 98 deletions account_vat_period_end_statement/report/vat_period_end_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,107 +73,11 @@ def _get_fiscal_page_base(self, statement_id):
self.cr, self.uid, statement_id, self.context)
return statement.fiscal_page_base

def _compute_tax_amount(self, tax, tax_code, base_code, context=None):
'''
The Tax is child of another main tax.
The main tax has more childs:
- Child with tax_code_id are deductible
- Child without tax_code_id are undeductible
'''
res = {}
vat_deductible = 0
vat_undeductible = 0
vat_name = False
if tax.parent_id:
vat_code = tax.parent_id.description
vat_name = tax.parent_id.name
for child in tax.parent_id.child_ids:
# deductibile
if (
child.tax_code_id and
child.tax_code_id.vat_statement_account_id
):
vat_deductible = child.tax_code_id.sum_period
# undeductibile
else:
vat_undeductible = child.tax_code_id.sum_period
else:
vat_code = tax_code.code
vat_name = tax_code.name
vat_deductible = tax_code.sum_period

res[vat_name] = {
'code': vat_code,
'tax_code_name': vat_name,
'vat': vat_deductible + vat_undeductible,
'vat_deductible': vat_deductible,
'vat_undeductible': vat_undeductible,
'base': base_code.sum_period
}

return res

def _build_codes_dict(self, tax_code, res=None, context=None):

if context is None:
context = {}
if res is None:
res = {}
tax_pool = self.pool.get('account.tax')

# search for taxes linked to that code
tax_ids = tax_pool.search(
self.cr, self.uid, [('tax_code_id', '=', tax_code.id)],
context=context)
if tax_ids:
tax = tax_pool.browse(
self.cr, self.uid, tax_ids[0], context=context)
# search for the related base code
base_code = (
tax.base_code_id or tax.parent_id and
tax.parent_id.base_code_id or False)
if not base_code:
raise orm.except_orm(
_('Error'),
_('No base code found for tax code %s') % tax_code.name)
# check if every tax is linked to the same tax code and base code
for tax in tax_pool.browse(
self.cr, self.uid, tax_ids, context=context
):
test_base_code = (
tax.base_code_id or tax.parent_id and
tax.parent_id.base_code_id or False)
if test_base_code.id != base_code.id:
raise orm.except_orm(
_('Error'),
_('Not every tax linked to tax code %s is linked to '
'the same base code')
% tax_code.name)
if tax_code.sum_period or base_code.sum_period:
tax_vals = self._compute_tax_amount(
tax, tax_code, base_code, context)
res.update(tax_vals)

for child_code in tax_code.child_ids:
res = self._build_codes_dict(
child_code, res=res, context=context)

return res

def _get_tax_codes_amounts(self, period_id, tax_code_ids=None,
context=None):
if context is None:
context = {}
if tax_code_ids is None:
tax_code_ids = []
res = {}
code_pool = self.pool.get('account.tax.code')
context['period_id'] = period_id
for tax_code in code_pool.browse(
self.cr, self.uid, tax_code_ids, context=context
):
res = self._build_codes_dict(tax_code, res=res, context=context)
return res
return code_pool._get_tax_codes_amounts(
self.cr, self.uid, period_id, tax_code_ids, context)

def _get_account_vat_amounts(
self, type='credit', statement_account_line=None, context=None
Expand Down
98 changes: 97 additions & 1 deletion l10n_it_account/models/account_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#
#

from openerp import fields, models
from openerp import fields, models, api
from openerp.tools.translate import _
from openerp.exceptions import except_orm

Expand All @@ -39,6 +39,102 @@ class AccountTaxCode(models.Model):
help="This tax code is used for base amounts \
(field used by VAT registries)")

@api.model
def _compute_tax_amount(self, tax, tax_code, base_code):
'''
The Tax is child of another main tax.
The main tax has more childs:
- Child with tax_code_id are deductible
- Child without tax_code_id are undeductible
'''
res = {}
vat_deductible = 0
vat_undeductible = 0
if tax.parent_id:
vat_code = tax.parent_id.description
vat_name = tax.parent_id.name
for child in tax.parent_id.child_ids:
# deductibile
if (
child.tax_code_id and
child.tax_code_id.vat_statement_account_id
):
vat_deductible = child.tax_code_id.sum_period
# undeductibile
else:
vat_undeductible = child.tax_code_id.sum_period
else:
vat_code = tax_code.code
vat_name = tax_code.name
vat_deductible = tax_code.sum_period

res[vat_name] = {
'code': vat_code,
'tax_code_name': vat_name,
'vat': vat_deductible + vat_undeductible,
'vat_deductible': vat_deductible,
'vat_undeductible': vat_undeductible,
'base': base_code.sum_period
}

return res

@api.model
def _build_codes_dict(self, tax_code, res=None):
# TODO context
if res is None:
res = {}
tax_model = self.env['account.tax']
tax_code_model = self.env['account.tax.code']

# search for taxes linked to that code
taxes = tax_model.search(
[('tax_code_id', '=', tax_code.id)])
if taxes:
tax = taxes[0]
# search for the related base code
base_code = (
tax.base_code_id or tax.parent_id and
tax.parent_id.base_code_id or False)
if not base_code:
raise except_orm(
_('Error'),
_('No base code found for tax code %s') % tax_code.name)
# check if every tax is linked to the same tax code and base code
for tax in taxes:
test_base_code = (
tax.base_code_id or tax.parent_id and
tax.parent_id.base_code_id or False)
if test_base_code.id != base_code.id:
raise except_orm(
_('Error'),
_('Not every tax linked to tax code %s is linked to '
'the same base code')
% tax_code.name)
if tax_code.sum_period or base_code.sum_period:
tax_vals = tax_code_model._compute_tax_amount(
tax, tax_code, base_code)
res.update(tax_vals)

for child_code in tax_code.child_ids:
res = self._build_codes_dict(
child_code, res=res)

return res

@api.model
def _get_tax_codes_amounts(self, period_id, tax_code_ids=None):
if tax_code_ids is None:
tax_code_ids = []
res = {}
code_model = self.env['account.tax.code']
context = self.env.context.copy()
context['period_id'] = period_id
for tax_code in code_model.with_context(context).browse(tax_code_ids):
res = self.with_context(context)._build_codes_dict(
tax_code, res=res)
return res


class AccountTax(models.Model):
_inherit = 'account.tax'
Expand Down

0 comments on commit 5c20a72

Please sign in to comment.