(todayLocal());
const [pending, setPending] = useState(false);
const [error, setError] = useState("");
const remaining = totalAmount - paidAmount;
const today = todayLocal();
async function handleProcessPayment() {
setPending(true);
setError("");
const result = await processPayment({ poId });
if ("error" in result) { setError(result.error); setPending(false); }
else { setPending(false); router.refresh(); }
}
async function handleMarkPaid(e: React.FormEvent, forceFullPayment = false) {
e.preventDefault();
if (!ref.trim()) { setError("Payment reference is required."); return; }
if (!paymentDate) { setError("Payment date is required."); return; }
if (paymentDate > today) { setError("Payment date cannot be in the future."); return; }
const paymentAmount = forceFullPayment ? remaining : (parseFloat(amount) || undefined);
if (paymentAmount !== undefined && paymentAmount <= 0) {
setError("Payment amount must be greater than 0.");
return;
}
if (paymentAmount !== undefined && paymentAmount > remaining) {
setError(`Payment amount cannot exceed the remaining balance of ${remaining.toFixed(2)}.`);
return;
}
setPending(true);
setError("");
const result = await markPaid({ poId, paymentRef: ref, paymentAmount, paymentDate });
if ("error" in result) { setError(result.error); setPending(false); }
else { setPending(false); router.refresh(); }
}
if (poStatus === "MGR_APPROVED") {
return (
{error && {error}}
);
}
if (
poStatus === "SENT_FOR_PAYMENT" ||
poStatus === "PARTIALLY_PAID" ||
poStatus === "PARTIALLY_CLOSED"
) {
const parsedAmount = parseFloat(amount);
const isPartialPayment =
!isNaN(parsedAmount) && parsedAmount > 0 && parsedAmount < remaining;
return (
);
}
return null;
}