'use client'; import { useState, useEffect } from 'react'; import { ArrowLeft, Plus, Trash2, Search } from 'lucide-react'; import { useRouter } from 'next/navigation'; import { useToast } from '../../components/ToastProvider'; const docTypes = [ { value: 'QUOTE', label: 'Angebot' }, { value: 'ORDER_CONFIRMATION', label: 'Auftragsbestätigung' }, { value: 'DELIVERY_NOTE', label: 'Lieferschein' }, { value: 'INVOICE', label: 'Rechnung' }, ]; export default function NewSalesDocPage() { const router = useRouter(); const { toast } = useToast(); const [type, setType] = useState('QUOTE'); const [customerId, setCustomerId] = useState(null); const [customerSearch, setCustomerSearch] = useState(''); const [customerResults, setCustomerResults] = useState([]); const [selectedCustomer, setSelectedCustomer] = useState(null); const [notes, setNotes] = useState(''); const [validUntil, setValidUntil] = useState(''); const [items, setItems] = useState([{ description: '', quantity: 1, unitPrice: 0, productId: null }]); const [products, setProducts] = useState([]); const [saving, setSaving] = useState(false); useEffect(() => { fetch('/api/products').then(r => r.json()).then(setProducts).catch(() => {}); }, []); const searchCustomers = async (q: string) => { setCustomerSearch(q); if (q.length < 2) { setCustomerResults([]); return; } const res = await fetch(`/api/customers/search?q=${encodeURIComponent(q)}`); if (res.ok) setCustomerResults(await res.json()); }; const selectCustomer = (c: any) => { setSelectedCustomer(c); setCustomerId(c.id); setCustomerSearch(c.companyName || `${c.firstName} ${c.lastName}`); setCustomerResults([]); }; const updateItem = (idx: number, field: string, value: any) => { const newItems = [...items]; newItems[idx] = { ...newItems[idx], [field]: value }; // Auto-fill price from product if (field === 'productId' && value) { const prod = products.find(p => p.id === parseInt(value)); if (prod) { newItems[idx].unitPrice = prod.salePrice; newItems[idx].description = prod.name; } } setItems(newItems); }; const addItem = () => setItems([...items, { description: '', quantity: 1, unitPrice: 0, productId: null }]); const removeItem = (idx: number) => setItems(items.filter((_, i) => i !== idx)); const subtotal = items.reduce((sum, i) => sum + (i.quantity * i.unitPrice), 0); const tax = subtotal * 0.19; const total = subtotal + tax; const handleSubmit = async () => { if (!customerId) { toast('Bitte Kunde auswählen', 'error'); return; } if (items.length === 0 || !items[0].description) { toast('Bitte mindestens eine Position', 'error'); return; } setSaving(true); const res = await fetch('/api/sales', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ type, customerId, notes, validUntil: validUntil || null, items: items.map(i => ({ description: i.description, quantity: parseFloat(i.quantity) || 1, unitPrice: parseFloat(i.unitPrice) || 0, productId: i.productId ? parseInt(i.productId) : null })) }) }); if (res.ok) { const doc = await res.json(); toast('Beleg erstellt', 'success'); router.push(`/sales/${doc.id}`); } else { toast('Fehler beim Erstellen', 'error'); } setSaving(false); }; return (

Neuen Beleg erstellen

{/* Left: Details */}

Belegdaten

setValidUntil(e.target.value)} />
searchCustomers(e.target.value)} />
{customerResults.length > 0 && (
{customerResults.map(c => ( ))}
)} {selectedCustomer &&

✓ {selectedCustomer.companyName || `${selectedCustomer.firstName} ${selectedCustomer.lastName}`}

}