Skip to content

Commit

Permalink
[FIX] account_financial_report: Computed hierarchy get groups from ac…
Browse files Browse the repository at this point in the history
…count code (as expected)
  • Loading branch information
carlosdauden committed Jun 21, 2021
1 parent 220e274 commit 958a852
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 30 deletions.
8 changes: 7 additions & 1 deletion account_financial_report/report/templates/trial_balance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@
</t>
<t t-if="hierarchy_on == 'computed'">
<t t-if="balance['type'] == 'group_type'">
<t t-set="style" t-value="style + 'font-weight: bold; color: blue;'"/>
<t t-if="not limit_hierarchy_level or (
show_hierarchy_level &gt;= balance['level'] and (
not hide_parent_hierarchy_level or show_hierarchy_level == balance['level']))">
<t t-if="not limit_hierarchy_level or show_hierarchy_level &gt; balance['level']">
<t t-set="style" t-value="style + 'font-weight: bold; color: blue;'"/>
</t>
<t t-call="account_financial_report.report_trial_balance_line"/>
</t>
</t>
<t t-if="balance['type'] == 'account_type'">
<t t-call="account_financial_report.report_trial_balance_line"/>
Expand Down
117 changes: 89 additions & 28 deletions account_financial_report/report/trial_balance.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# © 2016 Julien Coux (Camptocamp)
# © 2018 Forest and Biomass Romania SA
# Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
# Copyright 2021 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models, api
from odoo import _, api, models
from odoo.exceptions import ValidationError


class TrialBalanceReport(models.AbstractModel):
Expand Down Expand Up @@ -520,33 +522,88 @@ def _get_groups_data(self, accounts_data, total_amount, foreign_currency):
)
return groups_data

@api.model
def _get_computed_account_groups(self, account_code, data, groups):
show_hierarchy_level = data["show_hierarchy_level"]
limit_hierarchy_level = data["limit_hierarchy_level"]
account_groups = []
if limit_hierarchy_level:
limit_level = show_hierarchy_level
else:
limit_level = len(account_code)
for i in range(limit_level + 1):
group_code = account_code[:i]
group = groups.filtered(lambda g: g.code_prefix == group_code)
# Only append existing groups if not limit_hierarchy_level
if limit_hierarchy_level or group:
account_groups.append((group_code, group))
return account_groups

