'use client' import { useState, useEffect, useMemo } from 'react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { useToast } from '@/components/ui/use-toast' import { JournalEntryReviewContent } from '@/components/bookkeeping/JournalEntryReviewContent' import { proposeSendLines } from '@/lib/bookkeeping/propose-send-lines' import { formatCurrency } from '@/lib/utils' import { createClient } from '@/lib/supabase/client' import { Loader2, Mail, Send } from 'lucide-react' import type { Invoice, InvoiceItem, Customer, EntityType } from '@/types' interface InvoiceWithRelations extends Invoice { customer: Customer items: InvoiceItem[] } interface SendInvoiceDialogProps { open: boolean onOpenChange: (open: boolean) => void invoice: InvoiceWithRelations /** 'email' sends via email, 'manual' marks as sent without email */ mode: 'email' | 'manual' onSuccess: () => void } export default function SendInvoiceDialog({ open, onOpenChange, invoice, mode, onSuccess, }: SendInvoiceDialogProps) { const { toast } = useToast() const supabase = createClient() const [isSubmitting, setIsSubmitting] = useState(false) const [accountingMethod, setAccountingMethod] = useState<'accrual' | 'cash'>('accrual') const [entityType, setEntityType] = useState('enskild_firma') const [periodName, setPeriodName] = useState('') const [isInitialized, setIsInitialized] = useState(false) useEffect(() => { if (!open) { setIsInitialized(false) return } let cancelled = false async function init() { try { // Fetch company settings const { data: settings, error } = await supabase .from('company_settings') .select('accounting_method, entity_type') .single() if (error) throw new Error('Kunde inte ladda företagsinställningar') if (cancelled) return // Fetch fiscal period for the invoice date const { data: period } = await supabase .from('fiscal_periods') .select('name') .lte('start_date', invoice.invoice_date) .gte('end_date', invoice.invoice_date) .single() if (cancelled) return setAccountingMethod((settings?.accounting_method || 'accrual') as 'accrual' | 'cash') setEntityType((settings?.entity_type as EntityType) || 'enskild_firma') setPeriodName(period?.name || '') setIsInitialized(true) } catch (err) { if (cancelled) return toast({ title: 'Kunde inte ladda inställningar', description: err instanceof Error ? err.message : 'Försök igen.', variant: 'destructive', }) onOpenChange(false) } } init() return () => { cancelled = true } }, [open, invoice.id, invoice.invoice_date]) const proposedLines = useMemo(() => { if (!isInitialized || accountingMethod !== 'accrual') return [] return proposeSendLines({ invoice: { invoice_number: invoice.invoice_number, total: invoice.total, total_sek: invoice.total_sek, subtotal: invoice.subtotal, subtotal_sek: invoice.subtotal_sek, vat_amount: invoice.vat_amount, vat_amount_sek: invoice.vat_amount_sek, currency: invoice.currency, exchange_rate: invoice.exchange_rate, vat_treatment: invoice.vat_treatment, items: invoice.items, }, entityType, }) }, [isInitialized, accountingMethod, entityType, invoice]) const { totalDebit, totalCredit } = useMemo(() => { let totalDebit = 0 let totalCredit = 0 for (const line of proposedLines) { totalDebit += parseFloat(line.debit_amount) || 0 totalCredit += parseFloat(line.credit_amount) || 0 } return { totalDebit, totalCredit } }, [proposedLines]) const handleConfirm = async () => { setIsSubmitting(true) try { const url = mode === 'email' ? `/api/invoices/${invoice.id}/send` : `/api/invoices/${invoice.id}/mark-sent` const response = await fetch(url, { method: 'POST' }) const data = await response.json() if (!response.ok) { throw new Error(data.error || 'Kunde inte skicka fakturan') } onSuccess() if (mode === 'email') { onOpenChange(false) toast({ title: 'Faktura skickad', description: data.message || `Fakturan har skickats till ${invoice.customer.email}`, }) } else { // For manual send, just close — no email to confirm onOpenChange(false) toast({ title: 'Faktura markerad som skickad', description: accountingMethod === 'accrual' ? 'Bokföringsverifikationen har skapats.' : undefined, }) } } catch (error) { toast({ title: 'Kunde inte skicka faktura', description: error instanceof Error ? error.message : 'Försök igen.', variant: 'destructive', }) } setIsSubmitting(false) } const handleClose = () => { onOpenChange(false) } const showJournalPreview = accountingMethod === 'accrual' && proposedLines.length > 0 return ( {mode === 'email' ? 'Skicka faktura' : 'Markera som skickad'} — {invoice.invoice_number} {formatCurrency(invoice.total, invoice.currency)} {invoice.currency !== 'SEK' && invoice.total_sek && ( <> ({formatCurrency(invoice.total_sek)} SEK) )} {mode === 'email' && invoice.customer.email && ( <> till {invoice.customer.email} )} {!isInitialized ? (
) : (
{showJournalPreview ? ( <>

Följande bokföringsverifikation skapas automatiskt:

) : (

{accountingMethod === 'cash' ? 'Kontantmetoden — bokföring sker vid betalning, inte vid fakturering.' : mode === 'email' ? `Fakturan skickas till ${invoice.customer.email}.` : 'Fakturan markeras som skickad.'}

)}
)}
) }