mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE): calculation penjualan telur + peti
This commit is contained in:
@@ -199,9 +199,12 @@ export const calculateAyam = (field: string, ctx: CalculationContext): void => {
|
||||
|
||||
/**
|
||||
* TELUR + PETI: Penjualan telur dalam satuan peti
|
||||
* - Formula: total_price = (price_per_convertion × total_peti) + price_sisa_berat
|
||||
*
|
||||
* Formulas:
|
||||
* - total_weight = (weight_per_convertion × total_peti) + sisa_berat
|
||||
* - Payload: unit_price = total_price / qty (normalisasi untuk BE)
|
||||
* - total_price = (price_per_convertion × total_peti) + price_sisa_berat
|
||||
* - unit_price = total_price / total_weight (untuk BE)
|
||||
* - avg_weight = total_weight / qty
|
||||
*/
|
||||
export const calculateTelurPeti = (
|
||||
field: string,
|
||||
@@ -217,27 +220,83 @@ export const calculateTelurPeti = (
|
||||
: 0;
|
||||
const qty = Number(values.qty || 0);
|
||||
|
||||
// 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));
|
||||
}
|
||||
};
|
||||
|
||||
switch (field) {
|
||||
case 'price_per_convertion':
|
||||
case 'total_peti':
|
||||
case 'price_sisa_berat': {
|
||||
// Recalculate total_price
|
||||
const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat;
|
||||
setFieldValue('total_price', roundPrice(totalPrice));
|
||||
// Recalculate unit_price (normalized for BE)
|
||||
if (qty > 0) {
|
||||
setFieldValue('unit_price', roundPrice(totalPrice / qty));
|
||||
case 'price_per_convertion': {
|
||||
// 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
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
updateUnitPrice(totalPrice, totalWeight);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'total_peti': {
|
||||
// Recalculate total_weight = (weight_per_convertion × total_peti) + sisa_berat
|
||||
let totalWeight = 0;
|
||||
if (weightPerConvertion > 0 && totalPeti > 0) {
|
||||
totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
setFieldValue('total_weight', roundWeight(totalWeight));
|
||||
// Recalculate avg_weight = total_weight / qty
|
||||
if (qty > 0) {
|
||||
setFieldValue('avg_weight', preciseWeight(totalWeight / qty));
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'price_sisa_berat': {
|
||||
// Recalculate total_price
|
||||
if (pricePerConvertion > 0 && totalPeti > 0) {
|
||||
const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat;
|
||||
setFieldValue('total_price', roundPrice(totalPrice));
|
||||
// Recalculate unit_price = total_price / total_weight
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
updateUnitPrice(totalPrice, totalWeight);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'weight_per_convertion': {
|
||||
// Recalculate total_weight = (weight_per_convertion × total_peti) + sisa_berat
|
||||
if (weightPerConvertion > 0 && totalPeti > 0) {
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
setFieldValue('total_weight', roundWeight(totalWeight));
|
||||
// Recalculate avg_weight = total_weight / qty
|
||||
if (qty > 0) {
|
||||
setFieldValue('avg_weight', preciseWeight(totalWeight / qty));
|
||||
}
|
||||
// Recalculate unit_price = total_price / total_weight
|
||||
const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat;
|
||||
updateUnitPrice(totalPrice, totalWeight);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'weight_per_convertion':
|
||||
case 'sisa_berat': {
|
||||
// Recalculate total_weight
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
setFieldValue('total_weight', roundWeight(totalWeight));
|
||||
// Recalculate avg_weight
|
||||
if (qty > 0) {
|
||||
setFieldValue('avg_weight', preciseWeight(totalWeight / qty));
|
||||
if (weightPerConvertion > 0 && totalPeti > 0) {
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
setFieldValue('total_weight', roundWeight(totalWeight));
|
||||
// Recalculate avg_weight = total_weight / qty
|
||||
if (qty > 0) {
|
||||
setFieldValue('avg_weight', preciseWeight(totalWeight / qty));
|
||||
}
|
||||
// Recalculate unit_price = total_price / total_weight
|
||||
const totalPrice = pricePerConvertion * totalPeti + priceSisaBerat;
|
||||
updateUnitPrice(totalPrice, totalWeight);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -250,10 +309,9 @@ export const calculateTelurPeti = (
|
||||
roundPrice((totalPrice - priceSisaBerat) / totalPeti)
|
||||
);
|
||||
}
|
||||
// Update unit_price (normalized for BE)
|
||||
if (qty > 0) {
|
||||
setFieldValue('unit_price', roundPrice(totalPrice / qty));
|
||||
}
|
||||
// Update unit_price = total_price / total_weight
|
||||
const totalWeight = weightPerConvertion * totalPeti + sisaBerat;
|
||||
updateUnitPrice(totalPrice, totalWeight);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user