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)

This commit is contained in:
ValdiANS
2026-04-07 17:24:19 +07:00
parent 05fbae680f
commit 9f0fbcf041
+26 -29
View File
@@ -370,13 +370,11 @@ export const calculateTelurKg = (
/** /**
* TELUR + QTY Workaround: * 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: * - FE calculates:
* - total_weight = avg_weight × qty * - total_weight = avg_weight × qty
* - total_price = qty × price_per_qty * - total_price = qty × unit_price
* - unit_price = total_price / total_weight (normalisasi untuk BE) * - price_per_qty = total_price / total_weight (harga per kg)
* - Kirim convertion_unit: "KG" karena BE tidak support "QTY"
* - BE akan hitung: total_price = total_weight × unit_price (hasil sama)
*/ */
export const calculateTelurQty = ( export const calculateTelurQty = (
field: string, field: string,
@@ -397,13 +395,13 @@ export const calculateTelurQty = (
if (avgWeight > 0 && qty > 0) { if (avgWeight > 0 && qty > 0) {
const tw = roundWeight(avgWeight * qty); const tw = roundWeight(avgWeight * qty);
setFieldValue('total_weight', tw); setFieldValue('total_weight', tw);
// total_price = qty × price_per_qty // total_price = qty × unit_price
if (pricePerQty > 0) { if (unitPrice > 0) {
const tp = qty * pricePerQty; const tp = qty * unitPrice;
setFieldValue('total_price', tp); setFieldValue('total_price', tp);
// unit_price = total_price / total_weight (untuk BE) // price_per_qty = total_price / total_weight
if (tw > 0) { 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 // avg_weight = total_weight / qty
if (totalWeight > 0 && qty > 0) { if (totalWeight > 0 && qty > 0) {
setFieldValue('avg_weight', preciseWeight(totalWeight / qty)); setFieldValue('avg_weight', preciseWeight(totalWeight / qty));
// Recalculate total_price jika ada unit_price // Recalculate total_price jika ada harga per butir
if (unitPrice > 0) { if (unitPrice > 0) {
setFieldValue('total_price', totalWeight * unitPrice); setFieldValue('total_price', qty * unitPrice);
} }
} }
break; break;
} }
case 'price_per_qty': { case 'price_per_qty': {
// total_price = qty × price_per_qty // total_price = total_weight × price_per_qty
if (pricePerQty > 0 && qty > 0) { if (pricePerQty > 0 && totalWeight > 0) {
const tp = qty * pricePerQty; const tp = totalWeight * pricePerQty;
setFieldValue('total_price', tp); setFieldValue('total_price', tp);
// unit_price = total_price / total_weight (untuk BE) // unit_price = total_price / qty
if (totalWeight > 0) { if (qty > 0) {
setFieldValue('unit_price', tp / totalWeight); setFieldValue('unit_price', tp / qty);
} }
} }
break; break;
} }
case 'total_price': { case 'total_price': {
// price_per_qty = total_price / qty // unit_price = total_price / qty
if (totalPrice > 0 && qty > 0) { if (totalPrice > 0 && qty > 0) {
setFieldValue('price_per_qty', totalPrice / qty); setFieldValue('unit_price', totalPrice / qty);
// unit_price = total_price / total_weight (untuk BE) // price_per_qty = total_price / total_weight
if (totalWeight > 0) { if (totalWeight > 0) {
setFieldValue('unit_price', totalPrice / totalWeight); setFieldValue('price_per_qty', totalPrice / totalWeight);
} }
} }
break; break;
} }
case 'unit_price': { 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 && qty > 0) {
if (unitPrice > 0 && totalWeight > 0) {
setFieldValue('total_price', newTotalPrice); setFieldValue('total_price', newTotalPrice);
} }
// price_per_qty = total_price / qty // price_per_qty = total_price / total_weight
if (newTotalPrice > 0 && qty > 0) { if (newTotalPrice > 0 && totalWeight > 0) {
setFieldValue('price_per_qty', newTotalPrice / qty); setFieldValue('price_per_qty', newTotalPrice / totalWeight);
} }
break; break;
} }