import { useState } from 'react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { usePOSStore } from '@/stores/pos.store' import { posMutations, posKeys, type Transaction } from '@/api/pos' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Separator } from '@/components/ui/separator' import { CheckCircle } from 'lucide-react' import { toast } from 'sonner' interface POSPaymentDialogProps { open: boolean onOpenChange: (open: boolean) => void paymentMethod: string transaction: Transaction onComplete: () => void } export function POSPaymentDialog({ open, onOpenChange, paymentMethod, transaction, onComplete }: POSPaymentDialogProps) { const queryClient = useQueryClient() const { currentTransactionId } = usePOSStore() const total = parseFloat(transaction.total) const [amountTendered, setAmountTendered] = useState('') const [checkNumber, setCheckNumber] = useState('') const [completed, setCompleted] = useState(false) const [result, setResult] = useState(null) const completeMutation = useMutation({ mutationFn: () => { const data: { paymentMethod: string; amountTendered?: number; checkNumber?: string } = { paymentMethod, } if (paymentMethod === 'cash') { data.amountTendered = parseFloat(amountTendered) || 0 } if (paymentMethod === 'check') { data.checkNumber = checkNumber || undefined } return posMutations.complete(currentTransactionId!, data) }, onSuccess: (txn) => { queryClient.invalidateQueries({ queryKey: posKeys.transaction(currentTransactionId!) }) setResult(txn) setCompleted(true) }, onError: (err) => toast.error(err.message), }) const tenderedAmount = parseFloat(amountTendered) || 0 const changeDue = paymentMethod === 'cash' ? Math.max(0, tenderedAmount - total) : 0 const canComplete = paymentMethod === 'cash' ? tenderedAmount >= total : true function handleDone() { onComplete() onOpenChange(false) } const QUICK_AMOUNTS = [1, 5, 10, 20, 50, 100] if (completed && result) { const changeGiven = parseFloat(result.changeGiven ?? '0') const roundingAdj = parseFloat(result.roundingAdjustment ?? '0') return ( handleDone()}>

Sale Complete

{result.transactionNumber}

Total ${parseFloat(result.total).toFixed(2)}
{roundingAdj !== 0 && (
Rounding {roundingAdj > 0 ? '+' : ''}{roundingAdj.toFixed(2)}
)} {paymentMethod === 'cash' && ( <>
Tendered ${parseFloat(result.amountTendered ?? '0').toFixed(2)}
{changeGiven > 0 && (
Change Due ${changeGiven.toFixed(2)}
)} )}
) } return ( {paymentMethod === 'cash' ? 'Cash Payment' : paymentMethod === 'check' ? 'Check Payment' : 'Card Payment'}
Total Due ${total.toFixed(2)}
{paymentMethod === 'cash' && ( <>
setAmountTendered(e.target.value)} placeholder="0.00" className="h-12 text-xl text-right font-mono" autoFocus />
{QUICK_AMOUNTS.map((amt) => ( ))}
{tenderedAmount >= total && (
Change ${changeDue.toFixed(2)}
)} )} {paymentMethod === 'check' && (
setCheckNumber(e.target.value)} placeholder="Check #" className="h-11" autoFocus />
)} {paymentMethod === 'card_present' && (

Process card payment on terminal, then confirm below.

)}
) }