books/backend/Books.Api/Saft/Services/SaftXmlBuilder.cs
Nicolaj Hartmann 1a0922b778 Audit v3: VAT alignment, security, encoding, UX, compliance
VAT System Alignment (LEGAL - Critical):
- Align frontend VAT codes with backend (S25→U25, K25→I25, etc.)
- Add missing codes: UEU, IVV, IVY, REP
- Fix output VAT account 5710→5611 to match StandardDanishAccounts
- Invoice posting now checks fiscal year status before allowing send
- Disallow custom invoice number override (always use auto-numbering)

Security:
- Fix open redirect in AuthController (validate returnUrl is local)
- Store seller CVR/name/address on invoice events (Momsloven §52)

Backend Compliance:
- Add description validation at posting (Bogføringsloven §7)
- SAF-T: add DefaultCurrencyCode, TaxAccountingBasis to header
- SAF-T: add TaxTable to MasterFiles with all VAT codes
- SAF-T: always write balance elements even when zero
- Add financial income account 9100 Renteindtægter

Danish Encoding (~25 fixes):
- Kassekladde: Bogført, Bogføring, Vælg, være, på, Tilføj, Differens
- AttachmentUpload: træk, Understøtter, påkrævet, Bogføringsloven
- keyboardShortcuts: Bogfør, Bogføring display name
- ShortcutsHelpModal: åbne
- DataTable: Genindlæs
- documentProcessing: være
- CloseFiscalYearWizard: årsafslutning

Bugs Fixed:
- Non-null assertion crashes in Kunder.tsx and Produkter.tsx (company!.id)
- StatusBadge typo "Succces"→"Succes"
- HTML entity ø in Kassekladde→proper UTF-8
- AmountText showSign prop was dead code (true || showSign)

UX Improvements:
- Add PageHeader to Bankafstemning and Dashboard loading/empty states
- Responsive columns in Bankafstemning (xs/sm/lg breakpoints)
- Disable misleading buttons: Settings preferences, Kontooversigt edit,
  Loenforstaelse export — with tooltips explaining status
- Add DemoDataDisclaimer to UserSettings
- Fix breadcrumb self-references on 3 pages
- Replace Dashboard fake progress bar with honest message
- Standardize date format DD-MM-YYYY in Bankafstemning and Ordrer
- Replace Input type="number" with InputNumber in Ordrer

Quality:
- Remove 8 redundant console.error statements
- Fix Kreditnotaer breadcrumb "Salg"→"Fakturering" for consistency

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

392 lines
14 KiB
C#

