compute unaffected balance in general ledger

This commit is contained in:
Florian du Garage Num 2024-11-21 19:58:33 +01:00
parent 24a3beb133
commit b4f483d6bd
3 changed files with 113 additions and 205 deletions

View File

@ -26,6 +26,7 @@ It is based on modules:
## ChangeLog
- 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
- v16.0.0.0.6: unaffected earnings movements in journal ledger
- v16.0.0.0.5:
@ -35,4 +36,4 @@ It is based on modules:
- v16.0.0.0.4: Add general ledger
- v16.0.0.0.3: Add Trial Balance
- v16.0.0.0.2: Add Mis Report
- v16.0.0.0.1: Add FEC
- v16.0.0.0.1: Add FEC

View File

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

View File

@ -11,120 +11,82 @@ from odoo.tools import float_is_zero
class GnGeneralLedgerReport(models.AbstractModel):
_inherit = "report.account_financial_report.general_ledger"
def _get_initial_balance_data(
self,
account_ids,
partner_ids,
company_id,
date_from,
foreign_currency,
only_posted_moves,
unaffected_earnings_account,
fy_start_date,
cost_center_ids,
extra_domain,
grouped_by,
):
# If explicit list of accounts is provided,
# don't include unaffected earnings account
if account_ids:
unaffected_earnings_account = False
base_domain = []
if company_id:
base_domain += [("company_id", "=", company_id)]
if partner_ids:
base_domain += [("partner_id", "in", partner_ids)]
if only_posted_moves:
base_domain += [("move_id.state", "=", "posted")]
else:
base_domain += [("move_id.state", "in", ["posted", "draft"])]
if cost_center_ids:
base_domain += [("analytic_account_ids", "in", cost_center_ids)]
if extra_domain:
base_domain += extra_domain
gl_initial_acc = self._get_gl_initial_acc(
account_ids, company_id, date_from, fy_start_date, base_domain, grouped_by
)
domain = self._get_initial_balances_bs_ml_domain(
account_ids, company_id, date_from, base_domain, grouped_by, acc_prt=True
)
data = self._prepare_gen_ld_data(gl_initial_acc, domain, grouped_by)
accounts_ids = list(data.keys())
unaffected_id = unaffected_earnings_account
if unaffected_id:
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 [unaffected_profit_id, unaffected_loss_id]:
if account_id not in accounts_ids:
accounts_ids.append(account_id)
data[account_id] = self._initialize_data(foreign_currency)
data[account_id]["id"] = unaffected_id
data[account_id]["mame"] = ""
data[account_id][grouped_by] = False
pl_initial_balance = self._get_pl_initial_balance(
account_ids, company_id, fy_start_date, foreign_currency, base_domain
)
for key_bal in ["init_bal", "fin_bal"]:
fields_balance = ["credit", "debit", "balance"]
if foreign_currency:
fields_balance.append("bal_curr")
for field_name in fields_balance:
if pl_initial_balance['balance'] > 0:
data[unaffected_loss_id][key_bal][field_name] += pl_initial_balance[
field_name
]
elif pl_initial_balance['balance'] < 0:
data[unaffected_profit_id][key_bal][field_name] += pl_initial_balance[
field_name
]
return data
def _get_period_ml_data(
self,
account_ids,
partner_ids,
company_id,
foreign_currency,
only_posted_moves,
date_from,
date_to,
gen_ld_data,
cost_center_ids,
extra_domain,
grouped_by,
):
domain = self._get_period_domain(
def _get_report_values(self, docids, data):
wizard_id = data["wizard_id"]
company = self.env["res.company"].browse(data["company_id"])
company_id = data["company_id"]
date_to = data["date_to"]
date_from = data["date_from"]
partner_ids = data["partner_ids"]
account_ids = data["account_ids"]
cost_center_ids = data["cost_center_ids"]
grouped_by = data["grouped_by"]
hide_account_at_0 = data["hide_account_at_0"]
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"]
extra_domain = data["domain"]
gen_ld_data = self._get_initial_balance_data(
account_ids,
partner_ids,
company_id,
only_posted_moves,
date_to,
date_from,
foreign_currency,
only_posted_moves,
unaffected_earnings_account,
fy_start_date,
cost_center_ids,
)
if extra_domain:
domain += extra_domain
ml_fields = self._get_ml_fields()
move_lines = self.env["account.move.line"].search_read(
domain=domain, fields=ml_fields, order="date,move_name"
extra_domain,
grouped_by,
)
# Modified part by GN (loss and profit account definition)
unaffected_earning_account = self.env['account.account'].search([('account_type', '=', 'equity_unaffected')])
unaffected_earning_id = False
if unaffected_earning_account and len(unaffected_earning_account) == 1:
unaffected_earning_id = unaffected_earning_account[0].id
centralize = data["centralize"]
(
gen_ld_data,
accounts_data,
journals_data,
full_reconcile_data,
taxes_data,
analytic_data,
rec_after_date_to_ids,
) = self._get_period_ml_data(
account_ids,
partner_ids,
company_id,
foreign_currency,
only_posted_moves,
date_from,
date_to,
gen_ld_data,
cost_center_ids,
extra_domain,
grouped_by,
)
general_ledger = self._create_general_ledger(
gen_ld_data,
accounts_data,
grouped_by,
rec_after_date_to_ids,
hide_account_at_0,
)
if centralize:
for account in general_ledger:
if account["centralized"]:
centralized_ml = self._get_centralized_ml(
account, date_to, grouped_by
)
account["move_lines"] = centralized_ml
account["move_lines"] = self._recalculate_cumul_balance(
account["move_lines"],
gen_ld_data[account["id"]]["init_bal"]["balance"],
rec_after_date_to_ids,
)
if grouped_by and account[grouped_by]:
account[grouped_by] = False
del account["list_grouped"]
general_ledger = sorted(general_ledger, key=lambda k: k["code"])
unaffected_loss_account = self.env['account.account'].search([('code', '=', '119000')])
if unaffected_loss_account and len(unaffected_loss_account) == 1:
@ -133,101 +95,46 @@ class GnGeneralLedgerReport(models.AbstractModel):
unaffected_profit_account = self.env['account.account'].search([('code', '=', '110000')])
if unaffected_profit_account and len(unaffected_profit_account) == 1:
unaffected_profit_id = unaffected_profit_account[0].id
# End of modified part by GN
if general_ledger[-1]['code'] == '999999':
unaffected_id = unaffected_loss_id
unaffected_account = unaffected_loss_account[0]
if general_ledger[-1]['fin_bal']['balance'] < 0:
unaffected_id = unaffected_profit_id
unaffected_account = unaffected_profit_account[0]
journal_ids = set()
full_reconcile_ids = set()
taxes_ids = set()
analytic_ids = set()
full_reconcile_data = {}
acc_prt_account_ids = self._get_acc_prt_accounts_ids(company_id, grouped_by)
for move_line in move_lines:
journal_ids.add(move_line["journal_id"][0])
for tax_id in move_line["tax_ids"]:
taxes_ids.add(tax_id)
for analytic_account in move_line["analytic_distribution"] or {}:
analytic_ids.add(int(analytic_account))
if move_line["full_reconcile_id"]:
rec_id = move_line["full_reconcile_id"][0]
if rec_id not in full_reconcile_ids:
full_reconcile_data.update(
{
rec_id: {
"id": rec_id,
"name": move_line["full_reconcile_id"][1],
}
}
)
full_reconcile_ids.add(rec_id)
acc_id = move_line["account_id"][0]
general_ledger[-1]['code'] = unaffected_account['code']
accounts_data[unaffected_earnings_account]['code'] = unaffected_account['code']
general_ledger[-1]['name'] = unaffected_account['name']
accounts_data[unaffected_earnings_account]['name'] = unaffected_account['name']
general_ledger[-1]['id'] = unaffected_id
accounts_data[unaffected_earnings_account]['id'] = unaffected_id
accounts_data[unaffected_earnings_account]['group_id'] = unaffected_account['group_id'].id
for move_line in general_ledger[-1]['move_lines']:
move_line['account_id'] = unaffected_id
# Unaffected earning lines treatment
if acc_id == unaffected_earning_id:
if move_line['balance'] > 0:
move_line["account_id"] = (unaffected_profit_id, unaffected_profit_account[0])
acc_id = unaffected_profit_id
elif move_line['balance'] < 0:
move_line["account_id"] = (unaffected_loss_id, unaffected_loss_account[0])
acc_id = unaffected_loss_id
# End of Gn mod
ml_id = move_line["id"]
if acc_id not in gen_ld_data.keys():
gen_ld_data[acc_id] = self._initialize_data(foreign_currency)
gen_ld_data[acc_id]["id"] = acc_id
gen_ld_data[acc_id]["mame"] = move_line["account_id"][1]
if grouped_by:
gen_ld_data[acc_id][grouped_by] = False
if acc_id in acc_prt_account_ids:
item_ids = self._prepare_ml_items(move_line, grouped_by)
for item in item_ids:
item_id = item["id"]
if item_id not in gen_ld_data[acc_id]:
if grouped_by:
gen_ld_data[acc_id][grouped_by] = True
gen_ld_data[acc_id][item_id] = self._initialize_data(
foreign_currency
)
gen_ld_data[acc_id][item_id]["id"] = item_id
gen_ld_data[acc_id][item_id]["name"] = item["name"]
gen_ld_data[acc_id][item_id][ml_id] = self._get_move_line_data(
move_line
)
gen_ld_data[acc_id][item_id]["fin_bal"]["credit"] += move_line[
"credit"
]
gen_ld_data[acc_id][item_id]["fin_bal"]["debit"] += move_line[
"debit"
]
gen_ld_data[acc_id][item_id]["fin_bal"]["balance"] += move_line[
"balance"
]
if foreign_currency:
gen_ld_data[acc_id][item_id]["fin_bal"][
"bal_curr"
] += move_line["amount_currency"]
else:
gen_ld_data[acc_id][ml_id] = self._get_move_line_data(move_line)
gen_ld_data[acc_id]["fin_bal"]["credit"] += move_line["credit"]
gen_ld_data[acc_id]["fin_bal"]["debit"] += move_line["debit"]
gen_ld_data[acc_id]["fin_bal"]["balance"] += move_line["balance"]
if foreign_currency:
gen_ld_data[acc_id]["fin_bal"]["bal_curr"] += move_line[
"amount_currency"
]
journals_data = self._get_journals_data(list(journal_ids))
accounts_data = self._get_accounts_data(gen_ld_data.keys())
taxes_data = self._get_taxes_data(list(taxes_ids))
analytic_data = self._get_analytic_data(list(analytic_ids))
rec_after_date_to_ids = self._get_reconciled_after_date_to_ids(
full_reconcile_data.keys(), date_to
)
return (
gen_ld_data,
accounts_data,
journals_data,
full_reconcile_data,
taxes_data,
analytic_data,
rec_after_date_to_ids,
)
general_ledger = sorted(general_ledger, key=lambda k: k["code"])
return {
"doc_ids": [wizard_id],
"doc_model": "general.ledger.report.wizard",
"docs": self.env["general.ledger.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_cost_center": data["show_cost_center"],
"general_ledger": general_ledger,
"accounts_data": accounts_data,
"journals_data": journals_data,
"full_reconcile_data": full_reconcile_data,
"taxes_data": taxes_data,
"centralize": centralize,
"analytic_data": analytic_data,
"filter_partner_ids": True if partner_ids else False,
"currency_model": self.env["res.currency"],
}