import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Separator } from '@/components/ui/separator'; import { Receipt, Users, CreditCard, Percent } from 'lucide-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { toast } from 'sonner'; interface Bill { id: string; tableNumber: number; items: BillItem[]; subtotal: number; tip: number; total: number; status: 'open' | 'split' | 'paid'; splits?: number; } interface BillItem { name: string; quantity: number; price: number; } const BillManagement = () => { const [bills, setBills] = useState([ { id: '1', tableNumber: 5, items: [ { name: 'Paella Valenciana', quantity: 2, price: 18.50 }, { name: 'Gazpacho', quantity: 2, price: 6.50 }, { name: 'Vino Tinto', quantity: 1, price: 12.00 } ], subtotal: 62.00, tip: 6.20, total: 68.20, status: 'open' }, { id: '2', tableNumber: 3, items: [ { name: 'Pulpo a la Gallega', quantity: 1, price: 16.00 } ], subtotal: 16.00, tip: 0, total: 16.00, status: 'open' } ]); const [selectedBill, setSelectedBill] = useState(null); const [tipPercentage, setTipPercentage] = useState('10'); const [splitCount, setSplitCount] = useState('2'); const calculateTip = (subtotal: number, percentage: string) => { const percent = parseFloat(percentage) || 0; return (subtotal * percent) / 100; }; const applySplitBill = () => { if (!selectedBill) return; const splits = parseInt(splitCount) || 2; const amountPerPerson = selectedBill.total / splits; setBills(bills.map(bill => bill.id === selectedBill.id ? { ...bill, status: 'split', splits } : bill )); toast.success(`Cuenta dividida en ${splits} partes: €${amountPerPerson.toFixed(2)} por persona`); }; const applyTip = (billId: string) => { setBills(bills.map(bill => { if (bill.id === billId) { const tip = calculateTip(bill.subtotal, tipPercentage); return { ...bill, tip, total: bill.subtotal + tip }; } return bill; })); toast.success(`Propina aplicada: €${calculateTip(selectedBill?.subtotal || 0, tipPercentage).toFixed(2)}`); }; const closeBill = (billId: string) => { setBills(bills.map(bill => bill.id === billId ? { ...bill, status: 'paid' } : bill )); toast.success('Cuenta cerrada correctamente'); }; return (
{bills.filter(b => b.status !== 'paid').map((bill) => (
Mesa {bill.tableNumber} {bill.status === 'split' ? `Dividida (${bill.splits})` : 'Abierta'}
{bill.items.map((item, idx) => (
{item.quantity}x {item.name} €{(item.quantity * item.price).toFixed(2)}
))}
Subtotal: €{bill.subtotal.toFixed(2)}
Propina: €{bill.tip.toFixed(2)}
Total: €{bill.total.toFixed(2)}
{bill.status === 'split' && (
Por persona:
€{(bill.total / (bill.splits || 1)).toFixed(2)}
)}
Dividir Cuenta - Mesa {bill.tableNumber}
setSplitCount(e.target.value)} />
Monto por persona:
€{(bill.total / (parseInt(splitCount) || 2)).toFixed(2)}
Agregar Propina - Mesa {bill.tableNumber}
setTipPercentage(e.target.value)} />
Subtotal: €{bill.subtotal.toFixed(2)}
Propina ({tipPercentage}%): €{calculateTip(bill.subtotal, tipPercentage).toFixed(2)}
Total: €{(bill.subtotal + calculateTip(bill.subtotal, tipPercentage)).toFixed(2)}
))}
{bills.filter(b => b.status !== 'paid').length === 0 && (

No hay cuentas abiertas

)}
); }; export default BillManagement;