using System.Text;
using System.Xml;
using Books.Api.Saft.Models;
namespace Books.Api.Saft.Services;
/// <summary>
/// Builds SAF-T DK compliant XML documents.
/// Based on OECD SAF-T 2.0 with Danish customizations.
/// </summary>
public class SaftXmlBuilder
{
private const string SaftNamespace = "urn:StandardAuditFile-Taxation-Financial:DK";
/// <summary>
/// Builds a SAF-T XML document from the provided data.
/// </summary>
public string Build(SaftDocument document)
{
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
Encoding = new UTF8Encoding(false), // UTF-8 without BOM
OmitXmlDeclaration = false
};
using var stream = new MemoryStream();
using (var writer = XmlWriter.Create(stream, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("AuditFile", SaftNamespace);
WriteHeader(writer, document.Header);
WriteMasterFiles(writer, document.MasterFiles);
WriteGeneralLedgerEntries(writer, document.Entries);
writer.WriteEndElement(); // AuditFile
writer.WriteEndDocument();
}
return Encoding.UTF8.GetString(stream.ToArray());
}
private static void WriteHeader(XmlWriter writer, SaftHeader header)
{
writer.WriteStartElement("Header");
writer.WriteElementString("AuditFileVersion", header.AuditFileVersion);
writer.WriteElementString("AuditFileCountry", "DK");
writer.WriteElementString("AuditFileDateCreated", header.AuditFileDateCreated);
writer.WriteElementString("SoftwareCompanyName", header.SoftwareCompanyName);
writer.WriteElementString("SoftwareID", header.SoftwareID);
writer.WriteElementString("SoftwareVersion", header.SoftwareVersion);
// SAF-T requires DefaultCurrencyCode (ISO 4217)
writer.WriteElementString("DefaultCurrencyCode", "DKK");
WriteCompany(writer, header.Company);
WriteSelectionCriteria(writer, header.SelectionCriteria);
// SAF-T requires TaxAccountingBasis: A = Accrual, C = Cash, O = Other
writer.WriteElementString("TaxAccountingBasis", "A");
writer.WriteEndElement(); // Header
}
private static void WriteCompany(XmlWriter writer, SaftCompany company)
{
writer.WriteStartElement("Company");
writer.WriteElementString("RegistrationNumber", company.RegistrationNumber);
writer.WriteElementString("Name", company.Name);
WriteAddress(writer, company.Address);
if (company.Contact != null)
{
WriteContact(writer, company.Contact);
}
// SAF-T DK requires TaxRegistrationNumber with "DK" prefix + CVR
if (!string.IsNullOrEmpty(company.RegistrationNumber))
{
writer.WriteElementString("TaxRegistrationNumber", "DK" + company.RegistrationNumber);
}
writer.WriteEndElement(); // Company
}
private static void WriteAddress(XmlWriter writer, SaftAddress address)
{
writer.WriteStartElement("Address");
if (!string.IsNullOrEmpty(address.StreetName))
writer.WriteElementString("StreetName", address.StreetName);
if (!string.IsNullOrEmpty(address.City))
writer.WriteElementString("City", address.City);
if (!string.IsNullOrEmpty(address.PostalCode))
writer.WriteElementString("PostalCode", address.PostalCode);
writer.WriteElementString("Country", address.Country);
writer.WriteEndElement(); // Address
}
private static void WriteContact(XmlWriter writer, SaftContact contact)
{
writer.WriteStartElement("Contact");
if (!string.IsNullOrEmpty(contact.Telephone))
writer.WriteElementString("Telephone", contact.Telephone);
if (!string.IsNullOrEmpty(contact.Email))
writer.WriteElementString("Email", contact.Email);
if (!string.IsNullOrEmpty(contact.Website))
writer.WriteElementString("Website", contact.Website);
writer.WriteEndElement(); // Contact
}
private static void WriteSelectionCriteria(XmlWriter writer, SaftSelectionCriteria criteria)
{
writer.WriteStartElement("SelectionCriteria");
writer.WriteElementString("PeriodStart", criteria.PeriodStart);
writer.WriteElementString("PeriodEnd", criteria.PeriodEnd);
writer.WriteEndElement(); // SelectionCriteria
}
private static void WriteMasterFiles(XmlWriter writer, SaftMasterFiles masterFiles)
{
writer.WriteStartElement("MasterFiles");
WriteGeneralLedgerAccounts(writer, masterFiles.GeneralLedgerAccounts);
WriteCustomers(writer, masterFiles.Customers);
WriteSuppliers(writer, masterFiles.Suppliers);
WriteTaxTable(writer);
writer.WriteEndElement(); // MasterFiles
}
/// <summary>
/// Writes the TaxTable element declaring all Danish VAT codes.
/// Required by SAF-T schema to describe the tax codes used in transactions.
/// </summary>
private static void WriteTaxTable(XmlWriter writer)
{
writer.WriteStartElement("TaxTable");
writer.WriteStartElement("TaxTableEntry");
writer.WriteElementString("TaxType", "MVA");
writer.WriteElementString("Description", "Dansk moms (VAT)");
// Output VAT codes
WriteTaxCodeDetails(writer, "U25", "Udgående moms 25%", 25.00m);
WriteTaxCodeDetails(writer, "UEU", "EU-salg (momsfrit)", 0.00m);
WriteTaxCodeDetails(writer, "UEXP", "Eksport uden for EU (momsfrit)", 0.00m);
// Input VAT codes
WriteTaxCodeDetails(writer, "I25", "Indgående moms 25%", 25.00m);
WriteTaxCodeDetails(writer, "IEUV", "EU-erhvervelse varer (reverse charge)", 25.00m);
WriteTaxCodeDetails(writer, "IEUY", "EU-erhvervelse ydelser (reverse charge)", 25.00m);
WriteTaxCodeDetails(writer, "IVV", "Import varer fra verden", 0.00m);
WriteTaxCodeDetails(writer, "IVY", "Import ydelser fra verden", 0.00m);
// Special codes
WriteTaxCodeDetails(writer, "REP", "Repræsentation (25% fradrag)", 25.00m);
WriteTaxCodeDetails(writer, "INGEN", "Ingen moms", 0.00m);
writer.WriteEndElement(); // TaxTableEntry
writer.WriteEndElement(); // TaxTable
}
private static void WriteTaxCodeDetails(XmlWriter writer, string taxCode, string description, decimal taxPercentage)
{
writer.WriteStartElement("TaxCodeDetails");
writer.WriteElementString("TaxCode", taxCode);
writer.WriteElementString("Description", description);
writer.WriteElementString("TaxPercentage", FormatDecimal(taxPercentage));
writer.WriteEndElement(); // TaxCodeDetails
}
private static void WriteGeneralLedgerAccounts(XmlWriter writer, List<SaftAccount> accounts)
{
if (accounts.Count == 0) return;
writer.WriteStartElement("GeneralLedgerAccounts");
// SAF-T DK 2027 requirement: StandardAccount metadata
// Ref: BEK nr. 97 af 26/01/2023, Erhvervsstyrelsen
writer.WriteElementString("NameOfStandardAccount", "Standardkontoplan");
writer.WriteElementString("VersionOfStandardAccount", "20230131");
foreach (var account in accounts)
{
writer.WriteStartElement("Account");
writer.WriteElementString("AccountID", account.AccountID);
writer.WriteElementString("AccountDescription", account.AccountDescription);
if (!string.IsNullOrEmpty(account.StandardAccountID))
writer.WriteElementString("StandardAccountID", account.StandardAccountID);
writer.WriteElementString("AccountType", account.AccountType);
// Opening balances (SAF-T schema requires these elements even when zero)
writer.WriteElementString("OpeningDebitBalance", FormatDecimal(account.OpeningDebitBalance));
writer.WriteElementString("OpeningCreditBalance", FormatDecimal(account.OpeningCreditBalance));
// Closing balances (SAF-T schema requires these elements even when zero)
writer.WriteElementString("ClosingDebitBalance", FormatDecimal(account.ClosingDebitBalance));
writer.WriteElementString("ClosingCreditBalance", FormatDecimal(account.ClosingCreditBalance));
writer.WriteEndElement(); // Account
}
writer.WriteEndElement(); // GeneralLedgerAccounts
}
private static void WriteCustomers(XmlWriter writer, List<SaftCustomer> customers)
{
if (customers.Count == 0) return;
writer.WriteStartElement("Customers");
foreach (var customer in customers)
{
writer.WriteStartElement("Customer");
writer.WriteElementString("CustomerID", customer.CustomerID);
if (!string.IsNullOrEmpty(customer.AccountID))
writer.WriteElementString("AccountID", customer.AccountID);
writer.WriteElementString("Name", customer.Name);
if (!string.IsNullOrEmpty(customer.RegistrationNumber))
writer.WriteElementString("RegistrationNumber", customer.RegistrationNumber);
if (customer.Address != null)
WriteAddress(writer, customer.Address);
if (customer.Contact != null)
WriteContact(writer, customer.Contact);
writer.WriteEndElement(); // Customer
}
writer.WriteEndElement(); // Customers
}
private static void WriteSuppliers(XmlWriter writer, List<SaftSupplier> suppliers)
{
if (suppliers.Count == 0) return;
writer.WriteStartElement("Suppliers");
foreach (var supplier in suppliers)
{
writer.WriteStartElement("Supplier");
writer.WriteElementString("SupplierID", supplier.SupplierID);
if (!string.IsNullOrEmpty(supplier.AccountID))
writer.WriteElementString("AccountID", supplier.AccountID);
writer.WriteElementString("Name", supplier.Name);
if (!string.IsNullOrEmpty(supplier.RegistrationNumber))
writer.WriteElementString("RegistrationNumber", supplier.RegistrationNumber);
if (supplier.Address != null)
WriteAddress(writer, supplier.Address);
if (supplier.Contact != null)
WriteContact(writer, supplier.Contact);
writer.WriteEndElement(); // Supplier
}
writer.WriteEndElement(); // Suppliers
}
private static void WriteGeneralLedgerEntries(XmlWriter writer, SaftGeneralLedgerEntries entries)
{
writer.WriteStartElement("GeneralLedgerEntries");
writer.WriteElementString("NumberOfEntries", entries.NumberOfEntries.ToString());
writer.WriteElementString("TotalDebit", FormatDecimal(entries.TotalDebit));
writer.WriteElementString("TotalCredit", FormatDecimal(entries.TotalCredit));
foreach (var journal in entries.Journals)
{
WriteJournal(writer, journal);
}
writer.WriteEndElement(); // GeneralLedgerEntries
}
private static void WriteJournal(XmlWriter writer, SaftJournal journal)
{
writer.WriteStartElement("Journal");
writer.WriteElementString("JournalID", journal.JournalID);
writer.WriteElementString("Description", journal.Description);
writer.WriteElementString("Type", journal.Type);
foreach (var transaction in journal.Transactions)
{
WriteTransaction(writer, transaction);
}
writer.WriteEndElement(); // Journal
}
private static void WriteTransaction(XmlWriter writer, SaftTransaction transaction)
{
writer.WriteStartElement("Transaction");
writer.WriteElementString("TransactionID", transaction.TransactionID);
writer.WriteElementString("Period", transaction.Period);
writer.WriteElementString("TransactionDate", transaction.TransactionDate);
writer.WriteElementString("Description", transaction.Description);
writer.WriteElementString("SystemEntryDate", transaction.SystemEntryDate);
writer.WriteElementString("GLPostingDate", transaction.GLPostingDate);
foreach (var line in transaction.Lines)
{
WriteTransactionLine(writer, line);
}
writer.WriteEndElement(); // Transaction
}
private static void WriteTransactionLine(XmlWriter writer, SaftTransactionLine line)
{
writer.WriteStartElement("Line");
writer.WriteElementString("RecordID", line.RecordID);
writer.WriteElementString("AccountID", line.AccountID);
if (!string.IsNullOrEmpty(line.Description))
writer.WriteElementString("Description", line.Description);
if (line.DebitAmount.HasValue && line.DebitAmount.Value != 0)
writer.WriteElementString("DebitAmount", FormatDecimal(line.DebitAmount.Value));
if (line.CreditAmount.HasValue && line.CreditAmount.Value != 0)
writer.WriteElementString("CreditAmount", FormatDecimal(line.CreditAmount.Value));
if (!string.IsNullOrEmpty(line.CustomerID))
writer.WriteElementString("CustomerID", line.CustomerID);
if (!string.IsNullOrEmpty(line.SupplierID))
writer.WriteElementString("SupplierID", line.SupplierID);
if (line.TaxInfo != null)
{
WriteTaxInformation(writer, line.TaxInfo);
}
writer.WriteEndElement(); // Line
}
private static void WriteTaxInformation(XmlWriter writer, SaftTaxInformation taxInfo)
{
writer.WriteStartElement("TaxInformation");
writer.WriteElementString("TaxCode", taxInfo.TaxCode);
if (taxInfo.TaxPercentage.HasValue)
writer.WriteElementString("TaxPercentage", FormatDecimal(taxInfo.TaxPercentage.Value));
if (taxInfo.TaxBase.HasValue)
writer.WriteElementString("TaxBase", FormatDecimal(taxInfo.TaxBase.Value));
writer.WriteElementString("TaxAmount", FormatDecimal(taxInfo.TaxAmount));
writer.WriteEndElement(); // TaxInformation
}
private static string FormatDecimal(decimal value)
{
// SAF-T requires decimal format with dot as decimal separator
return value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}
}