books/backend/Books.Api/Domain/JournalEntryDrafts/VatCalculationService.cs
Nicolaj Hartmann 8096a19081 Audit v4: VAT calc, SAF-T compliance, security hardening, frontend quality
Backend (17 files):
- VAT: REP 25% deductibility (§42), EU reverse charge double-entry (IEUV/IEUY/IVY),
  IVY rate 0%→25%, VatReport Box C/D populated, Basis1 from real revenue
- SAF-T: correct OECD namespace, closing balance net calc, zero-amount fallback,
  credit note auto-numbering (§52)
- Security: BankingController CSRF state token + company auth check,
  attachment canonical path traversal check, discount 0-100% validation,
  deactivated product/customer update guard
- Quality: redact bank API logs, remove dead code (VatCalcService,
  PaymentMatchingService), CompanyAggregate IEmit interfaces, fix URL encoding

Frontend (15 files):
- Fix double "kr." in AmountText and Dashboard Statistic components
- Fix UserSettings Switch defaultChecked desync with Form state
- Remove dual useCompany/useCompanyStore pattern (Dashboard, Moms, Bank)
- Correct SKAT VAT deadline calculation per period type
- Add half-yearly/yearly VAT period options
- Guard console.error with import.meta.env.DEV
- Use shared formatDate in BankConnectionsTab
- Remove dead NONE vatCode check, purge 7 legacy VAT codes from type union
- Migrate S25→U25, K25→I25 across all pages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-06 01:38:52 +01:00

233 lines
8 KiB
C#

