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 }); console.log(data) // 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.
); } console.log("Kartik", rowsWithBalance) return (
{columns.map((col) => ( ))} {Array.isArray(data) && data.length > 0 ? ( data.map((row) => ( {columns.map((col) => ( ))} )) ) : ( )}
{col.label}
{col.key === "credit" ? ( row.amount > 0 ? ( {row.amount.toLocaleString("en-IN")} ) : ( "-" ) ) : col.key === "debit" ? ( row.amount < 0 ? ( {Math.abs(row.amount).toLocaleString("en-IN")} ) : ( "-" ) ) : col.key === "balance" ? ( {row.currentBalance?.toLocaleString("en-IN")} ) : (
{formatUTCToLocalTime(row.paidAt)} {row.project?.name || "-"} {row.title || "-"}
)}
No advance payment records found.
Final Balance {currentBalance.toLocaleString("en-IN", { style: "currency", currency: "INR", })}
); }; export default AdvancePaymentList;