Files
accounted/lib/email/invoice-templates.ts
T
MattssonandClaude Fable 5.1 1c82baf553 feat(invoices): offert (quote) document type with own OF-series, decisions, conversion, MCP and v1 (#2163)
* fix(invoices): reminders, AR ledger, AR reconciliation and deadlines only read fakturor

The overdue-reminder run, the kundreskontra, the 1510 reconciliation and the
deadlines page selected invoices by status alone. A sent proforma past its
due date was chased with a betalningspaminnelse and flipped to 'overdue',
and it appeared as a receivable. All four now filter document_type =
'invoice', which is also the precondition for adding quotes (offert): a
quote carries a date but never a receivable.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* feat(invoices): offert (quote) document type with its own OF-series, decisions and conversion

Adds document_type 'quote' with valid_until, quote_status (open / accepted /
declined; expired is derived from valid_until, never stored) and
quote_decided_at. Quotes are numbered OF-nnn at insert from
company_settings.next_quote_number via generate_quote_number(), the same
pattern as delivery notes, so a declined quote never leaves a hole in the
F-series the way a proforma does. The column next_quote_number already
existed on prod and staging without a migration; the migration adopts it.

Engine: build-invoice-write writes the quote columns and keeps
remaining_amount at 0; the draft editor refuses accepted or declined
quotes; PATCH refuses changing a quote's or delivery note's document type
since the number belongs to the series; mark-paid refuses quotes.

New POST /api/invoices/[id]/quote-status records the decision and locks
once an invoice exists. Conversion is extracted into
lib/invoices/convert-to-invoice.ts (one implementation for the route and
the MCP staged commit, which had drifted): a converted quote stays and
flips to accepted, the invoice links back via converted_from_id and gets
its due date from the customer's payment terms; a declined or already
invoiced quote is refused. next-number previews the OF-series for quotes.

Migration applied to the staging branch and registered as 20260902140000;
the pg test runs in CI (pg-real).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* feat(invoices): quote PDF, email and filename surfaces

The customer-facing surfaces get a quote sibling for every proforma branch:
PDF title OFFERT / QUOTE with Offertdatum and Giltig till instead of the
due date, a notice that the document is not an invoice or a payment
request, and no payment box, OCR, bankgiro, Swish, QR or payment link.
The email says the quote is attached and valid until the expiry, drops
the payment details and pay-online button, and asks about the quote
rather than the invoice. Filenames read "Offert nr OF-001". Seller VAT
number and payment accounts are skipped for quotes as for proformas:
a quote is not a faktura under ML 17 kap.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* feat(invoices): offert in the editor, list and detail pages

Editor: "Offert" document type with a required "Giltig till" field
(default today + 30 days) in place of the due date; the wire body mirrors
it into due_date so the shared schema is satisfied. Payment link, ROT/RUT,
periodisering and the bank box are already gated on real invoices. The
type cannot be switched on an existing quote (its OF-number belongs to
the series).

List: an Offerter tab beside Proforma, "Ny offert" in the split button,
and a status column that shows the decision or the derived expiry:
Utgången and Avböjd are exception chips, Öppen and Accepterad muted text.

Detail: Acceptera and Skapa faktura in the header, Avböj in the overflow
menu; an expired quote asks before accepting or invoicing (bypassable);
once an invoice exists the page links to it as Fakturerad and hides the
decision actions. Strings in both sv and en.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* feat(mcp,v1): expose offert on the MCP tools and the v1 REST surface

MCP: create_invoice takes document_type quote with a required valid_until
and allocates the OF-number at insert; the convert tool keeps its id and
accepts quotes with the registry refusal codes; new set_quote_status;
list_invoices and get_invoice expose valid_until and the effective quote
status, including a derived expired filter. The tools/list payload stays
under its ceiling without a ledger change. The MCP staged convert now
uses the shared converter.

v1: POST /invoices/{id}/quote-status (registered in the endpoint registry,
scope map and route loader), valid_until and quote_status in the list,
create and detail shapes, and a quote_status list filter. Skill atoms
mention offert. Decision log lines for the own number series, derived
expiry, accepted-not-cancelled conversion and the header action layout.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* test(invoices): pass route params and period id in the new quote tests

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* refactor(invoices): literal update payloads in the converter so the phantom-column guard can read them

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W45yD8NfQ97JhyYaXpzN56

* fix(invoices): close the quote review findings in one pass

Skeptics (correctness, compliance, regression) and CodeRabbit on #2163:

- quote_status is no longer a write-builder output, so a v1 PATCH or MCP
  update_invoice can never reset a recorded accept/decline; new quotes are
  opened by the invoices_quote_defaults trigger (20260902141000), which
  also keeps due_date and valid_until equal. v1 PATCH and the MCP update
  executor now use the shared editable-draft predicate.
- One live invoice per converted source, enforced by a partial unique
  index; the converter maps 23505 to INVOICE_QUOTE_ALREADY_INVOICED and
  both quote-status routes compare-and-set on the decision they read.
- MCP-created quotes carry remaining_amount 0; mark-paid, transaction
  match and voucher link refuse non-invoices on the MCP staging tools,
  the executors and the dashboard link route.
- Conversion of a foreign-currency source refetches the rate for the
  conversion day (ML 8 kap 21-23 paragraphs) and fails closed without one;
  0-day payment terms mean due on receipt.
- bulk-create refuses quotes per item; list_invoices rejects a
  quote_status filter combined with another document_type; an omitted
  document_type on PATCH means unchanged.
- attention, push notifications, open-AR count, FX revaluation, year-end
  and accrual auto-detect and bank-match suggestions only read fakturor.
- Quote PDF and email print Summa / Total instead of Att betala.
- Regenerated skills/accounted-api for the new v1 endpoint.

Declined with reasons in DECISIONS.md: NOT VALID + VALIDATE and CONCURRENTLY
on the migrations (repo precedent, 13.8k rows, transactional apply);
re-validating VAT treatment at conversion (the converted invoice is a
draft the user reviews; follow-up).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0111fYAUxKtpxU1BHiBioqzs

* fix(invoices): second review round: migration versions, order links, batch allocation, races

- Migrations renamed to 20260902220000 / 20260902221000: #2166 shipped its
  own 20260902141000 to prod while this PR was in review and prod's head
  moved past both files; below-head versions are skipped by branching,
  which would have left the quote trigger off prod. Staging rows renamed.
- Quote lines never carry sales_order_item_id (an offer must not count as
  invoiced kundorder quantity); the converter carries a proforma line's
  order link onto the invoice.
- Converter compare-and-sets the source (proforma cancel, quote accept):
  a concurrent cancel, proforma-to-order conversion or decision removes
  the orphan invoice with INVOICE_CONVERT_SOURCE_CHANGED instead of a
  second document for the same sale.
- MCP set_quote_status gets the same compare-and-set as the HTTP routes;
  0-row updates report INVOICE_QUOTE_CHANGED_CONCURRENTLY everywhere.
  quote-status (dashboard, v1, MCP) accepts valid_until so an expired
  sent quote can be reopened, as the docs promised.
- MCP mark-paid refuses only quotes, parity with the dashboard route
  (a sent proforma marked paid is a supported prepayment record).
- Batch allocation (dashboard route and MCP tool) refuses non-invoices
  before the RPC, which gates on status alone.
- Customer AR drill-down, v1 customer open invoices and archive guard,
  and the calendar feed read fakturor only.
- Draft quote PDF says "UTKAST" instead of "not a valid invoice"; the
  editor locks the document type on existing quotes and delivery notes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0111fYAUxKtpxU1BHiBioqzs

* chore(invoices): use roundOre in the quote MCP summaries and FX test after main tightened the guard baseline

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0111fYAUxKtpxU1BHiBioqzs

* fix(invoices): third review round: atomic decision lock, viewer gate, lookup errors, quote payment terms

- 20260902222000: BEFORE UPDATE trigger locks an accepted quote while a
  live converted invoice exists (the compare-and-set in the three decision
  writers could still be beaten by a conversion landing in between); the
  routes and the MCP tool map the raise to 409 INVOICE_QUOTE_ALREADY_INVOICED.
  generate_quote_number now also requires a non-viewer membership so a
  viewer's session token cannot burn OF-numbers through PostgREST.
- Converter checks quote eligibility before the Riksbanken call and treats
  a failed company_settings read as a failure instead of a 30-day default.
- Re-sending the same decision keeps quote_decided_at (idempotent).
- gnubok_find_voucher_candidates_for_invoice refuses non-invoices like its
  write sibling; the dashboard link route surfaces a failed lookup.
- Late-fee and credit-term texts never print on a quote.
Applied and registered on staging; pg tests added.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0111fYAUxKtpxU1BHiBioqzs

* fix(invoices): review nits: fail-closed batch lookup, dry-run expiry, quote heading, quote-date CHECK

- match-batch surfaces a failed document lookup instead of allocating.
- v1 quote-status dry-run preview carries the new valid_until.
- Quote PDF heading reads Offertinformation / Quote information.
- 20260902222000 also pins the date invariants the trigger maintains as a
  CHECK: a quote always has valid_until = due_date, nothing else has one.
  Applied on staging.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0111fYAUxKtpxU1BHiBioqzs

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-02 22:40:25 +02:00

617 lines
27 KiB
TypeScript

import type { Invoice, Customer, CompanySettings, InvoiceDocumentType } from '@/types'
import { formatDate, getCompanyDisplayName } from '@/lib/utils'
import { getAmountToPay } from '@/lib/invoices/rounding'
import { companyWithInvoicePaymentAccount } from '@/lib/invoices/payment-accounts'
import { applyPlaceholders, escapeHtml, sanitizeSubjectLine, userTextToHtml } from './user-text'
type EmailLang = 'sv' | 'en'
// Customer-facing labels. Statutory chapter references stay intact in both
// locales. lib/utils.ts formatCurrency() keeps the Swedish "kr" symbol for
// in-app financial UI per the accounting standard; here we want the ISO code
// so a non-Swedish recipient understands the unit.
const LABELS = {
sv: {
docInvoice: 'Faktura',
docCreditNote: 'Kreditfaktura',
docProforma: 'Proformafaktura',
docQuote: 'Offert',
docDeliveryNote: 'Följesedel',
htmlLang: 'sv',
documentFrom: (doc: string, sender: string) => `${doc} från ${sender}`,
documentNumber: (doc: string) => `${doc}nummer:`,
documentDate: (doc: string) => `${doc}datum:`,
dueDate: 'Förfallodatum:',
validUntil: 'Giltig till:',
greeting: (firstName: string) => `Hej${firstName ? ` ${firstName}` : ''},`,
bodyCreditNote: 'Bifogat hittar du en kreditfaktura som korrigerar en tidigare faktura.',
bodyInvoice: 'Tack för ditt förtroende! Bifogat hittar du din faktura.',
bodyQuote: (validUntil: string) => `Tack för ditt intresse! Bifogat hittar du vår offert. Offerten är giltig till ${validUntil}.`,
questionsQuote: 'Har du frågor om offerten? Svara direkt på detta mejl så hjälper vi dig.',
toPay: 'Att betala:',
// A quote is not a payment request, so its grand total is a neutral sum.
totalQuote: 'Summa:',
payOnline: 'Betala online',
paymentHeading: 'Betalningsinformation',
bank: 'Bank:',
account: 'Kontonummer:',
iban: 'IBAN:',
bic: 'BIC/SWIFT:',
message: 'Meddelande:',
questions: 'Har du frågor om fakturan? Svara direkt på detta mejl så hjälper vi dig.',
sincerely: 'Med vänliga hälsningar,',
orgNo: 'Org.nr:',
vat: 'VAT:',
fSkatt: 'Innehar F-skattsedel',
documentSummary: (doc: string) => `${doc.toLowerCase()}sammanfattning:`,
subjectFrom: (doc: string, num: string, sender: string) => `${doc} ${num} från ${sender}`,
// Payment confirmation (#1693)
confirmationSubject: (num: string, sender: string) => `Betalningsbekräftelse för faktura ${num} från ${sender}`,
confirmationHeading: (sender: string) => `Betalningsbekräftelse från ${sender}`,
confirmationBody: (num: string) => `Tack för din betalning! Vi bekräftar att faktura ${num} är betald i sin helhet. Bifogat hittar du en betalningsbekräftelse.`,
confirmationPaidOn: 'Betald:',
confirmationPaidAmount: 'Betalt belopp:',
confirmationQuestions: 'Har du frågor om betalningen? Svara direkt på detta mejl så hjälper vi dig.',
},
en: {
docInvoice: 'Invoice',
docCreditNote: 'Credit note',
docProforma: 'Proforma invoice',
docQuote: 'Quote',
docDeliveryNote: 'Delivery note',
htmlLang: 'en',
documentFrom: (doc: string, sender: string) => `${doc} from ${sender}`,
documentNumber: (doc: string) => `${doc} number:`,
documentDate: (doc: string) => `${doc} date:`,
dueDate: 'Due date:',
validUntil: 'Valid until:',
greeting: (firstName: string) => `Hi${firstName ? ` ${firstName}` : ''},`,
bodyCreditNote: 'Attached you will find a credit note that corrects an earlier invoice.',
bodyInvoice: 'Thank you for your business. Attached you will find your invoice.',
bodyQuote: (validUntil: string) => `Thank you for your interest. Attached you will find our quote. The quote is valid until ${validUntil}.`,
questionsQuote: 'Questions about the quote? Reply directly to this email and we will help you.',
toPay: 'Total due:',
totalQuote: 'Total:',
payOnline: 'Pay online',
paymentHeading: 'Payment information',
bank: 'Bank:',
account: 'Account number:',
iban: 'IBAN:',
bic: 'BIC/SWIFT:',
message: 'Reference:',
questions: 'Questions about the invoice? Reply directly to this email and we will help you.',
sincerely: 'Kind regards,',
orgNo: 'Reg. no.:',
vat: 'VAT:',
// Statutory Swedish phrase: kept verbatim in both locales. F-skatt is a
// Swedish tax-authority designation; translating it has no legal standing.
fSkatt: 'Innehar F-skattsedel',
documentSummary: (doc: string) => `${doc} summary:`,
subjectFrom: (doc: string, num: string, sender: string) => `${doc} ${num} from ${sender}`,
confirmationSubject: (num: string, sender: string) => `Payment confirmation for invoice ${num} from ${sender}`,
confirmationHeading: (sender: string) => `Payment confirmation from ${sender}`,
confirmationBody: (num: string) => `Thank you for your payment. We confirm that invoice ${num} has been paid in full. A payment confirmation is attached.`,
confirmationPaidOn: 'Paid on:',
confirmationPaidAmount: 'Amount paid:',
confirmationQuestions: 'Questions about the payment? Reply directly to this email and we will help you.',
},
} as const
// Placeholder keys available in company-editable email texts
// (company_settings.invoice_email_texts). Rendered as a legend in the
// settings UI; kept here rather than in messages/*.json because ICU message
// syntax treats literal braces as interpolation.
export const INVOICE_EMAIL_PLACEHOLDER_KEYS = [
'fakturanummer',
'kundnamn',
'förnamn',
'företag',
'förfallodatum',
'belopp',
] as const
// Display strings for the settings UI's input placeholder attributes.
// subject and greeting are functions in LABELS, so their pattern form is
// hand-written here; body/signoff reference LABELS directly so they cannot
// drift from the actual defaults.
export const INVOICE_EMAIL_DEFAULT_TEXTS = {
sv: {
subject: 'Faktura {fakturanummer} från {företag}',
greeting: 'Hej {förnamn},',
body: LABELS.sv.bodyInvoice,
signoff: LABELS.sv.sincerely,
},
en: {
subject: 'Invoice {fakturanummer} from {företag}',
greeting: 'Hi {förnamn},',
body: LABELS.en.bodyInvoice,
signoff: LABELS.en.sincerely,
},
} as const
function resolveLang(customer: Customer): EmailLang {
return customer.language === 'en' ? 'en' : 'sv'
}
// Custom texts apply ONLY to standard invoices. Credit notes, proforma, quotes and
// delivery notes always use the stock texts: a custom "Tack för ditt
// förtroende..." body or "Faktura..." subject would be wrong on those.
function isStandardInvoice(invoice: Invoice): boolean {
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
return docType === 'invoice' && !invoice.credited_invoice_id
}
function getDocumentLabel(invoice: Invoice, lang: EmailLang): string {
const L = LABELS[lang]
if (invoice.credited_invoice_id) return L.docCreditNote
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
if (docType === 'proforma') return L.docProforma
if (docType === 'quote') return L.docQuote
if (docType === 'delivery_note') return L.docDeliveryNote
return L.docInvoice
}
// A quote's expiry: valid_until is authoritative, due_date only mirrors it
// (the column is NOT NULL), so fall back to it for rows written before
// valid_until existed.
function quoteValidUntil(invoice: Invoice): string {
return formatDate(invoice.valid_until || invoice.due_date)
}
// Currency for the customer-facing total: explicit ISO code so a non-Swedish
// recipient reads "1 234,56 SEK" instead of the Swedish symbol "kr". Use the
// English locale for digit grouping when the email is in English so the comma
// thousands separator matches reader expectation.
function formatCurrencyForCustomer(amount: number, currency: string, lang: EmailLang): string {
const formatted = new Intl.NumberFormat(lang === 'en' ? 'en-US' : 'sv-SE', {
style: 'decimal',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amount)
return `${formatted} ${currency}`
}
export interface InvoiceEmailData {
invoice: Invoice
customer: Customer
company: CompanySettings
}
function buildPlaceholderValues(data: InvoiceEmailData, lang: EmailLang): Record<string, string> {
const { invoice, customer, company } = data
const fullName = (customer.name || '').trim()
return {
fakturanummer: invoice.invoice_number ?? '',
kundnamn: fullName,
förnamn: fullName ? fullName.split(' ')[0] : '',
företag: getCompanyDisplayName(company),
förfallodatum: formatDate(invoice.due_date),
belopp: formatCurrencyForCustomer(getAmountToPay(invoice, company).toPay, invoice.currency, lang),
}
}
interface ResolvedCustomTexts {
subject?: string
greeting?: string
body?: string
signoff?: string
}
// Resolves the company's custom email texts for one language. Per-field
// fallback: missing / non-string / whitespace-only values return undefined
// and the caller uses the stock text. Returns RAW substituted strings:
// escaping is the caller's job per output variant (HTML vs text vs subject).
// Defensive typeof checks: rows can be written outside Zod (scripts, SQL).
function resolveCustomTexts(data: InvoiceEmailData, lang: EmailLang): ResolvedCustomTexts {
if (!isStandardInvoice(data.invoice)) return {}
const texts = data.company.invoice_email_texts
const langTexts = texts && typeof texts === 'object' ? texts[lang] : undefined
if (!langTexts || typeof langTexts !== 'object') return {}
const values = buildPlaceholderValues(data, lang)
const pick = (v: unknown): string | undefined =>
typeof v === 'string' && v.trim() !== '' ? applyPlaceholders(v.trim(), values) : undefined
return {
subject: pick(langTexts.subject),
greeting: pick(langTexts.greeting),
body: pick(langTexts.body),
signoff: pick(langTexts.signoff),
}
}
// Minimal hex validator: guards against branding values that bypass the
// settings UI and could inject CSS via crafted strings. Anything malformed
// falls back to the legacy default.
function safeBrandingColor(value: string | null | undefined, fallback: string): string {
if (!value) return fallback
return /^#[0-9A-Fa-f]{6}$/.test(value) ? value : fallback
}
/**
* Generate HTML email for sending an invoice
*/
export function generateInvoiceEmailHtml(data: InvoiceEmailData): string {
const { invoice, customer } = data
const company = companyWithInvoicePaymentAccount(data.company, invoice.currency)
const lang = resolveLang(customer)
const L = LABELS[lang]
const documentType = getDocumentLabel(invoice, lang)
const isCreditNote = !!invoice.credited_invoice_id
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
const isDeliveryNote = docType === 'delivery_note'
const isProforma = docType === 'proforma'
// A quote (offert) is never a payment request: no payment details, no
// pay-online button; its expiry replaces the due date.
const isQuote = docType === 'quote'
const hidePayment = isCreditNote || isDeliveryNote || isProforma || isQuote
const firstName = customer.name ? customer.name.split(' ')[0] : ''
const custom = resolveCustomTexts(data, lang)
const stockBody = isCreditNote
? L.bodyCreditNote
: isQuote
? L.bodyQuote(quoteValidUntil(invoice))
: L.bodyInvoice
// Primary color drives the heading accent and the highlighted total. The
// accent is sanitized to a strict hex pattern: anything else falls back
// to the legacy dark neutral. Credit notes intentionally use the success
// green for the total regardless of branding, because the customer's brain
// is wired to expect "money coming back = green".
const primaryColor = safeBrandingColor(company.invoice_primary_color, '#111111')
return `
<!DOCTYPE html>
<html lang="${L.htmlLang}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${documentType} ${invoice.invoice_number}</title>
</head>
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; line-height: 1.6; color: #333;">
<div style="max-width: 600px; margin: 0 auto; padding: 40px 20px;">
<!-- Header -->
<div style="margin-bottom: 30px; border-bottom: 2px solid ${primaryColor}; padding-bottom: 16px;">
<h1 style="margin: 0 0 10px 0; font-size: 24px; font-weight: 600; color: ${primaryColor};">
${L.documentFrom(documentType, getCompanyDisplayName(company))}
</h1>
<p style="margin: 0; color: #666; font-size: 14px;">
${L.documentNumber(documentType)} ${invoice.invoice_number}
</p>
</div>
<!-- Greeting -->
<div style="margin-bottom: 30px;">
<p style="margin: 0 0 15px 0;">
${custom.greeting !== undefined ? userTextToHtml(custom.greeting) : L.greeting(firstName)}
</p>
<p style="margin: 0;">
${custom.body !== undefined ? userTextToHtml(custom.body) : stockBody}
</p>
</div>
<!-- Summary Box -->
<div style="background: #f8f9fa; border-radius: 8px; padding: 25px; margin-bottom: 30px;">
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${L.documentNumber(documentType)}</td>
<td style="padding: 8px 0; text-align: right; font-weight: 500;">${invoice.invoice_number}</td>
</tr>
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${L.documentDate(documentType)}</td>
<td style="padding: 8px 0; text-align: right;">${formatDate(invoice.invoice_date)}</td>
</tr>
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${isQuote ? L.validUntil : L.dueDate}</td>
<td style="padding: 8px 0; text-align: right; font-weight: 500; color: ${isCreditNote || isQuote ? '#333' : '#e11d48'};">
${isQuote ? quoteValidUntil(invoice) : formatDate(invoice.due_date)}
</td>
</tr>
<tr>
<td colspan="2" style="padding: 15px 0 8px 0; border-top: 1px solid #e5e7eb;"></td>
</tr>
<tr>
<td style="padding: 8px 0; font-size: 18px; font-weight: 600;">${isQuote ? L.totalQuote : L.toPay}</td>
<td style="padding: 8px 0; text-align: right; font-size: 18px; font-weight: 600; color: ${isCreditNote ? '#059669' : primaryColor};">
${formatCurrencyForCustomer(getAmountToPay(invoice, company).toPay, invoice.currency, lang)}
</td>
</tr>
</table>
</div>
<!-- Pay-online button: only when the user pasted a payment link on this
invoice. The URL is schema-validated (https-only) but still escaped
for the attribute context: a URL may legally contain quotes. -->
${!hidePayment && invoice.payment_link_url ? `
<div style="margin-bottom: 30px; text-align: center;">
<a href="${escapeHtml(invoice.payment_link_url)}" style="display: inline-block; background: ${primaryColor}; color: #ffffff; text-decoration: none; padding: 12px 32px; border-radius: 6px; font-size: 16px; font-weight: 600;">
${L.payOnline}
</a>
</div>
` : ''}
<!-- Payment Details -->
${!hidePayment ? `
<div style="margin-bottom: 30px;">
<h2 style="margin: 0 0 15px 0; font-size: 16px; font-weight: 600; color: ${primaryColor};">
${L.paymentHeading}
</h2>
<table style="width: 100%; border-collapse: collapse;">
${company.bank_name ? `
<tr>
<td style="padding: 6px 0; color: #666; font-size: 14px; width: 140px;">${L.bank}</td>
<td style="padding: 6px 0;">${company.bank_name}</td>
</tr>
` : ''}
${company.clearing_number && company.account_number ? `
<tr>
<td style="padding: 6px 0; color: #666; font-size: 14px;">${L.account}</td>
<td style="padding: 6px 0;">${company.clearing_number}-${company.account_number}</td>
</tr>
` : ''}
${company.iban ? `
<tr>
<td style="padding: 6px 0; color: #666; font-size: 14px;">${L.iban}</td>
<td style="padding: 6px 0;">${company.iban}</td>
</tr>
` : ''}
${company.bic ? `
<tr>
<td style="padding: 6px 0; color: #666; font-size: 14px;">${L.bic}</td>
<td style="padding: 6px 0;">${company.bic}</td>
</tr>
` : ''}
<tr>
<td style="padding: 6px 0; color: #666; font-size: 14px;">${L.message}</td>
<td style="padding: 6px 0; font-weight: 500;">${invoice.invoice_number}</td>
</tr>
</table>
</div>
` : ''}
<!-- Footer -->
<div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
<p style="margin: 0 0 10px 0; color: #666; font-size: 14px;">
${isQuote ? L.questionsQuote : L.questions}
</p>
<p style="margin: 0; color: #666; font-size: 14px;">
${custom.signoff !== undefined ? userTextToHtml(custom.signoff) : L.sincerely}<br>
<strong style="color: ${primaryColor};">${getCompanyDisplayName(company)}</strong>
</p>
${company.org_number ? `
<p style="margin: 10px 0 0 0; color: #999; font-size: 12px;">
${L.orgNo} ${company.org_number}
${company.vat_number ? ` | ${L.vat} ${company.vat_number}` : ''}
${company.f_skatt ? ` | ${L.fSkatt}` : ''}
</p>
` : ''}
</div>
</div>
</body>
</html>
`
}
/**
* Generate plain text email for sending an invoice
*/
export function generateInvoiceEmailText(data: InvoiceEmailData): string {
const { invoice, customer } = data
const company = companyWithInvoicePaymentAccount(data.company, invoice.currency)
const lang = resolveLang(customer)
const L = LABELS[lang]
const documentType = getDocumentLabel(invoice, lang)
const isCreditNote = !!invoice.credited_invoice_id
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
const isDeliveryNote = docType === 'delivery_note'
const isProforma = docType === 'proforma'
const isQuote = docType === 'quote'
const hidePayment = isCreditNote || isDeliveryNote || isProforma || isQuote
const firstName = customer.name ? customer.name.split(' ')[0] : ''
const custom = resolveCustomTexts(data, lang)
const stockBody = isCreditNote
? L.bodyCreditNote
: isQuote
? L.bodyQuote(quoteValidUntil(invoice))
: L.bodyInvoice
let text = `${L.documentFrom(documentType, getCompanyDisplayName(company))}\n`
text += `${L.documentNumber(documentType)} ${invoice.invoice_number}\n\n`
text += `${custom.greeting ?? L.greeting(firstName)}\n\n`
text += `${custom.body ?? stockBody}\n\n`
text += `${L.documentSummary(documentType)}\n`
text += `---\n`
text += `${L.documentNumber(documentType)} ${invoice.invoice_number}\n`
text += `${L.documentDate(documentType)} ${formatDate(invoice.invoice_date)}\n`
text += isQuote
? `${L.validUntil} ${quoteValidUntil(invoice)}\n`
: `${L.dueDate} ${formatDate(invoice.due_date)}\n`
text += `${isQuote ? L.totalQuote : L.toPay} ${formatCurrencyForCustomer(getAmountToPay(invoice, company).toPay, invoice.currency, lang)}\n`
text += `---\n\n`
if (!hidePayment) {
text += `${L.paymentHeading}:\n`
if (invoice.payment_link_url) text += `${L.payOnline}: ${invoice.payment_link_url}\n`
if (company.bank_name) text += `${L.bank} ${company.bank_name}\n`
if (company.clearing_number && company.account_number) {
text += `${L.account} ${company.clearing_number}-${company.account_number}\n`
}
if (company.iban) text += `${L.iban} ${company.iban}\n`
if (company.bic) text += `${L.bic} ${company.bic}\n`
text += `${L.message} ${invoice.invoice_number}\n\n`
}
text += `${isQuote ? L.questionsQuote : L.questions}\n\n`
text += `${custom.signoff ?? L.sincerely}\n`
text += `${getCompanyDisplayName(company)}\n`
if (company.org_number) {
text += `\n${L.orgNo} ${company.org_number}`
if (company.vat_number) text += ` | ${L.vat} ${company.vat_number}`
if (company.f_skatt) text += ` | ${L.fSkatt}`
text += `\n`
}
return text
}
/**
* Generate email subject for an invoice
*/
export function generateInvoiceEmailSubject(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const lang = resolveLang(customer)
const L = LABELS[lang]
// Sanitization runs after substitution, so a pathological placeholder
// value containing a newline is also flattened to a single header line.
const custom = resolveCustomTexts(data, lang)
if (custom.subject !== undefined) return sanitizeSubjectLine(custom.subject)
const documentType = getDocumentLabel(invoice, lang)
return L.subjectFrom(documentType, invoice.invoice_number ?? '', getCompanyDisplayName(company))
}
// ---------------------------------------------------------------------------
// Payment confirmation (#1693)
//
// Sent on request for a paid faktura, with the BETALD re-render attached. It
// is not an invoice send: no custom invoice texts apply, no payment details
// are listed (nothing is due), and nothing here touches delivery history.
// ---------------------------------------------------------------------------
function paidAmountForCustomer(invoice: Invoice, company: CompanySettings): number {
// What the customer actually paid; the deduction-aware amount to pay is only
// the fallback for legacy rows marked paid before paid_amount was recorded.
return invoice.paid_amount ?? getAmountToPay(invoice, company).toPay
}
function paidDateForCustomer(invoice: Invoice): string | null {
return invoice.paid_at ? formatDate(invoice.paid_at) : null
}
export function generatePaymentConfirmationEmailSubject(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const L = LABELS[resolveLang(customer)]
return sanitizeSubjectLine(
L.confirmationSubject(invoice.invoice_number ?? '', getCompanyDisplayName(company)),
)
}
export function generatePaymentConfirmationEmailHtml(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const lang = resolveLang(customer)
const L = LABELS[lang]
const firstName = customer.name ? customer.name.split(' ')[0] : ''
const primaryColor = safeBrandingColor(company.invoice_primary_color, '#111111')
const paidDate = paidDateForCustomer(invoice)
const paidAmount = formatCurrencyForCustomer(paidAmountForCustomer(invoice, company), invoice.currency, lang)
const invoiceNumber = escapeHtml(invoice.invoice_number ?? '')
return `
<!DOCTYPE html>
<html lang="${L.htmlLang}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${escapeHtml(L.confirmationSubject(invoice.invoice_number ?? '', getCompanyDisplayName(company)))}</title>
</head>
<body style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; line-height: 1.6; color: #333;">
<div style="max-width: 600px; margin: 0 auto; padding: 40px 20px;">
<div style="margin-bottom: 30px; border-bottom: 2px solid ${primaryColor}; padding-bottom: 16px;">
<h1 style="margin: 0 0 10px 0; font-size: 24px; font-weight: 600; color: ${primaryColor};">
${escapeHtml(L.confirmationHeading(getCompanyDisplayName(company)))}
</h1>
<p style="margin: 0; color: #666; font-size: 14px;">
${L.documentNumber(L.docInvoice)} ${invoiceNumber}
</p>
</div>
<div style="margin-bottom: 30px;">
<p style="margin: 0 0 15px 0;">${escapeHtml(L.greeting(firstName))}</p>
<p style="margin: 0;">${escapeHtml(L.confirmationBody(invoice.invoice_number ?? ''))}</p>
</div>
<div style="background: #f8f9fa; border-radius: 8px; padding: 25px; margin-bottom: 30px;">
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${L.documentNumber(L.docInvoice)}</td>
<td style="padding: 8px 0; text-align: right; font-weight: 500;">${invoiceNumber}</td>
</tr>
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${L.documentDate(L.docInvoice)}</td>
<td style="padding: 8px 0; text-align: right;">${formatDate(invoice.invoice_date)}</td>
</tr>
${paidDate ? `
<tr>
<td style="padding: 8px 0; color: #666; font-size: 14px;">${L.confirmationPaidOn}</td>
<td style="padding: 8px 0; text-align: right;">${paidDate}</td>
</tr>
` : ''}
<tr>
<td colspan="2" style="padding: 15px 0 8px 0; border-top: 1px solid #e5e7eb;"></td>
</tr>
<tr>
<td style="padding: 8px 0; font-size: 18px; font-weight: 600;">${L.confirmationPaidAmount}</td>
<td style="padding: 8px 0; text-align: right; font-size: 18px; font-weight: 600; color: #059669;">
${paidAmount}
</td>
</tr>
</table>
</div>
<div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
<p style="margin: 0 0 10px 0; color: #666; font-size: 14px;">${L.confirmationQuestions}</p>
<p style="margin: 0; color: #666; font-size: 14px;">
${L.sincerely}<br>
<strong style="color: ${primaryColor};">${escapeHtml(getCompanyDisplayName(company))}</strong>
</p>
${company.org_number ? `
<p style="margin: 10px 0 0 0; color: #999; font-size: 12px;">
${L.orgNo} ${escapeHtml(company.org_number)}
${company.vat_number ? ` | ${L.vat} ${escapeHtml(company.vat_number)}` : ''}
${company.f_skatt ? ` | ${L.fSkatt}` : ''}
</p>
` : ''}
</div>
</div>
</body>
</html>
`
}
export function generatePaymentConfirmationEmailText(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const lang = resolveLang(customer)
const L = LABELS[lang]
const firstName = customer.name ? customer.name.split(' ')[0] : ''
const paidDate = paidDateForCustomer(invoice)
const number = invoice.invoice_number ?? ''
let text = `${L.confirmationHeading(getCompanyDisplayName(company))}\n`
text += `${L.documentNumber(L.docInvoice)} ${number}\n\n`
text += `${L.greeting(firstName)}\n\n`
text += `${L.confirmationBody(number)}\n\n`
text += `---\n`
text += `${L.documentNumber(L.docInvoice)} ${number}\n`
text += `${L.documentDate(L.docInvoice)} ${formatDate(invoice.invoice_date)}\n`
if (paidDate) text += `${L.confirmationPaidOn} ${paidDate}\n`
text += `${L.confirmationPaidAmount} ${formatCurrencyForCustomer(paidAmountForCustomer(invoice, company), invoice.currency, lang)}\n`
text += `---\n\n`
text += `${L.confirmationQuestions}\n\n`
text += `${L.sincerely}\n`
text += `${getCompanyDisplayName(company)}\n`
if (company.org_number) {
text += `\n${L.orgNo} ${company.org_number}`
if (company.vat_number) text += ` | ${L.vat} ${company.vat_number}`
if (company.f_skatt) text += ` | ${L.fSkatt}`
text += `\n`
}
return text
}