correct computation in trial balance

This commit is contained in:
Florian du Garage Num 2024-11-21 23:59:45 +01:00
parent 7fdd35345a
commit a6283dbc52
4 changed files with 114 additions and 236 deletions

View File

@ -26,6 +26,7 @@ It is based on modules:
## ChangeLog
- v16.0.0.0.10: computing ok in trial balance
- v16.0.0.0.9: no cumulative initial balance in general ledger
- v16.0.0.0.8: computing for init and end balance for unaffected in general ledger
- v16.0.0.0.7: unaffected earnings movements in general ledger

View File

@ -1,6 +1,6 @@
{
'name': "Gn Financial Report",
'version': '16.0.0.0.9',
'version': '16.0.0.0.10',
'author': 'Garage Numérique',
'category': 'Accounting',
'description': """

View File

@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-
from . import trial_balance, general_ledger, journal_ledger
from . import trial_balance, general_ledger, journal_ledger

View File

@ -1,9 +1,3 @@
# © 2016 Julien Coux (Camptocamp)
# © 2018 Forest and Biomass Romania SA
# Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, api, models
from odoo.tools.float_utils import float_is_zero
import logging
@ -12,244 +6,127 @@ _logger = logging.getLogger(__name__)
class TrialBalanceReport(models.AbstractModel):
_inherit = "report.account_financial_report.trial_balance"
@api.model
def _get_data(
self,
account_ids,
journal_ids,
partner_ids,
company_id,
date_to,
date_from,
foreign_currency,
only_posted_moves,
show_partner_details,
hide_account_at_0,
unaffected_earnings_account,
fy_start_date,
):
accounts_domain = [("company_id", "=", company_id)]
if account_ids:
accounts_domain += [("id", "in", account_ids)]
# If explicit list of accounts is provided,
# don't include unaffected earnings account
unaffected_earnings_account = False
accounts = self.env["account.account"].search(accounts_domain)
tb_initial_acc = []
for account in accounts:
tb_initial_acc.append(
{"account_id": account.id, "balance": 0.0, "amount_currency": 0.0}
)
initial_domain_bs = self._get_initial_balances_bs_ml_domain(
account_ids,
journal_ids,
partner_ids,
company_id,
date_from,
only_posted_moves,
show_partner_details,
)
tb_initial_acc_bs = self.env["account.move.line"].read_group(
domain=initial_domain_bs,
fields=["account_id", "balance", "amount_currency:sum"],
groupby=["account_id"],
)
initial_domain_pl = self._get_initial_balances_pl_ml_domain(
account_ids,
journal_ids,
partner_ids,
company_id,
date_from,
only_posted_moves,
show_partner_details,
fy_start_date,
)
tb_initial_acc_pl = self.env["account.move.line"].read_group(
domain=initial_domain_pl,
fields=["account_id", "balance", "amount_currency:sum"],
groupby=["account_id"],
)
tb_initial_acc_rg = tb_initial_acc_bs + tb_initial_acc_pl
for account_rg in tb_initial_acc_rg:
element = list(
filter(
lambda acc_dict: acc_dict["account_id"]
== account_rg["account_id"][0],
tb_initial_acc,
)
)
if element:
element[0]["balance"] += account_rg["balance"]
element[0]["amount_currency"] += account_rg["amount_currency"]
if hide_account_at_0:
tb_initial_acc = [p for p in tb_initial_acc if p["balance"] != 0]
period_domain = self._get_period_ml_domain(
def _get_report_values(self, docids, data):
show_partner_details = data["show_partner_details"]
wizard_id = data["wizard_id"]
company = self.env["res.company"].browse(data["company_id"])
company_id = data["company_id"]
partner_ids = data["partner_ids"]
journal_ids = data["journal_ids"]
account_ids = data["account_ids"]
date_to = data["date_to"]
date_from = data["date_from"]
hide_account_at_0 = data["hide_account_at_0"]
show_hierarchy = data["show_hierarchy"]
show_hierarchy_level = data["show_hierarchy_level"]
foreign_currency = data["foreign_currency"]
only_posted_moves = data["only_posted_moves"]
unaffected_earnings_account = data["unaffected_earnings_account"]
fy_start_date = data["fy_start_date"]
total_amount, accounts_data, partners_data = self._get_data(
account_ids,
journal_ids,
partner_ids,
company_id,
date_to,
date_from,
only_posted_moves,
show_partner_details,
)
tb_period_acc = self.env["account.move.line"].read_group(
domain=period_domain,
fields=["account_id", "debit", "credit", "balance", "amount_currency:sum"],
groupby=["account_id"],
)
if show_partner_details:
tb_initial_prt_bs = self.env["account.move.line"].read_group(
domain=initial_domain_bs,
fields=["account_id", "partner_id", "balance", "amount_currency:sum"],
groupby=["account_id", "partner_id"],
lazy=False,
)
tb_initial_prt_pl = self.env["account.move.line"].read_group(
domain=initial_domain_pl,
fields=["account_id", "partner_id", "balance", "amount_currency:sum"],
groupby=["account_id", "partner_id"],
)
tb_initial_prt = tb_initial_prt_bs + tb_initial_prt_pl
if hide_account_at_0:
tb_initial_prt = [p for p in tb_initial_prt if p["balance"] != 0]
tb_period_prt = self.env["account.move.line"].read_group(
domain=period_domain,
fields=[
"account_id",
"partner_id",
"debit",
"credit",
"balance",
"amount_currency:sum",
],
groupby=["account_id", "partner_id"],
lazy=False,
)
total_amount = {}
partners_data = []
total_amount = self._compute_account_amount(
total_amount, tb_initial_acc, tb_period_acc, foreign_currency
)
if show_partner_details:
total_amount, partners_data = self._compute_partner_amount(
total_amount, tb_initial_prt, tb_period_prt, foreign_currency
)
# # Remove accounts a 0 from collections
# if hide_account_at_0:
# company = self.env["res.company"].browse(company_id)
# self._remove_accounts_at_cero(total_amount, show_partner_details, company)
accounts_ids = list(total_amount.keys())
# Adding accounts for year's result
# result_profit_account = self.env['account.account'].search([('code', '=', '120000')])
# result_loss_account = self.env['account.account'].search([('code', '=', '129000')])
unaffected_profit_account = self.env['account.account'].search([('code', '=', '110000')])
unaffected_loss_account = self.env['account.account'].search([('code', '=', '119000')])
if unaffected_profit_account and \
unaffected_loss_account:
unaffected_profit_id = unaffected_profit_account[0]['id']
unaffected_loss_id = unaffected_loss_account[0]['id']
if unaffected_profit_id and unaffected_loss_id:
#for account_id in [result_profit_id, result_loss_id, unaffected_profit_id, unaffected_loss_id]:
for account_id in [unaffected_profit_id, unaffected_loss_id]:
if account_id not in accounts_ids:
accounts_ids.append(account_id)
total_amount[account_id] = {}
total_amount[account_id]["initial_balance"] = 0.0
total_amount[account_id]["balance"] = 0.0
total_amount[account_id]["credit"] = 0.0
total_amount[account_id]["debit"] = 0.0
total_amount[account_id]["ending_balance"] = 0.0
if foreign_currency:
total_amount[account_id]["initial_currency_balance"] = 0.0
total_amount[account_id]["ending_currency_balance"] = 0.0
(
pl_initial_balance,
pl_initial_currency_balance,
) = self._get_pl_initial_balance(
account_ids,
journal_ids,
partner_ids,
company_id,
fy_start_date,
only_posted_moves,
show_partner_details,
foreign_currency,
only_posted_moves,
show_partner_details,
hide_account_at_0,
unaffected_earnings_account,
fy_start_date,
)
# (
# pl_year_balance,
# pl_year_currency_balance,
# ) = self._get_pl_year_balance(
# account_ids,
# journal_ids,
# partner_ids,
# company_id,
# date_from,
# date_to,
# only_posted_moves,
# show_partner_details,
# foreign_currency,
# )
# _logger.warning("pl_year_balance: %s" % pl_year_balance)
loss_account = self.env['account.account'].search([('code', '=', '119000')])
if loss_account and len(loss_account) == 1:
loss_id = loss_account[0].id
profit_account = self.env['account.account'].search([('code', '=', '110000')])
if profit_account and len(profit_account) == 1:
profit_id = profit_account[0].id
if total_amount[unaffected_earnings_account]['ending_balance'] > 0:
result_id = loss_id
elif total_amount[unaffected_earnings_account]['ending_balance'] < 0:
result_id = profit_id
result_account_data = self._get_accounts_data([result_id])
accounts_data |= result_account_data
del accounts_data[unaffected_earnings_account]
if pl_initial_balance > 0:
total_amount[unaffected_loss_id]["initial_balance"] += pl_initial_balance
total_amount[unaffected_loss_id]["ending_balance"] += pl_initial_balance
total_amount[result_id] = total_amount.pop(unaffected_earnings_account)
trial_balance = []
if not show_partner_details:
for account_id in accounts_data.keys():
if account_id != result_id:
ref_id = account_id
else:
ref_id = result_id
accounts_data[account_id].update(
{
"initial_balance": total_amount[ref_id]["initial_balance"],
"credit": total_amount[ref_id]["credit"],
"debit": total_amount[ref_id]["debit"],
"balance": total_amount[ref_id]["balance"],
"ending_balance": total_amount[ref_id]["ending_balance"],
"type": "account_type",
}
)
if foreign_currency:
accounts_data[account_id].update(
{
"ending_currency_balance": total_amount[ref_id][
"ending_currency_balance"
],
"initial_currency_balance": total_amount[account_id][
"initial_currency_balance"
],
}
)
if show_hierarchy:
groups_data = self._get_groups_data(
accounts_data, total_amount, foreign_currency
)
trial_balance = list(groups_data.values())
trial_balance += list(accounts_data.values())
trial_balance = sorted(trial_balance, key=lambda k: k["complete_code"])
for trial in trial_balance:
counter = trial["complete_code"].count("/")
trial["level"] = counter
else:
trial_balance = list(accounts_data.values())
trial_balance = sorted(trial_balance, key=lambda k: k["code"])
else:
if foreign_currency:
total_amount[unaffected_loss_id][
"ending_currency_balance"
] += pl_initial_currency_balance
total_amount[unaffected_loss_id][
"initial_currency_balance"
] += pl_initial_currency_balance
elif pl_initial_balance < 0:
total_amount[unaffected_profit_id]["initial_balance"] += pl_initial_balance
total_amount[unaffected_profit_id]["ending_balance"] += pl_initial_balance
if foreign_currency:
total_amount[unaffected_profit_id][
"ending_currency_balance"
] += pl_initial_currency_balance
total_amount[unaffected_profit_id][
"initial_currency_balance"
] += pl_initial_currency_balance
# if pl_year_balance > 0:
# total_amount[result_loss_id]["ending_balance"] += pl_year_balance
# total_amount[result_loss_id]["debit"] += pl_year_balance
# total_amount[result_loss_id]["balance"] += pl_year_balance
# if foreign_currency:
# total_amount[result_loss_id][
# "ending_currency_balance"
# ] += pl_year_currency_balance
# total_amount[result_loss_id][
# "initial_currency_balance"
# ] += pl_year_currency_balance
# elif pl_year_balance < 0:
# total_amount[result_profit_id]["ending_balance"] += pl_year_balance
# total_amount[result_profit_id]["credit"] -= pl_year_balance
# total_amount[result_profit_id]["balance"] += pl_year_balance
# if foreign_currency:
# total_amount[result_profit_id][
# "ending_currency_balance"
# ] += pl_year_currency_balance
for account_id in accounts_data.keys():
total_amount[account_id]["currency_id"] = accounts_data[account_id][
"currency_id"
]
total_amount[account_id]["currency_name"] = accounts_data[
account_id
]["currency_name"]
if hide_account_at_0:
company = self.env["res.company"].browse(company_id)
self._remove_accounts_at_cero(total_amount, show_partner_details, company)
accounts_ids = list(total_amount.keys())
accounts_data = self._get_accounts_data(accounts_ids)
return total_amount, accounts_data, partners_data
return {
"doc_ids": [wizard_id],
"doc_model": "trial.balance.report.wizard",
"docs": self.env["trial.balance.report.wizard"].browse(wizard_id),
"foreign_currency": data["foreign_currency"],
"company_name": company.display_name,
"company_currency": company.currency_id,
"currency_name": company.currency_id.name,
"date_from": data["date_from"],
"date_to": data["date_to"],
"only_posted_moves": data["only_posted_moves"],
"hide_account_at_0": data["hide_account_at_0"],
"show_partner_details": data["show_partner_details"],
"limit_hierarchy_level": data["limit_hierarchy_level"],
"show_hierarchy": show_hierarchy,
"hide_parent_hierarchy_level": data["hide_parent_hierarchy_level"],
"trial_balance": trial_balance,
"total_amount": total_amount,
"accounts_data": accounts_data,
"partners_data": partners_data,
"show_hierarchy_level": show_hierarchy_level,
"currency_model": self.env["res.currency"],
}