"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { formatCurrency, cn } from "@/lib/utils"; import { CartItem } from "./cart"; type PaymentMethod = "CASH" | "CARD" | "TRANSFER"; interface PaymentDialogProps { items: CartItem[]; total: number; siteId: string; onClose: () => void; onPaymentComplete: (saleId: string) => void; } const paymentMethods: { value: PaymentMethod; label: string; icon: string }[] = [ { value: "CASH", label: "Efectivo", icon: "💵" }, { value: "TRANSFER", label: "Transferencia", icon: "🏦" }, { value: "CARD", label: "Terminal", icon: "💳" }, ]; export function PaymentDialog({ items, total, siteId, onClose, onPaymentComplete, }: PaymentDialogProps) { const [selectedMethod, setSelectedMethod] = useState("CASH"); const [cashReceived, setCashReceived] = useState(""); const [reference, setReference] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const cashReceivedAmount = parseFloat(cashReceived) || 0; const change = cashReceivedAmount - total; const canPay = selectedMethod !== "CASH" || cashReceivedAmount >= total; const handleSubmit = async () => { if (!canPay) return; setLoading(true); setError(null); try { const saleData = { siteId, items: items.map((item) => ({ productId: item.id, quantity: item.quantity, price: item.price, })), payments: [ { amount: total, method: selectedMethod, reference: reference || undefined, }, ], }; const response = await fetch("/api/sales", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(saleData), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || "Error al procesar la venta"); } const sale = await response.json(); onPaymentComplete(sale.id); } catch (err) { setError(err instanceof Error ? err.message : "Error desconocido"); } finally { setLoading(false); } }; const handleQuickCash = (amount: number) => { setCashReceived(amount.toString()); }; // Generate quick cash options based on total const quickCashOptions = [ Math.ceil(total / 10) * 10, Math.ceil(total / 50) * 50, Math.ceil(total / 100) * 100, Math.ceil(total / 500) * 500, ].filter((value, index, self) => value >= total && self.indexOf(value) === index ).slice(0, 4); return (
Procesar Pago
{/* Total Display */}

Total a pagar

{formatCurrency(total)}

{/* Payment Method Selection */}

Metodo de pago

{paymentMethods.map((method) => ( ))}
{/* Cash Payment Options */} {selectedMethod === "CASH" && (
setCashReceived(e.target.value)} className="text-lg font-medium" min={0} step={0.01} disabled={loading} />
{/* Quick Cash Buttons */}
{quickCashOptions.map((amount) => ( ))}
{/* Change Display */} {cashReceivedAmount > 0 && (
= 0 ? "bg-green-50" : "bg-red-50" )} >

= 0 ? "text-green-600" : "text-red-600" )} > {change >= 0 ? "Cambio" : "Falta"}

= 0 ? "text-green-700" : "text-red-700" )} > {formatCurrency(Math.abs(change))}

)}
)} {/* Transfer Reference */} {(selectedMethod === "TRANSFER" || selectedMethod === "CARD") && (
setReference(e.target.value)} disabled={loading} />
)} {/* Error Display */} {error && (
{error}
)}
); }