import type { Invoice, Customer, CompanySettings, InvoiceDocumentType } from '@/types'
import { formatCurrency, formatDate } from '@/lib/utils'
function getDocumentLabel(invoice: Invoice): string {
if (invoice.credited_invoice_id) return 'Kreditfaktura'
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
if (docType === 'proforma') return 'Proformafaktura'
if (docType === 'delivery_note') return 'Följesedel'
return 'Faktura'
}
export interface InvoiceEmailData {
invoice: Invoice
customer: Customer
company: CompanySettings
}
/**
* Generate HTML email for sending an invoice
*/
export function generateInvoiceEmailHtml(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const documentType = getDocumentLabel(invoice)
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 hidePayment = isCreditNote || isDeliveryNote || isProforma
return `
${documentType} ${invoice.invoice_number}
${documentType} från ${company.company_name}
${documentType}nummer: ${invoice.invoice_number}
Hej${customer.name ? ` ${customer.name.split(' ')[0]}` : ''},
${isCreditNote
? `Bifogat hittar du en kreditfaktura som korrigerar en tidigare faktura.`
: `Tack för ditt förtroende! Bifogat hittar du din faktura.`
}
| ${documentType}nummer: |
${invoice.invoice_number} |
| ${documentType}datum: |
${formatDate(invoice.invoice_date)} |
| Förfallodatum: |
${formatDate(invoice.due_date)}
|
|
| Att betala: |
${formatCurrency(invoice.total, invoice.currency)}
|
${!hidePayment ? `
Betalningsinformation
${company.bank_name ? `
| Bank: |
${company.bank_name} |
` : ''}
${company.clearing_number && company.account_number ? `
| Kontonummer: |
${company.clearing_number}-${company.account_number} |
` : ''}
${company.iban ? `
| IBAN: |
${company.iban} |
` : ''}
${company.bic ? `
| BIC/SWIFT: |
${company.bic} |
` : ''}
| Meddelande: |
${invoice.invoice_number} |
` : ''}
Har du frågor om fakturan? Svara direkt på detta mejl så hjälper vi dig.
Med vänliga hälsningar,
${company.company_name}
${company.org_number ? `
Org.nr: ${company.org_number}
${company.vat_number ? ` | VAT: ${company.vat_number}` : ''}
${company.f_skatt ? ' | Innehar F-skattsedel' : ''}
` : ''}
`
}
/**
* Generate plain text email for sending an invoice
*/
export function generateInvoiceEmailText(data: InvoiceEmailData): string {
const { invoice, customer, company } = data
const documentType = getDocumentLabel(invoice)
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 hidePayment = isCreditNote || isDeliveryNote || isProforma
let text = `${documentType} från ${company.company_name}\n`
text += `${documentType}nummer: ${invoice.invoice_number}\n\n`
text += `Hej${customer.name ? ` ${customer.name.split(' ')[0]}` : ''},\n\n`
if (isCreditNote) {
text += `Bifogat hittar du en kreditfaktura som korrigerar en tidigare faktura.\n\n`
} else {
text += `Tack för ditt förtroende! Bifogat hittar du din faktura.\n\n`
}
text += `${documentType}sammanfattning:\n`
text += `---\n`
text += `${documentType}nummer: ${invoice.invoice_number}\n`
text += `${documentType}datum: ${formatDate(invoice.invoice_date)}\n`
text += `Förfallodatum: ${formatDate(invoice.due_date)}\n`
text += `Att betala: ${formatCurrency(invoice.total, invoice.currency)}\n`
text += `---\n\n`
if (!hidePayment) {
text += `Betalningsinformation:\n`
if (company.bank_name) text += `Bank: ${company.bank_name}\n`
if (company.clearing_number && company.account_number) {
text += `Kontonummer: ${company.clearing_number}-${company.account_number}\n`
}
if (company.iban) text += `IBAN: ${company.iban}\n`
if (company.bic) text += `BIC/SWIFT: ${company.bic}\n`
text += `Meddelande: ${invoice.invoice_number}\n\n`
}
text += `Har du frågor om fakturan? Svara direkt på detta mejl så hjälper vi dig.\n\n`
text += `Med vänliga hälsningar,\n`
text += `${company.company_name}\n`
if (company.org_number) {
text += `\nOrg.nr: ${company.org_number}`
if (company.vat_number) text += ` | VAT: ${company.vat_number}`
if (company.f_skatt) text += ` | Innehar F-skattsedel`
text += `\n`
}
return text
}
/**
* Generate email subject for an invoice
*/
export function generateInvoiceEmailSubject(data: InvoiceEmailData): string {
const { invoice, company } = data
const documentType = getDocumentLabel(invoice)
return `${documentType} ${invoice.invoice_number} från ${company.company_name}`
}