Enhance financial report accuracy by including closing journals in profit and loss calculations and trial balances; add idempotency check for journal entry creation.

This commit is contained in:
2026-10-20 18:52:04 +05:30
parent 3f16e0cb44
commit 21c7ba54ea
10 changed files with 56 additions and 26 deletions
+9 -4
View File
@@ -189,8 +189,13 @@ namespace General_Ledger_Service.Services
// project owner 2026-07-20 - see CLAUDE.md/03_Progress_Tracker.md. Folded into the
// Equity section as its own line (retrofit 2026-07-31) rather than a separate
// top-level field, since it's conceptually an Equity line item.
var profitAndLoss = await BuildProfitAndLossReportAsync(DateOnly.MinValue, asOfDate);
equity.Lines.Add(new BalanceSheetLineDto { AccountCode = "", AccountName = "Current Year Earnings", Balance = profitAndLoss.NetProfitForPeriod });
var profitAndLoss = await BuildProfitAndLossReportAsync(DateOnly.MinValue, asOfDate, includeClosing: true);
// Equity shows the real equity accounts plus ONE calculated line: the profit of the period not yet
// closed into Retained Earnings. Closing journals are included above, so once a year is closed its
// profit sits in Retained Earnings and this line only carries the open period. The income/expense
// detail belongs to the ProfitAndLoss report, not the balance sheet.
equity.Lines.Add(new BalanceSheetLineDto { AccountCode = "", AccountName = "Current Year Profit", Balance = profitAndLoss.NetProfitForPeriod });
equity.Total += profitAndLoss.NetProfitForPeriod;
var totalAssets = nonCurrentAssets.Total + currentAssets.Total + unclassifiedAssets.Total;
@@ -223,9 +228,9 @@ namespace General_Ledger_Service.Services
// profitForPeriod - all three just need the resulting NetProfitForPeriod figure, computed
// once here from the raw leaf rows so the Expense-side sign convention lives in exactly
// one place.
private async Task<ProfitAndLossReportDto> BuildProfitAndLossReportAsync(DateOnly periodStart, DateOnly periodEnd)
private async Task<ProfitAndLossReportDto> BuildProfitAndLossReportAsync(DateOnly periodStart, DateOnly periodEnd, bool includeClosing = false)
{
var leafRows = await _accountRepository.GetProfitAndLossLeafRowsAsync(periodStart, periodEnd);
var leafRows = await _accountRepository.GetProfitAndLossLeafRowsAsync(periodStart, periodEnd, includeClosing);
// Raw Amount (credit - debit) is already positive-normal for Income (account_type_id
// 4) but negative-normal for Expense (account_type_id 5, debit-normal) - flip Expense