refactor: BonusCalculator runs all 3 variants and picks winner

This commit is contained in:
Kenearos 2026-05-12 00:15:38 +02:00
parent f7153a5e53
commit bc1d92937c
2 changed files with 170 additions and 142 deletions

View file

@ -1,41 +1,26 @@
/** /**
* Duty Schedule Bonus Calculator * Bonus Calculator (NRW Psychiatrie 2011)
* Calculates bonuses based on weekend and holiday duty shifts * Orchestrator: classifies duties, runs all three variants (V1/V2/V3), picks the winner.
* Pure variant logic lives in variants.js.
*/ */
class BonusCalculator { class BonusCalculator {
constructor(holidayProvider) { constructor(holidayProvider) {
this.holidayProvider = holidayProvider; this.holidayProvider = holidayProvider;
this.RATE_NORMAL = 250; // Normal day rate (not weekend/holiday) this.RATE_NORMAL = 250;
this.RATE_WEEKEND = 450; // Weekend/holiday rate this.RATE_WEEKEND = 450;
this.MIN_QUALIFYING_DAYS = 2.0; // Minimum qualifying days to trigger bonus
this.DEDUCTION_AMOUNT = 2.0; // Deduction after reaching threshold
} }
/** /**
* Check if a date is a qualifying day (weekend or holiday related) * Whether the given date is a "qualifying" day (used by UI for badge coloring).
* Qualifying days: Friday, Saturday, Sunday, Public Holiday, Day before public holiday * Mirrors the old isQualifyingDay so app.js does not break.
* @param {Date} date
* @returns {boolean}
*/ */
isQualifyingDay(date) { isQualifyingDay(date) {
const dayOfWeek = date.getDay(); // 0 = Sunday, 5 = Friday, 6 = Saturday const slot = classify(date, this.holidayProvider);
return slot !== 'weekday';
// Weekend: Friday (5), Saturday (6), Sunday (0)
const isWeekend = dayOfWeek === 5 || dayOfWeek === 6 || dayOfWeek === 0;
// Public holiday
const isHoliday = this.holidayProvider.isHoliday(date);
// Day before public holiday
const isDayBeforeHoliday = this.holidayProvider.isDayBeforeHoliday(date);
return isWeekend || isHoliday || isDayBeforeHoliday;
} }
/** /**
* Get day type label for display * Human-readable label for the date's day type (used by UI).
* @param {Date} date
* @returns {string}
*/ */
getDayTypeLabel(date) { getDayTypeLabel(date) {
const dayOfWeek = date.getDay(); const dayOfWeek = date.getDay();
@ -43,12 +28,8 @@ class BonusCalculator {
const holidayName = this.holidayProvider.getHolidayName(date); const holidayName = this.holidayProvider.getHolidayName(date);
const isDayBefore = this.holidayProvider.isDayBeforeHoliday(date); const isDayBefore = this.holidayProvider.isDayBeforeHoliday(date);
if (isHoliday) { if (isHoliday) return `Feiertag (${holidayName})`;
return `Feiertag (${holidayName})`; if (isDayBefore) return 'Tag vor Feiertag';
}
if (isDayBefore) {
return 'Tag vor Feiertag';
}
if (dayOfWeek === 5) return 'Freitag'; if (dayOfWeek === 5) return 'Freitag';
if (dayOfWeek === 6) return 'Samstag'; if (dayOfWeek === 6) return 'Samstag';
if (dayOfWeek === 0) return 'Sonntag'; if (dayOfWeek === 0) return 'Sonntag';
@ -58,143 +39,90 @@ class BonusCalculator {
} }
/** /**
* Calculate bonus for a single employee for a given month * Build the dutyDetails array (date, share, isQualifying, dayType) for the UI.
* @param {Array} duties - Array of duty objects: {date: Date, share: number (1.0 or 0.5)}
* @returns {Object} Calculation result
*/ */
calculateMonthlyBonus(duties) { buildDutyDetails(duties) {
return duties.map(duty => ({
date: duty.date,
share: duty.share,
isQualifying: this.isQualifyingDay(duty.date),
dayType: this.getDayTypeLabel(duty.date)
}));
}
/**
* Calculate the bonus for a single employee for a given month.
* @param {Array} duties - Array of { date: Date, share: number }
* @param {boolean} isVacation - Vacation toggle (halves thresholds + deductions)
* @returns {Object} new-shape result (winner, allResults, totalBonus, classified, isVacation, dutyDetails)
*/
calculateMonthlyBonus(duties, isVacation = false) {
if (!duties || duties.length === 0) { if (!duties || duties.length === 0) {
return this.getEmptyResult(); return this.getEmptyResult(isVacation);
} }
// Separate qualifying days (Friday separate from others) and non-qualifying days const classified = classifyDuties(duties, this.holidayProvider);
let qualifyingDaysFriday = 0; const v1 = variant1(classified, isVacation);
let qualifyingDaysOther = 0; const v2 = variant2(classified, isVacation);
let normalDays = 0; const v3 = variant3(classified, isVacation);
const dutyDetails = []; const results = [v1, v2, v3];
duties.forEach(duty => { // Pick winner: highest bonus, tie-breaker = lowest variantId (strict >)
const isQualifying = this.isQualifyingDay(duty.date); let winner = results[0];
const dayType = this.getDayTypeLabel(duty.date); for (let i = 1; i < results.length; i++) {
const isFriday = duty.date.getDay() === 5; if (results[i].bonus > winner.bonus) {
winner = results[i];
if (isQualifying) {
if (isFriday) {
qualifyingDaysFriday += duty.share;
} else {
qualifyingDaysOther += duty.share;
}
} else {
normalDays += duty.share;
} }
dutyDetails.push({
date: duty.date,
share: duty.share,
isQualifying: isQualifying,
dayType: dayType
});
});
const qualifyingDaysTotal = qualifyingDaysFriday + qualifyingDaysOther;
// Check if threshold is reached
const thresholdReached = qualifyingDaysTotal >= this.MIN_QUALIFYING_DAYS;
let bonus = 0;
let normalDaysPaid = 0;
let qualifyingDaysPaid = 0;
let deductionFromFriday = 0;
let deductionFromOther = 0;
let totalDeduction = 0;
if (thresholdReached) {
// Deduct qualifying days with Friday priority
totalDeduction = this.DEDUCTION_AMOUNT;
// First deduct from Friday
deductionFromFriday = Math.min(totalDeduction, qualifyingDaysFriday);
// Remaining deduction from other qualifying days
deductionFromOther = Math.max(0, totalDeduction - deductionFromFriday);
// Calculate paid days
const qualifyingDaysFridayPaid = Math.max(0, qualifyingDaysFriday - deductionFromFriday);
const qualifyingDaysOtherPaid = Math.max(0, qualifyingDaysOther - deductionFromOther);
qualifyingDaysPaid = qualifyingDaysFridayPaid + qualifyingDaysOtherPaid;
normalDaysPaid = normalDays;
// Calculate bonus
bonus = (normalDaysPaid * this.RATE_NORMAL) + (qualifyingDaysPaid * this.RATE_WEEKEND);
} }
// If threshold not reached: no bonus paid (neither WT nor WE) winner.isWinner = true;
return { return {
classified,
isVacation,
winner,
allResults: results,
totalBonus: winner.bonus,
totalDuties: duties.length, totalDuties: duties.length,
totalDaysWorked: qualifyingDaysTotal + normalDays, dutyDetails: this.buildDutyDetails(duties)
normalDays: normalDays,
qualifyingDaysFriday: qualifyingDaysFriday,
qualifyingDaysOther: qualifyingDaysOther,
qualifyingDays: qualifyingDaysTotal,
thresholdReached: thresholdReached,
deductionFromFriday: deductionFromFriday,
deductionFromOther: deductionFromOther,
qualifyingDaysDeducted: totalDeduction,
normalDaysPaid: normalDaysPaid,
qualifyingDaysPaid: qualifyingDaysPaid,
bonusNormalDays: normalDaysPaid * this.RATE_NORMAL,
bonusQualifyingDays: qualifyingDaysPaid * this.RATE_WEEKEND,
totalBonus: bonus,
dutyDetails: dutyDetails
}; };
} }
/** /**
* Calculate bonuses for all employees * Calculate for all employees. vacationMap: { [employeeName]: boolean }
* @param {Object} employeeDuties - Object with employee names as keys and duty arrays as values
* @returns {Object} Results for all employees
*/ */
calculateAllEmployees(employeeDuties) { calculateAllEmployees(employeeDuties, vacationMap = {}) {
const results = {}; const results = {};
for (const [name, duties] of Object.entries(employeeDuties)) {
for (const [employeeName, duties] of Object.entries(employeeDuties)) { const isVac = Boolean(vacationMap[name]);
results[employeeName] = this.calculateMonthlyBonus(duties); results[name] = this.calculateMonthlyBonus(duties, isVac);
} }
return results; return results;
} }
/** getEmptyResult(isVacation = false) {
* Get empty result structure const empty = {
* @returns {Object} variantId: 1,
*/ eligible: false,
getEmptyResult() { threshold: null,
deduction: { fr: 0, sa: 0, so: 0, weekday: 0 },
paidShares: { fr: 0, sa: 0, so: 0, weekday: 0 },
bonus: 0,
isWinner: true
};
return { return {
totalDuties: 0, classified: { fr: 0, sa: 0, so: 0, weekday: 0 },
totalDaysWorked: 0, isVacation,
normalDays: 0, winner: empty,
qualifyingDaysFriday: 0, allResults: [empty,
qualifyingDaysOther: 0, { ...empty, variantId: 2, isWinner: false },
qualifyingDays: 0, { ...empty, variantId: 3, isWinner: false }
thresholdReached: false, ],
deductionFromFriday: 0,
deductionFromOther: 0,
qualifyingDaysDeducted: 0,
normalDaysPaid: 0,
qualifyingDaysPaid: 0,
bonusNormalDays: 0,
bonusQualifyingDays: 0,
totalBonus: 0, totalBonus: 0,
totalDuties: 0,
dutyDetails: [] dutyDetails: []
}; };
} }
/**
* Format currency for display
* @param {number} amount
* @returns {string}
*/
formatCurrency(amount) { formatCurrency(amount) {
return new Intl.NumberFormat('de-DE', { return new Intl.NumberFormat('de-DE', {
style: 'currency', style: 'currency',

View file

@ -793,6 +793,106 @@ runner.test('variant2: threshold-Shape normal {sa:1, weekday:2}', (t) => {
t.assertEqual(r.threshold.weekday, 2, 'threshold.weekday=2'); t.assertEqual(r.threshold.weekday, 2, 'threshold.weekday=2');
}); });
// ============================================================================
// BonusCalculator - Winner Selection (new shape)
// ============================================================================
runner.test('Winner: klarer Sieger mit weekdays + 1 Fr', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
const duties = [
{ date: new Date('2025-11-21T12:00:00'), share: 1.0 }, // Fr
{ date: new Date('2025-11-24T12:00:00'), share: 1.0 }, // Mo
{ date: new Date('2025-11-25T12:00:00'), share: 1.0 }, // Di
{ date: new Date('2025-11-26T12:00:00'), share: 1.0 }, // Mi
{ date: new Date('2025-11-27T12:00:00'), share: 1.0 }, // Do
{ date: new Date('2025-11-04T12:00:00'), share: 1.0 } // Di
];
const result = calc.calculateMonthlyBonus(duties, false);
t.assertTrue(result.winner.isWinner, 'winner.isWinner=true');
t.assertEqual(result.allResults.length, 3, '3 Varianten im allResults');
t.assertTrue(result.totalBonus > 0, 'Bonus > 0');
});
runner.test('Winner: klarer V3-Sieger (nur WE-Dienste)', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
const duties = [
{ date: new Date('2025-11-22T12:00:00'), share: 1.0 }, // Sa
{ date: new Date('2025-11-23T12:00:00'), share: 1.0 }, // So
{ date: new Date('2025-11-29T12:00:00'), share: 1.0 } // Sa
];
const result = calc.calculateMonthlyBonus(duties, false);
// V1: fr+so=1, weekday=0 -> not eligible
// V2: sa=2, weekday=0 -> not eligible
// V3: pool=3 -> eligible, deduction 2 (fr=0,so=1 abgezogen, sa=1 abgezogen) -> 1 sa paid -> 450
t.assertEqual(result.winner.variantId, 3, 'V3 muss Sieger sein');
t.assertEqual(result.totalBonus, 450, 'bonus=450');
});
runner.test('Winner: Tie-Breaker - alle three not eligible -> V1 nominal winner, totalBonus 0', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
// fr=1, sa=0, so=0, weekday=3:
// V1: fr+so=1 ok, weekday=3 ok -> eligible. Abzug fr=1, weekday=3 -> alles weg, bonus 0.
// V2: sa=0 -> not eligible (0).
// V3: pool=1 < 2 -> not eligible (0).
// -> tie at 0; V1 has eligible=true so its result is still 0. Strict > keeps v1 as winner.
const duties = [
{ date: new Date('2025-11-21T12:00:00'), share: 1.0 }, // Fr
{ date: new Date('2025-11-24T12:00:00'), share: 1.0 }, // Mo
{ date: new Date('2025-11-25T12:00:00'), share: 1.0 }, // Di
{ date: new Date('2025-11-26T12:00:00'), share: 1.0 } // Mi
];
const result = calc.calculateMonthlyBonus(duties, false);
t.assertEqual(result.winner.variantId, 1, 'V1 wins tie (lowest variantId)');
t.assertEqual(result.totalBonus, 0, 'totalBonus=0 (all-zero tie)');
});
runner.test('Winner: nur V3 produziert positive bonus -> V3 winner', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
// Three Saturdays: V1 not eligible, V2 not eligible (weekday=0), V3 eligible with positive bonus.
const duties = [
{ date: new Date('2025-11-22T12:00:00'), share: 1.0 },
{ date: new Date('2025-11-29T12:00:00'), share: 1.0 },
{ date: new Date('2025-11-15T12:00:00'), share: 1.0 }
];
const result = calc.calculateMonthlyBonus(duties, false);
// V3: pool=3, abzug 2 (so=0, fr=0, sa=2 abgezogen) -> 1 sa paid -> 450
t.assertEqual(result.winner.variantId, 3, 'V3 winner');
t.assertEqual(result.totalBonus, 450, 'bonus=450');
});
runner.test('Winner: result-Shape enthaelt classified, isVacation, dutyDetails', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
const duties = [
{ date: new Date('2025-11-22T12:00:00'), share: 1.0 },
{ date: new Date('2025-11-23T12:00:00'), share: 1.0 }
];
const result = calc.calculateMonthlyBonus(duties, false);
t.assertTrue('classified' in result, 'classified field exists');
t.assertTrue('isVacation' in result, 'isVacation field exists');
t.assertTrue('dutyDetails' in result, 'dutyDetails field exists');
t.assertEqual(result.dutyDetails.length, 2, 'dutyDetails has 2 entries');
t.assertEqual(result.isVacation, false, 'isVacation=false');
});
runner.test('Winner: Urlaubsmodus halbiert alle Schwellen', (t) => {
const hp = new HolidayProvider();
const calc = new BonusCalculator(hp);
// fr=0.5, weekday=1.5 -> V1 eligible im Urlaub (0.5 >= 0.5, 1.5 >= 1.5)
const duties = [
{ date: new Date('2025-11-21T12:00:00'), share: 0.5 }, // Fr
{ date: new Date('2025-11-24T12:00:00'), share: 1.0 }, // Mo
{ date: new Date('2025-11-25T12:00:00'), share: 0.5 } // Di
];
const result = calc.calculateMonthlyBonus(duties, true);
t.assertEqual(result.isVacation, true, 'isVacation propagated');
t.assertEqual(result.winner.variantId, 1, 'V1 wins under vacation');
});
// ============================================================================ // ============================================================================
// Display Functions // Display Functions
// ============================================================================ // ============================================================================