import React, { useEffect, useMemo } from "react"; import { useExpenseAllTransactionsList, useExpenseTransactions } from "../../hooks/useExpense"; import Error from "../common/Error"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; import Loader, { SpinnerLoader } from "../common/Loader"; import { useForm, useFormContext } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { employee } from "../../data/masters"; import { useAdvancePaymentContext } from "../../pages/AdvancePayment/AdvancePaymentPageDetails"; import { formatFigure } from "../../utils/appUtils"; const AdvancePaymentListDetails = ({ employeeId, searchString,tableRef }) => { const { setBalance } = useAdvancePaymentContext(); const { data, isError, isLoading, error, isFetching } = useExpenseTransactions(employeeId, { enabled: !!employeeId }); 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, financeUId: r.financeUId, balance: currentBalance, }; }); useEffect(() => { if (!employeeId) { setBalance(null); return; } if (rowsWithBalance.length > 0) { setBalance(rowsWithBalance[rowsWithBalance.length - 1].balance); } else { setBalance(0); } }, [employeeId, data, setBalance]); if (!employeeId) { return (

Please select an employee

); } if (isLoading || isFetching) { return (
); } if (isError) { return (
{error?.status === 404 ? "No advance payment transactions found." : }
); } const columns = [ { key: "date", label: ( <> Date ), align: "text-start", }, { 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.
); } const DecideCreditOrDebit = ({ financeUId }) => { if (!financeUId) return null; const prefix = financeUId?.substring(0, 2).toUpperCase(); if (prefix === "PR") return +; if (prefix === "EX") return -; return null; }; 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" ? (
{/* */} {formatFigure(row.currentBalance)}
) : col.key === "date" ? ( {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 AdvancePaymentListDetails;