diff --git a/src/components/pages/inventory/movement/form/MovementForm.schema.ts b/src/components/pages/inventory/movement/form/MovementForm.schema.ts index c448754a..1de5747e 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.schema.ts +++ b/src/components/pages/inventory/movement/form/MovementForm.schema.ts @@ -123,23 +123,61 @@ const DeliveryObjectSchema: Yup.ObjectSchema = Yup.object({ .transform((value) => isNaN(value) || value === '' || value === null ? undefined : value ) - .optional() - .nullable() - .min(1, 'Biaya minimal 1!') - .typeError('Biaya harus berupa angka!'), + .when('supplier_id', { + is: (supplier_id: number | null | undefined) => + supplier_id !== null && supplier_id !== undefined && supplier_id > 0, + then: (schema) => + schema + .required('Biaya pengiriman wajib diisi!') + .min(1, 'Biaya minimal 1!') + .typeError('Biaya harus berupa angka!'), + otherwise: (schema) => + schema + .optional() + .nullable() + .min(1, 'Biaya minimal 1!') + .typeError('Biaya harus berupa angka!'), + }), delivery_cost_per_item: Yup.number() .transform((value) => isNaN(value) || value === '' || value === null ? undefined : value ) - .optional() - .nullable() - .min(1, 'Biaya per item minimal 1!') - .typeError('Biaya per item harus berupa angka!'), + .when('supplier_id', { + is: (supplier_id: number | null | undefined) => + supplier_id !== null && supplier_id !== undefined && supplier_id > 0, + then: (schema) => + schema + .required('Biaya per item wajib diisi!') + .min(1, 'Biaya per item minimal 1!') + .typeError('Biaya per item harus berupa angka!'), + otherwise: (schema) => + schema + .optional() + .nullable() + .min(1, 'Biaya per item minimal 1!') + .typeError('Biaya per item harus berupa angka!'), + }), document_path: Yup.string().nullable().optional(), document_index: Yup.number().optional(), document: DeliveryDocumentSchema, - driver_name: Yup.string().optional().nullable(), - vehicle_plate: Yup.string().optional().nullable(), + driver_name: Yup.string().when('supplier_id', { + is: (supplier_id: number | null | undefined) => + supplier_id !== null && supplier_id !== undefined && supplier_id > 0, + then: (schema) => + schema + .required('Nama sopir wajib diisi!') + .min(1, 'Nama sopir wajib diisi!'), + otherwise: (schema) => schema.optional().nullable(), + }), + vehicle_plate: Yup.string().when('supplier_id', { + is: (supplier_id: number | null | undefined) => + supplier_id !== null && supplier_id !== undefined && supplier_id > 0, + then: (schema) => + schema + .required('Plat nomor wajib diisi!') + .min(1, 'Plat nomor wajib diisi!'), + otherwise: (schema) => schema.optional().nullable(), + }), supplier: Yup.object({ value: Yup.number().min(1).required(), label: Yup.string().required(), diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 05d5b052..b2f50361 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -984,6 +984,28 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { [formik.values.deliveries, formik.values.products, type] ); + const isSupplierSelected = useCallback( + (deliveryIdx: number) => { + const delivery = formik.values.deliveries?.[deliveryIdx]; + return ( + delivery && + delivery.supplier_id !== null && + delivery.supplier_id !== undefined && + delivery.supplier_id > 0 + ); + }, + [formik.values.deliveries] + ); + + const hasAnySupplierSelected = useMemo(() => { + return formik.values.deliveries?.some( + (delivery) => + delivery.supplier_id !== null && + delivery.supplier_id !== undefined && + delivery.supplier_id > 0 + ); + }, [formik.values.deliveries]); + // ===== COMPUTED VALUES ===== const invalidQtyRows = useMemo( () => @@ -1659,12 +1681,62 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { * - Supplier - Plat Nomor + + Supplier + {hasAnySupplierSelected && ( + + * + + )} + + + Plat Nomor + {hasAnySupplierSelected && ( + + * + + )} + Dokumen - Biaya Pengiriman (Rp.) - Biaya Per Item (Rp.) - Nama Sopir + + Biaya Pengiriman (Rp.) + {hasAnySupplierSelected && ( + + * + + )} + + + Biaya Per Item (Rp.) + {hasAnySupplierSelected && ( + + * + + )} + + + Nama Sopir + {hasAnySupplierSelected && ( + + * + + )} + {type !== 'detail' && Aksi} @@ -1772,6 +1844,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { idx )} readOnly={type === 'detail'} + required={isSupplierSelected(idx)} className={{ wrapper: 'w-full min-w-52 md:min-w-72 lg:min-w-80', }} @@ -1867,6 +1940,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { idx )} readOnly={type === 'detail'} + required={isSupplierSelected(idx)} className={{ wrapper: 'w-full min-w-52 md:min-w-72 lg:min-w-80', }} @@ -1890,6 +1964,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { idx )} readOnly={type === 'detail'} + required={isSupplierSelected(idx)} className={{ wrapper: 'w-full min-w-52 md:min-w-72 lg:min-w-80', }} @@ -1908,6 +1983,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { idx )} readOnly={type === 'detail'} + required={isSupplierSelected(idx)} className={{ wrapper: 'w-full min-w-52 md:min-w-72 lg:min-w-80', }}