Update links in report, add account group file, update trial balance with hierarchy.
Update indentation, remove empty lines from header. Update test. Update pylint. Remove company_id on computing accounts, since account.group is not a company based model, filtering accounts is done on trial balance report. Update account variables. Improve condition in padding on accounts. Add option to print hierarchy based on defined accounts/computed accounts. Add VAT report, hierarchy from tax tags ans taxes. Fix pylint, xlsx report generation header. Update code to select code_prefix or name. Update code to select code_prefix or name. Update code to select code_prefix or name. Fix domain in base amounts in vat report. Change trial balance code_prefix or name. Update trail balance, add tests for vat report. Update pylint, amounts as monetary, many2one option on generation excels. Update pulint. Add VAT Report in readme. Add VAT Report in readme. Update array_agg. Update array_agg. Update array_agg. Add option in VAT Report to be printed on Tax Tags - Tax Groups. Add widget to hierarchy_on on trial balance.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# ?? 2016 Julien Coux (Camptocamp)
|
||||
# ?? 2018 Forest and Biomass Romania SA
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import models, fields, api
|
||||
@@ -10,8 +11,8 @@ class TrialBalanceReport(models.TransientModel):
|
||||
|
||||
The class hierarchy is :
|
||||
* TrialBalanceReport
|
||||
** TrialBalanceReportAccount
|
||||
*** TrialBalanceReportPartner
|
||||
*** TrialBalanceReportAccount
|
||||
**** TrialBalanceReportPartner
|
||||
If "show_partner_details" is selected
|
||||
"""
|
||||
|
||||
@@ -27,6 +28,11 @@ class TrialBalanceReport(models.TransientModel):
|
||||
filter_account_ids = fields.Many2many(comodel_name='account.account')
|
||||
filter_partner_ids = fields.Many2many(comodel_name='res.partner')
|
||||
show_partner_details = fields.Boolean()
|
||||
hierarchy_on = fields.Selection([('computed', 'Computed Accounts'),
|
||||
('relation', 'Child Accounts')],
|
||||
string='Hierarchy On',
|
||||
required=True,
|
||||
default='computed')
|
||||
|
||||
# General Ledger Report Data fields,
|
||||
# used as base for compute the data reports
|
||||
@@ -43,7 +49,7 @@ class TrialBalanceReport(models.TransientModel):
|
||||
|
||||
class TrialBalanceReportAccount(models.TransientModel):
|
||||
_name = 'report_trial_balance_account'
|
||||
_order = 'code ASC'
|
||||
_order = 'sequence, code ASC, name'
|
||||
|
||||
report_id = fields.Many2one(
|
||||
comodel_name='report_trial_balance',
|
||||
@@ -51,12 +57,30 @@ class TrialBalanceReportAccount(models.TransientModel):
|
||||
index=True
|
||||
)
|
||||
|
||||
# Data fields, used to keep link with real object
|
||||
sequence = fields.Integer(index=True, default=0)
|
||||
level = fields.Integer(index=True, default=0)
|
||||
|
||||
# Data fields, used to keep link with real object
|
||||
account_id = fields.Many2one(
|
||||
'account.account',
|
||||
index=True
|
||||
)
|
||||
|
||||
account_group_id = fields.Many2one(
|
||||
'account.group',
|
||||
index=True
|
||||
)
|
||||
parent_id = fields.Many2one(
|
||||
'account.group',
|
||||
index=True
|
||||
)
|
||||
child_account_ids = fields.Char(
|
||||
string="Accounts")
|
||||
compute_account_ids = fields.Many2many(
|
||||
'account.account',
|
||||
string="Accounts", store=True)
|
||||
|
||||
# Data fields, used for report display
|
||||
code = fields.Char()
|
||||
name = fields.Char()
|
||||
@@ -138,14 +162,14 @@ class TrialBalanceReportCompute(models.TransientModel):
|
||||
rcontext['o'] = report
|
||||
result['html'] = self.env.ref(
|
||||
'account_financial_report.report_trial_balance').render(
|
||||
rcontext)
|
||||
rcontext)
|
||||
return result
|
||||
|
||||
@api.model
|
||||
def get_html(self, given_context=None):
|
||||
return self._get_html()
|
||||
|
||||
def _prepare_report_general_ledger(self):
|
||||
def _prepare_report_general_ledger(self, account_ids):
|
||||
self.ensure_one()
|
||||
return {
|
||||
'date_from': self.date_from,
|
||||
@@ -153,7 +177,7 @@ class TrialBalanceReportCompute(models.TransientModel):
|
||||
'only_posted_moves': self.only_posted_moves,
|
||||
'hide_account_balance_at_0': self.hide_account_balance_at_0,
|
||||
'company_id': self.company_id.id,
|
||||
'filter_account_ids': [(6, 0, self.filter_account_ids.ids)],
|
||||
'filter_account_ids': [(6, 0, account_ids.ids)],
|
||||
'filter_partner_ids': [(6, 0, self.filter_partner_ids.ids)],
|
||||
'fy_start_date': self.fy_start_date,
|
||||
}
|
||||
@@ -165,21 +189,38 @@ class TrialBalanceReportCompute(models.TransientModel):
|
||||
# The data of Trial Balance Report
|
||||
# are based on General Ledger Report data.
|
||||
model = self.env['report_general_ledger']
|
||||
if self.filter_account_ids:
|
||||
account_ids = self.filter_account_ids
|
||||
else:
|
||||
account_ids = self.env['account.account'].search(
|
||||
[('company_id', '=', self.company_id.id)])
|
||||
self.general_ledger_id = model.create(
|
||||
self._prepare_report_general_ledger()
|
||||
self._prepare_report_general_ledger(account_ids)
|
||||
)
|
||||
self.general_ledger_id.compute_data_for_report(
|
||||
with_line_details=False, with_partners=self.show_partner_details
|
||||
)
|
||||
|
||||
# Compute report data
|
||||
self._inject_account_values()
|
||||
self._inject_account_values(account_ids)
|
||||
if self.show_partner_details:
|
||||
self._inject_partner_values()
|
||||
# Refresh cache because all data are computed with SQL requests
|
||||
if not self.filter_account_ids:
|
||||
self._inject_account_group_values()
|
||||
if self.hierarchy_on == 'computed':
|
||||
self._update_account_group_computed_values()
|
||||
else:
|
||||
self._update_account_group_child_values()
|
||||
self._update_account_sequence()
|
||||
self._add_account_group_account_values()
|
||||
self.refresh()
|
||||
if not self.filter_account_ids:
|
||||
self._compute_group_accounts()
|
||||
else:
|
||||
for line in self.account_ids:
|
||||
line.write({'level': 0})
|
||||
|
||||
def _inject_account_values(self):
|
||||
def _inject_account_values(self, account_ids):
|
||||
"""Inject report values for report_trial_balance_account"""
|
||||
query_inject_account = """
|
||||
INSERT INTO
|
||||
@@ -189,6 +230,7 @@ INSERT INTO
|
||||
create_uid,
|
||||
create_date,
|
||||
account_id,
|
||||
parent_id,
|
||||
code,
|
||||
name,
|
||||
initial_balance,
|
||||
@@ -200,22 +242,29 @@ SELECT
|
||||
%s AS report_id,
|
||||
%s AS create_uid,
|
||||
NOW() AS create_date,
|
||||
rag.account_id,
|
||||
rag.code,
|
||||
rag.name,
|
||||
rag.initial_balance AS initial_balance,
|
||||
rag.final_debit - rag.initial_debit AS debit,
|
||||
rag.final_credit - rag.initial_credit AS credit,
|
||||
rag.final_balance AS final_balance
|
||||
acc.id,
|
||||
acc.group_id,
|
||||
acc.code,
|
||||
acc.name,
|
||||
coalesce(rag.initial_balance, 0) AS initial_balance,
|
||||
coalesce(rag.final_debit - rag.initial_debit, 0) AS debit,
|
||||
coalesce(rag.final_credit - rag.initial_credit, 0) AS credit,
|
||||
coalesce(rag.final_balance, 0) AS final_balance
|
||||
FROM
|
||||
report_general_ledger_account rag
|
||||
account_account acc
|
||||
LEFT OUTER JOIN report_general_ledger_account AS rag
|
||||
ON rag.account_id = acc.id AND rag.report_id = %s
|
||||
WHERE
|
||||
rag.report_id = %s
|
||||
acc.id in %s
|
||||
"""
|
||||
if self.hide_account_balance_at_0:
|
||||
query_inject_account += """ AND
|
||||
final_balance IS NOT NULL AND final_balance != 0"""
|
||||
query_inject_account_params = (
|
||||
self.id,
|
||||
self.env.uid,
|
||||
self.general_ledger_id.id,
|
||||
account_ids._ids,
|
||||
)
|
||||
self.env.cr.execute(query_inject_account, query_inject_account_params)
|
||||
|
||||
@@ -261,3 +310,179 @@ AND ra.report_id = %s
|
||||
self.id,
|
||||
)
|
||||
self.env.cr.execute(query_inject_partner, query_inject_partner_params)
|
||||
|
||||
def _inject_account_group_values(self):
|
||||
"""Inject report values for report_trial_balance_account"""
|
||||
query_inject_account_group = """
|
||||
INSERT INTO
|
||||
report_trial_balance_account
|
||||
(
|
||||
report_id,
|
||||
create_uid,
|
||||
create_date,
|
||||
account_group_id,
|
||||
parent_id,
|
||||
code,
|
||||
name,
|
||||
sequence,
|
||||
level
|
||||
)
|
||||
SELECT
|
||||
%s AS report_id,
|
||||
%s AS create_uid,
|
||||
NOW() AS create_date,
|
||||
accgroup.id,
|
||||
accgroup.parent_id,
|
||||
coalesce(accgroup.code_prefix, accgroup.name),
|
||||
accgroup.name,
|
||||
accgroup.parent_left * 100000,
|
||||
accgroup.level
|
||||
FROM
|
||||
account_group accgroup"""
|
||||
query_inject_account_params = (
|
||||
self.id,
|
||||
self.env.uid,
|
||||
)
|
||||
self.env.cr.execute(query_inject_account_group,
|
||||
query_inject_account_params)
|
||||
|
||||
def _update_account_group_child_values(self):
|
||||
"""Compute values for report_trial_balance_account group in child."""
|
||||
query_update_account_group = """
|
||||
WITH computed AS (WITH RECURSIVE cte AS (
|
||||
SELECT account_group_id, code, account_group_id AS parent_id,
|
||||
initial_balance, debit, credit, final_balance
|
||||
FROM report_trial_balance_account
|
||||
WHERE report_id = %s
|
||||
GROUP BY report_trial_balance_account.id
|
||||
|
||||
UNION ALL
|
||||
SELECT c.account_group_id, c.code, p.account_group_id,
|
||||
p.initial_balance, p.debit, p.credit, p.final_balance
|
||||
FROM cte c
|
||||
JOIN report_trial_balance_account p USING (parent_id)
|
||||
WHERE p.report_id = %s
|
||||
)
|
||||
SELECT account_group_id, code,
|
||||
sum(initial_balance) AS initial_balance, sum(debit) AS debit,
|
||||
sum(credit) AS credit, sum(final_balance) AS final_balance
|
||||
FROM cte
|
||||
GROUP BY cte.account_group_id, cte.code
|
||||
ORDER BY account_group_id
|
||||
)
|
||||
UPDATE report_trial_balance_account
|
||||
SET initial_balance = computed.initial_balance,
|
||||
debit = computed.debit,
|
||||
credit = computed.credit,
|
||||
final_balance = computed.final_balance
|
||||
FROM computed
|
||||
WHERE report_trial_balance_account.account_group_id = computed.account_group_id
|
||||
AND report_trial_balance_account.report_id = %s
|
||||
"""
|
||||
query_update_account_params = (self.id, self.id, self.id,)
|
||||
self.env.cr.execute(query_update_account_group,
|
||||
query_update_account_params)
|
||||
|
||||
def _add_account_group_account_values(self):
|
||||
"""Compute values for report_trial_balance_account group in child."""
|
||||
query_update_account_group = """
|
||||
DROP AGGREGATE IF EXISTS array_concat_agg(anyarray);
|
||||
CREATE AGGREGATE array_concat_agg(anyarray) (
|
||||
SFUNC = array_cat,
|
||||
STYPE = anyarray
|
||||
);
|
||||
WITH aggr AS(WITH computed AS (WITH RECURSIVE cte AS (
|
||||
SELECT account_group_id, account_group_id AS parent_id,
|
||||
ARRAY[account_id]::int[] as child_account_ids
|
||||
FROM report_trial_balance_account
|
||||
WHERE report_id = %s
|
||||
GROUP BY report_trial_balance_account.id
|
||||
|
||||
UNION ALL
|
||||
SELECT c.account_group_id, p.account_group_id, ARRAY[p.account_id]::int[]
|
||||
FROM cte c
|
||||
JOIN report_trial_balance_account p USING (parent_id)
|
||||
WHERE p.report_id = %s
|
||||
)
|
||||
SELECT account_group_id,
|
||||
array_concat_agg(DISTINCT child_account_ids)::int[] as child_account_ids
|
||||
FROM cte
|
||||
GROUP BY cte.account_group_id, cte.child_account_ids
|
||||
ORDER BY account_group_id
|
||||
)
|
||||
SELECT account_group_id,
|
||||
array_concat_agg(DISTINCT child_account_ids)::int[]
|
||||
AS child_account_ids from computed
|
||||
GROUP BY account_group_id)
|
||||
UPDATE report_trial_balance_account
|
||||
SET child_account_ids = aggr.child_account_ids
|
||||
FROM aggr
|
||||
WHERE report_trial_balance_account.account_group_id = aggr.account_group_id
|
||||
AND report_trial_balance_account.report_id = %s
|
||||
"""
|
||||
query_update_account_params = (self.id, self.id, self.id,)
|
||||
self.env.cr.execute(query_update_account_group,
|
||||
query_update_account_params)
|
||||
|
||||
def _update_account_group_computed_values(self):
|
||||
"""Compute values for report_trial_balance_account group in compute."""
|
||||
query_update_account_group = """
|
||||
WITH RECURSIVE accgroup AS
|
||||
(SELECT
|
||||
accgroup.id,
|
||||
coalesce(sum(ra.initial_balance),0) as initial_balance,
|
||||
coalesce(sum(ra.debit),0) as debit,
|
||||
coalesce(sum(ra.credit),0) as credit,
|
||||
coalesce(sum(ra.final_balance),0) as final_balance
|
||||
FROM
|
||||
account_group accgroup
|
||||
LEFT OUTER JOIN account_account AS acc
|
||||
ON strpos(acc.code, accgroup.code_prefix) = 1
|
||||
LEFT OUTER JOIN report_trial_balance_account AS ra
|
||||
ON ra.account_id = acc.id
|
||||
WHERE ra.report_id = %s
|
||||
GROUP BY accgroup.id
|
||||
)
|
||||
UPDATE report_trial_balance_account
|
||||
SET initial_balance = accgroup.initial_balance,
|
||||
debit = accgroup.debit,
|
||||
credit = accgroup.credit,
|
||||
final_balance = accgroup.final_balance
|
||||
FROM accgroup
|
||||
WHERE report_trial_balance_account.account_group_id = accgroup.id
|
||||
"""
|
||||
query_update_account_params = (self.id,)
|
||||
self.env.cr.execute(query_update_account_group,
|
||||
query_update_account_params)
|
||||
|
||||
def _update_account_sequence(self):
|
||||
"""Compute sequence, level for report_trial_balance_account account."""
|
||||
query_update_account_group = """
|
||||
UPDATE report_trial_balance_account
|
||||
SET sequence = newline.sequence + 1,
|
||||
level = newline.level + 1
|
||||
FROM report_trial_balance_account as newline
|
||||
WHERE newline.account_group_id = report_trial_balance_account.parent_id
|
||||
AND report_trial_balance_account.report_id = newline.report_id
|
||||
AND report_trial_balance_account.account_id is not null
|
||||
AND report_trial_balance_account.report_id = %s"""
|
||||
query_update_account_params = (self.id,)
|
||||
self.env.cr.execute(query_update_account_group,
|
||||
query_update_account_params)
|
||||
|
||||
def _compute_group_accounts(self):
|
||||
groups = self.account_ids.filtered(
|
||||
lambda a: a.account_group_id is not False)
|
||||
for group in groups:
|
||||
if self.hierarchy_on == 'compute':
|
||||
group.compute_account_ids = \
|
||||
group.account_group_id.compute_account_ids
|
||||
else:
|
||||
if group.child_account_ids:
|
||||
chacc = group.child_account_ids.replace(
|
||||
'}', '').replace('{', '').split(',')
|
||||
if 'NULL' in chacc:
|
||||
chacc.remove('NULL')
|
||||
if chacc:
|
||||
group.compute_account_ids = [
|
||||
(6, 0, [int(g) for g in chacc])]
|
||||
|
||||
Reference in New Issue
Block a user