feat(accounting): make outgoing e-invoices opt-in per tenant

This commit is contained in:
2026-11-05 10:28:15 +00:00
parent e9697ef197
commit d9d12ef58f

View File

@@ -30,9 +30,18 @@ export class EInvoiceRequirementsError extends Error {
export const breakingIssues = (issues: ZugferdIssue[]) => issues.filter((issue) => issue.type === "breaking")
/** Ausgangs-E-Rechnungen sind pro Mandant ausdrücklich zu aktivieren. */
async function outgoingEInvoiceEnabled(server: FastifyInstance, tenantId: number) {
const [tenant] = await server.db.select({ businessInfo: tenants.businessInfo }).from(tenants)
.where(eq(tenants.id, tenantId)).limit(1)
if (!tenant) throw new Error("Mandant nicht gefunden")
return (tenant.businessInfo as any)?.outgoingEInvoiceEnabled === true
}
/** Prüft die Pflichtangaben eines (auch ungespeicherten) Belegs. Andere Belegarten liefern keine Hinweise. */
export async function checkOutgoingEInvoice(server: FastifyInstance, tenantId: number, document: any, options: { requireNumber?: boolean } = {}) {
if (!document || !isZugferdDocumentType(document.type)) return [] as ZugferdIssue[]
if (!await outgoingEInvoiceEnabled(server, tenantId)) return [] as ZugferdIssue[]
const context = await loadZugferdContext(server, tenantId, document)
return checkZugferdRequirements(document, context, options)
}
@@ -92,11 +101,12 @@ export async function loadZugferdContext(server: FastifyInstance, tenantId: numb
}
/**
* Macht aus der gerenderten Rechnungs-PDF eine ZUGFeRD-Rechnung. Fehlende Pflichtangaben führen zu
* Bei aktivierter Ausgangs-E-Rechnung wird die PDF zur ZUGFeRD-Rechnung. Fehlende Pflichtangaben führen zu
* EInvoiceRequirementsError, damit keine Rechnung ohne gültige E-Rechnung abgelegt wird.
*/
export async function attachZugferdToPdf(server: FastifyInstance, tenantId: number, document: any, pdf: Buffer) {
if (!isZugferdDocument(document)) return { pdf, issues: [] as ZugferdIssue[], einvoice: false }
if (!await outgoingEInvoiceEnabled(server, tenantId)) return { pdf, issues: [] as ZugferdIssue[], einvoice: false }
const context = await loadZugferdContext(server, tenantId, document)
const { xml, issues } = buildZugferdInvoiceXml(document, context)
const blocking = breakingIssues(issues)