handle proforma to base , Tax, amount condition
This commit is contained in:
parent
8dfc8e4336
commit
258bd82b54
@ -136,6 +136,8 @@ const ManagePurchase = ({ onClose, purchaseId }) => {
|
||||
},
|
||||
[purchaseId, generatePatchOps, updatePurchase, CreateInvoice]
|
||||
);
|
||||
console.log(formState.errors)
|
||||
console.log(formState.formData)
|
||||
return (
|
||||
<div className="bs-stepper horizontically mt-2 b-secondry shadow-none border-0">
|
||||
{/* --- Steps Header --- */}
|
||||
|
||||
@ -202,7 +202,7 @@ const PurchasePartyDetails = () => {
|
||||
{...register("proformaInvoiceAmount")}
|
||||
/>
|
||||
|
||||
{errors?.proformaInvoiceAmountt && (
|
||||
{errors?.proformaInvoiceAmount && (
|
||||
<div className="danger-text">
|
||||
{errors.proformaInvoiceAmount.message}
|
||||
</div>
|
||||
|
||||
@ -24,7 +24,8 @@ export const AttachmentSchema = z.object({
|
||||
documentId: z.string().nullable().default(null),
|
||||
});
|
||||
|
||||
export const PurchaseSchema = z.object({
|
||||
export const PurchaseSchema = z
|
||||
.object({
|
||||
title: z.string().min(1, { message: "Title is required" }),
|
||||
projectId: z.string().min(1, { message: "Project is required" }),
|
||||
organizationId: z.string().min(1, { message: "Organization is required" }),
|
||||
@ -47,7 +48,6 @@ export const PurchaseSchema = z.object({
|
||||
invoiceReferenceNumber: z.string().nullable(),
|
||||
acknowledgmentDate: z.coerce.date().nullable(),
|
||||
acknowledgmentNumber: z.string().nullable(),
|
||||
|
||||
baseAmount: z
|
||||
.number()
|
||||
.or(z.nan()) // allow NaN
|
||||
@ -69,7 +69,79 @@ export const PurchaseSchema = z.object({
|
||||
description: z.string(),
|
||||
invoiceAttachmentTypeId: z.string().nullable(),
|
||||
attachments: z.array(AttachmentSchema),
|
||||
});
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
const hasNumber = !!data.proformaInvoiceNumber;
|
||||
const hasAmount =
|
||||
data.proformaInvoiceAmount !== null && data.proformaInvoiceAmount > 0;
|
||||
const hasDate = !!data.proformaInvoiceDate;
|
||||
|
||||
// If any one field is filled → all must be valid
|
||||
const anyField = hasNumber || hasAmount || hasDate;
|
||||
|
||||
if (anyField) {
|
||||
// Number required
|
||||
if (!hasNumber) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Proforma Invoice Number is required.",
|
||||
path: ["proformaInvoiceNumber"],
|
||||
});
|
||||
}
|
||||
|
||||
// Amount required
|
||||
if (!hasAmount) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Proforma Amount must be greater than 0.",
|
||||
path: ["proformaInvoiceAmount"],
|
||||
});
|
||||
}
|
||||
|
||||
// Date required
|
||||
if (!hasDate) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Proforma Invoice Date is required.",
|
||||
path: ["proformaInvoiceDate"],
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!anyField) {
|
||||
if (data.baseAmount === null || data.baseAmount === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Base amount is required.",
|
||||
path: ["baseAmount"],
|
||||
});
|
||||
}
|
||||
if (data.taxAmount === null || data.taxAmount === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Tax amount is required.",
|
||||
path: ["taxAmount"],
|
||||
});
|
||||
}
|
||||
|
||||
if (data.totalAmount === null || data.totalAmount === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Total amount is required.",
|
||||
path: ["totalAmount"],
|
||||
});
|
||||
}
|
||||
|
||||
if (data.transportCharges === null || data.transportCharges === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Transport charges are required.",
|
||||
path: ["transportCharges"],
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const defaultPurchaseValue = {
|
||||
title: "",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user