32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
namespace FinancialApi.Domain;
|
|
|
|
public record BalancedJournal
|
|
{
|
|
public JournalEntry Entry { get; }
|
|
|
|
private BalancedJournal(JournalEntry entry) => Entry = entry;
|
|
|
|
public static BalancedJournal Create(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
|
|
{
|
|
var debits = lines.Where(l => l.Type == EntryType.Debit).Sum(l => l.Amount);
|
|
var credits = lines.Where(l => l.Type == EntryType.Credit).Sum(l => l.Amount);
|
|
|
|
if (debits != credits)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"GAAP Violation: Total Debits ({debits}) must equal Total Credits ({credits})!"
|
|
);
|
|
}
|
|
|
|
return new(new JournalEntry(id, description, createdAt, lines));
|
|
}
|
|
|
|
public static BalancedJournal CreateReversal(JournalEntry original, string reason, DateTimeOffset now)
|
|
{
|
|
var reversedLines = original.Lines.Select(l =>
|
|
l with { Type = l.Type == EntryType.Debit ? EntryType.Credit : EntryType.Debit })
|
|
.ToList();
|
|
|
|
return Create(Guid.NewGuid(), reason, now, reversedLines);
|
|
}
|
|
} |