38 lines
1.7 KiB
JavaScript
38 lines
1.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ExpenseEngine = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
class ExpenseEngine {
|
|
calculateSplit(totalCost, adultCount, childCount, childDiscountPercent) {
|
|
if (totalCost < 0 || adultCount < 0 || childCount < 0 || childDiscountPercent < 0) {
|
|
throw new common_1.BadRequestException('Dữ liệu đầu vào không được là số âm.');
|
|
}
|
|
if (childDiscountPercent > 100) {
|
|
throw new common_1.BadRequestException('Phần trăm giảm giá không thể vượt quá 100%.');
|
|
}
|
|
if (adultCount + childCount === 0) {
|
|
throw new common_1.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,
|
|
};
|
|
}
|
|
}
|
|
exports.ExpenseEngine = ExpenseEngine;
|
|
//# sourceMappingURL=expense-engine.service.js.map
|