import React, { useEffect, useState } from "react"; import Label from "../common/Label"; import { Controller, useForm } from "react-hook-form"; import { useCurrencies, useExpenseCategory, useRecurringStatus, } from "../../hooks/masterHook/useMaster"; import DatePicker from "../common/DatePicker"; import { zodResolver } from "@hookform/resolvers/zod"; import { defaultRecurringExpense, PaymentRecurringExpense, } from "./RecurringExpenseSchema"; import { FREQUENCY_FOR_RECURRING, INR_CURRENCY_CODE, } from "../../utils/constants"; import { useProjectName } from "../../hooks/useProjects"; import { useCreateRecurringExpense, usePayee, useRecurringExpenseDetail, useUpdateRecurringExpense, } from "../../hooks/useExpense"; import InputSuggestions from "../common/InputSuggestion"; import { useEmployeesName } from "../../hooks/useEmployees"; import PmsEmployeeInputTag from "../common/PmsEmployeeInputTag"; import HoverPopup from "../common/HoverPopup"; const ManageRecurringExpense = ({ closeModal, requestToEdit = null }) => { const { data, isLoading, isError, error: requestError, } = useRecurringExpenseDetail(requestToEdit); const { data: employees } = useEmployeesName(null, null, true); //APIs const { projectNames, loading: projectLoading, error, isError: isProjectError, } = useProjectName(); const { data: currencyData, isLoading: currencyLoading, isError: currencyError, } = useCurrencies(); const { data: statusData, isLoading: statusLoading, isError: statusError, } = useRecurringStatus(); const { data: Payees, isLoading: isPayeeLoaing, isError: isPayeeError, error: payeeError, } = usePayee(); const { expenseCategories, loading: ExpenseLoading, error: ExpenseError, } = useExpenseCategory(); const schema = PaymentRecurringExpense(); const { register, control, watch, handleSubmit, setValue, reset, formState: { errors }, } = useForm({ resolver: zodResolver(schema), defaultValues: defaultRecurringExpense, }); const handleClose = () => { reset(); closeModal(); }; const { mutate: CreateRecurringExpense, isPending: createPending } = useCreateRecurringExpense(() => { handleClose(); }); const { mutate: RecurringExpenseUpdate, isPending } = useUpdateRecurringExpense(() => handleClose()); const handleEmailGetting = (userArray = []) => { if (!Array.isArray(userArray) || userArray.length === 0) return []; return userArray .map((empId) => { const foundUser = employees?.data?.find((user) => user.id === empId); return foundUser?.email || null; }) .filter(Boolean) .join(","); }; useEffect(() => { if (requestToEdit && data) { reset({ title: data.title || "", description: data.description || "", payee: data.payee || "", notifyTo: data.notifyTo ? data.notifyTo.map((usr) => usr.id) : [], currencyId: data.currency.id || "", amount: data.amount || "", strikeDate: data.strikeDate?.slice(0, 10) || "", projectId: data.project.id || "", paymentBufferDays: data.paymentBufferDays || "", // numberOfIteration: data.numberOfIteration || "", endDate: data.endDate?.slice(0, 10) || "", expenseCategoryId: data.expenseCategory.id || "", statusId: data.status.id || "", frequency: data.frequency || "", isVariable: data.isVariable || false, }); } }, [data, reset]); useEffect(() => { if (!requestToEdit && currencyData && currencyData.length > 0) { const inrCurrency = currencyData.find((c) => c.id === INR_CURRENCY_CODE); if (inrCurrency) { setValue("currencyId", INR_CURRENCY_CODE, { shouldValidate: true }); } } }, [currencyData, requestToEdit, setValue]); const StrikeDate = watch("strikeDate") const onSubmit = (fromdata) => { let payload = { ...fromdata, strikeDate: fromdata.strikeDate ? new Date(fromdata.strikeDate).toISOString() : null, endDate: fromdata.endDate ? new Date(fromdata.endDate).toISOString() : null, notifyTo: handleEmailGetting(fromdata.notifyTo), }; if (requestToEdit) { const editPayload = { ...payload, id: data.id }; RecurringExpenseUpdate({ id: data.id, payload: editPayload }); } else { CreateRecurringExpense(payload); } }; return (
{requestToEdit ? "Update Expense Recurring " : "Create Expense Recurring"}
{/* Project and Category */}
{errors.projectId && ( {errors.projectId.message} )}
{errors.expenseCategoryId && ( {errors.expenseCategoryId.message} )}
{/* Title and Is Variable */}
{errors.title && ( {errors.title.message} )}
Choose whether the payment amount varies or remains fixed each cycle.
Is Variable: Amount changes per cycle.
Fixed: Amount stays constant.

} >
(
field.onChange(true)} />
field.onChange(false)} />
)} /> {errors.isVariable && ( {errors.isVariable.message} )}
{/* Date and Amount */}
{errors.strikeDate && ( {errors.strikeDate.message} )}
{errors.amount && ( {errors.amount.message} )}
{/* Payee and Currency */}
setValue("payee", val, { shouldValidate: true }) } error={errors.payee?.message} placeholder="Select or enter payee" />
{errors.currencyId && ( {errors.currencyId.message} )}
{/* Frequency To and Status Id */}
Defines how often payments or billing occur, such as monthly, quarterly, or annually.

} >
{errors.frequency && ( {errors.frequency.message} )}
{errors.statusId && ( {errors.statusId.message} )}
{/* Payment Buffer Days and End Date */}
Number of extra days allowed after the due date before payment is considered late.

} >
{errors.paymentBufferDays && ( {errors.paymentBufferDays.message} )}
The date when the last payment in the recurrence occurs.

} >
{errors.endDate && ( {errors.endDate.message} )}
{/* */} {errors.notifyTo && ( {errors.notifyTo.message} )}
{/* Description */}
{errors.description && ( {errors.description.message} )}
); }; export default ManageRecurringExpense;