{
+ 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' && (
diff --git a/src/lib/marketing-calculation.ts b/src/lib/marketing-calculation.ts
index 5ad5a1e6..3d4930c5 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;
}
@@ -376,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,
@@ -403,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 = roundPrice(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', roundPrice(tp / tw));
+ setFieldValue('price_per_qty', tp / tw);
}
}
}
@@ -419,44 +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', roundPrice(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 = roundPrice(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', roundPrice(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', roundPrice(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', roundPrice(totalPrice / totalWeight));
+ setFieldValue('price_per_qty', totalPrice / totalWeight);
}
}
break;
}
case 'unit_price': {
- // total_price = total_weight × unit_price
- if (unitPrice > 0 && totalWeight > 0) {
- setFieldValue('total_price', roundPrice(totalWeight * unitPrice));
+ // total_price = qty × unit_price
+ const newTotalPrice = qty * unitPrice;
+
+ if (unitPrice > 0 && qty > 0) {
+ setFieldValue('total_price', newTotalPrice);
}
- // price_per_qty = total_price / qty
- if (totalPrice > 0 && qty > 0) {
- setFieldValue('price_per_qty', roundPrice(totalPrice / qty));
+
+ // price_per_qty = total_price / total_weight
+ if (newTotalPrice > 0 && totalWeight > 0) {
+ setFieldValue('price_per_qty', newTotalPrice / totalWeight);
}
break;
}