From 9f0fbcf0417436476752d08d570d3ee9d0636ad8 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Tue, 7 Apr 2026 17:24:19 +0700 Subject: [PATCH] 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; }