fix report withholding add option group tax or detailed

This commit is contained in:
wilsongomez 2022-04-08 17:56:32 -05:00
parent 569a3751d8
commit 1476535a91
4 changed files with 697 additions and 452 deletions

View File

@ -5,6 +5,7 @@ from datetime import date
from timeit import default_timer as timer
import operator
from sql import Null
from sql.aggregate import Sum
from sql.conditionals import Coalesce
from collections import OrderedDict
@ -1308,6 +1309,7 @@ class PartyWithholdingStart(ModelView):
('start_date', '>=', (Eval('start_period'), 'start_date'))
],
depends=['fiscalyear', 'start_period'])
detailed = fields.Boolean('Detailed', help='Detailed the registers when applied tax')
@staticmethod
def default_company():
@ -1350,6 +1352,7 @@ class PrintPartyWithholding(Wizard):
'classification': self.start.classification,
'start_period': start_period,
'end_period': end_period,
'detailed': self.start.detailed,
}
return action, data
@ -1360,9 +1363,13 @@ class PartyWithholding(Report):
@classmethod
def get_context(cls, records, header, data):
report_context = super().get_context(records, header, data)
cursor = Transaction().connection.cursor()
pool = Pool()
InvoiceTax = pool.get('account.invoice.tax')
# InvoiceTax = pool.get('account.invoice.tax')
Tax = pool.get('account.tax')
MoveLine = pool.get('account.move.line')
Move = pool.get('account.move')
Account = pool.get('account.account')
Company = pool.get('company.company')
Period = pool.get('account.period')
Fiscalyear = pool.get('account.fiscalyear')
@ -1370,13 +1377,16 @@ class PartyWithholding(Report):
company = Company(data['company'])
move = Move.__table__()
line = MoveLine.__table__()
tax = Tax.__table__()
account = Account.__table__()
where = tax.classification != Null
if data['party']:
party_dom = [
('id', '=', data['party']),
]
where &= line.party == data['party']
else:
party_dom = []
parties = Party.search(party_dom)
where &= line.party != null
dom_periods = [
('fiscalyear', '=', data['fiscalyear']),
@ -1391,43 +1401,64 @@ class PartyWithholding(Report):
periods = Period.search(dom_periods, order=[('start_date', 'ASC')])
period_ids = [p.id for p in periods]
if data['classification'] is None:
dom_taxes = []
else:
dom_taxes = [('classification', '=', data['classification'])]
taxes = Tax.search(dom_taxes)
tax_ids = [t.id for t in taxes]
records = []
for party in parties:
invoice_taxes = InvoiceTax.search([
('invoice.move.period', 'in', period_ids),
('invoice.party', '=', party.id),
('invoice.state', 'in', ['posted', 'paid']),
('tax', 'in', tax_ids),
], order=[('create_date', 'ASC')])
if not invoice_taxes:
continue
group_taxes = {}
for it in invoice_taxes:
if not it.tax.classification:
continue
key_id = it.tax.classification + str(party.id)
if key_id not in group_taxes.keys():
group_taxes[key_id] = {
'party': party,
'tax_type': it.tax.classification,
'taxes_with': [],
'total_amount': _ZERO,
'total_untaxed': _ZERO,
}
group_taxes[key_id]['taxes_with'].append(it)
group_taxes[key_id]['total_amount'] += (it.amount) * (-1)
group_taxes[key_id]['total_untaxed'] += it.base
records.extend(group_taxes.values())
if not records:
raise UserError(gettext('account_col.msg_report_missing_information'))
report_context['records'] = records
where &= move.period.in_(period_ids)
if data['classification']:
where &= tax.classification == data['classification']
if data['detailed']:
columns= [
line.party,
tax.classification,
tax.id.as_('tax_id'),
tax.rate,
account.name.as_('account'),
tax.description,
move.date,
line.id.as_('line_id'),
(line.debit-line.credit).as_('amount'),
line.move
]
query = tax.join(line, condition=((tax.credit_note_account==line.account) or (tax.invoice_account==line.account))
).join(move, condition=line.move == move.id
).join(account, condition=line.account == account.id
).select(*columns,
where=where,
order_by=[line.party, tax.classification, line.create_date.asc])
records, move_ids = cls.query_to_dict_detailed(query)
moves = Move.search_read([('id', 'in', move_ids)], fields_names=['origin.rec_name'])
moves_ = {}
for v in moves:
try:
moves_[v['id']]= v['origin.']['rec_name']
except:
moves_[v['id']]=None
report_context['moves'] = moves_
else:
columns= [
line.party,
tax.classification,
tax.id.as_('tax_id'),
tax.rate,
account.name.as_('account'),
tax.description,
Sum(line.debit-line.credit).as_('amount'),
]
query = tax.join(line, condition=((tax.credit_note_account==line.account) or (tax.invoice_account==line.account))
).join(move, condition=line.move == move.id
).join(account, condition=line.account == account.id
).select(*columns,
where=where,
group_by=[line.party, tax.classification, tax.id, tax.rate, account.name, tax.description],
order_by=[line.party, tax.classification])
records = cls.query_to_dict(query)
# move.origin.name,
report_context['records'] = records.values()
report_context['detailed'] = data['detailed']
report_context['fiscalyear'] = Fiscalyear(data['fiscalyear'])
report_context['start_date'] = periods[0].start_date
report_context['end_date'] = periods[-1].end_date
@ -1435,6 +1466,67 @@ class PartyWithholding(Report):
report_context['company'] = company
return report_context
@classmethod
def query_to_dict_detailed(cls, query):
cursor = Transaction().connection.cursor()
Party = Pool().get('party.party')
cursor.execute(*query)
columns = list(cursor.description)
result = cursor.fetchall()
res_dict = {}
moves_ids = set()
for row in result:
row_dict = {}
key_id = str(row[0])+row[1]
for i, col in enumerate(columns):
row_dict[col.name] = row[i]
row_dict['base'] = row[8] / row[3] * (-1)
try:
res_dict[key_id]['taxes_with'].append(row_dict)
res_dict[key_id]['total_amount'] += row_dict['amount']
res_dict[key_id]['total_untaxed'] += row_dict['base']
moves_ids.add(row_dict['move'])
except:
res_dict[key_id] = {
'party': Party(row[0]),
'tax_type': row_dict['classification'],
'taxes_with': [row_dict],
'total_amount': row_dict['amount'],
'total_untaxed': row_dict['base']
}
moves_ids.add(row_dict['move'])
return res_dict, moves_ids
@classmethod
def query_to_dict(cls, query):
cursor = Transaction().connection.cursor()
Party = Pool().get('party.party')
cursor.execute(*query)
columns = list(cursor.description)
result = cursor.fetchall()
res_dict = {}
for row in result:
row_dict = {}
key_id = str(row[0])+row[1]
for i, col in enumerate(columns):
row_dict[col.name] = row[i]
row_dict['base'] = row[6] / row[3] * (-1)
try:
res_dict[key_id]['amount'] += row_dict['amount']
res_dict[key_id]['base'] += row_dict['base']
except:
res_dict[key_id] = {
'party': Party(row[0]),
'tax_type': row_dict['classification'],
'account': row_dict['account'],
'description': row_dict['description'],
'amount': row_dict['amount'],
'base': row_dict['base']
}
return res_dict
class AccountConfiguration(ModelSQL, ModelView):
__name__ = 'account.configuration'

View File

@ -729,5 +729,4 @@ class InvoiceTax(ModelSQL):
date=self.invoice.invoice_date,
)
ln.analytic_lines = [analytic_line]
print('ok ............')
return lines

File diff suppressed because it is too large Load Diff

View File

@ -14,4 +14,6 @@ this repository contains the full copyright notices and license terms. -->
<field name="party"/>
<label name="classification"/>
<field name="classification"/>
<label name="detailed"/>
<field name="detailed"/>
</form>