Files
travelplanning/dist/expense-engine.service.js

34 lines
1.5 KiB
JavaScript

import { BadRequestException } from '@nestjs/common';
export class ExpenseEngine {
calculateSplit(totalCost, adultCount, childCount, childDiscountPercent) {
if (totalCost < 0 || adultCount < 0 || childCount < 0 || childDiscountPercent < 0) {
throw new BadRequestException('Dữ liệu đầu vào không được là số âm.');
}
if (childDiscountPercent > 100) {
throw new BadRequestException('Phần trăm giảm giá không thể vượt quá 100%.');
}
if (adultCount + childCount === 0) {
throw new BadRequestException('Số lượng người tham gia phải lớn hơn 0.');
}
const discountRate = childDiscountPercent / 100;
const childRateFactor = 1 - discountRate;
const weightedParticipantCount = adultCount + (childCount * childRateFactor);
const adultPriceRaw = totalCost / weightedParticipantCount;
const childPriceRaw = adultPriceRaw * childRateFactor;
const perAdult = Math.round(adultPriceRaw);
const perChild = Math.round(childPriceRaw);
const adultsTotal = perAdult * adultCount;
const childrenTotal = perChild * childCount;
const actualSum = adultsTotal + childrenTotal;
return {
targetTotal: totalCost,
actualTotal: actualSum,
perAdult,
perChild,
roundingDifference: totalCost - actualSum,
adultsTotal,
childrenTotal,
};
}
}
//# sourceMappingURL=expense-engine.service.js.map