Per-line VAT rates: - Add generatePerRateLines() to group invoice items by vat_rate with separate revenue + VAT lines per rate group (invoice-entries.ts) - Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts) - PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices - Invoice create/review UI supports per-line rate selection - Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput Invoice document types (proforma, delivery note): - Add InvoiceDocumentType, document_type and converted_from_id to Invoice type - PDF hides prices for delivery notes, adds proforma notice - Email templates support all document types - mark-paid skips journal entries for non-invoice document types - Migration 031: invoice_document_type Accounting method support: - Add AccountingMethod type (accrual/cash) - Migration 032: add_accounting_method column to company_settings VAT declaration rewrite: - Rewrite to read directly from general ledger (26xx/3xxx account lines) instead of aggregating invoices/transactions/receipts - ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances Bank reconciliation: - Transaction ingest now pre-fetches unlinked GL lines and attempts auto-reconciliation during import - Add transaction.reconciled event type - Add ReconciliationMethod type and reconciliation_method on Transaction - Migration 030: bank_reconciliation - New reconciliation engine, API routes, and BankReconciliationView component Pagination (fetchAllRows): - New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit - Adopted in all report generators, SIE/SRU export, account list APIs Fiscal period validation: - New validate-period-duration.ts enforces max 18 months per BFL 3 kap. - Applied in period-service.ts and fiscal-periods API Account mapper simplification: - Remove Levenshtein/fuzzy matching, use exact account number match only Swedbank parser improvements: - Support abbreviated headers (Clnr, Bokfdag, Radnr) - Use Referens column as counterparty Chart of accounts management: - Add DELETE endpoint with system account and usage protection - PUT uses partial updates - New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager Tax deadline corrections: - Rewrite inkomstdeklaration_ab using Skatteverket lookup table - Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3 Onboarding first fiscal year: - Add first fiscal year toggle with date pickers and 18-month validation UI terminology: - Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout Report column fix: - Fix start_date/end_date to period_start/period_end in report queries Supplier invoice input: - CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept) Misc: - SIE import uses upsert for idempotent account creation - account-descriptions.ts falls back to BAS reference data - Add invoice_default_notes to CompanySettings - Update CLAUDE.md to reflect current project state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
311 lines
14 KiB
TypeScript
311 lines
14 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import Link from 'next/link'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent } from '@/components/ui/card'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
|
import { PageHeader } from '@/components/ui/page-header'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { formatCurrency, formatDate } from '@/lib/utils'
|
|
import { Plus, Search, Receipt, FileText, Send, CheckCircle, Clock, XCircle, ReceiptText, AlertTriangle, FileQuestion, Truck } from 'lucide-react'
|
|
import { EmptyInvoices } from '@/components/ui/empty-state'
|
|
import type { Invoice, InvoiceStatus } from '@/types'
|
|
|
|
const statusConfig: Record<InvoiceStatus, { label: string; variant: 'default' | 'secondary' | 'success' | 'warning' | 'destructive'; icon: React.ElementType; borderColor: string }> = {
|
|
draft: { label: 'Utkast', variant: 'secondary', icon: FileText, borderColor: 'border-l-muted-foreground/30' },
|
|
sent: { label: 'Skickad', variant: 'default', icon: Send, borderColor: 'border-l-warning' },
|
|
paid: { label: 'Betald', variant: 'success', icon: CheckCircle, borderColor: 'border-l-success' },
|
|
overdue: { label: 'Förfallen', variant: 'destructive', icon: Clock, borderColor: 'border-l-destructive' },
|
|
cancelled: { label: 'Makulerad', variant: 'secondary', icon: XCircle, borderColor: 'border-l-muted-foreground/30' },
|
|
credited: { label: 'Krediterad', variant: 'secondary', icon: XCircle, borderColor: 'border-l-muted-foreground/30' },
|
|
}
|
|
|
|
function getRelativeTimeLabel(dueDateStr: string, status: InvoiceStatus): { text: string; color: string } | null {
|
|
if (status === 'paid' || status === 'cancelled' || status === 'credited' || status === 'draft') return null
|
|
|
|
const today = new Date()
|
|
today.setHours(0, 0, 0, 0)
|
|
const dueDate = new Date(dueDateStr)
|
|
dueDate.setHours(0, 0, 0, 0)
|
|
const diffDays = Math.round((dueDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24))
|
|
|
|
if (diffDays < 0) {
|
|
return { text: `${Math.abs(diffDays)} dagar försenad`, color: 'text-destructive' }
|
|
} else if (diffDays === 0) {
|
|
return { text: 'Förfaller idag', color: 'text-warning-foreground' }
|
|
} else if (diffDays <= 3) {
|
|
return { text: `${diffDays} dagar kvar`, color: 'text-warning-foreground' }
|
|
} else if (diffDays <= 7) {
|
|
return { text: `${diffDays} dagar kvar`, color: 'text-muted-foreground' }
|
|
}
|
|
return null
|
|
}
|
|
|
|
export default function InvoicesPage() {
|
|
const [invoices, setInvoices] = useState<Invoice[]>([])
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [searchTerm, setSearchTerm] = useState('')
|
|
const [activeTab, setActiveTab] = useState('all')
|
|
const { toast } = useToast()
|
|
const supabase = createClient()
|
|
|
|
useEffect(() => {
|
|
fetchInvoices()
|
|
}, [])
|
|
|
|
async function fetchInvoices() {
|
|
setIsLoading(true)
|
|
const { data, error } = await supabase
|
|
.from('invoices')
|
|
.select('*, customer:customers(name)')
|
|
.order('invoice_date', { ascending: false })
|
|
|
|
if (error) {
|
|
toast({
|
|
title: 'Fel',
|
|
description: 'Kunde inte hämta fakturor',
|
|
variant: 'destructive',
|
|
})
|
|
} else {
|
|
setInvoices(data || [])
|
|
}
|
|
setIsLoading(false)
|
|
}
|
|
|
|
const filteredInvoices = invoices.filter((invoice) => {
|
|
const matchesSearch =
|
|
invoice.invoice_number.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
(invoice.customer as { name: string })?.name?.toLowerCase().includes(searchTerm.toLowerCase())
|
|
|
|
const isCreditNote = !!invoice.credited_invoice_id
|
|
const docType = (invoice as Invoice & { document_type?: string }).document_type || 'invoice'
|
|
const matchesTab =
|
|
activeTab === 'all' ||
|
|
(activeTab === 'unpaid' && ['sent', 'overdue'].includes(invoice.status) && !isCreditNote && docType === 'invoice') ||
|
|
(activeTab === 'credit' && isCreditNote) ||
|
|
(activeTab === 'proforma' && docType === 'proforma') ||
|
|
(activeTab === 'delivery_note' && docType === 'delivery_note') ||
|
|
(activeTab !== 'proforma' && activeTab !== 'delivery_note' && invoice.status === activeTab)
|
|
|
|
return matchesSearch && matchesTab
|
|
})
|
|
|
|
const stats = {
|
|
unpaid: invoices.filter((i) => ['sent', 'overdue'].includes(i.status)).length,
|
|
unpaidAmount: invoices
|
|
.filter((i) => ['sent', 'overdue'].includes(i.status))
|
|
.reduce((sum, i) => sum + Number(i.total_sek || i.total), 0),
|
|
overdue: invoices.filter((i) => i.status === 'overdue').length,
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<PageHeader
|
|
title="Fakturor"
|
|
description="Skapa och hantera dina fakturor"
|
|
action={
|
|
<Link href="/invoices/new">
|
|
<Button>
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
Ny faktura
|
|
</Button>
|
|
</Link>
|
|
}
|
|
/>
|
|
|
|
{/* Stats */}
|
|
<div className="grid gap-4 md:grid-cols-3">
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-4">
|
|
<div className="h-12 w-12 rounded-lg bg-muted flex items-center justify-center">
|
|
<Receipt className="h-6 w-6 text-muted-foreground" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-muted-foreground">Totalt antal</p>
|
|
<p className="text-2xl font-bold tabular-nums">{invoices.length}</p>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-4">
|
|
<div className="h-12 w-12 rounded-lg bg-muted flex items-center justify-center">
|
|
<Clock className="h-6 w-6 text-muted-foreground" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-muted-foreground">Obetalda</p>
|
|
<div className="flex items-center gap-2">
|
|
<p className="text-2xl font-bold tabular-nums">{stats.unpaid}</p>
|
|
{stats.overdue > 0 && (
|
|
<Badge variant="destructive" className="text-xs">
|
|
{stats.overdue} förfallna
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-4">
|
|
<div className="h-12 w-12 rounded-lg bg-muted flex items-center justify-center">
|
|
<Send className="h-6 w-6 text-muted-foreground" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-muted-foreground">Att få in</p>
|
|
<p className="text-2xl font-bold tabular-nums">{formatCurrency(stats.unpaidAmount)}</p>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
|
|
{/* Search and tabs */}
|
|
<div className="flex flex-col sm:flex-row gap-4">
|
|
<div className="relative flex-1">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input
|
|
placeholder="Sök på fakturanummer eller kund..."
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
className="pl-10"
|
|
/>
|
|
</div>
|
|
<Tabs value={activeTab} onValueChange={setActiveTab}>
|
|
<TabsList>
|
|
<TabsTrigger value="all">Alla</TabsTrigger>
|
|
<TabsTrigger value="unpaid">Obetalda</TabsTrigger>
|
|
<TabsTrigger value="paid">Betalda</TabsTrigger>
|
|
<TabsTrigger value="draft">Utkast</TabsTrigger>
|
|
<TabsTrigger value="proforma">Proforma</TabsTrigger>
|
|
<TabsTrigger value="delivery_note">Följesedel</TabsTrigger>
|
|
<TabsTrigger value="credit">Kredit</TabsTrigger>
|
|
</TabsList>
|
|
</Tabs>
|
|
</div>
|
|
|
|
{/* Invoice list */}
|
|
{isLoading ? (
|
|
<div className="space-y-4">
|
|
{[1, 2, 3].map((i) => (
|
|
<Card key={i} className="animate-pulse">
|
|
<CardContent className="py-4">
|
|
<div className="flex items-center justify-between">
|
|
<div className="space-y-2">
|
|
<div className="h-5 bg-muted rounded w-32" />
|
|
<div className="h-4 bg-muted rounded w-48" />
|
|
</div>
|
|
<div className="h-8 bg-muted rounded w-24" />
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : filteredInvoices.length === 0 ? (
|
|
<Card>
|
|
<CardContent>
|
|
{searchTerm ? (
|
|
<div className="flex flex-col items-center justify-center py-12">
|
|
<Receipt className="h-12 w-12 text-muted-foreground mb-4" />
|
|
<h3 className="text-lg font-medium">Inga träffar</h3>
|
|
<p className="text-muted-foreground text-center mt-1">
|
|
Inga fakturor matchar "{searchTerm}"
|
|
</p>
|
|
</div>
|
|
) : invoices.length === 0 ? (
|
|
<EmptyInvoices />
|
|
) : (
|
|
<div className="flex flex-col items-center justify-center py-12">
|
|
<Receipt className="h-12 w-12 text-muted-foreground mb-4" />
|
|
<h3 className="text-lg font-medium">Inga fakturor i denna kategori</h3>
|
|
<p className="text-muted-foreground text-center mt-1">
|
|
Prova att byta flik för att se fler fakturor
|
|
</p>
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="space-y-2">
|
|
{filteredInvoices.map((invoice) => {
|
|
const status = statusConfig[invoice.status]
|
|
const isCreditNote = !!invoice.credited_invoice_id
|
|
const docType = (invoice as Invoice & { document_type?: string }).document_type || 'invoice'
|
|
const isProforma = docType === 'proforma'
|
|
const isDeliveryNote = docType === 'delivery_note'
|
|
const StatusIcon = isCreditNote ? ReceiptText : isProforma ? FileQuestion : isDeliveryNote ? Truck : status.icon
|
|
const relativeTime = invoice.due_date ? getRelativeTimeLabel(invoice.due_date, invoice.status) : null
|
|
const borderClass = isCreditNote ? 'border-l-4 border-l-destructive/50' : isProforma ? 'border-l-4 border-l-blue-400' : isDeliveryNote ? 'border-l-4 border-l-emerald-400' : `border-l-4 ${status.borderColor}`
|
|
|
|
return (
|
|
<Link key={invoice.id} href={`/invoices/${invoice.id}`}>
|
|
<Card className={`hover:border-primary/50 transition-colors cursor-pointer ${borderClass} ${invoice.status === 'overdue' ? 'ring-1 ring-destructive/20' : ''}`}>
|
|
<CardContent className="py-4">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-4">
|
|
<div className={`h-10 w-10 rounded-lg flex items-center justify-center ${isCreditNote ? 'bg-destructive/10' : 'bg-muted'}`}>
|
|
<StatusIcon className={`h-5 w-5 ${isCreditNote ? 'text-destructive' : 'text-muted-foreground'}`} />
|
|
</div>
|
|
<div>
|
|
<div className="flex items-center gap-2">
|
|
<p className="font-medium">{invoice.invoice_number}</p>
|
|
{isCreditNote && (
|
|
<Badge variant="destructive" className="text-xs">
|
|
Kredit
|
|
</Badge>
|
|
)}
|
|
{isProforma && (
|
|
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-700">
|
|
Proforma
|
|
</Badge>
|
|
)}
|
|
{isDeliveryNote && (
|
|
<Badge variant="secondary" className="text-xs bg-emerald-100 text-emerald-700">
|
|
Följesedel
|
|
</Badge>
|
|
)}
|
|
<Badge variant={status.variant as 'default' | 'secondary' | 'destructive'}>
|
|
{status.label}
|
|
</Badge>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<p className="text-sm text-muted-foreground">
|
|
{(invoice.customer as { name: string })?.name} · {formatDate(invoice.invoice_date)}
|
|
</p>
|
|
{relativeTime && (
|
|
<span className={`text-xs font-medium ${relativeTime.color}`}>
|
|
{relativeTime.text}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="text-right">
|
|
<p className={`font-medium tabular-nums ${isCreditNote ? 'text-destructive' : ''}`}>
|
|
{formatCurrency(Number(invoice.total), invoice.currency)}
|
|
</p>
|
|
{invoice.currency !== 'SEK' && invoice.total_sek && (
|
|
<p className={`text-sm tabular-nums ${isCreditNote ? 'text-destructive/70' : 'text-muted-foreground'}`}>
|
|
{formatCurrency(Number(invoice.total_sek))}
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</Link>
|
|
)
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|