feat(FE-170,174): add total_weight field and update calculations in body_weights for RecordingForm

This commit is contained in:
rstubryan
2025-11-04 16:23:29 +07:00
parent b7ab537b95
commit 04a1f5e014
3 changed files with 50 additions and 14 deletions
@@ -17,6 +17,7 @@ type RecordingGrowingFormSchemaType = {
weight: number | string; weight: number | string;
avg_weight: number | string; avg_weight: number | string;
qty: number | string; qty: number | string;
total_weight: number | string;
}[]; }[];
stocks: { stocks: {
product_warehouse_id: number; product_warehouse_id: number;
@@ -47,6 +48,7 @@ export type BodyWeightSchema = {
weight: number | string; weight: number | string;
avg_weight: number | string; avg_weight: number | string;
qty: number | string; qty: number | string;
total_weight: number | string;
}; };
export type StockSchema = { export type StockSchema = {
@@ -77,6 +79,10 @@ const BodyWeightObjectSchema: Yup.ObjectSchema<BodyWeightSchema> = Yup.object({
.required('Jumlah ayam wajib diisi!') .required('Jumlah ayam wajib diisi!')
.min(1, 'Jumlah ayam minimal 1 ekor!') .min(1, 'Jumlah ayam minimal 1 ekor!')
.typeError('Jumlah ayam harus berupa angka!'), .typeError('Jumlah ayam harus berupa angka!'),
total_weight: Yup.number()
.required('Berat ayam total wajib diisi!')
.min(0, 'Berat ayam total tidak boleh negatif!')
.typeError('Berat ayam total harus berupa angka!'),
}); });
const StockObjectSchema: Yup.ObjectSchema<StockSchema> = Yup.object({ const StockObjectSchema: Yup.ObjectSchema<StockSchema> = Yup.object({
@@ -249,12 +255,14 @@ export const getRecordingGrowingFormInitialValues = (
weight: bw.avg_weight * bw.qty, weight: bw.avg_weight * bw.qty,
avg_weight: bw.avg_weight, avg_weight: bw.avg_weight,
qty: bw.qty, qty: bw.qty,
total_weight: bw.total_weight,
}) })
) ?? [ ) ?? [
{ {
weight: '', weight: '',
avg_weight: '', avg_weight: '',
qty: '', qty: '',
total_weight: 0,
}, },
], ],
stocks: initialValues?.stocks?.map( stocks: initialValues?.stocks?.map(
@@ -420,13 +420,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const layingPayload = { const layingPayload = {
project_flock_kandang_id: layingValues.project_flock_kandang_id, project_flock_kandang_id: layingValues.project_flock_kandang_id,
body_weights: (layingValues.body_weights ?? []).map((bw) => ({ body_weights: (layingValues.body_weights ?? []).map((bw) => {
avg_weight: const qty = Number(bw.qty) || 0;
typeof bw.avg_weight === 'number' const weight = Number(bw.weight) || 0;
? bw.avg_weight const totalWeight = qty * weight;
: parseFloat(String(bw.avg_weight)) || 0,
qty: Number(bw.qty) || 0, return {
})), avg_weight:
typeof bw.avg_weight === 'number'
? bw.avg_weight
: parseFloat(String(bw.avg_weight)) || 0,
qty: qty,
total_weight: parseFloat(String(totalWeight)) || 0,
};
}),
stocks: (layingValues.stocks ?? []).map((stock) => ({ stocks: (layingValues.stocks ?? []).map((stock) => ({
product_warehouse_id: stock.product_warehouse_id, product_warehouse_id: stock.product_warehouse_id,
qty: Number(stock.qty) || 0, qty: Number(stock.qty) || 0,
@@ -459,13 +466,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const growingPayload = { const growingPayload = {
project_flock_kandang_id: growingValues.project_flock_kandang_id, project_flock_kandang_id: growingValues.project_flock_kandang_id,
body_weights: (growingValues.body_weights ?? []).map((bw) => ({ body_weights: (growingValues.body_weights ?? []).map((bw) => {
avg_weight: const qty = Number(bw.qty) || 0;
typeof bw.avg_weight === 'number' const weight = Number(bw.weight) || 0;
? bw.avg_weight const totalWeight = qty * weight;
: parseFloat(String(bw.avg_weight)) || 0,
qty: Number(bw.qty) || 0, return {
})), avg_weight:
typeof bw.avg_weight === 'number'
? bw.avg_weight
: parseFloat(String(bw.avg_weight)) || 0,
qty: qty,
total_weight: parseFloat(String(totalWeight)) || 0,
};
}),
stocks: (growingValues.stocks ?? []).map((stock) => ({ stocks: (growingValues.stocks ?? []).map((stock) => ({
product_warehouse_id: stock.product_warehouse_id, product_warehouse_id: stock.product_warehouse_id,
qty: Number(stock.qty) || 0, qty: Number(stock.qty) || 0,
@@ -801,12 +815,17 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const currentWeight = formik.values.body_weights?.[idx]; const currentWeight = formik.values.body_weights?.[idx];
if (currentWeight) { if (currentWeight) {
const qty = Number(currentWeight.qty) || 0; const qty = Number(currentWeight.qty) || 0;
const totalWeight = qty * value;
if (qty > 0 && value > 0) { if (qty > 0 && value > 0) {
const avgWeight = parseFloat((value / qty).toFixed(2)); const avgWeight = parseFloat((value / qty).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight); formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else { } else {
formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0); formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0);
} }
// Update total_weight
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
} }
}; };
@@ -825,8 +844,11 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
if (qty > 0 && value > 0) { if (qty > 0 && value > 0) {
const totalWeight = value * qty; const totalWeight = value * qty;
formik.setFieldValue(`body_weights.${idx}.weight`, totalWeight); formik.setFieldValue(`body_weights.${idx}.weight`, totalWeight);
// Update total_weight
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
} else { } else {
formik.setFieldValue(`body_weights.${idx}.weight`, 0); formik.setFieldValue(`body_weights.${idx}.weight`, 0);
formik.setFieldValue(`body_weights.${idx}.total_weight`, 0);
} }
} }
}; };
@@ -843,12 +865,17 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const currentWeight = formik.values.body_weights?.[idx]; const currentWeight = formik.values.body_weights?.[idx];
if (currentWeight) { if (currentWeight) {
const weight = Number(currentWeight.weight) || 0; const weight = Number(currentWeight.weight) || 0;
const totalWeight = value * weight;
if (value > 0 && weight > 0) { if (value > 0 && weight > 0) {
const avgWeight = parseFloat((weight / value).toFixed(2)); const avgWeight = parseFloat((weight / value).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight); formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else { } else {
formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0); formik.setFieldValue(`body_weights.${idx}.avg_weight`, 0);
} }
// Update total_weight
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
} }
}; };
+1
View File
@@ -78,6 +78,7 @@ export type CreateGrowingRecordingPayload = {
body_weights: { body_weights: {
avg_weight: number; avg_weight: number;
qty: number; qty: number;
total_weight: number;
}[]; }[];
stocks?: { stocks?: {
product_warehouse_id: number; product_warehouse_id: number;