Added view for Payment Request.
This commit is contained in:
parent
465b67e25c
commit
494b1b2b77
324
src/components/PaymentRequest/ManagePaymentRequest.jsx
Normal file
324
src/components/PaymentRequest/ManagePaymentRequest.jsx
Normal file
@ -0,0 +1,324 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import { useProjectName } from '../../hooks/useProjects';
|
||||||
|
import Label from '../common/Label';
|
||||||
|
import { useForm } from 'react-hook-form';
|
||||||
|
import { useExpenseType } from '../../hooks/masterHook/useMaster';
|
||||||
|
import DatePicker from '../common/DatePicker';
|
||||||
|
|
||||||
|
function ManagePaymentRequest({ expenseToEdit = null}) {
|
||||||
|
|
||||||
|
const { projectNames, loading: projectLoading, error, isError: isProjectError,
|
||||||
|
} = useProjectName();
|
||||||
|
const { register, control, watch, formState: { errors }, } = useForm();
|
||||||
|
|
||||||
|
const {
|
||||||
|
ExpenseTypes,
|
||||||
|
loading: ExpenseLoading,
|
||||||
|
error: ExpenseError,
|
||||||
|
} = useExpenseType();
|
||||||
|
|
||||||
|
const files = watch("billAttachments");
|
||||||
|
const onFileChange = async (e) => {
|
||||||
|
const newFiles = Array.from(e.target.files);
|
||||||
|
if (newFiles.length === 0) return;
|
||||||
|
|
||||||
|
const existingFiles = watch("billAttachments") || [];
|
||||||
|
|
||||||
|
const parsedFiles = await Promise.all(
|
||||||
|
newFiles.map(async (file) => {
|
||||||
|
const base64Data = await toBase64(file);
|
||||||
|
return {
|
||||||
|
fileName: file.name,
|
||||||
|
base64Data,
|
||||||
|
contentType: file.type,
|
||||||
|
fileSize: file.size,
|
||||||
|
description: "",
|
||||||
|
isActive: true,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const combinedFiles = [
|
||||||
|
...existingFiles,
|
||||||
|
...parsedFiles.filter(
|
||||||
|
(newFile) =>
|
||||||
|
!existingFiles.some(
|
||||||
|
(f) =>
|
||||||
|
f.fileName === newFile.fileName && f.fileSize === newFile.fileSize
|
||||||
|
)
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
setValue("billAttachments", combinedFiles, {
|
||||||
|
shouldDirty: true,
|
||||||
|
shouldValidate: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const toBase64 = (file) =>
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
reader.onload = () => resolve(reader.result.split(",")[1]);
|
||||||
|
reader.onerror = (error) => reject(error);
|
||||||
|
});
|
||||||
|
const removeFile = (index) => {
|
||||||
|
if (expenseToEdit) {
|
||||||
|
const newFiles = files.map((file, i) => {
|
||||||
|
if (file.documentId !== index) return file;
|
||||||
|
return {
|
||||||
|
...file,
|
||||||
|
isActive: false,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
setValue("billAttachments", newFiles, { shouldValidate: true });
|
||||||
|
} else {
|
||||||
|
const newFiles = files.filter((_, i) => i !== index);
|
||||||
|
setValue("billAttachments", newFiles, { shouldValidate: true });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="container p-3">
|
||||||
|
<h5 className="m-0">
|
||||||
|
{expenseToEdit ? "Update Expense " : "Create New Expense"}
|
||||||
|
</h5>
|
||||||
|
<form>
|
||||||
|
<div className="row my-2 text-start">
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label className="form-label" required>
|
||||||
|
Select Project
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
className="form-select form-select-sm"
|
||||||
|
{...register("projectId")}
|
||||||
|
>
|
||||||
|
<option value="">Select Project</option>
|
||||||
|
{projectLoading ? (
|
||||||
|
<option>Loading...</option>
|
||||||
|
) : (
|
||||||
|
projectNames?.map((project) => (
|
||||||
|
<option key={project.id} value={project.id}>
|
||||||
|
{project.name}
|
||||||
|
</option>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</select>
|
||||||
|
{errors.projectId && (
|
||||||
|
<small className="danger-text">{errors.projectId.message}</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label htmlFor="expensesTypeId" className="form-label" required>
|
||||||
|
Expense Category
|
||||||
|
</Label>
|
||||||
|
<select
|
||||||
|
className="form-select form-select-sm"
|
||||||
|
id="expensesTypeId"
|
||||||
|
{...register("expensesTypeId")}
|
||||||
|
>
|
||||||
|
<option value="" disabled>
|
||||||
|
Select Type
|
||||||
|
</option>
|
||||||
|
{ExpenseLoading ? (
|
||||||
|
<option disabled>Loading...</option>
|
||||||
|
) : (
|
||||||
|
ExpenseTypes?.map((expense) => (
|
||||||
|
<option key={expense.id} value={expense.id}>
|
||||||
|
{expense.name}
|
||||||
|
</option>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</select>
|
||||||
|
{errors.expensesTypeId && (
|
||||||
|
<small className="danger-text">
|
||||||
|
{errors.expensesTypeId.message}
|
||||||
|
</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="row my-2 text-start">
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label htmlFor="transactionDate" className="form-label" required>
|
||||||
|
Transaction Date
|
||||||
|
</Label>
|
||||||
|
<DatePicker
|
||||||
|
name="transactionDate"
|
||||||
|
control={control}
|
||||||
|
maxDate={new Date()}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{errors.transactionDate && (
|
||||||
|
<small className="danger-text">
|
||||||
|
{errors.transactionDate.message}
|
||||||
|
</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label htmlFor="amount" className="form-label" required>
|
||||||
|
Amount
|
||||||
|
</Label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
id="amount"
|
||||||
|
className="form-control form-control-sm"
|
||||||
|
min="1"
|
||||||
|
step="0.01"
|
||||||
|
inputMode="decimal"
|
||||||
|
{...register("amount", { valueAsNumber: true })}
|
||||||
|
/>
|
||||||
|
{errors.amount && (
|
||||||
|
<small className="danger-text">{errors.amount.message}</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="row my-2 text-start">
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label htmlFor="supplerName" className="form-label" required>
|
||||||
|
Supplier Name/Transporter Name/Other
|
||||||
|
</Label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="supplerName"
|
||||||
|
className="form-control form-control-sm"
|
||||||
|
{...register("supplerName")}
|
||||||
|
/>
|
||||||
|
{errors.supplerName && (
|
||||||
|
<small className="danger-text">
|
||||||
|
{errors.supplerName.message}
|
||||||
|
</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="col-md-6">
|
||||||
|
<Label htmlFor="location" className="form-label" required>
|
||||||
|
Location
|
||||||
|
</Label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="location"
|
||||||
|
className="form-control form-control-sm"
|
||||||
|
{...register("location")}
|
||||||
|
/>
|
||||||
|
{errors.location && (
|
||||||
|
<small className="danger-text">{errors.location.message}</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="row my-2 text-start">
|
||||||
|
<div className="col-md-12">
|
||||||
|
<Label htmlFor="description" className="form-label" required>
|
||||||
|
Description
|
||||||
|
</Label>
|
||||||
|
<textarea
|
||||||
|
id="description"
|
||||||
|
className="form-control form-control-sm"
|
||||||
|
{...register("description")}
|
||||||
|
rows="2"
|
||||||
|
></textarea>
|
||||||
|
{errors.description && (
|
||||||
|
<small className="danger-text">
|
||||||
|
{errors.description.message}
|
||||||
|
</small>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="row my-2 text-start">
|
||||||
|
<div className="col-md-12">
|
||||||
|
<Label className="form-label" required>
|
||||||
|
Upload Bill{" "}
|
||||||
|
</Label>
|
||||||
|
|
||||||
|
<div
|
||||||
|
className="border border-secondary border-dashed rounded p-4 text-center bg-textMuted position-relative"
|
||||||
|
style={{ cursor: "pointer" }}
|
||||||
|
onClick={() => document.getElementById("billAttachments").click()}
|
||||||
|
>
|
||||||
|
<i className="bx bx-cloud-upload d-block bx-lg"> </i>
|
||||||
|
<span className="text-muted d-block">
|
||||||
|
Click to select or click here to browse
|
||||||
|
</span>
|
||||||
|
<small className="text-muted">(PDF, JPG, PNG, max 5MB)</small>
|
||||||
|
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
id="billAttachments"
|
||||||
|
accept=".pdf,.jpg,.jpeg,.png"
|
||||||
|
multiple
|
||||||
|
style={{ display: "none" }}
|
||||||
|
{...register("billAttachments")}
|
||||||
|
onChange={(e) => {
|
||||||
|
onFileChange(e);
|
||||||
|
e.target.value = "";
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{errors.billAttachments && (
|
||||||
|
<small className="danger-text">
|
||||||
|
{errors.billAttachments.message}
|
||||||
|
</small>
|
||||||
|
)}
|
||||||
|
{Array.isArray(files) && files.length > 0 && (
|
||||||
|
|
||||||
|
<div className="d-block">
|
||||||
|
{files
|
||||||
|
.filter((file) => {
|
||||||
|
if (expenseToEdit) {
|
||||||
|
return file.isActive;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.map((file, idx) => (
|
||||||
|
<a
|
||||||
|
key={idx}
|
||||||
|
className="d-flex justify-content-between text-start p-1"
|
||||||
|
href={file.preSignedUrl || "#"}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<span className="mb-0 text-secondary small d-block">
|
||||||
|
{file.fileName}
|
||||||
|
</span>
|
||||||
|
<span className="text-body-secondary small d-block">
|
||||||
|
{file.fileSize ? formatFileSize(file.fileSize) : ""}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<i
|
||||||
|
className="bx bx-trash bx-sm cursor-pointer text-danger"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
removeFile(expenseToEdit ? file.documentId : idx);
|
||||||
|
}}
|
||||||
|
></i>
|
||||||
|
</a>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{Array.isArray(errors.billAttachments) &&
|
||||||
|
errors.billAttachments.map((fileError, index) => (
|
||||||
|
<div key={index} className="danger-text small mt-1">
|
||||||
|
{
|
||||||
|
(fileError?.fileSize?.message ||
|
||||||
|
fileError?.contentType?.message ||
|
||||||
|
fileError?.base64Data?.message,
|
||||||
|
fileError?.documentId?.message)
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ManagePaymentRequest
|
||||||
@ -1,10 +1,73 @@
|
|||||||
import React from "react";
|
import React, { useState } from "react";
|
||||||
import Breadcrumb from "../../components/common/Breadcrumb";
|
import Breadcrumb from "../../components/common/Breadcrumb";
|
||||||
|
import GlobalModel from "../../components/common/GlobalModel";
|
||||||
|
import ManagePaymentRequest from "../../components/PaymentRequest/ManagePaymentRequest";
|
||||||
const PaymentRequestPage = () => {
|
const PaymentRequestPage = () => {
|
||||||
|
const [ManagePaymentRequestModal, setManagePaymentRequestModal] = useState({
|
||||||
|
IsOpen: null,
|
||||||
|
expenseId: null,
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container-fluid">
|
<div className="container-fluid">
|
||||||
<Breadcrumb data={[{ label: "Home", link: "/" }, { label: "Finance", link: "/Payment Request" } ,{ label: "Payment Request" }]} />
|
{/* Breadcrumb */}
|
||||||
|
<Breadcrumb data={[{ label: "Home", link: "/" }, { label: "Finance", link: "/Payment Request" }, { label: "Payment Request" }]} />
|
||||||
|
|
||||||
|
{/* Top Bar */}
|
||||||
|
<div className="card my-3 px-sm-4 px-0">
|
||||||
|
<div className="card-body py-2 px-3">
|
||||||
|
<div className="row align-items-center">
|
||||||
|
<div className="col-6">
|
||||||
|
<input
|
||||||
|
type="search"
|
||||||
|
className="form-control form-control-sm w-auto"
|
||||||
|
placeholder="Search Payment Req.."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="col-6 text-end mt-2 mt-sm-0">
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-primary"
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
setManagePaymentRequestModal({
|
||||||
|
IsOpen: true,
|
||||||
|
expenseId: null,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<i className="bx bx-plus-circle me-2"></i>
|
||||||
|
<span className="d-none d-md-inline-block">Add Payment Request</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{ManagePaymentRequestModal.IsOpen && (
|
||||||
|
<GlobalModel
|
||||||
|
isOpen
|
||||||
|
size="lg"
|
||||||
|
closeModal={() =>
|
||||||
|
setManagePaymentRequestModal({ IsOpen: null, expenseId: null })
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<ManagePaymentRequest
|
||||||
|
key={ManagePaymentRequestModal.expenseId ?? "new"}
|
||||||
|
expenseToEdit={ManagePaymentRequestModal.expenseId}
|
||||||
|
closeModal={() =>
|
||||||
|
setManagePaymentRequestModal({ IsOpen: null, expenseId: null })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</GlobalModel>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Expense List Placeholder */}
|
||||||
|
<div className="card">
|
||||||
|
<div className="card-body text-center text-muted py-5">
|
||||||
|
<p>No Expense Data found</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
78
src/pages/PaymentRequest/PaymentRequestSchema.js
Normal file
78
src/pages/PaymentRequest/PaymentRequestSchema.js
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const PaymentRequestSchema = (expenseTypes) => {
|
||||||
|
return z
|
||||||
|
.object({
|
||||||
|
projectId: z.string().min(1, { message: "Project is required" }),
|
||||||
|
expensesTypeId: z
|
||||||
|
.string()
|
||||||
|
.min(1, { message: "Expense type is required" }),
|
||||||
|
transactionDate: z.string().min(1, { message: "Date is required" }),
|
||||||
|
description: z.string().min(1, { message: "Description is required" }),
|
||||||
|
supplerName: z.string().min(1, { message: "Supplier name is required" }),
|
||||||
|
amount: z.coerce
|
||||||
|
.number({
|
||||||
|
invalid_type_error: "Amount is required and must be a number",
|
||||||
|
})
|
||||||
|
.min(1, "Amount must be Enter")
|
||||||
|
.refine((val) => /^\d+(\.\d{1,2})?$/.test(val.toString()), {
|
||||||
|
message: "Amount must have at most 2 decimal places",
|
||||||
|
}),
|
||||||
|
billAttachments: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
fileName: z.string().min(1, { message: "Filename is required" }),
|
||||||
|
base64Data: z.string().nullable(),
|
||||||
|
contentType: z
|
||||||
|
.string()
|
||||||
|
.refine((val) => ALLOWED_TYPES.includes(val), {
|
||||||
|
message: "Only PDF, PNG, JPG, or JPEG files are allowed",
|
||||||
|
}),
|
||||||
|
documentId: z.string().optional(),
|
||||||
|
fileSize: z.number().max(MAX_FILE_SIZE, {
|
||||||
|
message: "File size must be less than or equal to 5MB",
|
||||||
|
}),
|
||||||
|
description: z.string().optional(),
|
||||||
|
isActive: z.boolean().default(true),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.nonempty({ message: "At least one file attachment is required" }),
|
||||||
|
})
|
||||||
|
.refine(
|
||||||
|
(data) => {
|
||||||
|
return (
|
||||||
|
!data.projectId || (data.paidById && data.paidById.trim() !== "")
|
||||||
|
);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Please select who paid (employee)",
|
||||||
|
path: ["paidById"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.superRefine((data, ctx) => {
|
||||||
|
const expenseType = expenseTypes.find(
|
||||||
|
(et) => et.id === data.expensesTypeId
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
expenseType?.noOfPersonsRequired &&
|
||||||
|
(!data.noOfPersons || data.noOfPersons < 1)
|
||||||
|
) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: "No. of Persons is required and must be at least 1",
|
||||||
|
path: ["noOfPersons"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const defaultPaymentRequest = {
|
||||||
|
projectId: "",
|
||||||
|
expensesTypeId: "",
|
||||||
|
transactionDate: "",
|
||||||
|
description: "",
|
||||||
|
supplerName: "",
|
||||||
|
amount: "",
|
||||||
|
billAttachments: [],
|
||||||
|
};
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user