From 2dc3bcf9f0faeae70fe8ba63af87441f55b96c2e Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 16:55:23 +0700 Subject: [PATCH 1/7] fix: make convertion unit support QTY when hitting update/create API --- .../pages/marketing/SalesOrderFormModal.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/pages/marketing/SalesOrderFormModal.tsx b/src/components/pages/marketing/SalesOrderFormModal.tsx index 8fc4a031..e06ecbf3 100644 --- a/src/components/pages/marketing/SalesOrderFormModal.tsx +++ b/src/components/pages/marketing/SalesOrderFormModal.tsx @@ -195,7 +195,9 @@ const SalesOrderFormModal = ({ product.marketing_type?.value?.toLowerCase() === 'telur' ? convertionUnitValue === 'PETI' ? 'PETI' - : 'KG' // termasuk "QTY" dan "KG" + : convertionUnitValue === 'QTY' + ? 'QTY' + : 'KG' : undefined; // Jika value dari data product ada week, kirim "AYAM_PULLET, jika tidak ada kirim "AYAM" @@ -205,10 +207,15 @@ const SalesOrderFormModal = ({ marketingTypeValue = product.week ? 'AYAM_PULLET' : 'AYAM'; } + const formattedUnitPrice = + convertionUnitValue === 'QTY' + ? parseFloat(String(product.price_per_qty || 0)) + : parseFloat(String(product.unit_price || 0)); + return { vehicle_number: product.vehicle_number as string, product_warehouse_id: product.product_warehouse_id as number, - unit_price: parseFloat(String(product.unit_price || 0)), + unit_price: formattedUnitPrice, total_weight: parseFloat(String(product.total_weight || 0)), qty: parseFloat(String(product.qty || 0)), avg_weight: parseFloat(String(product.avg_weight || 0)), From 8961004000f2fcb25fed41690407e374b14492a5 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 16:55:39 +0700 Subject: [PATCH 2/7] fix: set initialPricePerConvertion to unit_price --- .../sales-order/SalesOrderProductForm.tsx | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx index 5fd5c3cb..1a09ed7a 100644 --- a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx +++ b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx @@ -61,14 +61,18 @@ const SalesOrderProductForm = ({ Number(initialValues.total_peti) : 0; - const initialPricePerConvertion = - initialValues?.total_price && - initialValues?.total_peti && - Number(initialValues.total_peti) !== 0 - ? (Number(initialValues.total_price) - - initialSisaBerat * Number(initialValues.unit_price || 0)) / - Number(initialValues.total_peti) - : 0; + // const initialPricePerConvertion = + // initialValues?.total_price && + // initialValues?.total_peti && + // Number(initialValues.total_peti) !== 0 + // ? (Number(initialValues.total_price) - + // initialSisaBerat * Number(initialValues.unit_price || 0)) / + // Number(initialValues.total_peti) + // : 0; + + const initialPricePerConvertion = initialValues?.unit_price + ? Number(initialValues?.unit_price) + : 0; const initialPriceSisaBerat = initialValues?.total_price && initialValues?.total_peti @@ -135,6 +139,11 @@ const SalesOrderProductForm = ({ ); }, [selectedProductWarehouse, formik.values.marketing_type]); + console.log({ + initialValues, + values: formik.values, + }); + // ===== Options ===== const { options: warehouseOptions, @@ -523,7 +532,7 @@ const SalesOrderProductForm = ({ null} @@ -546,7 +555,9 @@ const SalesOrderProductForm = ({ } per ${formik.values.convertion_unit?.value}`} value={formik.values.weight_per_convertion ?? ''} onChange={(e) => { - const value = Number(e.target.value); + const value = Number(e.target.value) + ? Number(e.target.value) + : ''; handleFieldChange('weight_per_convertion', value, () => setCurrentInput(e.target.name) ); @@ -699,7 +710,8 @@ const SalesOrderProductForm = ({ formik.values.convertion_unit?.value.toLowerCase() === 'kg') && ( { @@ -745,7 +757,7 @@ const SalesOrderProductForm = ({ formik.values.convertion_unit?.value.toLowerCase() !== 'kg' && ( { From 429ff58bfd541559e93317b78b8e3bff6acccdbd Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 16:57:01 +0700 Subject: [PATCH 3/7] chore: remove unnecessary code --- .../form/repeater/sales-order/SalesOrderProductForm.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx index 1a09ed7a..49d8a9f1 100644 --- a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx +++ b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx @@ -710,8 +710,7 @@ const SalesOrderProductForm = ({ formik.values.convertion_unit?.value.toLowerCase() === 'kg') && ( { @@ -757,7 +756,7 @@ const SalesOrderProductForm = ({ formik.values.convertion_unit?.value.toLowerCase() !== 'kg' && ( { From ef1ce2c78c66f709620c90cdf53cd17a407d1faf Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 16:59:35 +0700 Subject: [PATCH 4/7] fix: remove roundPrice, update unit price calculation in calculateTelurPeti --- src/lib/marketing-calculation.ts | 76 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/src/lib/marketing-calculation.ts b/src/lib/marketing-calculation.ts index 5ad5a1e6..95db7eb0 100644 --- a/src/lib/marketing-calculation.ts +++ b/src/lib/marketing-calculation.ts @@ -76,13 +76,13 @@ export const calculateTrading = ( case 'unit_price': case 'qty': { if (unitPrice > 0 && qty > 0) { - setFieldValue('total_price', roundPrice(unitPrice * qty)); + setFieldValue('total_price', unitPrice * qty); } break; } case 'total_price': { if (totalPrice > 0 && qty > 0) { - setFieldValue('unit_price', roundPrice(totalPrice / qty)); + setFieldValue('unit_price', totalPrice / qty); } break; } @@ -112,7 +112,7 @@ export const calculateAyamPullet = ( case 'qty': { // total_price = unit_price × week × qty if (unitPrice > 0 && week > 0 && qty > 0) { - setFieldValue('total_price', roundPrice(unitPrice * week * qty)); + setFieldValue('total_price', unitPrice * week * qty); } // total_weight = avg_weight × qty if (avgWeight > 0 && qty > 0) { @@ -135,7 +135,7 @@ export const calculateAyamPullet = ( case 'total_price': { // Reverse: unit_price = total_price / (week × qty) if (totalPrice > 0 && week > 0 && qty > 0) { - setFieldValue('unit_price', roundPrice(totalPrice / (week * qty))); + setFieldValue('unit_price', totalPrice / (week * qty)); } break; } @@ -164,7 +164,7 @@ export const calculateAyam = (field: string, ctx: CalculationContext): void => { setFieldValue('total_weight', tw); // total_price = total_weight × unit_price if (unitPrice > 0) { - setFieldValue('total_price', roundPrice(tw * unitPrice)); + setFieldValue('total_price', tw * unitPrice); } } break; @@ -176,21 +176,21 @@ export const calculateAyam = (field: string, ctx: CalculationContext): void => { } // total_price = total_weight × unit_price if (unitPrice > 0 && totalWeight > 0) { - setFieldValue('total_price', roundPrice(totalWeight * unitPrice)); + setFieldValue('total_price', totalWeight * unitPrice); } break; } case 'unit_price': { // total_price = total_weight × unit_price if (unitPrice > 0 && totalWeight > 0) { - setFieldValue('total_price', roundPrice(totalWeight * unitPrice)); + setFieldValue('total_price', totalWeight * unitPrice); } break; } case 'total_price': { // unit_price = total_price / total_weight if (totalPrice > 0 && totalWeight > 0) { - setFieldValue('unit_price', roundPrice(totalPrice / totalWeight)); + setFieldValue('unit_price', totalPrice / totalWeight); } break; } @@ -223,7 +223,8 @@ export const calculateTelurPeti = ( // Helper untuk menghitung dan set unit_price = total_price / total_weight const updateUnitPrice = (tp: number, tw: number) => { if (tw > 0 && tp > 0) { - setFieldValue('unit_price', roundPrice(tp / tw)); + const unitPrice = tp / tw; + setFieldValue('unit_price', unitPrice); } }; @@ -232,10 +233,12 @@ export const calculateTelurPeti = ( // Recalculate total_price = (price_per_convertion × total_peti) + price_sisa_berat if (pricePerConvertion > 0 && totalPeti > 0) { const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat; - setFieldValue('total_price', roundPrice(totalPrice)); + setFieldValue('total_price', totalPrice); // Recalculate unit_price = total_price / total_weight + + // TODO: consider sisa berat later const totalWeight = weightPerConvertion * totalPeti + sisaBerat; - updateUnitPrice(totalPrice, totalWeight); + updateUnitPrice(totalPrice, totalPeti); } break; } @@ -253,9 +256,9 @@ export const calculateTelurPeti = ( // Recalculate total_price = (price_per_convertion × total_peti) + price_sisa_berat if (pricePerConvertion > 0 && totalPeti > 0) { const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat; - setFieldValue('total_price', roundPrice(totalPrice)); - // Recalculate unit_price = total_price / total_weight - updateUnitPrice(totalPrice, totalWeight); + setFieldValue('total_price', totalPrice); + // Recalculate unit_price = total_price / total_peti + updateUnitPrice(totalPrice, totalPeti); } break; } @@ -263,7 +266,7 @@ export const calculateTelurPeti = ( // Recalculate total_price if (pricePerConvertion > 0 && totalPeti > 0) { const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat; - setFieldValue('total_price', roundPrice(totalPrice)); + setFieldValue('total_price', totalPrice); // Recalculate unit_price = total_price / total_weight const totalWeight = weightPerConvertion * totalPeti + sisaBerat; updateUnitPrice(totalPrice, totalWeight); @@ -306,7 +309,7 @@ export const calculateTelurPeti = ( if (totalPeti > 0 && totalPrice > priceSisaBerat) { setFieldValue( 'price_per_convertion', - roundPrice((totalPrice - priceSisaBerat) / totalPeti) + (totalPrice - priceSisaBerat) / totalPeti ); } // Update unit_price = total_price / total_weight @@ -341,10 +344,7 @@ export const calculateTelurKg = ( } // total_price = total_weight × unit_price if (pricePerConvertion > 0 && totalWeight > 0) { - setFieldValue( - 'total_price', - roundPrice(totalWeight * pricePerConvertion) - ); + setFieldValue('total_price', totalWeight * pricePerConvertion); setFieldValue('unit_price', pricePerConvertion); } break; @@ -352,10 +352,7 @@ export const calculateTelurKg = ( case 'price_per_convertion': { // total_price = total_weight × price_per_convertion if (pricePerConvertion > 0 && totalWeight > 0) { - setFieldValue( - 'total_price', - roundPrice(totalWeight * pricePerConvertion) - ); + setFieldValue('total_price', totalWeight * pricePerConvertion); setFieldValue('unit_price', pricePerConvertion); } break; @@ -363,11 +360,8 @@ export const calculateTelurKg = ( case 'total_price': { // unit_price = total_price / total_weight if (totalPrice > 0 && totalWeight > 0) { - setFieldValue('unit_price', roundPrice(totalPrice / totalWeight)); - setFieldValue( - 'price_per_convertion', - roundPrice(totalPrice / totalWeight) - ); + setFieldValue('unit_price', totalPrice / totalWeight); + setFieldValue('price_per_convertion', totalPrice / totalWeight); } break; } @@ -405,11 +399,11 @@ export const calculateTelurQty = ( setFieldValue('total_weight', tw); // total_price = qty × price_per_qty if (pricePerQty > 0) { - const tp = roundPrice(qty * pricePerQty); + const tp = qty * pricePerQty; setFieldValue('total_price', tp); // unit_price = total_price / total_weight (untuk BE) if (tw > 0) { - setFieldValue('unit_price', roundPrice(tp / tw)); + setFieldValue('unit_price', tp / tw); } } } @@ -421,7 +415,7 @@ export const calculateTelurQty = ( setFieldValue('avg_weight', preciseWeight(totalWeight / qty)); // Recalculate total_price jika ada unit_price if (unitPrice > 0) { - setFieldValue('total_price', roundPrice(totalWeight * unitPrice)); + setFieldValue('total_price', totalWeight * unitPrice); } } break; @@ -429,11 +423,11 @@ export const calculateTelurQty = ( case 'price_per_qty': { // total_price = qty × price_per_qty if (pricePerQty > 0 && qty > 0) { - const tp = roundPrice(qty * pricePerQty); + const tp = qty * pricePerQty; setFieldValue('total_price', tp); // unit_price = total_price / total_weight (untuk BE) if (totalWeight > 0) { - setFieldValue('unit_price', roundPrice(tp / totalWeight)); + setFieldValue('unit_price', tp / totalWeight); } } break; @@ -441,22 +435,26 @@ export const calculateTelurQty = ( case 'total_price': { // price_per_qty = total_price / qty if (totalPrice > 0 && qty > 0) { - setFieldValue('price_per_qty', roundPrice(totalPrice / qty)); + setFieldValue('price_per_qty', totalPrice / qty); // unit_price = total_price / total_weight (untuk BE) if (totalWeight > 0) { - setFieldValue('unit_price', roundPrice(totalPrice / totalWeight)); + setFieldValue('unit_price', totalPrice / totalWeight); } } break; } case 'unit_price': { // total_price = total_weight × unit_price + + const newTotalPrice = totalWeight * unitPrice; + if (unitPrice > 0 && totalWeight > 0) { - setFieldValue('total_price', roundPrice(totalWeight * unitPrice)); + setFieldValue('total_price', newTotalPrice); } + // price_per_qty = total_price / qty - if (totalPrice > 0 && qty > 0) { - setFieldValue('price_per_qty', roundPrice(totalPrice / qty)); + if (newTotalPrice > 0 && qty > 0) { + setFieldValue('price_per_qty', newTotalPrice / qty); } break; } From 444c475cb42086b3ef7274330c9c18310f0cad1b Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 17:22:46 +0700 Subject: [PATCH 5/7] fix: remove formattedUnitPrice --- src/components/pages/marketing/SalesOrderFormModal.tsx | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/components/pages/marketing/SalesOrderFormModal.tsx b/src/components/pages/marketing/SalesOrderFormModal.tsx index e06ecbf3..17a59a92 100644 --- a/src/components/pages/marketing/SalesOrderFormModal.tsx +++ b/src/components/pages/marketing/SalesOrderFormModal.tsx @@ -207,15 +207,10 @@ const SalesOrderFormModal = ({ marketingTypeValue = product.week ? 'AYAM_PULLET' : 'AYAM'; } - const formattedUnitPrice = - convertionUnitValue === 'QTY' - ? parseFloat(String(product.price_per_qty || 0)) - : parseFloat(String(product.unit_price || 0)); - return { vehicle_number: product.vehicle_number as string, product_warehouse_id: product.product_warehouse_id as number, - unit_price: formattedUnitPrice, + unit_price: parseFloat(String(product.unit_price || 0)), total_weight: parseFloat(String(product.total_weight || 0)), qty: parseFloat(String(product.qty || 0)), avg_weight: parseFloat(String(product.avg_weight || 0)), From 05fbae680faa797f6d6e8e3698c76eed651649ae Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 17:23:23 +0700 Subject: [PATCH 6/7] fix: reorder input for price_per_qty and unit_price --- .../sales-order/SalesOrderProductForm.tsx | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx index 49d8a9f1..a17dbda1 100644 --- a/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx +++ b/src/components/pages/marketing/form/repeater/sales-order/SalesOrderProductForm.tsx @@ -74,6 +74,24 @@ const SalesOrderProductForm = ({ ? Number(initialValues?.unit_price) : 0; + const isInitialTelurQty = + initialValues?.marketing_type?.value?.toLowerCase() === 'telur' && + initialValues?.convertion_unit?.value?.toLowerCase() === 'qty'; + + const initialUnitPrice = + isInitialTelurQty && + Number(initialValues?.total_price || 0) > 0 && + Number(initialValues?.qty || 0) > 0 + ? Number(initialValues?.total_price) / Number(initialValues?.qty) + : initialValues?.unit_price || ''; + + const initialPricePerQty = + isInitialTelurQty && + Number(initialValues?.total_price || 0) > 0 && + Number(initialValues?.total_weight || 0) > 0 + ? Number(initialValues?.total_price) / Number(initialValues?.total_weight) + : (initialValues?.price_per_qty ?? null); + const initialPriceSisaBerat = initialValues?.total_price && initialValues?.total_peti ? Number(initialValues.total_price) - @@ -97,7 +115,7 @@ const SalesOrderProductForm = ({ product_warehouse: initialValues?.product_warehouse || null, product_warehouse_data: initialValues?.product_warehouse_data || null, product_warehouse_id: initialValues?.product_warehouse_id || undefined, - unit_price: initialValues?.unit_price || '', + unit_price: initialUnitPrice, total_weight: initialValues?.total_weight || '', qty: initialValues?.qty || '', avg_weight: initialValues?.avg_weight || '', @@ -111,7 +129,7 @@ const SalesOrderProductForm = ({ convertion_unit: initialValues?.convertion_unit || null, marketing_type: initialValues?.marketing_type || null, total_peti: initialValues?.total_peti ?? null, - price_per_qty: initialValues?.price_per_qty ?? null, + price_per_qty: initialPricePerQty, sisa_berat: initialSisaBerat, price_sisa_berat: initialPriceSisaBerat, week: initialValues?.week ?? null, @@ -728,35 +746,12 @@ const SalesOrderProductForm = ({ /> )} - {/* Harga per butir untuk TELUR + QTY */} - {formik.values.marketing_type?.value.toLowerCase() === 'telur' && - formik.values.convertion_unit?.value.toLowerCase() === 'qty' && ( - { - const value = Number(e.target.value); - handleFieldChange('price_per_qty', value, () => - setCurrentInput('price_per_qty') - ); - }} - isError={ - formik.touched.price_per_qty && - Boolean(formik.errors.price_per_qty) - } - errorMessage={formik.errors.price_per_qty} - placeholder='Masukan Harga per Butir' - /> - )} - {/* Harga Satuan per Uom Produk Warehouse */} {formik.values.convertion_unit?.value.toLowerCase() !== 'peti' && formik.values.convertion_unit?.value.toLowerCase() !== 'kg' && ( { @@ -773,6 +768,29 @@ const SalesOrderProductForm = ({ /> )} + {/* Harga per kg untuk TELUR + QTY */} + {formik.values.marketing_type?.value.toLowerCase() === 'telur' && + formik.values.convertion_unit?.value.toLowerCase() === 'qty' && ( + { + const value = Number(e.target.value); + handleFieldChange('price_per_qty', value, () => + setCurrentInput('price_per_qty') + ); + }} + isError={ + formik.touched.price_per_qty && + Boolean(formik.errors.price_per_qty) + } + errorMessage={formik.errors.price_per_qty} + placeholder='Masukan Harga per Kg' + /> + )} + {/* Sisa kg diluar peti */} {formik.values.convertion_unit?.value.toLowerCase() === 'peti' && (
From 9f0fbcf0417436476752d08d570d3ee9d0636ad8 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 17:24:19 +0700 Subject: [PATCH 7/7] fix: make price_per_qty calculation to price per kg and make unit_price calculation to price per egg (if category is TELUR and convertion unit QTY) --- src/lib/marketing-calculation.ts | 55 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/lib/marketing-calculation.ts b/src/lib/marketing-calculation.ts index 95db7eb0..3d4930c5 100644 --- a/src/lib/marketing-calculation.ts +++ b/src/lib/marketing-calculation.ts @@ -370,13 +370,11 @@ export const calculateTelurKg = ( /** * TELUR + QTY Workaround: - * - User inputs: qty, avg_weight, price_per_qty (harga per butir) + * - User inputs: qty, avg_weight, unit_price (harga per butir) * - FE calculates: * - total_weight = avg_weight × qty - * - total_price = qty × price_per_qty - * - unit_price = total_price / total_weight (normalisasi untuk BE) - * - Kirim convertion_unit: "KG" karena BE tidak support "QTY" - * - BE akan hitung: total_price = total_weight × unit_price (hasil sama) + * - total_price = qty × unit_price + * - price_per_qty = total_price / total_weight (harga per kg) */ export const calculateTelurQty = ( field: string, @@ -397,13 +395,13 @@ export const calculateTelurQty = ( if (avgWeight > 0 && qty > 0) { const tw = roundWeight(avgWeight * qty); setFieldValue('total_weight', tw); - // total_price = qty × price_per_qty - if (pricePerQty > 0) { - const tp = qty * pricePerQty; + // total_price = qty × unit_price + if (unitPrice > 0) { + const tp = qty * unitPrice; setFieldValue('total_price', tp); - // unit_price = total_price / total_weight (untuk BE) + // price_per_qty = total_price / total_weight if (tw > 0) { - setFieldValue('unit_price', tp / tw); + setFieldValue('price_per_qty', tp / tw); } } } @@ -413,48 +411,47 @@ export const calculateTelurQty = ( // avg_weight = total_weight / qty if (totalWeight > 0 && qty > 0) { setFieldValue('avg_weight', preciseWeight(totalWeight / qty)); - // Recalculate total_price jika ada unit_price + // Recalculate total_price jika ada harga per butir if (unitPrice > 0) { - setFieldValue('total_price', totalWeight * unitPrice); + setFieldValue('total_price', qty * unitPrice); } } break; } case 'price_per_qty': { - // total_price = qty × price_per_qty - if (pricePerQty > 0 && qty > 0) { - const tp = qty * pricePerQty; + // total_price = total_weight × price_per_qty + if (pricePerQty > 0 && totalWeight > 0) { + const tp = totalWeight * pricePerQty; setFieldValue('total_price', tp); - // unit_price = total_price / total_weight (untuk BE) - if (totalWeight > 0) { - setFieldValue('unit_price', tp / totalWeight); + // unit_price = total_price / qty + if (qty > 0) { + setFieldValue('unit_price', tp / qty); } } break; } case 'total_price': { - // price_per_qty = total_price / qty + // unit_price = total_price / qty if (totalPrice > 0 && qty > 0) { - setFieldValue('price_per_qty', totalPrice / qty); - // unit_price = total_price / total_weight (untuk BE) + setFieldValue('unit_price', totalPrice / qty); + // price_per_qty = total_price / total_weight if (totalWeight > 0) { - setFieldValue('unit_price', totalPrice / totalWeight); + setFieldValue('price_per_qty', totalPrice / totalWeight); } } break; } case 'unit_price': { - // total_price = total_weight × unit_price + // total_price = qty × unit_price + const newTotalPrice = qty * unitPrice; - const newTotalPrice = totalWeight * unitPrice; - - if (unitPrice > 0 && totalWeight > 0) { + if (unitPrice > 0 && qty > 0) { setFieldValue('total_price', newTotalPrice); } - // price_per_qty = total_price / qty - if (newTotalPrice > 0 && qty > 0) { - setFieldValue('price_per_qty', newTotalPrice / qty); + // price_per_qty = total_price / total_weight + if (newTotalPrice > 0 && totalWeight > 0) { + setFieldValue('price_per_qty', newTotalPrice / totalWeight); } break; }