diff --git a/src/components/pages/production/recording/form/RecordingForm.schema.ts b/src/components/pages/production/recording/form/RecordingForm.schema.ts index b39f94ca..7ff6b7f3 100644 --- a/src/components/pages/production/recording/form/RecordingForm.schema.ts +++ b/src/components/pages/production/recording/form/RecordingForm.schema.ts @@ -29,8 +29,8 @@ type RecordingGrowingFormSchemaType = { } | null; project_flock_kandang_id: number; stocks: { - product_warehouse_id: number; - qty: number | string; + product_warehouse_id?: number; + qty?: number | string; }[]; depletions: { product_warehouse_id?: number; @@ -73,6 +73,18 @@ const StockObjectSchema: Yup.ObjectSchema = Yup.object({ .typeError('Jumlah penggunaan harus berupa angka!'), }); +const OptionalStockObjectSchema: Yup.ObjectSchema<{ + product_warehouse_id?: number; + qty?: number | string; +}> = Yup.object({ + product_warehouse_id: Yup.number() + .optional() + .typeError('Produk harus berupa angka!'), + qty: Yup.number() + .optional() + .typeError('Jumlah penggunaan harus berupa angka!'), +}); + const DepletionObjectSchema: Yup.ObjectSchema = Yup.object({ product_warehouse_id: Yup.number() .optional() @@ -90,7 +102,9 @@ const EggObjectSchema: Yup.ObjectSchema = Yup.object({ weight: Yup.number().optional().typeError('Berat telur harus berupa angka!'), }); -export const RecordingGrowingFormSchema: Yup.ObjectSchema = +export const RecordingGrowingFormSchema = ( + isTransitioningToLaying = false +): Yup.ObjectSchema => Yup.object({ record_date: Yup.string() .required('Tanggal recording wajib diisi!') @@ -150,20 +164,24 @@ export const RecordingGrowingFormSchema: Yup.ObjectSchema = - RecordingGrowingFormSchema.shape({ + RecordingGrowingFormSchema().shape({ eggs: Yup.array().of(EggObjectSchema).default([]), }); -export const UpdateRecordingGrowingFormSchema = - RecordingGrowingFormSchema.shape({ +export const UpdateRecordingGrowingFormSchema = ( + isTransitioningToLaying = false +) => + RecordingGrowingFormSchema(isTransitioningToLaying).shape({ location_id: Yup.number().nullable().optional(), project_flock_id: Yup.number().nullable().optional(), kandang_id: Yup.number().nullable().optional(), @@ -193,10 +211,13 @@ export const UpdateRecordingLayingFormSchema = RecordingLayingFormSchema.shape({ .required('Project Flock Kandang wajib diisi!'), }); -export type RecordingGrowingFormValues = Yup.InferType< +type RecordingGrowingFormSchemaFn = ReturnType< typeof RecordingGrowingFormSchema >; +export type RecordingGrowingFormValues = + Yup.InferType; + export type RecordingLayingFormValues = Yup.InferType< typeof RecordingLayingFormSchema >; diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index bd196c7e..94676f43 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -591,6 +591,14 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { projectFlockKandangLookup?.project_flock?.category === 'GROWING' || projectFlockKandangDetail?.project_flock?.category === 'GROWING'; + const isTransitioningToLaying = useMemo(() => { + if (!isGrowingCategory) return false; + return ( + initialValues?.population_can_change === true || + initialValues?.transfer_executed === true + ); + }, [initialValues, isGrowingCategory]); + const recordingApprovalLines = useMemo(() => { if (isLayingCategory) { return LAYING_RECORDING_APPROVAL_LINE; @@ -951,8 +959,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { } else { schema = type === 'edit' - ? UpdateRecordingGrowingFormSchema - : RecordingGrowingFormSchema; + ? UpdateRecordingGrowingFormSchema(isTransitioningToLaying) + : RecordingGrowingFormSchema(isTransitioningToLaying); } return schema.clone().concat( Yup.object().shape({ @@ -2333,21 +2341,25 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { )} Persediaan - - * - + {!isTransitioningToLaying && ( + + * + + )} Jumlah Pakai - - * - + {!isTransitioningToLaying && ( + + * + + )} {(type as 'add' | 'edit' | 'detail') !== 'detail' && ( Action @@ -2382,7 +2394,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { )} {