refactoring of the unaffected earnings reporting in the general ledger
and trial balance.pull/481/head
parent
6110d0e78c
commit
19bc8116c0
|
@ -1287,167 +1287,9 @@ ORDER BY
|
||||||
query_inject_move_line_centralized_params
|
query_inject_move_line_centralized_params
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_unaffected_earnings_account_sub_subquery_sum_initial(
|
|
||||||
self
|
|
||||||
):
|
|
||||||
""" Return subquery used to compute sum amounts on
|
|
||||||
unaffected earnings accounts """
|
|
||||||
sub_subquery_sum_amounts = """
|
|
||||||
SELECT
|
|
||||||
SUM(ml.balance) AS initial_balance,
|
|
||||||
0.0 AS final_balance
|
|
||||||
FROM
|
|
||||||
account_account a
|
|
||||||
INNER JOIN
|
|
||||||
account_move_line ml
|
|
||||||
ON a.id = ml.account_id
|
|
||||||
AND ml.date < %(date_from)s
|
|
||||||
"""
|
|
||||||
if self.only_posted_moves:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
INNER JOIN
|
|
||||||
account_move m ON ml.move_id = m.id AND m.state = 'posted'
|
|
||||||
"""
|
|
||||||
if self.filter_cost_center_ids:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
INNER JOIN
|
|
||||||
account_analytic_account aa
|
|
||||||
ON
|
|
||||||
ml.analytic_account_id = aa.id
|
|
||||||
AND aa.id IN %(cost_center_ids)s
|
|
||||||
"""
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
WHERE
|
|
||||||
a.company_id = %(company_id)s
|
|
||||||
AND
|
|
||||||
a.id IN %(unaffected_earnings_account_ids)s
|
|
||||||
"""
|
|
||||||
if self.filter_journal_ids:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
AND
|
|
||||||
ml.journal_id in %(filter_journal_ids)s
|
|
||||||
"""
|
|
||||||
return sub_subquery_sum_amounts
|
|
||||||
|
|
||||||
def _get_unaffected_earnings_account_sub_subquery_sum_final(self):
|
|
||||||
""" Return subquery used to compute sum amounts on
|
|
||||||
unaffected earnings accounts """
|
|
||||||
|
|
||||||
sub_subquery_sum_amounts = """
|
|
||||||
SELECT
|
|
||||||
0.0 AS initial_balance,
|
|
||||||
SUM(ml.balance) AS final_balance
|
|
||||||
FROM
|
|
||||||
account_account a
|
|
||||||
INNER JOIN
|
|
||||||
account_move_line ml
|
|
||||||
ON a.id = ml.account_id
|
|
||||||
AND ml.date <= %(date_to)s
|
|
||||||
"""
|
|
||||||
if self.only_posted_moves:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
INNER JOIN
|
|
||||||
account_move m ON ml.move_id = m.id AND m.state = 'posted'
|
|
||||||
"""
|
|
||||||
if self.filter_cost_center_ids:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
INNER JOIN
|
|
||||||
account_analytic_account aa
|
|
||||||
ON
|
|
||||||
ml.analytic_account_id = aa.id
|
|
||||||
AND aa.id IN %(cost_center_ids)s
|
|
||||||
"""
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
WHERE
|
|
||||||
a.company_id = %(company_id)s
|
|
||||||
AND
|
|
||||||
a.id IN %(unaffected_earnings_account_ids)s
|
|
||||||
"""
|
|
||||||
if self.filter_journal_ids:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
AND
|
|
||||||
ml.journal_id in %(filter_journal_ids)s
|
|
||||||
"""
|
|
||||||
return sub_subquery_sum_amounts
|
|
||||||
|
|
||||||
def _inject_unaffected_earnings_account_values(self):
|
def _inject_unaffected_earnings_account_values(self):
|
||||||
"""Inject the report values of the unaffected earnings account
|
"""Inject the report values of the unaffected earnings account
|
||||||
for report_general_ledger_account."""
|
for report_general_ledger_account."""
|
||||||
subquery_sum_amounts = """
|
|
||||||
SELECT
|
|
||||||
SUM(COALESCE(sub.initial_balance, 0.0)) AS initial_balance,
|
|
||||||
SUM(COALESCE(sub.final_balance, 0.0)) AS final_balance
|
|
||||||
FROM
|
|
||||||
(
|
|
||||||
"""
|
|
||||||
# Initial balances
|
|
||||||
subquery_sum_amounts += \
|
|
||||||
self._get_unaffected_earnings_account_sub_subquery_sum_initial()
|
|
||||||
subquery_sum_amounts += """
|
|
||||||
UNION
|
|
||||||
"""
|
|
||||||
subquery_sum_amounts += \
|
|
||||||
self._get_unaffected_earnings_account_sub_subquery_sum_final()
|
|
||||||
subquery_sum_amounts += """
|
|
||||||
) sub
|
|
||||||
"""
|
|
||||||
|
|
||||||
# pylint: disable=sql-injection
|
|
||||||
query_inject_account = """
|
|
||||||
WITH
|
|
||||||
sum_amounts AS ( """ + subquery_sum_amounts + """ )
|
|
||||||
INSERT INTO
|
|
||||||
report_general_ledger_account
|
|
||||||
(
|
|
||||||
report_id,
|
|
||||||
create_uid,
|
|
||||||
create_date,
|
|
||||||
account_id,
|
|
||||||
code,
|
|
||||||
name,
|
|
||||||
is_partner_account,
|
|
||||||
initial_balance,
|
|
||||||
final_balance,
|
|
||||||
currency_id
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
%(report_id)s AS report_id,
|
|
||||||
%(user_id)s AS create_uid,
|
|
||||||
NOW() AS create_date,
|
|
||||||
a.id AS account_id,
|
|
||||||
a.code,
|
|
||||||
a.name,
|
|
||||||
False AS is_partner_account,
|
|
||||||
COALESCE(i.initial_balance, 0.0) AS initial_balance,
|
|
||||||
COALESCE(i.final_balance, 0.0) AS final_balance,
|
|
||||||
c.id as currency_id
|
|
||||||
FROM
|
|
||||||
account_account a
|
|
||||||
LEFT JOIN
|
|
||||||
res_currency c ON c.id = a.currency_id,
|
|
||||||
sum_amounts i
|
|
||||||
WHERE
|
|
||||||
a.company_id = %(company_id)s
|
|
||||||
AND a.id = %(unaffected_earnings_account_id)s
|
|
||||||
"""
|
|
||||||
query_inject_account_params = {
|
|
||||||
'date_from': self.date_from,
|
|
||||||
'date_to': self.date_to,
|
|
||||||
'fy_start_date': self.fy_start_date,
|
|
||||||
}
|
|
||||||
if self.filter_cost_center_ids:
|
|
||||||
query_inject_account_params['cost_center_ids'] = \
|
|
||||||
tuple(self.filter_cost_center_ids.ids)
|
|
||||||
query_inject_account_params['company_id'] = self.company_id.id
|
|
||||||
query_inject_account_params['unaffected_earnings_account_id'] = \
|
|
||||||
self.unaffected_earnings_account.id
|
|
||||||
query_inject_account_params['report_id'] = self.id
|
|
||||||
query_inject_account_params['user_id'] = self.env.uid
|
|
||||||
|
|
||||||
if self.filter_journal_ids:
|
|
||||||
query_inject_account_params['filter_journal_ids'] = (tuple(
|
|
||||||
self.filter_journal_ids.ids,
|
|
||||||
),)
|
|
||||||
# Fetch the profit and loss accounts
|
# Fetch the profit and loss accounts
|
||||||
query_unaffected_earnings_account_ids = """
|
query_unaffected_earnings_account_ids = """
|
||||||
SELECT a.id
|
SELECT a.id
|
||||||
|
@ -1458,7 +1300,155 @@ ORDER BY
|
||||||
"""
|
"""
|
||||||
self.env.cr.execute(query_unaffected_earnings_account_ids)
|
self.env.cr.execute(query_unaffected_earnings_account_ids)
|
||||||
pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
|
pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
|
||||||
query_inject_account_params['unaffected_earnings_account_ids'] = \
|
unaffected_earnings_account_ids = \
|
||||||
tuple(pl_account_ids + [self.unaffected_earnings_account.id])
|
pl_account_ids + [self.unaffected_earnings_account.id]
|
||||||
|
# Fetch the current fiscal year start date
|
||||||
|
date = fields.Datetime.from_string(self.date_from)
|
||||||
|
res = self.company_id.compute_fiscalyear_dates(date)
|
||||||
|
fy_start_date = res['date_from']
|
||||||
|
query_select_previous_fy_unaffected_earnings_params = {
|
||||||
|
'date_to': fy_start_date,
|
||||||
|
'company_id': self.company_id.id,
|
||||||
|
'account_ids': tuple(unaffected_earnings_account_ids),
|
||||||
|
}
|
||||||
|
query_select_previous_fy_unaffected_earnings = """
|
||||||
|
SELECT sum(aml.balance) as balance
|
||||||
|
FROM account_move_line as aml
|
||||||
|
INNER JOIN account_move as am
|
||||||
|
ON am.id = aml.move_id
|
||||||
|
INNER JOIN account_journal j
|
||||||
|
ON am.journal_id = j.id
|
||||||
|
"""
|
||||||
|
if self.filter_cost_center_ids:
|
||||||
|
query_select_previous_fy_unaffected_earnings += """
|
||||||
|
INNER JOIN account_analytic_account aa
|
||||||
|
ON aml.analytic_account_id = aa.id
|
||||||
|
AND aa.id IN %(cost_center_ids)s
|
||||||
|
"""
|
||||||
|
query_select_previous_fy_unaffected_earnings_params[
|
||||||
|
'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
|
||||||
|
|
||||||
|
query_select_previous_fy_unaffected_earnings += """
|
||||||
|
WHERE aml.date < %(date_to)s
|
||||||
|
AND aml.company_id = %(company_id)s
|
||||||
|
AND aml.account_id IN %(account_ids)s
|
||||||
|
"""
|
||||||
|
if self.filter_journal_ids:
|
||||||
|
query_select_previous_fy_unaffected_earnings += """
|
||||||
|
AND j.id IN %(journal_ids)s
|
||||||
|
"""
|
||||||
|
query_select_previous_fy_unaffected_earnings_params[
|
||||||
|
'journal_ids'] = tuple(self.filter_journal_ids.ids)
|
||||||
|
if self.only_posted_moves:
|
||||||
|
query_select_previous_fy_unaffected_earnings += """
|
||||||
|
AND am.state = 'posted'
|
||||||
|
"""
|
||||||
|
self.env.cr.execute(
|
||||||
|
query_select_previous_fy_unaffected_earnings,
|
||||||
|
query_select_previous_fy_unaffected_earnings_params)
|
||||||
|
res = self.env.cr.fetchone()
|
||||||
|
unaffected_earnings_initial_balance = res[0] or 0.0
|
||||||
|
# Now select the current period unaffected earnings,
|
||||||
|
# excluding the current period P&L.
|
||||||
|
query_select_period_unaffected_earnings_params = {
|
||||||
|
'date_from': self.date_from,
|
||||||
|
'date_to': self.date_to,
|
||||||
|
'company_id': self.company_id.id,
|
||||||
|
'unaffected_earnings_id': self.unaffected_earnings_account.id,
|
||||||
|
}
|
||||||
|
query_select_period_unaffected_earnings = """
|
||||||
|
SELECT
|
||||||
|
sum(aml.debit) as sum_debit,
|
||||||
|
sum(aml.credit) as sum_credit,
|
||||||
|
sum(aml.balance) as balance
|
||||||
|
FROM account_move_line as aml
|
||||||
|
INNER JOIN account_move as am
|
||||||
|
ON am.id = aml.move_id
|
||||||
|
INNER JOIN account_journal j
|
||||||
|
ON am.journal_id = j.id
|
||||||
|
"""
|
||||||
|
if self.filter_cost_center_ids:
|
||||||
|
query_select_period_unaffected_earnings += """
|
||||||
|
INNER JOIN account_analytic_account aa
|
||||||
|
ON aml.analytic_account_id = aa.id
|
||||||
|
AND aa.id IN %(cost_center_ids)s
|
||||||
|
"""
|
||||||
|
query_select_period_unaffected_earnings_params[
|
||||||
|
'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
|
||||||
|
query_select_period_unaffected_earnings += """
|
||||||
|
WHERE am.date >= %(date_from)s
|
||||||
|
AND aml.date <= %(date_to)s
|
||||||
|
AND aml.company_id = %(company_id)s
|
||||||
|
AND aml.account_id = %(unaffected_earnings_id)s
|
||||||
|
"""
|
||||||
|
if self.filter_journal_ids:
|
||||||
|
query_select_period_unaffected_earnings += """
|
||||||
|
AND j.id IN %(journal_ids)s
|
||||||
|
"""
|
||||||
|
query_select_period_unaffected_earnings_params[
|
||||||
|
'journal_ids'] = tuple(self.filter_journal_ids.ids)
|
||||||
|
if self.only_posted_moves:
|
||||||
|
query_select_period_unaffected_earnings += """
|
||||||
|
AND am.state = 'posted'
|
||||||
|
"""
|
||||||
|
self.env.cr.execute(query_select_period_unaffected_earnings,
|
||||||
|
query_select_period_unaffected_earnings_params)
|
||||||
|
res = self.env.cr.fetchone()
|
||||||
|
unaffected_earnings_period_debit = res[0] or 0.0
|
||||||
|
unaffected_earnings_period_credit = res[1] or 0.0
|
||||||
|
unaffected_earnings_period_balance = res[2] or 0.0
|
||||||
|
# pylint: disable=sql-injection
|
||||||
|
query_inject_account = """
|
||||||
|
INSERT INTO
|
||||||
|
report_general_ledger_account (
|
||||||
|
report_id,
|
||||||
|
create_uid,
|
||||||
|
create_date,
|
||||||
|
account_id,
|
||||||
|
code,
|
||||||
|
name,
|
||||||
|
is_partner_account,
|
||||||
|
initial_debit,
|
||||||
|
initial_credit,
|
||||||
|
initial_balance,
|
||||||
|
final_debit,
|
||||||
|
final_credit,
|
||||||
|
final_balance
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
%(report_id)s,
|
||||||
|
%(user_id)s,
|
||||||
|
NOW(),
|
||||||
|
%(account_id)s,
|
||||||
|
%(code)s,
|
||||||
|
%(name)s,
|
||||||
|
False,
|
||||||
|
%(initial_debit)s,
|
||||||
|
%(initial_credit)s,
|
||||||
|
%(initial_balance)s,
|
||||||
|
%(final_debit)s,
|
||||||
|
%(final_credit)s,
|
||||||
|
%(final_balance)s
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
initial_debit = unaffected_earnings_initial_balance >= 0 and \
|
||||||
|
unaffected_earnings_initial_balance or 0
|
||||||
|
initial_credit = unaffected_earnings_initial_balance < 0 and \
|
||||||
|
-1 * unaffected_earnings_initial_balance or 0
|
||||||
|
final_balance = unaffected_earnings_initial_balance + \
|
||||||
|
unaffected_earnings_period_balance
|
||||||
|
query_inject_account_params = {
|
||||||
|
'report_id': self.id,
|
||||||
|
'user_id': self.env.uid,
|
||||||
|
'account_id': self.unaffected_earnings_account.id,
|
||||||
|
'code': self.unaffected_earnings_account.code,
|
||||||
|
'name': self.unaffected_earnings_account.name,
|
||||||
|
'initial_debit': initial_debit,
|
||||||
|
'initial_credit': initial_credit,
|
||||||
|
'initial_balance': unaffected_earnings_initial_balance,
|
||||||
|
'final_debit': initial_debit + unaffected_earnings_period_debit,
|
||||||
|
'final_credit': initial_credit + unaffected_earnings_period_credit,
|
||||||
|
'final_balance': final_balance,
|
||||||
|
}
|
||||||
self.env.cr.execute(query_inject_account,
|
self.env.cr.execute(query_inject_account,
|
||||||
query_inject_account_params)
|
query_inject_account_params)
|
||||||
|
|
|
@ -99,15 +99,8 @@
|
||||||
</div>
|
</div>
|
||||||
</t>
|
</t>
|
||||||
</t>
|
</t>
|
||||||
<!-- Display filters -->
|
|
||||||
<t t-call="account_financial_report.report_trial_balance_footer"/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template id="account_financial_report.report_trial_balance_footer">
|
|
||||||
(*) This report applies the current year Profit and Loss balances to the Undistributed Profit/Loss account.
|
|
||||||
To ensure that the Trial Balance totals total to zero, this line represents the reversal of the current year P & L Balance.
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<template id="account_financial_report.report_trial_balance_filters">
|
<template id="account_financial_report.report_trial_balance_filters">
|
||||||
<div class="act_as_table data_table" style="width: 100%;">
|
<div class="act_as_table data_table" style="width: 100%;">
|
||||||
<div class="act_as_row labels">
|
<div class="act_as_row labels">
|
||||||
|
|
|
@ -308,182 +308,6 @@ WHERE
|
||||||
)
|
)
|
||||||
self.env.cr.execute(query_inject_account, query_inject_account_params)
|
self.env.cr.execute(query_inject_account, query_inject_account_params)
|
||||||
|
|
||||||
# Inject current period debits and credits for the unaffected earnings
|
|
||||||
# account.
|
|
||||||
account_type = self.env.ref('account.data_unaffected_earnings')
|
|
||||||
unaffected_earnings_account = self.env['account.account'].search(
|
|
||||||
[
|
|
||||||
('user_type_id', '=', account_type.id),
|
|
||||||
('company_id', '=', self.company_id.id)
|
|
||||||
])
|
|
||||||
|
|
||||||
if self.filter_account_ids and unaffected_earnings_account not in \
|
|
||||||
self.filter_account_ids:
|
|
||||||
return True
|
|
||||||
|
|
||||||
query_unaffected_earnings_account_ids = """
|
|
||||||
SELECT a.id
|
|
||||||
FROM account_account as a
|
|
||||||
INNER JOIN account_account_type as at
|
|
||||||
ON at.id = a.user_type_id
|
|
||||||
WHERE at.include_initial_balance = FALSE
|
|
||||||
"""
|
|
||||||
self.env.cr.execute(query_unaffected_earnings_account_ids)
|
|
||||||
pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
|
|
||||||
unaffected_earnings_account_ids = pl_account_ids + [
|
|
||||||
unaffected_earnings_account.id]
|
|
||||||
query_select_period_balances = """
|
|
||||||
SELECT sum(aml.debit) as sum_debit,
|
|
||||||
sum(aml.credit) as sum_credit
|
|
||||||
FROM account_move_line as aml
|
|
||||||
INNER JOIN account_move as am
|
|
||||||
ON am.id = aml.move_id
|
|
||||||
WHERE aml.date >= %(date_from)s
|
|
||||||
AND aml.date <= %(date_to)s
|
|
||||||
AND aml.company_id = %(company_id)s
|
|
||||||
AND aml.account_id IN %(account_ids)s
|
|
||||||
"""
|
|
||||||
if self.only_posted_moves:
|
|
||||||
query_select_period_balances += """
|
|
||||||
AND am.state = 'posted'
|
|
||||||
"""
|
|
||||||
query_select_period_balances_params = {
|
|
||||||
'date_from': self.date_from,
|
|
||||||
'date_to': self.date_to,
|
|
||||||
'company_id': self.company_id.id,
|
|
||||||
'account_ids': tuple(unaffected_earnings_account_ids)
|
|
||||||
}
|
|
||||||
self.env.cr.execute(query_select_period_balances,
|
|
||||||
query_select_period_balances_params)
|
|
||||||
sum_debit, sum_credit = self.env.cr.fetchone()
|
|
||||||
query_update_unaffected_earnings_account = """
|
|
||||||
UPDATE report_trial_balance_account
|
|
||||||
SET
|
|
||||||
name = %(unaffected_earnings_account_name)s,
|
|
||||||
debit = %(sum_debit)s,
|
|
||||||
credit = %(sum_credit)s
|
|
||||||
WHERE account_id = %(unaffected_earning_account_id)s
|
|
||||||
"""
|
|
||||||
query_update_unaffected_earnings_account_params = {
|
|
||||||
'sum_debit': sum_debit,
|
|
||||||
'sum_credit': sum_credit,
|
|
||||||
'unaffected_earning_account_id': unaffected_earnings_account.id,
|
|
||||||
'unaffected_earnings_account_name':
|
|
||||||
unaffected_earnings_account.name,
|
|
||||||
}
|
|
||||||
self.env.cr.execute(query_update_unaffected_earnings_account,
|
|
||||||
query_update_unaffected_earnings_account_params)
|
|
||||||
# P&L allocated in the current fiscal year.
|
|
||||||
date = fields.Datetime.from_string(self.date_from)
|
|
||||||
res = self.company_id.compute_fiscalyear_dates(date)
|
|
||||||
fy_start_date = res['date_from']
|
|
||||||
# Fetch the initial balance
|
|
||||||
query_select_initial_pl_balance = """
|
|
||||||
SELECT
|
|
||||||
sum(aml.balance) as sum_balance
|
|
||||||
FROM
|
|
||||||
account_move_line as aml
|
|
||||||
INNER JOIN
|
|
||||||
account_move as am
|
|
||||||
ON am.id = aml.move_id
|
|
||||||
WHERE aml.date >= %(date_from)s
|
|
||||||
AND aml.date < %(date_to)s
|
|
||||||
AND aml.company_id = %(company_id)s
|
|
||||||
AND aml.account_id IN %(account_ids)s
|
|
||||||
"""
|
|
||||||
if self.only_posted_moves:
|
|
||||||
query_select_initial_pl_balance += """
|
|
||||||
AND am.state = 'posted'
|
|
||||||
"""
|
|
||||||
query_select_initial_pl_balance_params = {
|
|
||||||
'date_from': fy_start_date,
|
|
||||||
'date_to': self.date_from,
|
|
||||||
'company_id': self.company_id.id,
|
|
||||||
'account_ids': tuple(pl_account_ids),
|
|
||||||
}
|
|
||||||
self.env.cr.execute(query_select_initial_pl_balance,
|
|
||||||
query_select_initial_pl_balance_params)
|
|
||||||
res = self.env.cr.fetchone()
|
|
||||||
allocated_pl_initial_balance = res[0] or 0.0
|
|
||||||
# Fetch the period balance
|
|
||||||
query_select_period_pl_balance = """
|
|
||||||
SELECT
|
|
||||||
sum(aml.debit) as sum_debit,
|
|
||||||
sum(aml.credit) as sum_credit
|
|
||||||
FROM account_move_line as aml
|
|
||||||
INNER JOIN account_move as am
|
|
||||||
ON am.id = aml.move_id
|
|
||||||
WHERE am.date >= %(date_from)s
|
|
||||||
AND aml.date <= %(date_to)s
|
|
||||||
AND aml.company_id = %(company_id)s
|
|
||||||
AND aml.account_id IN %(account_ids)s
|
|
||||||
"""
|
|
||||||
if self.only_posted_moves:
|
|
||||||
query_select_period_pl_balance += """
|
|
||||||
AND am.state = 'posted'
|
|
||||||
"""
|
|
||||||
query_select_period_pl_balance_params = {
|
|
||||||
'date_from': self.date_from,
|
|
||||||
'date_to': self.date_to,
|
|
||||||
'company_id': self.company_id.id,
|
|
||||||
'account_ids': tuple(pl_account_ids),
|
|
||||||
}
|
|
||||||
self.env.cr.execute(query_select_period_pl_balance,
|
|
||||||
query_select_period_pl_balance_params)
|
|
||||||
res = self.env.cr.fetchone()
|
|
||||||
allocated_pl_debit = res[0] or 0.0
|
|
||||||
allocated_pl_credit = res[1] or 0.0
|
|
||||||
allocated_pl_period_balance = allocated_pl_credit - allocated_pl_debit
|
|
||||||
allocated_pl_final_balance = \
|
|
||||||
allocated_pl_initial_balance + allocated_pl_period_balance
|
|
||||||
allocated_pl_initial_balance = allocated_pl_initial_balance * -1
|
|
||||||
query_inject_pl_allocation = """
|
|
||||||
INSERT INTO
|
|
||||||
report_trial_balance_account (
|
|
||||||
report_id,
|
|
||||||
create_uid,
|
|
||||||
create_date,
|
|
||||||
account_id,
|
|
||||||
code,
|
|
||||||
name,
|
|
||||||
initial_balance,
|
|
||||||
debit,
|
|
||||||
credit,
|
|
||||||
period_balance,
|
|
||||||
final_balance,
|
|
||||||
initial_balance_foreign_currency,
|
|
||||||
final_balance_foreign_currency)
|
|
||||||
VALUES (
|
|
||||||
%(report_id)s,
|
|
||||||
%(create_uid)s,
|
|
||||||
NOW(),
|
|
||||||
%(account_id)s,
|
|
||||||
%(code)s,
|
|
||||||
%(name)s,
|
|
||||||
%(initial_balance)s,
|
|
||||||
%(debit)s,
|
|
||||||
%(credit)s,
|
|
||||||
%(period_balance)s,
|
|
||||||
%(final_balance)s,
|
|
||||||
0.0,
|
|
||||||
0.0
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
query_inject_pl_allocation_params = {
|
|
||||||
'report_id': self.id,
|
|
||||||
'create_uid': self.env.uid,
|
|
||||||
'account_id': unaffected_earnings_account.id,
|
|
||||||
'code': unaffected_earnings_account.code,
|
|
||||||
'name': '%s (*)' % unaffected_earnings_account.name,
|
|
||||||
'initial_balance': allocated_pl_initial_balance,
|
|
||||||
'debit': allocated_pl_credit,
|
|
||||||
'credit': allocated_pl_debit,
|
|
||||||
'period_balance': allocated_pl_period_balance,
|
|
||||||
'final_balance': allocated_pl_final_balance
|
|
||||||
}
|
|
||||||
self.env.cr.execute(query_inject_pl_allocation,
|
|
||||||
query_inject_pl_allocation_params)
|
|
||||||
|
|
||||||
def _inject_partner_values(self):
|
def _inject_partner_values(self):
|
||||||
"""Inject report values for report_trial_balance_partner"""
|
"""Inject report values for report_trial_balance_partner"""
|
||||||
query_inject_partner = """
|
query_inject_partner = """
|
||||||
|
|
|
@ -13,13 +13,6 @@ class TrialBalanceXslx(models.AbstractModel):
|
||||||
def _get_report_name(self):
|
def _get_report_name(self):
|
||||||
return _('Trial Balance')
|
return _('Trial Balance')
|
||||||
|
|
||||||
def _get_report_footer(self):
|
|
||||||
return _('(*) This report applies the current year Profit and Loss '
|
|
||||||
'balances to the Undistributed Profit/Loss account.'
|
|
||||||
'To ensure that the Trial Balance totals total to zero, '
|
|
||||||
'this line represents the reversal of the current year '
|
|
||||||
'P&L Balance.')
|
|
||||||
|
|
||||||
def _get_report_columns(self, report):
|
def _get_report_columns(self, report):
|
||||||
if not report.show_partner_details:
|
if not report.show_partner_details:
|
||||||
res = {
|
res = {
|
||||||
|
|
|
@ -54,8 +54,6 @@ class TestGeneralLedger(a_t_f_c.AbstractTestForeignCurrency):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@common.at_install(False)
|
|
||||||
@common.post_install(True)
|
|
||||||
class TestGeneralLedgerReport(common.TransactionCase):
|
class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -344,10 +342,10 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
|
|
||||||
# Check the initial and final balance
|
# Check the initial and final balance
|
||||||
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
self.assertEqual(lines['unaffected'].initial_credit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, -0)
|
self.assertEqual(lines['unaffected'].final_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, -1000)
|
self.assertEqual(lines['unaffected'].final_balance, -1000)
|
||||||
|
|
||||||
# Add reversale move of the initial move the first day of fiscal year
|
# Add reversale move of the initial move the first day of fiscal year
|
||||||
|
@ -369,11 +367,11 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
|
|
||||||
# Check the initial and final balance
|
# Check the initial and final balance
|
||||||
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
self.assertEqual(lines['unaffected'].initial_credit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 0)
|
self.assertEqual(lines['unaffected'].final_debit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, -1000)
|
self.assertEqual(lines['unaffected'].final_balance, 0)
|
||||||
|
|
||||||
# Add another move at the end day of fiscal year
|
# Add another move at the end day of fiscal year
|
||||||
# to check that it correctly used on report
|
# to check that it correctly used on report
|
||||||
|
@ -393,11 +391,11 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
|
|
||||||
# Check the initial and final balance
|
# Check the initial and final balance
|
||||||
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
self.assertEqual(lines['unaffected'].initial_credit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 0)
|
self.assertEqual(lines['unaffected'].final_debit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 4000)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, -4000)
|
self.assertEqual(lines['unaffected'].final_balance, -3000)
|
||||||
|
|
||||||
def test_04_unaffected_account_balance_2_years(self):
|
def test_04_unaffected_account_balance_2_years(self):
|
||||||
# Generate the general ledger line
|
# Generate the general ledger line
|
||||||
|
@ -427,10 +425,10 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
self.assertEqual(len(lines['unaffected']), 1)
|
self.assertEqual(len(lines['unaffected']), 1)
|
||||||
|
|
||||||
# Check the initial and final balance
|
# Check the initial and final balance
|
||||||
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
self.assertEqual(lines['unaffected'].initial_debit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, 1000)
|
self.assertEqual(lines['unaffected'].initial_balance, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 0)
|
self.assertEqual(lines['unaffected'].final_debit, 1000)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, 1000)
|
self.assertEqual(lines['unaffected'].final_balance, 1000)
|
||||||
|
|
||||||
|
@ -460,9 +458,9 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
self.assertEqual(len(lines['unaffected']), 1)
|
self.assertEqual(len(lines['unaffected']), 1)
|
||||||
|
|
||||||
# Check the initial and final balance
|
# Check the initial and final balance
|
||||||
self.assertEqual(lines['unaffected'].initial_debit, 0)
|
self.assertEqual(lines['unaffected'].initial_debit, 500)
|
||||||
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
self.assertEqual(lines['unaffected'].initial_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, 500)
|
self.assertEqual(lines['unaffected'].initial_balance, 500)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 0)
|
self.assertEqual(lines['unaffected'].final_debit, 500)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, 500)
|
self.assertEqual(lines['unaffected'].final_balance, 500)
|
||||||
|
|
|
@ -500,9 +500,8 @@ class TestTrialBalanceReport(common.TransactionCase):
|
||||||
self.assertEqual(lines['partner_receivable'].final_balance, -1000)
|
self.assertEqual(lines['partner_receivable'].final_balance, -1000)
|
||||||
|
|
||||||
def test_04_undistributed_pl(self):
|
def test_04_undistributed_pl(self):
|
||||||
# Generate the trial balance and check that we have 2 lines for the
|
# Add a P&L Move in the previous FY
|
||||||
# undistributed P&L.
|
move_name = 'current year pl move'
|
||||||
move_name = 'journal previous fy'
|
|
||||||
journal = self.env['account.journal'].search([], limit=1)
|
journal = self.env['account.journal'].search([], limit=1)
|
||||||
move_vals = {
|
move_vals = {
|
||||||
'journal_id': journal.id,
|
'journal_id': journal.id,
|
||||||
|
@ -513,16 +512,15 @@ class TestTrialBalanceReport(common.TransactionCase):
|
||||||
'name': move_name,
|
'name': move_name,
|
||||||
'debit': 0.0,
|
'debit': 0.0,
|
||||||
'credit': 1000.0,
|
'credit': 1000.0,
|
||||||
'account_id': self.account100.id}),
|
'account_id': self.account300.id}),
|
||||||
(0, 0, {
|
(0, 0, {
|
||||||
'name': move_name,
|
'name': move_name,
|
||||||
'debit': 1000.0,
|
'debit': 1000.0,
|
||||||
'credit': 0.0,
|
'credit': 0.0,
|
||||||
'account_id': self.account110.id})
|
'account_id': self.account100.id})
|
||||||
]}
|
]}
|
||||||
move = self.env['account.move'].create(move_vals)
|
move = self.env['account.move'].create(move_vals)
|
||||||
move.post()
|
move.post()
|
||||||
|
|
||||||
# Generate the trial balance line
|
# Generate the trial balance line
|
||||||
report_account_model = self.env['report_trial_balance_account']
|
report_account_model = self.env['report_trial_balance_account']
|
||||||
company = self.env.ref('base.main_company')
|
company = self.env.ref('base.main_company')
|
||||||
|
@ -541,17 +539,12 @@ class TestTrialBalanceReport(common.TransactionCase):
|
||||||
('report_id', '=', trial_balance.id),
|
('report_id', '=', trial_balance.id),
|
||||||
('account_id', '=', self.account110.id),
|
('account_id', '=', self.account110.id),
|
||||||
])
|
])
|
||||||
self.assertEqual(len(unaffected_balance_lines), 2)
|
self.assertEqual(len(unaffected_balance_lines), 1)
|
||||||
self.assertEqual(unaffected_balance_lines[0].initial_balance, 1000)
|
self.assertEqual(unaffected_balance_lines[0].initial_balance, -1000)
|
||||||
self.assertEqual(unaffected_balance_lines[0].debit, 0)
|
self.assertEqual(unaffected_balance_lines[0].debit, 0)
|
||||||
self.assertEqual(unaffected_balance_lines[0].credit, 0)
|
self.assertEqual(unaffected_balance_lines[0].credit, 0)
|
||||||
self.assertEqual(unaffected_balance_lines[0].final_balance, 1000)
|
self.assertEqual(unaffected_balance_lines[0].final_balance, -1000)
|
||||||
# Test P&L Allocation
|
# Add a P&L Move to the current FY
|
||||||
self.assertEqual(unaffected_balance_lines[1].initial_balance, 0)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].debit, 0)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].credit, 0)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].final_balance, 0)
|
|
||||||
# Add a P&L Move
|
|
||||||
move_name = 'current year pl move'
|
move_name = 'current year pl move'
|
||||||
journal = self.env['account.journal'].search([], limit=1)
|
journal = self.env['account.journal'].search([], limit=1)
|
||||||
move_vals = {
|
move_vals = {
|
||||||
|
@ -587,18 +580,56 @@ class TestTrialBalanceReport(common.TransactionCase):
|
||||||
('report_id', '=', trial_balance.id),
|
('report_id', '=', trial_balance.id),
|
||||||
('account_id', '=', self.account110.id),
|
('account_id', '=', self.account110.id),
|
||||||
])
|
])
|
||||||
# The unaffected earnings account is affected by this new entry
|
# The unaffected earnings account is not affected by a journal entry
|
||||||
self.assertEqual(len(unaffected_balance_lines), 2)
|
# made to the P&L in the current fiscal year.
|
||||||
self.assertEqual(unaffected_balance_lines[0].initial_balance, 1000)
|
self.assertEqual(len(unaffected_balance_lines), 1)
|
||||||
|
self.assertEqual(unaffected_balance_lines[0].initial_balance, -1000)
|
||||||
|
self.assertEqual(unaffected_balance_lines[0].debit, 0)
|
||||||
|
self.assertEqual(unaffected_balance_lines[0].credit, 0)
|
||||||
|
self.assertEqual(unaffected_balance_lines[0].final_balance, -1000)
|
||||||
|
# Add a Move including Unaffected Earnings to the current FY
|
||||||
|
move_name = 'current year unaffected earnings move'
|
||||||
|
journal = self.env['account.journal'].search([], limit=1)
|
||||||
|
move_vals = {
|
||||||
|
'journal_id': journal.id,
|
||||||
|
'name': move_name,
|
||||||
|
'date': self.date_start,
|
||||||
|
'line_ids': [
|
||||||
|
(0, 0, {
|
||||||
|
'name': move_name,
|
||||||
|
'debit': 0.0,
|
||||||
|
'credit': 1000.0,
|
||||||
|
'account_id': self.account110.id}),
|
||||||
|
(0, 0, {
|
||||||
|
'name': move_name,
|
||||||
|
'debit': 1000.0,
|
||||||
|
'credit': 0.0,
|
||||||
|
'account_id': self.account100.id})
|
||||||
|
]}
|
||||||
|
move = self.env['account.move'].create(move_vals)
|
||||||
|
move.post()
|
||||||
|
# Re Generate the trial balance line
|
||||||
|
trial_balance = self.env['report_trial_balance'].create({
|
||||||
|
'date_from': self.date_start,
|
||||||
|
'date_to': self.date_end,
|
||||||
|
'only_posted_moves': True,
|
||||||
|
'hide_account_balance_at_0': False,
|
||||||
|
'hierarchy_on': 'none',
|
||||||
|
'company_id': company.id,
|
||||||
|
'fy_start_date': self.fy_date_start,
|
||||||
|
})
|
||||||
|
trial_balance.compute_data_for_report()
|
||||||
|
# The unaffected earnings account affected by a journal entry
|
||||||
|
# made to the unaffected earnings in the current fiscal year.
|
||||||
|
unaffected_balance_lines = report_account_model.search([
|
||||||
|
('report_id', '=', trial_balance.id),
|
||||||
|
('account_id', '=', self.account110.id),
|
||||||
|
])
|
||||||
|
self.assertEqual(len(unaffected_balance_lines), 1)
|
||||||
|
self.assertEqual(unaffected_balance_lines[0].initial_balance, -1000)
|
||||||
self.assertEqual(unaffected_balance_lines[0].debit, 0)
|
self.assertEqual(unaffected_balance_lines[0].debit, 0)
|
||||||
self.assertEqual(unaffected_balance_lines[0].credit, 1000)
|
self.assertEqual(unaffected_balance_lines[0].credit, 1000)
|
||||||
self.assertEqual(unaffected_balance_lines[0].final_balance, 0)
|
self.assertEqual(unaffected_balance_lines[0].final_balance, -2000)
|
||||||
# The P&L is affected by this new entry, basically reversing the
|
|
||||||
# P&L balances.
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].initial_balance, 0)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].debit, 1000)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].credit, 0)
|
|
||||||
self.assertEqual(unaffected_balance_lines[1].final_balance, 1000)
|
|
||||||
# The totals for the Trial Balance are zero
|
# The totals for the Trial Balance are zero
|
||||||
all_lines = report_account_model.search([
|
all_lines = report_account_model.search([
|
||||||
('report_id', '=', trial_balance.id),
|
('report_id', '=', trial_balance.id),
|
||||||
|
|
Loading…
Reference in New Issue