diff --git a/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.schema.ts b/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.schema.ts index e0273a9a..2e26a36d 100644 --- a/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.schema.ts +++ b/src/components/pages/production/transfer-to-laying/form/TransferToLayingForm.schema.ts @@ -1,4 +1,7 @@ import * as Yup from 'yup'; +import { TransferToLaying } from '@/types/api/production/transfer-to-laying'; +import { TransferToLayingApi } from '@/services/api/production/transfer-to-laying'; +import { formatDate } from '@/lib/helper'; type TransferToLayingFormSchemaType = { transfer_date?: string; @@ -14,7 +17,7 @@ type TransferToLayingFormSchemaType = { totalQuantity?: number; maxTotalQuantity?: number; // original cap (hidden), helper - kandangs: { + flockSourceKandangs: { kandang: { value: number; label: string; @@ -22,6 +25,16 @@ type TransferToLayingFormSchemaType = { quantity: number | string; // editable maxQuantity?: number; // original cap (hidden), helper }[]; + + flockDestinationKandangs: { + kandang: { + value: number; + label: string; + }; + quantity: number | string; // editable + maxQuantity?: number; // original cap (hidden), helper + }[]; + reason?: string; }; @@ -51,7 +64,29 @@ export const TransferToLayingFormSchema: Yup.ObjectSchema `Kuantitas maksimal ${max}!` + ) + .required('Kuantitas wajib diisi!'), + + maxQuantity: Yup.number().min(1).required(), // internal helper field + }) + ) + .min(1, 'Minimal 1 kandang terisi!') + .required('Kandang wajib diisi!'), + + flockDestinationKandangs: Yup.array() .of( Yup.object({ kandang: Yup.object({ @@ -81,3 +116,122 @@ export const UpdateTransferToLayingFormSchema = TransferToLayingFormSchema; export type TransferToLayingFormValues = Yup.InferType< typeof TransferToLayingFormSchema >; + +export const getTransferToLayingFormInitialValues = ( + initialValues?: TransferToLaying +): TransferToLayingFormValues => { + return { + transfer_date: initialValues?.transfer_date + ? formatDate(initialValues.transfer_date, 'YYYY-MM-DD') + : '', + flockSource: initialValues?.from_project_flock + ? { + value: initialValues?.from_project_flock.id, + label: initialValues?.from_project_flock.flock_name, + } + : undefined, + flockDestination: initialValues?.to_project_flock + ? { + value: initialValues?.to_project_flock.id, + label: initialValues?.to_project_flock.flock_name, + } + : undefined, + totalQuantity: + initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? undefined, + + flockSourceKandangs: initialValues?.sources + ? initialValues.sources.map((sourceKandang) => ({ + kandang: { + value: sourceKandang.source_project_flock_kandang.kandang.id, + label: sourceKandang.source_project_flock_kandang.kandang.name, + }, + quantity: sourceKandang.qty, + })) + : [], + + flockDestinationKandangs: initialValues?.targets + ? initialValues.targets.map((targetKandang) => ({ + kandang: { + value: targetKandang.target_project_flock_kandang.kandang.id, + label: targetKandang.target_project_flock_kandang.kandang.name, + }, + quantity: targetKandang.qty, + })) + : [], + + reason: initialValues?.notes ?? undefined, + }; +}; + +export const getFilledTransferToLayingFormInitialValues = async ( + initialValues?: TransferToLaying +): Promise => { + const mappedFlockSourceKandangsAvailableQty = + await TransferToLayingApi.getMappedFlockKandangsAvailability( + initialValues?.from_project_flock.id as number + ); + + const formattedFlockSourceKandangs = initialValues?.sources + ? initialValues.sources.map((sourceKandang) => ({ + kandang: { + value: sourceKandang.source_project_flock_kandang.kandang.id, + label: sourceKandang.source_project_flock_kandang.kandang.name, + }, + quantity: sourceKandang.qty, + maxQuantity: + (mappedFlockSourceKandangsAvailableQty && + mappedFlockSourceKandangsAvailableQty[ + sourceKandang.source_project_flock_kandang.id + ].available_qty) ?? + 0, + })) + : []; + + let maxTotalQuantity = 0; + formattedFlockSourceKandangs.forEach((item) => { + maxTotalQuantity += item.maxQuantity; + }); + + return { + transfer_date: initialValues?.transfer_date + ? formatDate(initialValues.transfer_date, 'YYYY-MM-DD') + : '', + flockSource: initialValues?.from_project_flock + ? { + value: initialValues?.from_project_flock.id, + label: initialValues?.from_project_flock.flock_name, + } + : undefined, + flockDestination: initialValues?.to_project_flock + ? { + value: initialValues?.to_project_flock.id, + label: initialValues?.to_project_flock.flock_name, + } + : undefined, + totalQuantity: + initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? undefined, + maxTotalQuantity: maxTotalQuantity, + + flockSourceKandangs: formattedFlockSourceKandangs, + + flockDestinationKandangs: initialValues?.targets + ? initialValues.targets.map((targetKandang) => ({ + kandang: { + value: targetKandang.target_project_flock_kandang.kandang.id, + label: targetKandang.target_project_flock_kandang.kandang.name, + }, + quantity: targetKandang.qty, + + // maxQuantity: + // targetKandang.target_project_flock_kandang.kandang.capacity, + + // TODO: integrate this to real API kandang capacity + maxQuantity: + targetKandang.target_project_flock_kandang.kandang.capacity ?? + Infinity, + })) + : [], + + reason: initialValues?.notes ?? undefined, + }; +};