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;
avg_weight: number | string;
qty: number | string;
total_weight: number | string;
}[];
stocks: {
product_warehouse_id: number;
@@ -47,6 +48,7 @@ export type BodyWeightSchema = {
weight: number | string;
avg_weight: number | string;
qty: number | string;
total_weight: number | string;
};
export type StockSchema = {
@@ -77,6 +79,10 @@ const BodyWeightObjectSchema: Yup.ObjectSchema<BodyWeightSchema> = Yup.object({
.required('Jumlah ayam wajib diisi!')
.min(1, 'Jumlah ayam minimal 1 ekor!')
.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({
@@ -249,12 +255,14 @@ export const getRecordingGrowingFormInitialValues = (
weight: bw.avg_weight * bw.qty,
avg_weight: bw.avg_weight,
qty: bw.qty,
total_weight: bw.total_weight,
})
) ?? [
{
weight: '',
avg_weight: '',
qty: '',
total_weight: 0,
},
],
stocks: initialValues?.stocks?.map(
@@ -420,13 +420,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const layingPayload = {
project_flock_kandang_id: layingValues.project_flock_kandang_id,
body_weights: (layingValues.body_weights ?? []).map((bw) => ({
avg_weight:
typeof bw.avg_weight === 'number'
? bw.avg_weight
: parseFloat(String(bw.avg_weight)) || 0,
qty: Number(bw.qty) || 0,
})),
body_weights: (layingValues.body_weights ?? []).map((bw) => {
const qty = Number(bw.qty) || 0;
const weight = Number(bw.weight) || 0;
const totalWeight = qty * weight;
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) => ({
product_warehouse_id: stock.product_warehouse_id,
qty: Number(stock.qty) || 0,
@@ -459,13 +466,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const growingPayload = {
project_flock_kandang_id: growingValues.project_flock_kandang_id,
body_weights: (growingValues.body_weights ?? []).map((bw) => ({
avg_weight:
typeof bw.avg_weight === 'number'
? bw.avg_weight
: parseFloat(String(bw.avg_weight)) || 0,
qty: Number(bw.qty) || 0,
})),
body_weights: (growingValues.body_weights ?? []).map((bw) => {
const qty = Number(bw.qty) || 0;
const weight = Number(bw.weight) || 0;
const totalWeight = qty * weight;
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) => ({
product_warehouse_id: stock.product_warehouse_id,
qty: Number(stock.qty) || 0,
@@ -801,12 +815,17 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const currentWeight = formik.values.body_weights?.[idx];
if (currentWeight) {
const qty = Number(currentWeight.qty) || 0;
const totalWeight = qty * value;
if (qty > 0 && value > 0) {
const avgWeight = parseFloat((value / qty).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else {
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) {
const totalWeight = value * qty;
formik.setFieldValue(`body_weights.${idx}.weight`, totalWeight);
// Update total_weight
formik.setFieldValue(`body_weights.${idx}.total_weight`, totalWeight);
} else {
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];
if (currentWeight) {
const weight = Number(currentWeight.weight) || 0;
const totalWeight = value * weight;
if (value > 0 && weight > 0) {
const avgWeight = parseFloat((weight / value).toFixed(2));
formik.setFieldValue(`body_weights.${idx}.avg_weight`, avgWeight);
} else {
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: {
avg_weight: number;
qty: number;
total_weight: number;
}[];
stocks?: {
product_warehouse_id: number;