def _get_computed_groups_data(self, accounts_data, total_amount,
foreign_currency):
groups = self.env['account.group'].search([('id', '!=', False)])
groups_data = {}
for group in groups:
len_group_code = len(group.code_prefix)
groups_data.update({group.id: {'id': group.id,
'code': group.code_prefix,
'name': group.name,
'parent_id': group.parent_id.id,
'parent_path': group.parent_path,
'type': 'group_type',
'complete_code': group.complete_code,
'account_ids':
group.compute_account_ids.ids,
'initial_balance': 0.0,
'credit': 0.0,
'debit': 0.0,
'balance': 0.0,
'ending_balance': 0.0}})
if foreign_currency:
groups_data[group.id]['initial_currency_balance'] = 0.0
groups_data[group.id]['ending_currency_balance'] = 0.0
for account in accounts_data.values():
if group.code_prefix == account['code'][:len_group_code]:
foreign_currency, data):
show_hierarchy_level = data["show_hierarchy_level"]
hide_parent_hierarchy_level = data["hide_parent_hierarchy_level"]
groups = self.env['account.group'].search([])
groups_data = {'': {
'id': False,
'code': '',
'name': 'Total',
'parent_id': False,
'parent_path': '',
'level': show_hierarchy_level if hide_parent_hierarchy_level else 0,
'type': 'group_type',
'complete_code': '',
'account_ids': [],
'initial_balance': 0.0,
'debit': 0.0,
'credit': 0.0,
'balance': 0.0,
'ending_balance': 0.0
}}
if foreign_currency:
groups_data[''].update({
'initial_currency_balance': 0.0,
'ending_currency_balance': 0.0,
})
for account in accounts_data.values():
previous_group = False
for group_code, group in self._get_computed_account_groups(
account["code"], data, groups):
if group_code and not group and not previous_group:
raise ValidationError(_(
"Code %s can not be related with any account group"
) % group_code)
if group_code not in groups_data:
len_group = len(group_code)
groups_data[group_code] = {
'id': group.id if group else previous_group.id,
'code': group_code,
'name': group.name if group else previous_group.name,
'parent_id': group.parent_id.id if group else previous_group.id,
'parent_path':
group.parent_path if group else previous_group.parent_path,
'level': len_group,
'type': 'group_type',
'complete_code':
'/'.join([group_code[:i] for i in range(1, len_group)]),
'account_ids': [account['id']],
'initial_balance': total_amount[acc_id]['initial_balance'],
'debit': total_amount[acc_id]['debit'],
'credit': total_amount[acc_id]['credit'],
'balance': total_amount[acc_id]['balance'],
'ending_balance': total_amount[acc_id]['ending_balance'],
}
if foreign_currency:
groups_data[group_code].update({
'initial_currency_balance':
total_amount[acc_id]['initial_currency_balance'],
'ending_currency_balance':
total_amount[acc_id]['ending_currency_balance'],
})
else:
acc_id = account['id']
group_id = group.id
group_id = group_code
groups_data[group_id]['account_ids'].append(acc_id)
groups_data[group_id]['initial_balance'] += total_amount[
acc_id]['initial_balance']
groups_data[group_id]['debit'] += total_amount[
Expand All @@ -562,6 +619,8 @@ def _get_computed_groups_data(self, accounts_data, total_amount,
total_amount[acc_id]['initial_currency_balance']
groups_data[group_id]['ending_currency_balance'] += \
total_amount[acc_id]['ending_currency_balance']
if group:
previous_group = group
return groups_data

@api.multi
Expand All @@ -577,6 +636,7 @@ def _get_report_values(self, docids, data):
date_from = data['date_from']
hide_account_at_0 = data['hide_account_at_0']
hierarchy_on = data['hierarchy_on']
limit_hierarchy_level = data['limit_hierarchy_level']
show_hierarchy_level = data['show_hierarchy_level']
foreign_currency = data['foreign_currency']
only_posted_moves = data['only_posted_moves']
Expand Down Expand Up @@ -619,9 +679,10 @@ def _get_report_values(self, docids, data):
trial['level'] = counter
if hierarchy_on == 'computed':
groups_data = self._get_computed_groups_data(
accounts_data, total_amount, foreign_currency)
accounts_data, total_amount, foreign_currency, data)
trial_balance = list(groups_data.values())
trial_balance += list(accounts_data.values())
if not limit_hierarchy_level:
trial_balance += list(accounts_data.values())
trial_balance = sorted(trial_balance, key=lambda k: k['code'])
if hierarchy_on == 'none':
trial_balance = list(accounts_data.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<field name="hide_account_at_0"/>
<field name="show_partner_details"/>
<field name="hierarchy_on" widget="radio" attrs="{'invisible':[('show_partner_details','=',True)]}"/>
<field name="limit_hierarchy_level" attrs="{'invisible':['|', ('hierarchy_on','in',['none', 'computed']),('show_partner_details','=',True)]}"/>
<field name="limit_hierarchy_level" attrs="{'invisible':['|', ('hierarchy_on','=','none'),('show_partner_details','=',True)]}"/>
<field name="show_hierarchy_level" attrs="{'invisible':[('limit_hierarchy_level','=', False)]}"/>
<field name="hide_parent_hierarchy_level" attrs="{'invisible':[('limit_hierarchy_level','=', False)]}"/>
<field name="foreign_currency"/>
Expand Down

0 comments on commit 958a852

Please sign in to comment.