using Books.Api.Domain.JournalEntryDrafts;
namespace Books.Api.Domain;
/// <summary>
/// Service for calculating VAT (moms) on journal entry lines.
/// Handles Danish VAT rules including automatic calculation of VAT amounts
/// and generation of VAT posting lines.
/// </summary>
public interface IVatCalculationService
{
/// <summary>
/// Calculate VAT for a list of draft lines.
/// Returns the original lines plus generated VAT posting lines.
/// </summary>
VatCalculationResult CalculateVat(
IEnumerable<DraftLine> lines,
VatCalculationMode mode,
string inputVatAccountId,
string outputVatAccountId);
}
/// <summary>
/// Determines how amounts on lines should be interpreted.
/// </summary>
public enum VatCalculationMode
{
/// <summary>
/// Amounts are exclusive of VAT (ekskl. moms).
/// VAT will be added to the total.
/// </summary>
Exclusive,
/// <summary>
/// Amounts are inclusive of VAT (inkl. moms).
/// VAT will be extracted from the total.
/// </summary>
Inclusive
}
/// <summary>
/// Result of VAT calculation containing original lines, VAT lines, and totals.
/// </summary>
public class VatCalculationResult
{
/// <summary>
/// The original lines (unchanged).
/// </summary>
public required List<DraftLine> OriginalLines { get; init; }
/// <summary>
/// Generated VAT posting lines (to be added to the journal entry).
/// </summary>
public required List<VatPostingLine> VatLines { get; init; }
/// <summary>
/// Breakdown of VAT by code for reporting purposes.
/// </summary>
public required List<VatSummary> VatSummary { get; init; }
/// <summary>
/// Total VAT amount (sum of all VAT lines).
/// </summary>
public decimal TotalVatAmount { get; init; }
}
/// <summary>
/// A generated VAT posting line.
/// </summary>
public record VatPostingLine
{
public required string AccountId { get; init; }
public required decimal DebitAmount { get; init; }
public required decimal CreditAmount { get; init; }
public required string Description { get; init; }
/// <summary>
/// The VAT code this line relates to.
/// </summary>
public required string VatCode { get; init; }
/// <summary>
/// The original line number this VAT line was calculated from.
/// </summary>
public required int SourceLineNumber { get; init; }
}
/// <summary>
/// Summary of VAT for a specific VAT code.
/// </summary>
public record VatSummary
{
public required string VatCode { get; init; }
public required decimal Rate { get; init; }
/// <summary>
/// Total base amount (before VAT) for this VAT code.
/// </summary>
public required decimal BaseAmount { get; init; }
/// <summary>
/// Total VAT amount for this VAT code.
/// </summary>
public required decimal VatAmount { get; init; }
/// <summary>
/// Is this input VAT (købsmoms/fradrag) or output VAT (salgsmoms/skyldig)?
/// </summary>
public required bool IsInputVat { get; init; }
}
public class VatCalculationService : IVatCalculationService
{
public VatCalculationResult CalculateVat(
IEnumerable<DraftLine> lines,
VatCalculationMode mode,
string inputVatAccountId,
string outputVatAccountId)
{
var linesList = lines.ToList();
var vatLines = new List<VatPostingLine>();
var vatByCode = new Dictionary<string, VatSummary>();
foreach (var line in linesList)
{
if (string.IsNullOrEmpty(line.VatCode) || line.VatCode == VatCodes.INGEN)
continue;
var rate = VatCodes.GetRate(line.VatCode);
if (rate == 0)
continue;
// Determine if this is a debit or credit line
var isDebit = line.DebitAmount > 0;
var amount = isDebit ? line.DebitAmount : line.CreditAmount;
// Calculate base and VAT amounts based on mode
decimal baseAmount;
decimal vatAmount;
if (mode == VatCalculationMode.Inclusive)
{
// Amount includes VAT, extract it
// VAT = Amount * rate / (1 + rate)
vatAmount = Math.Round(amount * rate / (1 + rate), 2, MidpointRounding.AwayFromZero);
baseAmount = amount - vatAmount;
}
else
{
// Amount excludes VAT, calculate it
baseAmount = amount;
vatAmount = Math.Round(amount * rate, 2, MidpointRounding.AwayFromZero);
}
// Apply deductibility percentage (Momsloven §42 stk. 2 for REP)
var deductiblePercent = VatCodes.GetDeductiblePercent(line.VatCode);
var deductibleVatAmount = Math.Round(vatAmount * deductiblePercent, 2, MidpointRounding.AwayFromZero);
// Determine if this is input or output VAT
var isInputVat = VatCodes.IsInputVat(line.VatCode);
var vatAccountId = isInputVat ? inputVatAccountId : outputVatAccountId;
var isReverseCharge = line.VatCode is VatCodes.IEUV or VatCodes.IEUY or VatCodes.IVY;
if (isReverseCharge)
{
// Reverse charge requires TWO VAT lines:
// 1. Debit EU acquisition VAT account (5620) - VAT payable
// 2. Credit input VAT account (5610) - VAT deductible
// These offset each other, resulting in no net VAT effect
const string euAcquisitionVatAccount = "5620";
var reverseChargeDebitLine = new VatPostingLine
{
AccountId = euAcquisitionVatAccount,
DebitAmount = isDebit ? vatAmount : 0,
CreditAmount = !isDebit ? vatAmount : 0,
Description = $"Moms {line.VatCode} ({rate * 100:0}%) - EU erhvervelse",
VatCode = line.VatCode,
SourceLineNumber = line.LineNumber
};
vatLines.Add(reverseChargeDebitLine);
var reverseChargeCreditLine = new VatPostingLine
{
AccountId = inputVatAccountId,
DebitAmount = !isDebit ? vatAmount : 0,
CreditAmount = isDebit ? vatAmount : 0,
Description = $"Moms {line.VatCode} ({rate * 100:0}%) - indgående moms",
VatCode = line.VatCode,
SourceLineNumber = line.LineNumber
};
vatLines.Add(reverseChargeCreditLine);
}
else
{
// Standard VAT posting line (using deductible amount for REP)
var vatLine = new VatPostingLine
{
AccountId = vatAccountId,
DebitAmount = isDebit ? deductibleVatAmount : 0,
CreditAmount = !isDebit ? deductibleVatAmount : 0,
Description = $"Moms {line.VatCode} ({rate * 100:0}%)",
VatCode = line.VatCode,
SourceLineNumber = line.LineNumber
};
vatLines.Add(vatLine);
}
// Accumulate VAT summary
if (!vatByCode.TryGetValue(line.VatCode, out var summary))
{
summary = new VatSummary
{
VatCode = line.VatCode,
Rate = rate,
BaseAmount = 0,
VatAmount = 0,
IsInputVat = isInputVat
};
vatByCode[line.VatCode] = summary;
}
vatByCode[line.VatCode] = summary with
{
BaseAmount = summary.BaseAmount + baseAmount,
VatAmount = summary.VatAmount + vatAmount
};
}
return new VatCalculationResult
{
OriginalLines = linesList,
VatLines = vatLines,
VatSummary = vatByCode.Values.ToList(),
TotalVatAmount = vatLines.Sum(v => v.DebitAmount - v.CreditAmount)
};
}
}