account_general_ledger_company/models/account_general_ledger.py
2026-06-09 17:17:59 +07:00

52 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
import json
from odoo import models
class AccountGeneralLedgerReportHandler(models.AbstractModel):
_inherit = 'account.general.ledger.report.handler'
def _report_custom_engine_general_ledger(self, expressions, options, date_scope, current_groupby, next_groupby, offset=0, limit=None, warnings=None):
# 1. Fetch default general ledger rows
res = super()._report_custom_engine_general_ledger(
expressions, options, date_scope, current_groupby, next_groupby,
offset=offset, limit=limit, warnings=warnings
)
# 2. Inject company name for transaction rows
if isinstance(res, list):
if current_groupby == 'id_with_accumulated_balance':
aml_ids = []
aml_entries = []
for aml_key, entry in res:
entry['company_name'] = ''
# Exclude the "Initial Balance" summary lines
if aml_key and not aml_key.startswith('balance_line_'):
try:
key_list = json.loads(aml_key)
if len(key_list) > 1:
aml_ids.append(key_list[1])
aml_entries.append((key_list[1], entry))
except (json.JSONDecodeError, TypeError, ValueError):
pass
# Fetch company names in one fast batch query
if aml_ids:
self.env.cr.execute("""
SELECT aml.id, c.name
FROM account_move_line aml
JOIN res_company c ON c.id = aml.company_id
WHERE aml.id IN %s
""", [tuple(aml_ids)])
company_map = dict(self.env.cr.fetchall())
for aml_id, entry in aml_entries:
entry['company_name'] = company_map.get(aml_id, '')
else:
# For non-transaction rows (like summary accounts), keep it blank
for aml_key, entry in res:
entry['company_name'] = ''
elif isinstance(res, dict):
res['company_name'] = ''
return res