trytondo-analytic_account_co/analytic_account_report.py

194 lines
7.4 KiB
Python
Raw Normal View History

2021-03-25 01:00:07 +01:00
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.model import ModelView, fields
from trytond.wizard import Wizard, StateView, Button, StateReport
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.report import Report
from trytond.pool import Pool
def compute_report(data):
pool = Pool()
Analytic_Account = pool.get('analytic_account.account')
Analytic_Account_Line = pool.get('analytic_account.line')
res = {}
ctx = {
'company': data['company']
}
def is_child(child, parent):
if child in parent.childs:
return True
else:
for parent_child in parent.childs:
if child in parent_child.childs:
return True
else:
2024-07-19 07:36:47 +02:00
if is_child(child, parent_child):
2021-03-25 01:00:07 +01:00
return True
return False
2024-07-19 07:36:47 +02:00
2021-03-25 01:00:07 +01:00
with Transaction().set_context(ctx):
2024-07-19 07:36:47 +02:00
analytic_accounts_filter = Analytic_Account.search([
('parent', '!=', None),
('parent.type', '=', 'root'),
('id', 'in', data['analytic_account']),
], order=[('code', 'ASC')])
2021-03-25 01:00:07 +01:00
2024-07-19 07:36:47 +02:00
analytic_accounts = Analytic_Account.search(
[], order=[('code', 'ASC')])
2021-03-25 01:00:07 +01:00
aas = {}
total = 0
2024-07-19 07:36:47 +02:00
2021-03-25 01:00:07 +01:00
for aaf in analytic_accounts_filter:
aas[aaf.code] = {}
2024-07-19 07:36:47 +02:00
aas[aaf.code][aaf.code] = [aaf, 0, 0, 0, False]
2021-03-25 01:00:07 +01:00
for aa in analytic_accounts:
record = []
if is_child(aa, aaf):
record.append(aa)
2024-07-19 07:36:47 +02:00
record = record + [0, 0, 0]
2021-03-25 01:00:07 +01:00
if aa.type == 'normal':
aa_line = Analytic_Account_Line.search([
('account', '=', aa),
('date', '>=', data['start_date']),
('date', '<=', data['end_date']),
], order=[('date', 'ASC')])
lines = {}
2024-07-19 07:36:47 +02:00
2021-03-25 01:00:07 +01:00
for a_l in aa_line:
if a_l.move_line.account.code in lines:
2024-07-19 07:36:47 +02:00
lines[a_l.move_line.account.code][1] += \
a_l.debit
lines[a_l.move_line.account.code][2] += \
a_l.credit
lines[a_l.move_line.account.code][3].append(
a_l.move_line)
2021-03-25 01:00:07 +01:00
else:
2024-07-19 07:36:47 +02:00
move_line = [a_l.move_line]
2021-03-25 01:00:07 +01:00
lines[a_l.move_line.account.code] = [
a_l.move_line.account.name,
a_l.debit,
a_l.credit,
move_line
]
record[1] += a_l.debit
2021-03-30 17:08:36 +02:00
record[2] += a_l.credit * -(1)
2021-03-25 01:00:07 +01:00
record[3] += a_l.debit - a_l.credit
total += a_l.debit - a_l.credit
2024-07-19 07:36:47 +02:00
2021-03-25 01:00:07 +01:00
def is_parent(parent, account, debit, credit):
2024-07-19 07:36:47 +02:00
if parent and account and parent.code in aas[
aaf.code]:
aas[aaf.code][parent.code][3] +=\
debit - credit
2021-03-25 01:00:07 +01:00
if parent.parent and parent.parent.code:
2024-07-19 07:36:47 +02:00
is_parent(
parent.parent, account, debit, credit)
2021-03-25 01:00:07 +01:00
is_parent(aa.parent, aa, a_l.debit, a_l.credit)
record.append(lines)
aas[aaf.code][aa.code] = record
res['record'] = aas
res['total'] = -total
2024-07-19 07:36:47 +02:00
res['root'] = Analytic_Account.search([
2021-03-25 01:00:07 +01:00
('id', '=', data['analytic_account_root'])
2024-07-19 07:36:47 +02:00
])
2021-03-25 01:00:07 +01:00
res['start_date'] = data['start_date']
res['end_date'] = data['end_date']
res['detailed'] = data['detailed']
res['account_move'] = data['account_move']
res['without_balance'] = data['without_balance']
return res
class PrintAnalyticAccountStart(ModelView):
'Print Analytic Account'
__name__ = 'analytic_account_co.print_analytic_account.start'
company = fields.Many2One('company.company', 'Company', required=True)
start_date = fields.Date('Start Date', required=True)
end_date = fields.Date('End Date', required=True)
detailed = fields.Boolean('Detailed')
account_move = fields.Boolean('Account Move',
states={'invisible': ~Eval('detailed'),
},
depends=['detailed'],
help='Show account move detailed')
without_balance = fields.Boolean('Without Balance')
2024-07-19 07:36:47 +02:00
analytic_account_root = fields.Many2One(
'analytic_account.account',
'Analytic Account Root', required=True,
domain=[('type', '=', 'root')]
)
analytic_account = fields.Many2Many(
'analytic_account.account',
None, None, 'Analytic Account',
domain=[('parent', '=', Eval('analytic_account_root')),
('type', '!=', 'root')],
depends=['analytic_account_root']
)
2021-03-25 01:00:07 +01:00
@staticmethod
def default_company():
return Transaction().context.get('company')
class PrintAnalyticAccount(Wizard):
'Print Analytic Accoount'
__name__ = 'analytic_account_co.print_analytic_account'
start = StateView('analytic_account_co.print_analytic_account.start',
'analytic_account_co.print_analytic_account_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Print', 'print_', 'tryton-print', default=True),
])
print_ = StateReport('analytic_account_co.analytic_account')
def do_print_(self, action):
Analytic_Account = Pool().get('analytic_account.account')
if self.start.analytic_account:
2024-07-19 07:36:47 +02:00
analytic_account_ids = [
ac.id for ac in self.start.analytic_account]
2021-03-25 01:00:07 +01:00
else:
2024-07-19 07:36:47 +02:00
analytic_account = Analytic_Account.search([
('parent.type', '=', 'root'),
('parent', '=', self.start.analytic_account_root)])
2021-03-25 01:00:07 +01:00
analytic_account_ids = [ac.id for ac in analytic_account]
data = {
'start_date': self.start.start_date,
'end_date': self.start.end_date,
'company': self.start.company.id,
'detailed': self.start.detailed,
'account_move': self.start.account_move,
'without_balance': self.start.without_balance,
'analytic_account_root': self.start.analytic_account_root.id,
'analytic_account': analytic_account_ids
}
return action, data
def transition_print_(self):
return 'end'
class AnalyticAccount(Report):
__name__ = 'analytic_account_co.analytic_account'
@classmethod
def get_context(cls, records, data):
report_context = super(AnalyticAccount, cls).get_context(records, data)
pool = Pool()
Company = pool.get('company.company')
company = Company(data['company'])
res = compute_report(data)
report_context.update(res)
report_context['company'] = company
return report_context