Add tabbed Inspect Ledgers with General Journal (grundbok)
Restructured Inspect Ledgers into three tabs: - Account Ledger (huvudbok): existing view, moved to AccountLedgerTab - General Journal (grundbok): new transaction-grouped view - Search: placeholder (disabled) General Journal features: - Backend endpoint: /inspect/general-journal returns transactions with nested rows, account paths (Parent » Child), account types - Transaction header rows: Tx#, date+time, description, doc indicator - Expandable/collapsible per transaction with smooth animation - Child rows: account type chip (color-coded), account path with parent breadcrumb, debit/credit columns - Year/month selectors, transaction count, currency label - Foreign currency chips on rows where applicable Backend: queries all accounts for org, groups rows by transaction, builds account paths from parent hierarchy, returns structured JSON. Made-with: Cursor
This commit is contained in:
@@ -122,6 +122,95 @@ public static class InspectLedgerEndpoints
|
||||
}).OrderBy(a => a.accountType).ThenBy(a => a.name));
|
||||
});
|
||||
|
||||
group.MapGet("/general-journal", async (int orgId, int? year, int? month,
|
||||
FinancialRepository repo) =>
|
||||
{
|
||||
var yr = year ?? DateTime.UtcNow.Year;
|
||||
var mo = month ?? 0;
|
||||
|
||||
DateTime from, to;
|
||||
if (mo > 0 && mo <= 12)
|
||||
{
|
||||
from = new DateTime(yr, mo, 1);
|
||||
to = mo == 12 ? new DateTime(yr + 1, 1, 1) : new DateTime(yr, mo + 1, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
from = new DateTime(yr, 1, 1);
|
||||
to = new DateTime(yr + 1, 1, 1);
|
||||
}
|
||||
|
||||
var allAccounts = await repo.GetAccountsForOrganization(orgId);
|
||||
var accountMap = allAccounts.ToDictionary(a => a.FinancialAccountId);
|
||||
|
||||
string AccountPath(int acctId)
|
||||
{
|
||||
if (!accountMap.TryGetValue(acctId, out var acct)) return $"#{acctId}";
|
||||
if (acct.ParentFinancialAccountId != 0 && accountMap.TryGetValue(acct.ParentFinancialAccountId, out var parent))
|
||||
return $"{parent.Name} » {acct.Name}";
|
||||
return acct.Name;
|
||||
}
|
||||
|
||||
// Get ALL rows for ALL accounts in this org for the period, sorted by transaction
|
||||
var allRows = new List<(UsingCash.Basic.Types.BasicFinancialTransactionRow Row, int AccountId)>();
|
||||
foreach (var acct in allAccounts)
|
||||
{
|
||||
var rows = await repo.GetRowsForAccountInRange(acct.FinancialAccountId, from, to);
|
||||
allRows.AddRange(rows.Select(r => (r, acct.FinancialAccountId)));
|
||||
}
|
||||
|
||||
// Group by transaction, ordered by transaction datetime
|
||||
var txCache = new Dictionary<int, UsingCash.Basic.Types.BasicFinancialTransaction>();
|
||||
foreach (var (row, _) in allRows)
|
||||
{
|
||||
if (!txCache.ContainsKey(row.FinancialTransactionId))
|
||||
{
|
||||
var tx = await repo.GetTransaction(row.FinancialTransactionId);
|
||||
if (tx != null) txCache[tx.FinancialTransactionId] = tx;
|
||||
}
|
||||
}
|
||||
|
||||
var grouped = allRows
|
||||
.GroupBy(r => r.Row.FinancialTransactionId)
|
||||
.Where(g => txCache.ContainsKey(g.Key))
|
||||
.OrderBy(g => txCache[g.Key].DateTime)
|
||||
.ThenBy(g => g.Key)
|
||||
.Select(g =>
|
||||
{
|
||||
var tx = txCache[g.Key];
|
||||
return new
|
||||
{
|
||||
transactionId = tx.FinancialTransactionId,
|
||||
sequenceId = tx.OrganizationSequenceId,
|
||||
dateTime = tx.DateTime,
|
||||
description = tx.Comment,
|
||||
blockchainHash = tx.BlockchainHash,
|
||||
hasDocumentation = tx.HasDocumentation,
|
||||
rows = g.Select(r => new
|
||||
{
|
||||
rowId = r.Row.FinancialTransactionRowId,
|
||||
accountId = r.AccountId,
|
||||
accountName = AccountPath(r.AccountId),
|
||||
accountType = accountMap.TryGetValue(r.AccountId, out var a) ? a.AccountType.ToString() : "",
|
||||
debitCents = r.Row.AmountCents > 0 ? r.Row.AmountCents : 0,
|
||||
creditCents = r.Row.AmountCents < 0 ? -r.Row.AmountCents : 0,
|
||||
foreignCurrencyCode = r.Row.ForeignCurrencyCode,
|
||||
foreignCurrencyAmountCents = r.Row.ForeignCurrencyAmountCents,
|
||||
}).ToList(),
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
year = yr,
|
||||
month = mo,
|
||||
from = from.ToString("yyyy-MM-dd"),
|
||||
to = to.ToString("yyyy-MM-dd"),
|
||||
transactionCount = grouped.Count,
|
||||
transactions = grouped,
|
||||
});
|
||||
});
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user