mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 21:41:57 +00:00
chore: adjust getFilledTransferToLayingFormInitialValues function to set max quantity for flock destination kandang
This commit is contained in:
+42
-27
@@ -1,7 +1,10 @@
|
||||
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';
|
||||
import { formatDate, formatNumber } from '@/lib/helper';
|
||||
import { ProjectFlock } from '@/types/api/production/project-flock';
|
||||
import { ProjectFlockApi } from '@/services/api/production/project-flock';
|
||||
import { isResponseSuccess } from '@/lib/api-helper';
|
||||
|
||||
type TransferToLayingFormSchemaType = {
|
||||
transfer_date?: string;
|
||||
@@ -14,7 +17,7 @@ type TransferToLayingFormSchemaType = {
|
||||
label: string;
|
||||
};
|
||||
|
||||
totalQuantity?: number;
|
||||
totalQuantity?: number | string;
|
||||
maxTotalQuantity?: number; // original cap (hidden), helper
|
||||
|
||||
flockSourceKandangs: {
|
||||
@@ -53,15 +56,15 @@ export const TransferToLayingFormSchema: Yup.ObjectSchema<TransferToLayingFormSc
|
||||
}).required('Flock tujuan wajib diisi!'),
|
||||
|
||||
totalQuantity: Yup.number()
|
||||
.min(1, 'Jumlah transfer minimal 1')
|
||||
.min(0, 'Jumlah transfer minimal 0')
|
||||
.max(
|
||||
Yup.ref('maxTotalQuantity'),
|
||||
({ max }) => `Kuantitas maksimal ${max}!`
|
||||
({ max }) => `Kuantitas maksimal ${formatNumber(max)}!`
|
||||
)
|
||||
.required('Jumlah transfer wajib diisi!'),
|
||||
|
||||
maxTotalQuantity: Yup.number()
|
||||
.min(1, 'Jumlah transfer minimal 1')
|
||||
.min(0, 'Jumlah transfer minimal 0')
|
||||
.required('Jumlah transfer wajib diisi!'),
|
||||
|
||||
flockSourceKandangs: Yup.array()
|
||||
@@ -76,7 +79,7 @@ export const TransferToLayingFormSchema: Yup.ObjectSchema<TransferToLayingFormSc
|
||||
.min(0, 'Kuantitas minimal 0!')
|
||||
.max(
|
||||
Yup.ref('maxQuantity'),
|
||||
({ max }) => `Kuantitas maksimal ${max}!`
|
||||
({ max }) => `Kuantitas maksimal ${formatNumber(max)}!`
|
||||
)
|
||||
.required('Kuantitas wajib diisi!'),
|
||||
|
||||
@@ -98,7 +101,7 @@ export const TransferToLayingFormSchema: Yup.ObjectSchema<TransferToLayingFormSc
|
||||
.min(0, 'Kuantitas minimal 0!')
|
||||
.max(
|
||||
Yup.ref('maxQuantity'),
|
||||
({ max }) => `Kuantitas maksimal ${max}!`
|
||||
({ max }) => `Kuantitas maksimal ${formatNumber(max)}!`
|
||||
)
|
||||
.required('Kuantitas wajib diisi!'),
|
||||
|
||||
@@ -137,12 +140,12 @@ export const getTransferToLayingFormInitialValues = (
|
||||
}
|
||||
: undefined,
|
||||
totalQuantity:
|
||||
initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? undefined,
|
||||
initialValues?.usage_qty ?? initialValues?.pending_usage_qty ?? '',
|
||||
|
||||
flockSourceKandangs: initialValues?.sources
|
||||
? initialValues.sources.map((sourceKandang) => ({
|
||||
kandang: {
|
||||
value: sourceKandang.source_project_flock_kandang.kandang.id,
|
||||
value: sourceKandang.source_project_flock_kandang.id,
|
||||
label: sourceKandang.source_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: sourceKandang.qty,
|
||||
@@ -152,7 +155,7 @@ export const getTransferToLayingFormInitialValues = (
|
||||
flockDestinationKandangs: initialValues?.targets
|
||||
? initialValues.targets.map((targetKandang) => ({
|
||||
kandang: {
|
||||
value: targetKandang.target_project_flock_kandang.kandang.id,
|
||||
value: targetKandang.target_project_flock_kandang.id,
|
||||
label: targetKandang.target_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: targetKandang.qty,
|
||||
@@ -174,7 +177,7 @@ export const getFilledTransferToLayingFormInitialValues = async (
|
||||
const formattedFlockSourceKandangs = initialValues?.sources
|
||||
? initialValues.sources.map((sourceKandang) => ({
|
||||
kandang: {
|
||||
value: sourceKandang.source_project_flock_kandang.kandang.id,
|
||||
value: sourceKandang.source_project_flock_kandang.id,
|
||||
label: sourceKandang.source_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: sourceKandang.qty,
|
||||
@@ -189,9 +192,35 @@ export const getFilledTransferToLayingFormInitialValues = async (
|
||||
|
||||
let maxTotalQuantity = 0;
|
||||
formattedFlockSourceKandangs.forEach((item) => {
|
||||
maxTotalQuantity += item.maxQuantity;
|
||||
maxTotalQuantity += item.quantity;
|
||||
});
|
||||
|
||||
const flockDestination = await ProjectFlockApi.getSingle(
|
||||
initialValues?.to_project_flock.id as number
|
||||
);
|
||||
|
||||
const formattedFlockDestinationKandangs = initialValues?.targets
|
||||
? initialValues.targets.map((targetKandang) => {
|
||||
const kandang = isResponseSuccess(flockDestination)
|
||||
? flockDestination?.data?.kandangs.find(
|
||||
(kandang) =>
|
||||
String(kandang.project_flock_kandang_id) ===
|
||||
String(targetKandang.target_project_flock_kandang.id)
|
||||
)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
kandang: {
|
||||
value: targetKandang.target_project_flock_kandang.id,
|
||||
label: targetKandang.target_project_flock_kandang.kandang.name,
|
||||
},
|
||||
quantity: targetKandang.qty,
|
||||
|
||||
maxQuantity: kandang?.capacity ?? 0,
|
||||
};
|
||||
})
|
||||
: [];
|
||||
|
||||
return {
|
||||
transfer_date: initialValues?.transfer_date
|
||||
? formatDate(initialValues.transfer_date, 'YYYY-MM-DD')
|
||||
@@ -214,21 +243,7 @@ export const getFilledTransferToLayingFormInitialValues = async (
|
||||
|
||||
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: Infinity,
|
||||
}))
|
||||
: [],
|
||||
flockDestinationKandangs: formattedFlockDestinationKandangs,
|
||||
|
||||
reason: initialValues?.notes ?? undefined,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user