* fix: include reversed entries in all reports (general ledger, trial balance, VAT, SIE, NE, INK2) Reversed entries (storno) must appear alongside their original posted entries in reports for a complete audit trail. Previously, filtering by status='posted' excluded them, causing discrepancies when corrections had been made. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: semi-manual invoice payment booking with editable journal lines When marking an invoice as paid, users now see a dialog where they can: - Choose which bank/cash account the payment goes to (1910, 1920, 1930, etc.) - Review and edit the proposed journal entry lines before committing - The happy path remains fast — lines are pre-filled correctly Implementation: - Pure proposePaymentLines() function for line computation (accrual + cash) - PaymentBookingDialog with AccountCombobox, balance validation, date picker - API accepts optional custom lines, falls back to auto-generation without them - 18 tests (8 unit + 10 API) all passing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — validation fallback, balance check, error handling - P1: Return 400 on invalid body instead of silently falling back to auto-generated lines (split JSON parse from schema validation) - P1: Add server-side balance check for custom lines before committing (debit must equal credit, totalDebit > 0) - P2: Wrap PaymentBookingDialog init() in try/catch with toast on failure and auto-close instead of silent empty state - Add 2 new tests: unbalanced lines → 400, invalid schema → 400 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: OAuth callback redirect for local dev and timeout resilience - Pass redirectUri dynamically from NEXT_PUBLIC_APP_URL so OAuth callbacks work on localhost (not just production) - Encode consentId/provider in OAuth state (base64url JSON) so the callback doesn't depend on session storage - Add skipAuth flag to extension API routes for OAuth callbacks (external provider redirects have no user session cookie) - Wrap AbortError in descriptive timeout messages in arcim-client - Make preview endpoint resilient to partial failures (company info and SIE fetch are individually non-blocking) - Simplify login page (remove unused magic link auth mode) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: create journal entry before marking invoice as paid Move journal entry creation before the invoice status update so that if accounting fails, the invoice is not permanently marked paid without a corresponding entry. Previously the error was silently swallowed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update mark-paid tests for journal-first ordering Reorder mock queue to match new flow (settings before update), update failure test to expect 500 instead of silent success, add try-catch with proper error response in route handler. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add reverse charge VAT (ruta 20-32) and improve mobile UX across dashboard Add full reverse charge (omvänd skattskyldighet) support to the VAT declaration: - Map accounts 2614/2624/2634 to ruta 30/31/32 for self-assessed output VAT - Calculate purchase bases (ruta 20-24) from supplier invoices by supplier type - Include ruta 30-32 in ruta 49 formula and totalOutputVat summary - Display reverse charge section in reports UI and composition chart - Add comprehensive test coverage for all reverse charge scenarios Improve mobile UX across the app: - Convert nav drawer to bottom sheet with drag handle and safe area padding - Add mobile card layout for PaymentBookingDialog journal lines - Replace settings tab pills with dropdown selector on mobile - Make wizard step indicators responsive (collapsed on mobile) - Ensure all dialog footers stack buttons full-width on mobile - Add 44px minimum touch targets throughout - Make onboarding buttons full-width on mobile Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — indentation, query efficiency, tab dedup - Fix misleading try-block indentation in mark-paid route - Filter reversed entries at DB level (.eq('status', 'posted')) instead of fetching then discarding in memory - Extract shared settingsTabs array so mobile Select and desktop TabsList stay in sync automatically Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add resilience fallbacks, Arcim retry logic, and client tests Add FallbackPrompt component and integrate it across banking and migration error states so users always have a manual import escape hatch. Add retry with exponential backoff to Arcim API client for transient failures (429, 502, 503, 504) and timeouts. Expand import page deep-linking with ?mode= parameter. Add persistent error banner on settings page for bank connection failures. Include 18 new tests for the Arcim client covering retry, backoff, pagination, timeout, env validation, and singleton resource unwrapping. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — setActiveTab, test cleanup, redundant clearTimeout - Add missing setActiveTab('banking') when handling bank_error query param so the error banner is actually visible (P1) - Guard env-var cleanup with try/finally in arcim-client tests to prevent state leakage on assertion failure (P2) - Only mock retry-range setTimeout delays in backoff test, letting AbortController timers pass through real setTimeout (P2) - Remove redundant clearTimeout in catch block — finally handles it (P2) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add BFL-compliant counterparty names to journal descriptions and cancelled entry status Journal descriptions now include customer/supplier names for traceability (e.g. "Kundfaktura 1001, Foretag AB"). Failed draft entries are marked as 'cancelled' instead of deleted, respecting immutability constraints. Includes DB migration for the new journal_entries status value. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — Swedish typos, missing source type, trigger and reversal cleanup - Fix Swedish spelling: leverantor → leverantör in all supplier description prefixes - Add supplier_credit_note to supplierSourceTypes in VAT declaration so credit notes correctly reduce reverse-charge bases (ruta 20–24) - Mark orphaned concurrent reversals as cancelled instead of attempting deletion that the immutability trigger blocks - Allow posted → cancelled transition in trigger for orphaned reversal cleanup - Restrict cancelled entry line trigger to DELETE-only (block INSERT/UPDATE) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use main's Step3TaxRegistration (onboarding restructured in PR #54) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: retrigger Greptile review Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
538 lines
18 KiB
TypeScript
538 lines
18 KiB
TypeScript
import { createJournalEntry, findFiscalPeriod } from './engine'
|
|
import { resolveSekAmount, buildCurrencyMetadata } from './currency-utils'
|
|
import { generateSalesVatLines } from './vat-entries'
|
|
import { getVatTreatmentForRate } from '@/lib/invoices/vat-rules'
|
|
import { createLogger } from '@/lib/logger'
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import type {
|
|
CreateJournalEntryInput,
|
|
CreateJournalEntryLineInput,
|
|
EntityType,
|
|
Invoice,
|
|
InvoiceItem,
|
|
JournalEntry,
|
|
VatTreatment,
|
|
} from '@/types'
|
|
|
|
const log = createLogger('invoice-entries')
|
|
|
|
/**
|
|
* Build a BFL-compliant verifikation description with event type and counterparty.
|
|
* Falls back to prefix + invoiceNumber if name is not provided (backward compat).
|
|
*/
|
|
function buildInvoiceDescription(
|
|
prefix: string, invoiceNumber: string, counterpartyName?: string
|
|
): string {
|
|
return counterpartyName
|
|
? `${prefix} ${invoiceNumber}, ${counterpartyName}`
|
|
: `${prefix} ${invoiceNumber}`
|
|
}
|
|
|
|
/**
|
|
* Group invoice items by VAT rate and generate per-rate revenue + VAT lines.
|
|
* Returns credit lines only (revenue + VAT). The caller adds the debit side.
|
|
*/
|
|
function generatePerRateLines(
|
|
items: InvoiceItem[],
|
|
invoiceVatTreatment: VatTreatment,
|
|
entityType: EntityType,
|
|
invoiceNumber: string,
|
|
currency?: string | null,
|
|
exchangeRate?: number | null
|
|
): CreateJournalEntryLineInput[] {
|
|
const lines: CreateJournalEntryLineInput[] = []
|
|
const isForeign = currency != null && currency !== 'SEK'
|
|
|
|
// Helper: convert item amount to SEK when dealing with foreign currency
|
|
const toSek = (amount: number): number => {
|
|
if (!isForeign) return amount
|
|
if (exchangeRate != null && exchangeRate > 0) {
|
|
return Math.round(amount * exchangeRate * 100) / 100
|
|
}
|
|
return amount // fallback for legacy data
|
|
}
|
|
|
|
// Check if items have per-line vat_rate set (new invoices)
|
|
const hasPerLineVat = items.some((item) => item.vat_rate !== undefined && item.vat_rate !== null)
|
|
|
|
if (!hasPerLineVat) {
|
|
// Legacy fallback: single rate from invoice level
|
|
const revenueAccount = getRevenueAccount(invoiceVatTreatment, entityType)
|
|
const subtotal = items.reduce((sum, item) => sum + item.line_total, 0)
|
|
const subtotalSek = toSek(subtotal)
|
|
lines.push({
|
|
account_number: revenueAccount,
|
|
debit_amount: 0,
|
|
credit_amount: subtotalSek,
|
|
line_description: `Försäljning faktura ${invoiceNumber}`,
|
|
})
|
|
|
|
const totalVat = items.reduce((sum, item) => sum + (item.vat_amount || 0), 0)
|
|
if (totalVat > 0) {
|
|
if (isForeign) {
|
|
// For foreign currency, compute VAT in SEK directly
|
|
const vatSek = toSek(totalVat)
|
|
const vatAccount = getOutputVatAccount(invoiceVatTreatment)
|
|
lines.push({
|
|
account_number: vatAccount,
|
|
debit_amount: 0,
|
|
credit_amount: vatSek,
|
|
line_description: `Utgående moms`,
|
|
})
|
|
} else {
|
|
const vatLines = generateSalesVatLines({
|
|
vatTreatment: invoiceVatTreatment,
|
|
baseAmount: subtotal,
|
|
direction: 'sales',
|
|
})
|
|
lines.push(...vatLines)
|
|
}
|
|
}
|
|
return lines
|
|
}
|
|
|
|
// Group items by vat_rate
|
|
const rateGroups = new Map<number, { subtotal: number; vatAmount: number }>()
|
|
for (const item of items) {
|
|
const rate = item.vat_rate ?? 0
|
|
const group = rateGroups.get(rate) || { subtotal: 0, vatAmount: 0 }
|
|
group.subtotal += item.line_total
|
|
group.vatAmount += item.vat_amount || 0
|
|
rateGroups.set(rate, group)
|
|
}
|
|
|
|
// Generate revenue + VAT lines per rate group
|
|
for (const [rate, group] of rateGroups) {
|
|
const treatment = rate === 0 && (invoiceVatTreatment === 'reverse_charge' || invoiceVatTreatment === 'export')
|
|
? invoiceVatTreatment
|
|
: getVatTreatmentForRate(rate)
|
|
const revenueAccount = getRevenueAccount(treatment, entityType)
|
|
const roundedSubtotal = Math.round(toSek(group.subtotal) * 100) / 100
|
|
|
|
lines.push({
|
|
account_number: revenueAccount,
|
|
debit_amount: 0,
|
|
credit_amount: roundedSubtotal,
|
|
line_description: `Försäljning faktura ${invoiceNumber}`,
|
|
})
|
|
|
|
const roundedVat = Math.round(toSek(group.vatAmount) * 100) / 100
|
|
if (roundedVat !== 0) {
|
|
const vatAccount = getOutputVatAccount(treatment)
|
|
lines.push({
|
|
account_number: vatAccount,
|
|
debit_amount: 0,
|
|
credit_amount: roundedVat,
|
|
line_description: `Utgående moms ${rate}%`,
|
|
})
|
|
}
|
|
}
|
|
|
|
return lines
|
|
}
|
|
|
|
/**
|
|
* Create journal entry when an invoice is created (status != draft)
|
|
*
|
|
* Supports mixed VAT rates per line item. Groups items by vat_rate
|
|
* and creates separate revenue + VAT lines per rate.
|
|
*
|
|
* Standard domestic invoice (25% VAT):
|
|
* Debit 1510 Kundfordringar [total incl VAT]
|
|
* Credit 30xx Försäljning [subtotal per rate]
|
|
* Credit 26xx Utgående moms [vat per rate]
|
|
*
|
|
* EU reverse charge:
|
|
* Debit 1510 Kundfordringar [subtotal]
|
|
* Credit 3308 Försäljning tjänst EU [subtotal]
|
|
*
|
|
* Export (non-EU):
|
|
* Debit 1510 Kundfordringar [subtotal]
|
|
* Credit 3305 Försäljning tjänst Export [subtotal]
|
|
*/
|
|
export async function createInvoiceJournalEntry(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
invoice: Invoice,
|
|
entityType: EntityType = 'enskild_firma',
|
|
customerName?: string
|
|
): Promise<JournalEntry | null> {
|
|
const fiscalPeriodId = await findFiscalPeriod(supabase, userId, invoice.invoice_date)
|
|
if (!fiscalPeriodId) {
|
|
log.warn('No open fiscal period found for invoice date:', invoice.invoice_date)
|
|
return null
|
|
}
|
|
|
|
const lines: CreateJournalEntryLineInput[] = []
|
|
const isForeign = invoice.currency !== 'SEK'
|
|
|
|
// Credit lines: revenue + VAT per rate group (compute first to guarantee balance)
|
|
const creditLines: CreateJournalEntryLineInput[] = []
|
|
|
|
if (invoice.items && invoice.items.length > 0) {
|
|
creditLines.push(...generatePerRateLines(
|
|
invoice.items, invoice.vat_treatment, entityType, invoice.invoice_number,
|
|
invoice.currency, invoice.exchange_rate
|
|
))
|
|
} else {
|
|
// Fallback: no items available, use invoice-level amounts
|
|
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
|
const subtotalSek = resolveSekAmount(invoice.subtotal, invoice.subtotal_sek, invoice.currency, invoice.exchange_rate)
|
|
|
|
creditLines.push({
|
|
account_number: revenueAccount,
|
|
debit_amount: 0,
|
|
credit_amount: subtotalSek,
|
|
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
|
})
|
|
|
|
if (invoice.vat_amount > 0) {
|
|
if (isForeign) {
|
|
const vatSek = resolveSekAmount(invoice.vat_amount, invoice.vat_amount_sek, invoice.currency, invoice.exchange_rate)
|
|
const vatAccount = getOutputVatAccount(invoice.vat_treatment)
|
|
creditLines.push({
|
|
account_number: vatAccount,
|
|
debit_amount: 0,
|
|
credit_amount: vatSek,
|
|
line_description: `Utgående moms faktura ${invoice.invoice_number}`,
|
|
})
|
|
} else {
|
|
const vatLines = generateSalesVatLines({
|
|
vatTreatment: invoice.vat_treatment,
|
|
baseAmount: invoice.subtotal,
|
|
direction: 'sales',
|
|
})
|
|
creditLines.push(...vatLines)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Debit: Kundfordringar — balance guarantee: debit = sum of all credit lines
|
|
const totalCredits = creditLines.reduce((sum, l) => sum + l.credit_amount, 0)
|
|
const debitAmount = isForeign
|
|
? Math.round(totalCredits * 100) / 100
|
|
: resolveSekAmount(invoice.total, invoice.total_sek, invoice.currency, invoice.exchange_rate)
|
|
|
|
lines.push({
|
|
account_number: '1510',
|
|
debit_amount: debitAmount,
|
|
credit_amount: 0,
|
|
line_description: `Faktura ${invoice.invoice_number}`,
|
|
...buildCurrencyMetadata(invoice.currency, isForeign ? invoice.total : undefined, invoice.exchange_rate),
|
|
})
|
|
|
|
lines.push(...creditLines)
|
|
|
|
const input: CreateJournalEntryInput = {
|
|
fiscal_period_id: fiscalPeriodId,
|
|
entry_date: invoice.invoice_date,
|
|
description: buildInvoiceDescription('Kundfaktura', invoice.invoice_number, customerName),
|
|
source_type: 'invoice_created',
|
|
source_id: invoice.id,
|
|
lines,
|
|
}
|
|
|
|
return createJournalEntry(supabase, userId, input)
|
|
}
|
|
|
|
/**
|
|
* Create journal entry when an invoice is marked as paid
|
|
*
|
|
* Debit 1930 Företagskonto [total]
|
|
* Credit 1510 Kundfordringar [total]
|
|
*/
|
|
export async function createInvoicePaymentJournalEntry(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
invoice: Invoice,
|
|
paymentDate: string,
|
|
exchangeRateDifference?: number,
|
|
customerName?: string
|
|
): Promise<JournalEntry | null> {
|
|
const fiscalPeriodId = await findFiscalPeriod(supabase, userId, paymentDate)
|
|
if (!fiscalPeriodId) {
|
|
log.warn('No open fiscal period found for payment date:', paymentDate)
|
|
return null
|
|
}
|
|
|
|
const desc = buildInvoiceDescription('Inbetalning kundfaktura', invoice.invoice_number, customerName)
|
|
const bookedSekAmount = resolveSekAmount(invoice.total, invoice.total_sek, invoice.currency, invoice.exchange_rate)
|
|
const lines: CreateJournalEntryLineInput[] = []
|
|
|
|
if (exchangeRateDifference && exchangeRateDifference !== 0) {
|
|
// Foreign currency with exchange rate difference
|
|
// For receivables: positive diff = gain (received more), negative = loss (received less)
|
|
const actualSekReceived = bookedSekAmount + exchangeRateDifference
|
|
|
|
// Debit: Bank at actual SEK received
|
|
lines.push({
|
|
account_number: '1930',
|
|
debit_amount: Math.round(actualSekReceived * 100) / 100,
|
|
credit_amount: 0,
|
|
line_description: desc,
|
|
})
|
|
|
|
// Credit: Clear kundfordringar at original booked SEK amount
|
|
lines.push({
|
|
account_number: '1510',
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(bookedSekAmount * 100) / 100,
|
|
line_description: desc,
|
|
})
|
|
|
|
// Exchange rate difference
|
|
if (exchangeRateDifference > 0) {
|
|
// Gain: Credit 3960 (received more than booked)
|
|
lines.push({
|
|
account_number: '3960',
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(exchangeRateDifference * 100) / 100,
|
|
line_description: 'Valutakursvinst',
|
|
})
|
|
} else {
|
|
// Loss: Debit 7960 (received less than booked)
|
|
lines.push({
|
|
account_number: '7960',
|
|
debit_amount: Math.round(Math.abs(exchangeRateDifference) * 100) / 100,
|
|
credit_amount: 0,
|
|
line_description: 'Valutakursförlust',
|
|
})
|
|
}
|
|
} else {
|
|
// Standard SEK payment or no exchange rate difference
|
|
lines.push(
|
|
{
|
|
account_number: '1930',
|
|
debit_amount: Math.round(bookedSekAmount * 100) / 100,
|
|
credit_amount: 0,
|
|
line_description: desc,
|
|
},
|
|
{
|
|
account_number: '1510',
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(bookedSekAmount * 100) / 100,
|
|
line_description: desc,
|
|
}
|
|
)
|
|
}
|
|
|
|
const input: CreateJournalEntryInput = {
|
|
fiscal_period_id: fiscalPeriodId,
|
|
entry_date: paymentDate,
|
|
description: desc,
|
|
source_type: 'invoice_paid',
|
|
source_id: invoice.id,
|
|
lines,
|
|
}
|
|
|
|
return createJournalEntry(supabase, userId, input)
|
|
}
|
|
|
|
/**
|
|
* Create journal entry for a credit note (reversed version of original invoice entry)
|
|
* Supports per-item VAT rates with reversed debit/credit sides.
|
|
*
|
|
* Debit 30xx Försäljning [subtotal per rate]
|
|
* Debit 26xx Utgående moms [vat per rate]
|
|
* Credit 1510 Kundfordringar [total]
|
|
*/
|
|
export async function createCreditNoteJournalEntry(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
creditNote: Invoice,
|
|
entityType: EntityType = 'enskild_firma',
|
|
customerName?: string
|
|
): Promise<JournalEntry | null> {
|
|
const fiscalPeriodId = await findFiscalPeriod(supabase, userId, creditNote.invoice_date)
|
|
if (!fiscalPeriodId) {
|
|
log.warn('No open fiscal period found for credit note date:', creditNote.invoice_date)
|
|
return null
|
|
}
|
|
|
|
const lines: CreateJournalEntryLineInput[] = []
|
|
|
|
// Generate reversed revenue + VAT lines per rate group (debit side for credit notes)
|
|
const debitLines: CreateJournalEntryLineInput[] = []
|
|
|
|
if (creditNote.items && creditNote.items.length > 0) {
|
|
// Use absolute items for generatePerRateLines, then swap debit/credit
|
|
const creditLines = generatePerRateLines(
|
|
creditNote.items, creditNote.vat_treatment, entityType, creditNote.invoice_number,
|
|
creditNote.currency, creditNote.exchange_rate
|
|
)
|
|
for (const line of creditLines) {
|
|
debitLines.push({
|
|
...line,
|
|
debit_amount: Math.abs(line.credit_amount),
|
|
credit_amount: Math.abs(line.debit_amount),
|
|
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
|
})
|
|
}
|
|
} else {
|
|
// Fallback: invoice-level amounts
|
|
const revenueAccount = getRevenueAccount(creditNote.vat_treatment, entityType)
|
|
const absSubtotal = Math.abs(resolveSekAmount(creditNote.subtotal, creditNote.subtotal_sek, creditNote.currency, creditNote.exchange_rate))
|
|
const absVat = Math.abs(resolveSekAmount(creditNote.vat_amount, creditNote.vat_amount_sek, creditNote.currency, creditNote.exchange_rate))
|
|
|
|
debitLines.push({
|
|
account_number: revenueAccount,
|
|
debit_amount: absSubtotal,
|
|
credit_amount: 0,
|
|
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
|
})
|
|
|
|
if (absVat > 0) {
|
|
const vatAccount = getOutputVatAccount(creditNote.vat_treatment)
|
|
debitLines.push({
|
|
account_number: vatAccount,
|
|
debit_amount: absVat,
|
|
credit_amount: 0,
|
|
line_description: `Moms kreditfaktura ${creditNote.invoice_number}`,
|
|
})
|
|
}
|
|
}
|
|
|
|
lines.push(...debitLines)
|
|
|
|
// Credit: Kundfordringar — balance guarantee: credit = sum of all debit lines
|
|
const totalDebits = debitLines.reduce((sum, l) => sum + l.debit_amount, 0)
|
|
lines.push({
|
|
account_number: '1510',
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(totalDebits * 100) / 100,
|
|
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
|
})
|
|
|
|
const input: CreateJournalEntryInput = {
|
|
fiscal_period_id: fiscalPeriodId,
|
|
entry_date: creditNote.invoice_date,
|
|
description: buildInvoiceDescription('Kreditfaktura', creditNote.invoice_number, customerName),
|
|
source_type: 'credit_note',
|
|
source_id: creditNote.id,
|
|
lines,
|
|
}
|
|
|
|
return createJournalEntry(supabase, userId, input)
|
|
}
|
|
|
|
/**
|
|
* Create journal entry for kontantmetoden (cash method) when payment is received.
|
|
* Supports per-item VAT rates. Revenue + VAT recognised at payment.
|
|
*
|
|
* Debit 1930 Företagskonto [total]
|
|
* Credit 30xx Försäljning [subtotal per rate]
|
|
* Credit 26xx Utgående moms [vat per rate] (if applicable)
|
|
*/
|
|
export async function createInvoiceCashEntry(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
invoice: Invoice,
|
|
paymentDate: string,
|
|
entityType: EntityType = 'enskild_firma',
|
|
customerName?: string
|
|
): Promise<JournalEntry | null> {
|
|
const fiscalPeriodId = await findFiscalPeriod(supabase, userId, paymentDate)
|
|
if (!fiscalPeriodId) {
|
|
log.warn('No open fiscal period found for payment date:', paymentDate)
|
|
return null
|
|
}
|
|
|
|
const lines: CreateJournalEntryLineInput[] = []
|
|
const isForeign = invoice.currency !== 'SEK'
|
|
|
|
// Credit lines: revenue + VAT per rate group (compute first to guarantee balance)
|
|
const creditLines: CreateJournalEntryLineInput[] = []
|
|
|
|
if (invoice.items && invoice.items.length > 0) {
|
|
creditLines.push(...generatePerRateLines(
|
|
invoice.items, invoice.vat_treatment, entityType, invoice.invoice_number,
|
|
invoice.currency, invoice.exchange_rate
|
|
))
|
|
} else {
|
|
// Fallback: invoice-level amounts
|
|
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
|
const subtotalSek = resolveSekAmount(invoice.subtotal, invoice.subtotal_sek, invoice.currency, invoice.exchange_rate)
|
|
|
|
creditLines.push({
|
|
account_number: revenueAccount,
|
|
debit_amount: 0,
|
|
credit_amount: subtotalSek,
|
|
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
|
})
|
|
|
|
if (invoice.vat_amount > 0) {
|
|
const vatSek = resolveSekAmount(invoice.vat_amount, invoice.vat_amount_sek, invoice.currency, invoice.exchange_rate)
|
|
const vatAccount = getOutputVatAccount(invoice.vat_treatment)
|
|
creditLines.push({
|
|
account_number: vatAccount,
|
|
debit_amount: 0,
|
|
credit_amount: vatSek,
|
|
line_description: `Utgående moms faktura ${invoice.invoice_number}`,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Debit: Företagskonto — balance guarantee: debit = sum of credit lines
|
|
const totalCredits = creditLines.reduce((sum, l) => sum + l.credit_amount, 0)
|
|
lines.push({
|
|
account_number: '1930',
|
|
debit_amount: isForeign ? Math.round(totalCredits * 100) / 100 : resolveSekAmount(invoice.total, invoice.total_sek, invoice.currency, invoice.exchange_rate),
|
|
credit_amount: 0,
|
|
line_description: buildInvoiceDescription('Kontantbetalning kundfaktura', invoice.invoice_number, customerName),
|
|
})
|
|
|
|
lines.push(...creditLines)
|
|
|
|
const input: CreateJournalEntryInput = {
|
|
fiscal_period_id: fiscalPeriodId,
|
|
entry_date: paymentDate,
|
|
description: buildInvoiceDescription('Kontantbetalning kundfaktura', invoice.invoice_number, customerName),
|
|
source_type: 'invoice_cash_payment',
|
|
source_id: invoice.id,
|
|
lines,
|
|
}
|
|
|
|
return createJournalEntry(supabase, userId, input)
|
|
}
|
|
|
|
/**
|
|
* Get the appropriate revenue account based on VAT treatment
|
|
*
|
|
* For 'exempt': AB uses 3004 (Försäljning inom Sverige, momsfri),
|
|
* EF uses 3100 (Momsfria intäkter, mapped to R2 in NE engine).
|
|
*/
|
|
export function getRevenueAccount(vatTreatment: VatTreatment, entityType: EntityType = 'enskild_firma'): string {
|
|
switch (vatTreatment) {
|
|
case 'standard_25':
|
|
return '3001' // Försäljning 25%
|
|
case 'reduced_12':
|
|
return '3002' // Försäljning 12%
|
|
case 'reduced_6':
|
|
return '3003' // Försäljning 6%
|
|
case 'reverse_charge':
|
|
return '3308' // Försäljning tjänst EU
|
|
case 'export':
|
|
return '3305' // Försäljning tjänst Export
|
|
case 'exempt':
|
|
return entityType === 'aktiebolag' ? '3004' : '3100'
|
|
default:
|
|
return '3001'
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the output VAT account based on VAT treatment
|
|
*/
|
|
export function getOutputVatAccount(vatTreatment: VatTreatment): string {
|
|
switch (vatTreatment) {
|
|
case 'standard_25':
|
|
return '2611'
|
|
case 'reduced_12':
|
|
return '2621'
|
|
case 'reduced_6':
|
|
return '2631'
|
|
default:
|
|
return '2611'
|
|
}
|
|
}
|