Files
using.cash/src/UsingCash.Logic/Financial/Reports.cs
T
rick ff7b4b88ff
CI / docker (push) Has been cancelled
CI / build-and-test (push) Has been cancelled
Add Logic Layer — business rules and domain types (section 7)
Core infrastructure:
- SingularFactory: reflection-based Basic→Logic type hydration with
  startup cache warming and fail-fast on missing mappings
- PluralBase<TPlural, TSingular, TBasic>: typed collection hydration
- DependencyTypeCache: name→id mapping loaded once from DB

Financial core:
- FinancialAccount: balance computation, delta calculation, sign modifier
  for result accounts, closed-period enforcement on rename/reparent
- FinancialTransaction: AddRow with continuation transaction support
  (creates/reuses continuation in open period when tx is in closed year),
  RecalculateTransaction across continuation chains, dependency management
- Reports: BalanceSheet and ProfitAndLoss report generation from repo data
- ExchangeRateService: BTC-hub model, micro-BTC scaling (÷1M, BCH exception),
  provider chain with fallback support
- BitPayRateProvider: proper JSON deserialization replacing v5 regex parser
- IRateProvider interface for pluggable rate sources
- IBlockchainProvider + Utxo record for chain abstraction (BCH in Bucket 1)

AP/AR with VAT booking:
- ExpenseClaim.Create: VAT-aware split (net to budget, VAT to
  AssetsVatInboundUnreported), closed-year date bumping, self-attestation
- InboundInvoice.Create: same VAT split pattern, native currency support
- OutboundInvoice.Create: outbound VAT to DebtsVatOutboundUnreported

Structure & Identity:
- Organization: FromBasic with all denormalized OOD settings
- User: FromBasic, PBKDF2 password hashing, constant-time verification

Support:
- IDocumentStorage + LocalFilesystemStorage (yyyy/mm/dd paths, configurable root)
- MailService: template rendering with %placeholder% substitution, direct send
- EventBus: typed publish/consume with JSON serialization, retention purge
- DashboardTaskService: expense/invoice attestation tasks per budget owner

Made-with: Cursor
2026-04-08 17:24:05 +02:00

86 lines
3.1 KiB
C#

using UsingCash.Basic.Types;
using UsingCash.Common.Enums;
using UsingCash.Database.Repositories;
namespace UsingCash.Logic.Financial;
public record AccountBalance(int AccountId, string AccountName, FinancialAccountType AccountType,
int ParentAccountId, long BalanceCents);
public record BalanceSheetReport(
List<AccountBalance> Assets,
List<AccountBalance> Debts,
long TotalAssetsCents,
long TotalDebtsCents,
DateTime AsOfDate);
public record ProfitLossReport(
List<AccountBalance> Income,
List<AccountBalance> Costs,
long TotalIncomeCents,
long TotalCostsCents,
long NetResultCents,
int Year);
public record BudgetVsActualLine(int AccountId, string AccountName,
long BudgetCents, long ActualCents, long VarianceCents);
public static class Reports
{
public static async Task<BalanceSheetReport> GetBalanceSheet(
FinancialRepository repo, int orgId, DateTime asOfDate)
{
var accounts = await repo.GetAccountsForOrganization(orgId);
var assets = new List<AccountBalance>();
var debts = new List<AccountBalance>();
foreach (var account in accounts.Where(a => a.AccountType is FinancialAccountType.Asset or FinancialAccountType.Debt))
{
var balance = await repo.GetAccountBalanceCents(account.Identity, asOfDate);
if (balance == 0) continue;
var entry = new AccountBalance(account.Identity, account.Name, account.AccountType,
account.ParentFinancialAccountId, balance);
if (account.AccountType == FinancialAccountType.Asset)
assets.Add(entry);
else
debts.Add(entry);
}
return new BalanceSheetReport(assets, debts,
assets.Sum(a => a.BalanceCents), debts.Sum(d => d.BalanceCents), asOfDate);
}
public static async Task<ProfitLossReport> GetProfitAndLoss(
FinancialRepository repo, int orgId, int year)
{
var accounts = await repo.GetAccountsForOrganization(orgId);
var from = new DateTime(year, 1, 1);
var to = new DateTime(year + 1, 1, 1);
var income = new List<AccountBalance>();
var costs = new List<AccountBalance>();
foreach (var account in accounts.Where(a => a.AccountType is FinancialAccountType.Income or FinancialAccountType.Cost))
{
var rows = await repo.GetRowsForAccountInRange(account.Identity, from, to);
var total = rows.Sum(r => r.AmountCents);
if (total == 0) continue;
var entry = new AccountBalance(account.Identity, account.Name, account.AccountType,
account.ParentFinancialAccountId, total);
if (account.AccountType == FinancialAccountType.Income)
income.Add(entry);
else
costs.Add(entry);
}
long totalIncome = income.Sum(i => i.BalanceCents);
long totalCosts = costs.Sum(c => c.BalanceCents);
return new ProfitLossReport(income, costs, totalIncome, totalCosts,
-(totalIncome + totalCosts), year);
}
}