fix: remove roundPrice, update unit price calculation in calculateTelurPeti

This commit is contained in:
ValdiANS
2026-04-07 16:59:35 +07:00
parent 429ff58bfd
commit ef1ce2c78c
+37 -39
View File
@@ -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;
}