import React from "react"; import { useExpenseTransactions } from "../../hooks/useExpense"; import Error from "../common/Error"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; import Loader, { SpinnerLoader } from "../common/Loader"; const AdvancePaymentList = ({ employeeId }) => { const { data, isError, isLoading, error, isFetching } = useExpenseTransactions(employeeId, { enabled: !!employeeId }); // Handle no employee selected if (!employeeId) { return (

Please select an employee

); } // Handle loading state if (isLoading || isFetching) { return (
); } // Handle error state if (isError) { return (
{error?.status === 404 ? ( "No advance payment transactions found." ) : ( )}
); } const records = Array.isArray(data) ? data : []; let currentBalance = 0; const rowsWithBalance = records.map((r) => { const isCredit = r.amount > 0; const credit = isCredit ? r.amount : 0; const debit = !isCredit ? Math.abs(r.amount) : 0; currentBalance += credit - debit; return { id: r.id, description: r.title || "-", projectName: r.project?.name || "-", createdAt: r.createdAt, credit, debit, balance: currentBalance, }; }); const columns = [ { key: "description", label: "Description", align: "text-start" }, { key: "credit", label: ( <> Credit ), align: "text-end", }, { key: "debit", label: ( <> Debit ), align: "text-end", }, { key: "balance", label: ( <> Balance ), align: "text-end fw-bold", }, ]; // Handle empty records if (rowsWithBalance.length === 0) { return (
No advance payment records found.
); } return (
{columns.map((col) => ( ))} {rowsWithBalance.map((row) => ( {columns.map((col) => ( ))} ))}
{col.label}
{["balance", "credit", "debit"].includes(col.key) ? ( {row[col.key].toLocaleString("en-IN")} ) : (
{formatUTCToLocalTime(row.createdAt)} {row.projectName} {row.description}
)}
Final Balance {currentBalance.toLocaleString("en-IN", { style: "currency", currency: "INR", })}
); }; export default AdvancePaymentList;