* feat: separate AR/AP/accounting into distinct nav groups (#92) Split the flat "Finans" sidebar group into three visually distinct sections — Försäljning (AR), Inköp (AP), and Redovisning — so users coming from Fortnox immediately find customer invoicing and supplier invoices as top-level concepts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: journal entry detail view, correction chain, and account name display - Add journal entry detail page at /bookkeeping/[id] with full entry view - Add correction chain API and component showing storno relationships - Add JournalEntryStatusBadge component for entry status display - Show debit/credit account names in template picker and review dialogs - Expand client-side BAS account name mapping with additional accounts - Show account codes on transaction inbox suggestion buttons Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address review feedback — N+1 query, duplicate name, nav dedup - Batch reverse-lookup into single query per BFS iteration (was N+1) - Differentiate account 2393 from 2893 in display names - Extract shared loop for desktop/mobile nav group rendering Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: counterparty templates, Skatteverket extension, VAT form completeness, and UI cleanup - Add counterparty-based categorization templates (learned from user approvals and auto-ingestion) with fuzzy matching in the mapping engine - Add Skatteverket extension for direct VAT declaration submission via API - Complete VAT declaration form with all 30 SKV 4700 boxes (ruta 08, 35-42, 50, 60-62) - Fix ruta 49 formula to include import VAT (ruta 60+61+62) - Simplify dashboard UI: remove redundant icons from stat cards, customer cards, invoice list, supplier invoices; use Badge variants consistently - Add SkatteverketPanel component to reports page - Add categorization_templates and skatteverket_tokens migrations - Update tests and helpers for new types Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address PR review feedback — VAT detection, migration timestamps, dedup - Fix detectVatTreatment to derive actual rate (12%/6%) from VAT line description instead of hardcoding standard_25 - Rename skatteverket_tokens migration to 20260324120001 to avoid duplicate timestamp with categorization_templates (fixes Supabase deployment failure) - Make refreshAccessToken accept previousRefreshCount param to enforce refresh limit contract at the type level - Fix rate limiter TOCTOU by claiming slot before await - Extract formatRedovisare/formatRedovisningsperiod to shared lib/skatteverket/format.ts — eliminates duplication between mappers.ts and SkatteverketPanel.tsx Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
214 lines
7.7 KiB
TypeScript
214 lines
7.7 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { Plus, Search, Users } from 'lucide-react'
|
|
import CustomerForm from '@/components/customers/CustomerForm'
|
|
import { EmptyCustomers } from '@/components/ui/empty-state'
|
|
import Link from 'next/link'
|
|
import type { Customer, CustomerType, CreateCustomerInput } from '@/types'
|
|
|
|
const customerTypeLabels: Record<CustomerType, string> = {
|
|
individual: 'Privatperson',
|
|
swedish_business: 'Svenskt företag',
|
|
eu_business: 'EU-företag',
|
|
non_eu_business: 'Utanför EU',
|
|
}
|
|
|
|
function getInitials(name: string): string {
|
|
return name
|
|
.split(' ')
|
|
.map((w) => w[0])
|
|
.slice(0, 2)
|
|
.join('')
|
|
.toUpperCase()
|
|
}
|
|
|
|
export default function CustomersPage() {
|
|
const [customers, setCustomers] = useState<Customer[]>([])
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [searchTerm, setSearchTerm] = useState('')
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false)
|
|
const [isCreating, setIsCreating] = useState(false)
|
|
const { toast } = useToast()
|
|
const supabase = createClient()
|
|
|
|
async function fetchCustomers() {
|
|
setIsLoading(true)
|
|
const { data, error } = await supabase
|
|
.from('customers')
|
|
.select('*')
|
|
.order('name', { ascending: true })
|
|
|
|
if (error) {
|
|
toast({
|
|
title: 'Kunde inte ladda kunder',
|
|
description: 'Kontrollera din anslutning och försök igen.',
|
|
variant: 'destructive',
|
|
})
|
|
} else {
|
|
setCustomers(data || [])
|
|
}
|
|
setIsLoading(false)
|
|
}
|
|
|
|
useEffect(() => {
|
|
fetchCustomers()
|
|
}, [])
|
|
|
|
async function handleCreateCustomer(data: CreateCustomerInput) {
|
|
setIsCreating(true)
|
|
|
|
const response = await fetch('/api/customers', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
|
|
const result = await response.json()
|
|
|
|
if (!response.ok) {
|
|
toast({
|
|
title: 'Kunde inte skapa kund',
|
|
description: result.error || 'Försök igen.',
|
|
variant: 'destructive',
|
|
})
|
|
} else {
|
|
toast({
|
|
title: 'Kund skapad',
|
|
description: `${data.name} har lagts till`,
|
|
})
|
|
setCustomers([...customers, result.data])
|
|
setIsDialogOpen(false)
|
|
}
|
|
|
|
setIsCreating(false)
|
|
}
|
|
|
|
const filteredCustomers = customers.filter((customer) =>
|
|
customer.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
customer.email?.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
customer.org_number?.includes(searchTerm)
|
|
)
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
|
<div>
|
|
<h1 className="font-display text-2xl md:text-3xl font-medium tracking-tight">Kunder</h1>
|
|
<p className="text-muted-foreground">
|
|
Hantera dina kunder och deras faktureringsuppgifter
|
|
</p>
|
|
</div>
|
|
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button>
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
Ny kund
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-2xl max-h-[95dvh] sm:max-h-[90vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<DialogTitle>Lägg till kund</DialogTitle>
|
|
</DialogHeader>
|
|
<CustomerForm
|
|
onSubmit={handleCreateCustomer}
|
|
isLoading={isCreating}
|
|
/>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
|
|
{/* Search */}
|
|
<div className="relative">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input
|
|
placeholder="Sök på namn, e-post eller org.nr..."
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
className="pl-10"
|
|
/>
|
|
</div>
|
|
|
|
{/* Customer list */}
|
|
{isLoading ? (
|
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
|
{[1, 2, 3].map((i) => (
|
|
<Card key={i} className="animate-pulse">
|
|
<CardHeader>
|
|
<div className="h-5 bg-muted rounded w-1/2" />
|
|
<div className="h-4 bg-muted rounded w-1/3 mt-2" />
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="h-4 bg-muted rounded w-full" />
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : filteredCustomers.length === 0 ? (
|
|
<Card>
|
|
<CardContent>
|
|
{searchTerm ? (
|
|
<div className="flex flex-col items-center justify-center py-12">
|
|
<Users 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 kunder matchar "{searchTerm}"
|
|
</p>
|
|
</div>
|
|
) : (
|
|
<EmptyCustomers onAction={() => setIsDialogOpen(true)} />
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
|
{filteredCustomers.map((customer) => (
|
|
<Link key={customer.id} href={`/customers/${customer.id}`}>
|
|
<Card className="cursor-pointer transition-all duration-150 hover:border-primary/50 hover:bg-accent/50 hover:shadow-sm motion-safe:active:scale-[0.99] active:shadow-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 h-full group">
|
|
<CardHeader className="pb-3">
|
|
<div className="flex items-center gap-3.5">
|
|
<div className="h-11 w-11 rounded-full bg-primary/10 flex items-center justify-center shrink-0 text-sm font-semibold text-primary tracking-tight">
|
|
{getInitials(customer.name)}
|
|
</div>
|
|
<div className="min-w-0">
|
|
<CardTitle className="text-base truncate group-hover:text-primary transition-colors">{customer.name}</CardTitle>
|
|
<Badge variant="secondary" className="mt-1">
|
|
{customerTypeLabels[customer.customer_type]}
|
|
</Badge>
|
|
</div>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="space-y-1.5 text-sm text-muted-foreground">
|
|
{customer.email && (
|
|
<p className="truncate">{customer.email}</p>
|
|
)}
|
|
{customer.org_number && (
|
|
<div className="flex items-center gap-2">
|
|
<span>{customer.org_number}</span>
|
|
{customer.vat_number_validated && (
|
|
<Badge variant="success" className="text-xs">Verifierad</Badge>
|
|
)}
|
|
</div>
|
|
)}
|
|
{customer.city && (
|
|
<p>{customer.city}, {customer.country}</p>
|
|
)}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|