fixes the reporting of unaffected earnings account
parent
80cbd9e2a4
commit
5895de8491
|
@ -4,7 +4,7 @@
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
{
|
{
|
||||||
'name': 'Account Financial Reports',
|
'name': 'Account Financial Reports',
|
||||||
'version': '11.0.2.0.0',
|
'version': '11.0.2.1.0',
|
||||||
'category': 'Reporting',
|
'category': 'Reporting',
|
||||||
'summary': 'OCA Financial Reports',
|
'summary': 'OCA Financial Reports',
|
||||||
'author': 'Camptocamp SA,'
|
'author': 'Camptocamp SA,'
|
||||||
|
|
|
@ -264,12 +264,6 @@ class GeneralLedgerReportCompute(models.TransientModel):
|
||||||
if self.centralize:
|
if self.centralize:
|
||||||
self._inject_line_centralized_values()
|
self._inject_line_centralized_values()
|
||||||
|
|
||||||
# Complete unaffected earnings account
|
|
||||||
if (not self.filter_account_ids or
|
|
||||||
self.unaffected_earnings_account.id in
|
|
||||||
self.filter_account_ids.ids):
|
|
||||||
self._complete_unaffected_earnings_account_values()
|
|
||||||
|
|
||||||
if with_line_details:
|
if with_line_details:
|
||||||
# Compute display flag
|
# Compute display flag
|
||||||
self._compute_has_second_currency()
|
self._compute_has_second_currency()
|
||||||
|
@ -1237,22 +1231,15 @@ WHERE id = %s
|
||||||
params = (self.id,) * 3
|
params = (self.id,) * 3
|
||||||
self.env.cr.execute(query_update_has_second_currency, params)
|
self.env.cr.execute(query_update_has_second_currency, params)
|
||||||
|
|
||||||
def _get_unaffected_earnings_account_sub_subquery_sum_amounts(
|
def _get_unaffected_earnings_account_sub_subquery_sum_initial(
|
||||||
self, include_initial_balance
|
self
|
||||||
):
|
):
|
||||||
""" Return subquery used to compute sum amounts on
|
""" Return subquery used to compute sum amounts on
|
||||||
unaffected earnings accounts """
|
unaffected earnings accounts """
|
||||||
if not include_initial_balance:
|
sub_subquery_sum_amounts = """
|
||||||
sub_subquery_sum_amounts = """
|
|
||||||
SELECT
|
SELECT
|
||||||
-SUM(ml.balance) AS balance
|
SUM(ml.balance) AS initial_balance,
|
||||||
"""
|
0.0 AS final_balance
|
||||||
else:
|
|
||||||
sub_subquery_sum_amounts = """
|
|
||||||
SELECT
|
|
||||||
SUM(ml.balance) AS balance
|
|
||||||
"""
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
FROM
|
FROM
|
||||||
account_account a
|
account_account a
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
|
@ -1260,16 +1247,7 @@ WHERE id = %s
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
account_move_line ml
|
account_move_line ml
|
||||||
ON a.id = ml.account_id
|
ON a.id = ml.account_id
|
||||||
AND ml.date < %s
|
AND ml.date < %(date_from)s
|
||||||
"""
|
|
||||||
|
|
||||||
if not include_initial_balance:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
AND NOT(at.include_initial_balance != TRUE AND ml.date >= %s)
|
|
||||||
"""
|
|
||||||
else:
|
|
||||||
sub_subquery_sum_amounts += """
|
|
||||||
AND at.include_initial_balance = FALSE
|
|
||||||
"""
|
"""
|
||||||
if self.only_posted_moves:
|
if self.only_posted_moves:
|
||||||
sub_subquery_sum_amounts += """
|
sub_subquery_sum_amounts += """
|
||||||
|
@ -1282,35 +1260,72 @@ WHERE id = %s
|
||||||
account_analytic_account aa
|
account_analytic_account aa
|
||||||
ON
|
ON
|
||||||
ml.analytic_account_id = aa.id
|
ml.analytic_account_id = aa.id
|
||||||
AND aa.id IN %s
|
AND aa.id IN %(cost_center_ids)s
|
||||||
"""
|
"""
|
||||||
sub_subquery_sum_amounts += """
|
sub_subquery_sum_amounts += """
|
||||||
WHERE
|
WHERE
|
||||||
a.company_id =%s
|
a.company_id = %(company_id)s
|
||||||
AND a.id != %s
|
AND a.id IN %(unaffected_earnings_account_ids)s
|
||||||
"""
|
"""
|
||||||
return sub_subquery_sum_amounts
|
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
|
||||||
|
"""
|
||||||
|
sub_subquery_sum_amounts += """
|
||||||
|
FROM
|
||||||
|
account_account a
|
||||||
|
INNER JOIN
|
||||||
|
account_account_type at ON a.user_type_id = at.id
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
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 = """
|
subquery_sum_amounts = """
|
||||||
SELECT
|
SELECT
|
||||||
SUM(COALESCE(sub.balance, 0.0)) AS balance
|
SUM(COALESCE(sub.initial_balance, 0.0)) AS initial_balance,
|
||||||
|
SUM(COALESCE(sub.final_balance, 0.0)) AS final_balance
|
||||||
FROM
|
FROM
|
||||||
(
|
(
|
||||||
"""
|
"""
|
||||||
|
# Initial balances
|
||||||
subquery_sum_amounts += \
|
subquery_sum_amounts += \
|
||||||
self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
|
self._get_unaffected_earnings_account_sub_subquery_sum_initial()
|
||||||
include_initial_balance=False
|
|
||||||
)
|
|
||||||
subquery_sum_amounts += """
|
subquery_sum_amounts += """
|
||||||
UNION
|
UNION
|
||||||
"""
|
"""
|
||||||
subquery_sum_amounts += \
|
subquery_sum_amounts += \
|
||||||
self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
|
self._get_unaffected_earnings_account_sub_subquery_sum_final()
|
||||||
include_initial_balance=True
|
|
||||||
)
|
|
||||||
subquery_sum_amounts += """
|
subquery_sum_amounts += """
|
||||||
) sub
|
) sub
|
||||||
"""
|
"""
|
||||||
|
@ -1318,7 +1333,7 @@ WHERE id = %s
|
||||||
# pylint: disable=sql-injection
|
# pylint: disable=sql-injection
|
||||||
query_inject_account = """
|
query_inject_account = """
|
||||||
WITH
|
WITH
|
||||||
initial_sum_amounts AS ( """ + subquery_sum_amounts + """ )
|
sum_amounts AS ( """ + subquery_sum_amounts + """ )
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
report_general_ledger_account
|
report_general_ledger_account
|
||||||
(
|
(
|
||||||
|
@ -1329,102 +1344,51 @@ WHERE id = %s
|
||||||
code,
|
code,
|
||||||
name,
|
name,
|
||||||
is_partner_account,
|
is_partner_account,
|
||||||
initial_balance
|
initial_balance,
|
||||||
|
final_balance
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
%s AS report_id,
|
%(report_id)s AS report_id,
|
||||||
%s AS create_uid,
|
%(user_id)s AS create_uid,
|
||||||
NOW() AS create_date,
|
NOW() AS create_date,
|
||||||
a.id AS account_id,
|
a.id AS account_id,
|
||||||
a.code,
|
a.code,
|
||||||
a.name,
|
a.name,
|
||||||
False AS is_partner_account,
|
False AS is_partner_account,
|
||||||
COALESCE(i.balance, 0.0) AS initial_balance
|
COALESCE(i.initial_balance, 0.0) AS initial_balance,
|
||||||
|
COALESCE(i.final_balance, 0.0) AS final_balance
|
||||||
FROM
|
FROM
|
||||||
account_account a,
|
account_account a,
|
||||||
initial_sum_amounts i
|
sum_amounts i
|
||||||
WHERE
|
WHERE
|
||||||
a.company_id = %s
|
a.company_id = %(company_id)s
|
||||||
AND a.id = %s
|
AND a.id = %(unaffected_earnings_account_id)s
|
||||||
"""
|
"""
|
||||||
query_inject_account_params = (
|
query_inject_account_params = {
|
||||||
self.date_from,
|
'date_from': self.date_from,
|
||||||
self.fy_start_date,
|
'date_to': self.date_to,
|
||||||
)
|
'fy_start_date': self.fy_start_date,
|
||||||
|
}
|
||||||
if self.filter_cost_center_ids:
|
if self.filter_cost_center_ids:
|
||||||
query_inject_account_params += (
|
query_inject_account_params['cost_center_ids'] = \
|
||||||
tuple(self.filter_cost_center_ids.ids),
|
tuple(self.filter_cost_center_ids.ids)
|
||||||
)
|
query_inject_account_params['company_id'] = self.company_id.id
|
||||||
query_inject_account_params += (
|
query_inject_account_params['unaffected_earnings_account_id'] = \
|
||||||
self.company_id.id,
|
self.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
|
||||||
query_inject_account_params += (
|
|
||||||
self.date_from,
|
# Fetch the profit and loss accounts
|
||||||
)
|
query_unaffected_earnings_account_ids = """
|
||||||
if self.filter_cost_center_ids:
|
SELECT a.id
|
||||||
query_inject_account_params += (
|
FROM account_account as a
|
||||||
tuple(self.filter_cost_center_ids.ids),
|
INNER JOIN account_account_type as at
|
||||||
)
|
ON at.id = a.user_type_id
|
||||||
query_inject_account_params += (
|
WHERE at.include_initial_balance = FALSE
|
||||||
self.company_id.id,
|
"""
|
||||||
self.unaffected_earnings_account.id,
|
self.env.cr.execute(query_unaffected_earnings_account_ids)
|
||||||
)
|
pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
|
||||||
query_inject_account_params += (
|
query_inject_account_params['unaffected_earnings_account_ids'] = \
|
||||||
self.id,
|
tuple(pl_account_ids + [self.unaffected_earnings_account.id])
|
||||||
self.env.uid,
|
|
||||||
self.company_id.id,
|
|
||||||
self.unaffected_earnings_account.id,
|
|
||||||
)
|
|
||||||
self.env.cr.execute(query_inject_account,
|
self.env.cr.execute(query_inject_account,
|
||||||
query_inject_account_params)
|
query_inject_account_params)
|
||||||
|
|
||||||
def _complete_unaffected_earnings_account_values(self):
|
|
||||||
"""Complete the report values of the unaffected earnings account
|
|
||||||
for report_general_ledger_account."""
|
|
||||||
query_update_unaffected_earnings_account_values = """
|
|
||||||
WITH
|
|
||||||
sum_amounts AS
|
|
||||||
(
|
|
||||||
SELECT
|
|
||||||
SUM(COALESCE(rml.debit, 0.0)) AS debit,
|
|
||||||
SUM(COALESCE(rml.credit, 0.0)) AS credit,
|
|
||||||
SUM(
|
|
||||||
COALESCE(rml.debit, 0.0) -
|
|
||||||
COALESCE(rml.credit, 0.0)
|
|
||||||
) + ra.initial_balance AS balance
|
|
||||||
FROM
|
|
||||||
report_general_ledger_account ra
|
|
||||||
LEFT JOIN
|
|
||||||
report_general_ledger_move_line rml
|
|
||||||
ON ra.id = rml.report_account_id
|
|
||||||
WHERE
|
|
||||||
ra.report_id = %s
|
|
||||||
AND ra.account_id = %s
|
|
||||||
GROUP BY
|
|
||||||
ra.id
|
|
||||||
)
|
|
||||||
UPDATE
|
|
||||||
report_general_ledger_account ra
|
|
||||||
SET
|
|
||||||
initial_debit = 0.0,
|
|
||||||
initial_credit = 0.0,
|
|
||||||
final_debit = sum_amounts.debit,
|
|
||||||
final_credit = sum_amounts.credit,
|
|
||||||
final_balance = sum_amounts.balance
|
|
||||||
FROM
|
|
||||||
sum_amounts
|
|
||||||
WHERE
|
|
||||||
ra.report_id = %s
|
|
||||||
AND ra.account_id = %s
|
|
||||||
"""
|
|
||||||
params = (
|
|
||||||
self.id,
|
|
||||||
self.unaffected_earnings_account.id,
|
|
||||||
self.id,
|
|
||||||
self.unaffected_earnings_account.id,
|
|
||||||
)
|
|
||||||
self.env.cr.execute(
|
|
||||||
query_update_unaffected_earnings_account_values,
|
|
||||||
params
|
|
||||||
)
|
|
||||||
|
|
|
@ -371,9 +371,9 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
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, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 1000)
|
self.assertEqual(lines['unaffected'].final_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 0)
|
self.assertEqual(lines['unaffected'].final_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, 0)
|
self.assertEqual(lines['unaffected'].final_balance, -1000)
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -395,9 +395,9 @@ class TestGeneralLedgerReport(common.TransactionCase):
|
||||||
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, 0)
|
||||||
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
self.assertEqual(lines['unaffected'].initial_balance, -1000)
|
||||||
self.assertEqual(lines['unaffected'].final_debit, 1000)
|
self.assertEqual(lines['unaffected'].final_debit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_credit, 3000)
|
self.assertEqual(lines['unaffected'].final_credit, 0)
|
||||||
self.assertEqual(lines['unaffected'].final_balance, -3000)
|
self.assertEqual(lines['unaffected'].final_balance, -4000)
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue