add option to filter trial balance by dates

This commit is contained in:
Wilson Gomez 2023-11-23 17:31:04 -05:00
parent 3b5bebf063
commit 59d5ec9ad2
3 changed files with 104 additions and 53 deletions

View File

@ -19,7 +19,7 @@ from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateView, Button, StateReport
from trytond.report import Report
from trytond.pyson import Eval, Bool, PYSONEncoder
from trytond.pyson import Eval, Bool, PYSONEncoder, If
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta
from trytond.exceptions import UserError
@ -470,7 +470,7 @@ class AuxiliaryBook(Report):
posted=data['posted'],
colgaap=data['colgaap']):
start_accounts = Account.browse(accounts)
end1 = timer()
id2start_account = {}
for account in start_accounts:
id2start_account[account.id] = account
@ -500,8 +500,6 @@ class AuxiliaryBook(Report):
colgaap=['colgaap']):
end_accounts = Account.browse(accounts)
end2 = timer()
delta2 = (end2 - end1)
id2end_account = {}
for account in end_accounts:
id2end_account[account.id] = account
@ -518,9 +516,6 @@ class AuxiliaryBook(Report):
colgaap=['colgaap']):
accounts = Account.browse([a for a in accounts_ids if a in accounts_])
end3 = timer()
delta3 = (end3 - end2)
account_id2lines = cls.lines(accounts,
list(set(end_periods).difference(set(start_periods))),
data['posted'], data['party'], data['reference'], data['colgaap'])
@ -537,11 +532,6 @@ class AuxiliaryBook(Report):
report_context['lines'] = lambda account_id: account_id2lines[account_id]
report_context['company'] = company
end4 = timer()
delta4 = (end4 - end3)
end = timer()
delta_total = (end - start)
return report_context
@classmethod
@ -2174,12 +2164,14 @@ class PrintTrialBalanceStart(ModelView):
('fiscalyear', '=', Eval('fiscalyear')),
('start_date', '<=', (Eval('end_period'), 'start_date'))
],
states={'invisible': Eval('by_dates', False)},
depends=['end_period', 'fiscalyear'])
end_period = fields.Many2One('account.period', 'End Period',
domain=[
('fiscalyear', '=', Eval('fiscalyear')),
('start_date', '>=', (Eval('start_period'), 'start_date'))
],
states={'invisible': Eval('by_dates', False)},
depends=['start_period', 'fiscalyear'])
company = fields.Many2One('company.company', 'Company', required=True)
posted = fields.Boolean('Posted Move', help='Show only posted move')
@ -2190,6 +2182,30 @@ class PrintTrialBalanceStart(ModelView):
help='Include the accounts of kind view')
accounts_with_balance = fields.Boolean('Accounts with Balance',
help='Show accounts with balances in previous periods')
by_dates = fields.Boolean('By Dates',
help='Filter accounts by dates')
from_date = fields.Date("From Date",
domain=[
If(Eval('to_date') & Eval('from_date'),
('from_date', '<=', Eval('to_date')),
()),
],
states={
'invisible': ~Eval('by_dates', False),
'required': Eval('by_dates', False)
},
depends=['to_date', 'start_period', 'end_period'])
to_date = fields.Date("To Date",
domain=[
If(Eval('from_date') & Eval('to_date'),
('to_date', '>=', Eval('from_date')),
()),
],
states={
'invisible': ~Eval('by_dates', False),
'required': Eval('by_dates', False)
},
depends=['from_date', 'start_period', 'end_period'])
@staticmethod
def default_fiscalyear():
@ -2209,11 +2225,21 @@ class PrintTrialBalanceStart(ModelView):
def default_empty_account():
return False
@staticmethod
def default_by_dates():
return False
@fields.depends('fiscalyear')
def on_change_fiscalyear(self):
self.start_period = None
self.end_period = None
@fields.depends('by_dates')
def on_change_by_dates(self):
if self.by_dates:
self.start_period = None
self.end_period = None
class PrintTrialBalance(Wizard):
'Print Trial Balance'
@ -2244,6 +2270,9 @@ class PrintTrialBalance(Wizard):
'empty_account': self.start.empty_account,
'accounts_with_balance': self.start.accounts_with_balance,
'detailed': self.start.detailed,
'from_date': self.start.from_date,
'to_date': self.start.to_date,
'by_dates': self.start.by_dates
}
return action, data
@ -2291,71 +2320,86 @@ class TrialBalanceClassic(Report):
accounts = Account.search(dom_accounts)
start_periods = []
if data['start_period']:
start_period = Period(data['start_period'])
start_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
('end_date', '<=', start_period.start_date),
])
else:
fiscalyear = Fiscalyear(data['fiscalyear'])
start_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
('end_date', '<=', fiscalyear.start_date),
])
if data['end_period']:
end_period = Period(data['end_period'])
end_periods = Period.search([
start_period_ids, end_period_ids = None, None
from_date, to_date = None, None
if not data['by_dates']:
start_periods = []
if data['start_period']:
start_period = Period(data['start_period'])
start_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
('end_date', '<=', end_period.start_date),
])
end_periods = list(set(end_periods).difference(
set(start_periods)))
if end_period not in end_periods:
end_periods.append(end_period)
else:
end_periods = Period.search([
('end_date', '<=', start_period.start_date),
])
else:
fiscalyear = Fiscalyear(data['fiscalyear'])
start_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
])
end_periods = list(set(end_periods).difference(
set(start_periods)))
('end_date', '<=', fiscalyear.start_date),
])
start_period_ids = [p.id for p in start_periods] or [0]
end_period_ids = [p.id for p in end_periods]
if data['end_period']:
end_period = Period(data['end_period'])
end_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
('end_date', '<=', end_period.start_date),
])
end_periods = list(set(end_periods).difference(
set(start_periods)))
if end_period not in end_periods:
end_periods.append(end_period)
else:
end_periods = Period.search([
('fiscalyear', '=', data['fiscalyear']),
])
end_periods = list(set(end_periods).difference(
set(start_periods)))
start_period_ids = [p.id for p in start_periods] or [0]
end_period_ids = [p.id for p in end_periods]
from_date_ = start_period.start_date
else:
from_date = data['from_date']
to_date = data['to_date']
to_date_ = (from_date or from_date_) - timedelta(days=1)
print(to_date_, 'fhfjd')
with Transaction().set_context(
fiscalyear=data['fiscalyear'],
periods=start_period_ids,
date=to_date_,
posted=data['posted'],
colgaap=data['colgaap']
):
start_accounts = Account.browse(accounts)
if types:
to_date = start_period.start_date - timedelta(days=1)
with Transaction().set_context(to_date=to_date):
with Transaction().set_context(to_date=to_date_):
start_types = Type.browse(types)
with Transaction().set_context(
fiscalyear=None,
periods=end_period_ids,
from_date=from_date,
to_date=to_date,
posted=data['posted'],
colgaap=data['colgaap']):
in_accounts = Account.browse(accounts)
if types:
in_types = Type.browse(types)
all_periods = None
if not data['by_dates']:
all_periods = start_period_ids + end_period_ids
with Transaction().set_context(
fiscalyear=data['fiscalyear'],
periods=start_period_ids + end_period_ids,
periods=all_periods,
to_date=to_date,
posted=data['posted'],
colgaap=data['colgaap']):
end_accounts = Account.browse(accounts)
if types:
to_date = end_period.end_date
to_date = to_date or end_period.end_date
with Transaction().set_context(to_date=to_date):
end_types = Type.browse(types)
@ -2378,12 +2422,13 @@ class TrialBalanceClassic(Report):
start_accounts, in_accounts, end_accounts,
start_types, in_types, end_types)
periods = end_periods
if not data['by_dates']:
periods = end_periods
periods.sort(key=operator.attrgetter('start_date'))
report_context['start_period'] = periods[0]
periods.sort(key=operator.attrgetter('end_date'))
report_context['end_period'] = periods[-1]
report_context['accounts'] = accounts
periods.sort(key=operator.attrgetter('start_date'))
report_context['start_period'] = periods[0]
periods.sort(key=operator.attrgetter('end_date'))
report_context['end_period'] = periods[-1]
report_context['company'] = company
report_context['digits'] = company.currency.digits
report_context['sumto'] = lambda accounts, field: cls.sumto(accounts, field)
@ -2424,7 +2469,7 @@ class TrialBalanceClassic(Report):
dict_accounts[acc.parent]['end_balance'].extend(end_balance)
if acc.parent.parent and acc.parent.code:
sum_amount_to_parent(acc.parent)
except:
except Exception:
pass
if start_account.type and start_account.parent \

Binary file not shown.

View File

@ -10,9 +10,13 @@ this repository contains the full copyright notices and license terms. -->
<field name="start_period" widget="selection"/>
<label name="end_period"/>
<field name="end_period" widget="selection"/>
<label name="from_date"/>
<field name="from_date"/>
<label name="to_date"/>
<field name="to_date"/>
<label name="company"/>
<field name="company"/>
<group colspan="4" id="checkboxes" col="8">
<group colspan="4" id="checkboxes" col="10">
<label name="posted"/>
<field name="posted" xexpand="0" width="25"/>
<label name="accounts_with_balance"/>
@ -21,5 +25,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="detailed" xexpand="0" width="25"/>
<label name="colgaap"/>
<field name="colgaap" xexpand="0" width="25"/>
<label name="by_dates"/>
<field name="by_dates" xexpand="0" width="25"/>
</group>
</form>