fix(attendance): payroll recap per accountant spec; multi-shift day rows; shared computation
Rekapitulace (accountant notes, confirmed by owner 2026-07): - Odpracováno = all worked hours EXCEPT hours worked on holidays (was floor(worked/8)×8 — a 3h day printed as 0h) - Vč. svátků a přesčasů = ALL worked hours; line2 − line1 = Svátek (was worked + vacation − 8h×worked-holidays — could print less than actually worked, and vacation inflated it) - Přesčas = max(0, worked − fond), month-level, never negative (was the sub-8h remainder + vacation — printed 3h overtime for a 3h month, 8h overtime for pure vacation, and −6h when a holiday was worked) - Svátek = hours actually WORKED on holiday dates (was 8h per UNworked holiday — opposite meaning) - So/Ne, Práce v noci, Dovolená, Fond unchanged (verified correct) Single shared computeMzdaSummary now feeds BOTH the print and the admin screen (they had drifted: capped vs uncapped holiday adjustment). Print day table: one row PER RECORD — days with two shifts (real in prod data) silently dropped all but the last record from the table while the totals counted them all. +7 spec tests (accountant's own examples); print snapshot updated to the new spec values. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -337,7 +337,7 @@ exports[`buildUserSectionHtml > produces a stable per-user section (full output
|
|||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Odpracováno včetně svátků a přesčasů:</span>
|
<span class="summary-label">Odpracováno včetně svátků a přesčasů:</span>
|
||||||
<span class="summary-value">16,00h</span>
|
<span class="summary-value">8,00h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Dovolená:</span>
|
<span class="summary-label">Dovolená:</span>
|
||||||
@@ -345,7 +345,7 @@ exports[`buildUserSectionHtml > produces a stable per-user section (full output
|
|||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Přesčas:</span>
|
<span class="summary-label">Přesčas:</span>
|
||||||
<span class="summary-value">8,00h</span>
|
<span class="summary-value">0,00h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Svátek:</span>
|
<span class="summary-label">Svátek:</span>
|
||||||
|
|||||||
176
src/__tests__/attendance-mzda-summary.test.ts
Normal file
176
src/__tests__/attendance-mzda-summary.test.ts
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { computeMzdaSummary } from "../admin/utils/attendanceHelpers";
|
||||||
|
import { buildUserSectionHtml } from "../admin/hooks/useAttendanceAdmin";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payroll recap spec (accountant notes, confirmed by the owner 2026-07):
|
||||||
|
* - Odpracováno = all worked hours EXCEPT hours worked on holidays
|
||||||
|
* - Vč. svátků a přesčasů = ALL worked hours (so line2 − line1 = Svátek)
|
||||||
|
* - Dovolená = Σ vacation leave_hours (hour-based)
|
||||||
|
* - Přesčas = max(0, worked − fond) — month-level, never negative
|
||||||
|
* - Svátek = hours actually WORKED on public-holiday dates
|
||||||
|
* - So/Ne = worked hours on Sat/Sun
|
||||||
|
* - Fond = Mon–Fri (holidays included) × 8 h
|
||||||
|
* June 2026: 22 workdays, no holidays → fond 176.
|
||||||
|
* July 2026: 23 Mon–Fri days (6.7. Monday holiday counted; 5.7. is Sunday) → fond 184.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const work = (
|
||||||
|
day: string,
|
||||||
|
from: string,
|
||||||
|
to: string,
|
||||||
|
brk?: [string, string],
|
||||||
|
) => ({
|
||||||
|
shift_date: `${day}T02:00:00`,
|
||||||
|
arrival_time: `${day}T${from}:00`,
|
||||||
|
departure_time: `${day}T${to}:00`,
|
||||||
|
break_start: brk ? `${day}T${brk[0]}:00` : null,
|
||||||
|
break_end: brk ? `${day}T${brk[1]}:00` : null,
|
||||||
|
leave_type: "work",
|
||||||
|
});
|
||||||
|
const vacation = (day: string, hours: number) => ({
|
||||||
|
shift_date: `${day}T02:00:00`,
|
||||||
|
leave_type: "vacation",
|
||||||
|
leave_hours: hours,
|
||||||
|
arrival_time: null,
|
||||||
|
departure_time: null,
|
||||||
|
break_start: null,
|
||||||
|
break_end: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const JUNE_WORKDAYS = [
|
||||||
|
"01",
|
||||||
|
"02",
|
||||||
|
"03",
|
||||||
|
"04",
|
||||||
|
"05",
|
||||||
|
"08",
|
||||||
|
"09",
|
||||||
|
"10",
|
||||||
|
"11",
|
||||||
|
"12",
|
||||||
|
"15",
|
||||||
|
"16",
|
||||||
|
"17",
|
||||||
|
"18",
|
||||||
|
"19",
|
||||||
|
"22",
|
||||||
|
"23",
|
||||||
|
"24",
|
||||||
|
"25",
|
||||||
|
"26",
|
||||||
|
"29",
|
||||||
|
"30",
|
||||||
|
].map((d) => `2026-06-${d}`);
|
||||||
|
|
||||||
|
describe("computeMzdaSummary — accountant spec", () => {
|
||||||
|
it("single 3h day: worked 3, no overtime (173h below fond)", () => {
|
||||||
|
const s = computeMzdaSummary(
|
||||||
|
[work("2026-06-02", "09:00", "12:00")],
|
||||||
|
"2026-06",
|
||||||
|
);
|
||||||
|
expect(s.odpracovano).toBe(3);
|
||||||
|
expect(s.vcetneSvatku).toBe(3);
|
||||||
|
expect(s.prescas).toBe(0);
|
||||||
|
expect(s.fund).toBe(176);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accountant's Přesčas example: +3h one day, −2h elsewhere → 1h overtime", () => {
|
||||||
|
const recs = JUNE_WORKDAYS.slice(0, 20).map((d) =>
|
||||||
|
work(d, "08:00", "16:30", ["12:00", "12:30"]),
|
||||||
|
); // 20 × 8h
|
||||||
|
recs.push(work("2026-06-29", "08:00", "14:00")); // 6h (2h short)
|
||||||
|
recs.push(work("2026-06-30", "08:00", "19:30", ["12:00", "12:30"])); // 11h (+3h)
|
||||||
|
const s = computeMzdaSummary(recs, "2026-06");
|
||||||
|
expect(s.vcetneSvatku).toBe(177);
|
||||||
|
expect(s.prescas).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accountant's Svátek example: 10h worked on 6.7. → Svátek 10; excluded from Odpracováno", () => {
|
||||||
|
const julyWorkdays = [
|
||||||
|
"01",
|
||||||
|
"02",
|
||||||
|
"03",
|
||||||
|
"07",
|
||||||
|
"08",
|
||||||
|
"09",
|
||||||
|
"10",
|
||||||
|
"13",
|
||||||
|
"14",
|
||||||
|
"15",
|
||||||
|
"16",
|
||||||
|
"17",
|
||||||
|
"20",
|
||||||
|
"21",
|
||||||
|
"22",
|
||||||
|
"23",
|
||||||
|
"24",
|
||||||
|
"27",
|
||||||
|
"28",
|
||||||
|
"29",
|
||||||
|
"30",
|
||||||
|
"31",
|
||||||
|
].map((d) => `2026-07-${d}`);
|
||||||
|
const recs = julyWorkdays.map((d) =>
|
||||||
|
work(d, "08:00", "16:30", ["12:00", "12:30"]),
|
||||||
|
); // 22 × 8h
|
||||||
|
recs.push(work("2026-07-06", "08:00", "18:30", ["12:00", "12:30"])); // holiday, 10h
|
||||||
|
const s = computeMzdaSummary(recs, "2026-07");
|
||||||
|
expect(s.fund).toBe(184); // 23 Mon–Fri days incl. the Monday holiday
|
||||||
|
expect(s.svatekHours).toBe(10);
|
||||||
|
expect(s.vcetneSvatku).toBe(186); // all worked hours
|
||||||
|
expect(s.odpracovano).toBe(176); // minus the holiday-worked 10h
|
||||||
|
expect(s.prescas).toBe(2); // max(0, 186 − 184) — never negative
|
||||||
|
});
|
||||||
|
|
||||||
|
it("vacation does NOT flow into Přesčas (worked-hours-only rule)", () => {
|
||||||
|
const recs: ReturnType<typeof work | typeof vacation>[] =
|
||||||
|
JUNE_WORKDAYS.slice(0, 21).map((d) =>
|
||||||
|
work(d, "08:00", "16:30", ["12:00", "12:30"]),
|
||||||
|
); // 21 × 8 = 168
|
||||||
|
recs.push(vacation("2026-06-30", 8));
|
||||||
|
const s = computeMzdaSummary(recs, "2026-06");
|
||||||
|
expect(s.vacationHours).toBe(8);
|
||||||
|
expect(s.prescas).toBe(0); // 168 worked < 176 fond — vacation doesn't count
|
||||||
|
});
|
||||||
|
|
||||||
|
it("partial-day vacation next to a work shift on the same day (5h work + 3h vacation)", () => {
|
||||||
|
const recs = [
|
||||||
|
work("2026-06-02", "08:00", "13:00"), // 5h
|
||||||
|
vacation("2026-06-02", 3),
|
||||||
|
];
|
||||||
|
const s = computeMzdaSummary(recs, "2026-06");
|
||||||
|
expect(s.odpracovano).toBe(5);
|
||||||
|
expect(s.vacationHours).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("weekend hours count into So/Ne and into worked totals", () => {
|
||||||
|
const s = computeMzdaSummary(
|
||||||
|
[work("2026-06-06", "08:00", "12:00")], // Saturday 4h
|
||||||
|
"2026-06",
|
||||||
|
);
|
||||||
|
expect(s.weekendHours).toBe(4);
|
||||||
|
expect(s.odpracovano).toBe(4);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("print day table — multiple records on one day", () => {
|
||||||
|
it("renders one row per record (two shifts are both visible)", () => {
|
||||||
|
const records = [
|
||||||
|
work("2026-06-02", "06:00", "10:00"),
|
||||||
|
work("2026-06-02", "14:00", "18:00"),
|
||||||
|
];
|
||||||
|
const totalMins = 8 * 60;
|
||||||
|
const html = buildUserSectionHtml(
|
||||||
|
"1",
|
||||||
|
{ name: "Test", minutes: totalMins, vacation_hours: 0, records } as never,
|
||||||
|
{ month: "2026-06", month_name: "Červen 2026", user_totals: {} } as never,
|
||||||
|
);
|
||||||
|
// Both shifts appear, each with its own arrival time.
|
||||||
|
expect(html).toContain("06:00");
|
||||||
|
expect(html).toContain("14:00");
|
||||||
|
// Two rows dated 2. 6.
|
||||||
|
const rows = html.match(/<td>2\. 6\. 2026<\/td>/g) || [];
|
||||||
|
expect(rows.length).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -18,10 +18,8 @@ import {
|
|||||||
getDaysInMonth,
|
getDaysInMonth,
|
||||||
shiftDateForMonth,
|
shiftDateForMonth,
|
||||||
formatHoursDecimal,
|
formatHoursDecimal,
|
||||||
calculateNightMinutes,
|
|
||||||
normalizeDateStr,
|
normalizeDateStr,
|
||||||
holidaysInMonth,
|
computeMzdaSummary,
|
||||||
calculateFreeHolidayHours,
|
|
||||||
getCzechWeekday,
|
getCzechWeekday,
|
||||||
} from "../utils/attendanceHelpers";
|
} from "../utils/attendanceHelpers";
|
||||||
import type {
|
import type {
|
||||||
@@ -216,116 +214,35 @@ function computeUserTotals(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add fund data per user (matching PHP addFundDataToUserTotals)
|
// Rekapitulace per user — the SAME shared computation the print uses
|
||||||
const [yearStr, monthStr] = month.split("-");
|
// (computeMzdaSummary), so the screen and the printed sheet can never
|
||||||
const yr = parseInt(yearStr, 10);
|
// disagree. Accountant spec 2026-07: Odpracováno = worked minus
|
||||||
const mo = parseInt(monthStr, 10) - 1;
|
// holiday-worked; Vč. svátků = ALL worked; Přesčas = max(0, worked − fond);
|
||||||
|
// Svátek = hours actually worked on holidays.
|
||||||
// Count Mon-Fri business days in month (INCLUDING holidays).
|
|
||||||
// The fund is rawBizDays × 8 — holidays stay in the count so the
|
|
||||||
// user is paid for them via the fund. Svátek (free holiday hours) is
|
|
||||||
// computed separately as "8h per holiday the user did not work".
|
|
||||||
let rawBizDays = 0;
|
|
||||||
const cur = new Date(yr, mo, 1);
|
|
||||||
while (cur.getMonth() === mo) {
|
|
||||||
const dow = cur.getDay();
|
|
||||||
if (dow !== 0 && dow !== 6) rawBizDays++;
|
|
||||||
cur.setDate(cur.getDate() + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const holidayDates = holidaysInMonth(yr, mo + 1);
|
|
||||||
|
|
||||||
for (const uid of Object.keys(totals)) {
|
for (const uid of Object.keys(totals)) {
|
||||||
const t = totals[uid];
|
const t = totals[uid];
|
||||||
const userRecords = recordsByUser.get(Number(uid)) || [];
|
const userRecords = recordsByUser.get(Number(uid)) || [];
|
||||||
|
const s = computeMzdaSummary(userRecords, month);
|
||||||
// Odpracováno: floor(worked / 8) × 8 — round to whole days, drop remainder.
|
|
||||||
const workedHoursRaw = t.minutes / 60;
|
|
||||||
const odpracovano = Math.floor(workedHoursRaw / 8) * 8;
|
|
||||||
|
|
||||||
// So/Ne: sum of worked hours on Sat/Sun (raw, not rounded).
|
|
||||||
const weekendHours = userRecords
|
|
||||||
.filter(
|
|
||||||
(r) =>
|
|
||||||
(r.leave_type || "work") === "work" && isWeekendDate(r.shift_date),
|
|
||||||
)
|
|
||||||
.reduce((sum, r) => sum + calculateWorkMinutesPrint(r) / 60, 0);
|
|
||||||
|
|
||||||
// Práce v noci: minutes in 22:00-06:00 across all work records.
|
|
||||||
const nightMinutes = userRecords
|
|
||||||
.filter((r) => (r.leave_type || "work") === "work")
|
|
||||||
.reduce((sum, r) => sum + calculateNightMinutes(r), 0);
|
|
||||||
|
|
||||||
// Svátek (free): 8h per holiday the user did not work.
|
|
||||||
const freeHolidayHours = calculateFreeHolidayHours(
|
|
||||||
userRecords,
|
|
||||||
holidayDates,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Worked holidays: holidays the user DID work (counted toward
|
|
||||||
// Odpracováno, but their 8h must not flow into Přesčas).
|
|
||||||
const workedDatesSet = new Set(
|
|
||||||
userRecords
|
|
||||||
.filter((r) => (r.leave_type || "work") === "work")
|
|
||||||
.map((r) => normalizeDateStr(r.shift_date))
|
|
||||||
.filter(Boolean),
|
|
||||||
);
|
|
||||||
let workedHolidayCount = 0;
|
|
||||||
let workedHolidayHours = 0;
|
|
||||||
for (const hd of holidayDates) {
|
|
||||||
if (workedDatesSet.has(hd)) workedHolidayCount++;
|
|
||||||
}
|
|
||||||
// Sum the actual hours worked on holiday dates (for the KPI card's
|
|
||||||
// "fond used" total = real_work + vacation + worked_holiday + free_holiday).
|
|
||||||
for (const r of userRecords) {
|
|
||||||
if ((r.leave_type || "work") !== "work") continue;
|
|
||||||
const d = normalizeDateStr(r.shift_date);
|
|
||||||
if (d && holidayDates.includes(d)) {
|
|
||||||
workedHolidayHours += calculateWorkMinutesPrint(r) / 60;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fund = Mon-Fri × 8h. Holidays count as work days (already in rawBizDays).
|
|
||||||
const fund = rawBizDays * 8;
|
|
||||||
|
|
||||||
// Včetně svátků a přesčasů (mzda formula):
|
|
||||||
// odpracovano + vacation + remainder − min(freeHols, workedHols × 8)
|
|
||||||
//
|
|
||||||
// Two-way logic matching the print:
|
|
||||||
// • If the user worked at least one holiday, that worked holiday's
|
|
||||||
// 8h is in Odpracováno and shouldn't also count as Přesčas. We
|
|
||||||
// subtract one 8h per worked holiday (capped at the total free
|
|
||||||
// holiday hours — can't go negative).
|
|
||||||
// • If the user did NOT work any holiday, no adjustment is needed:
|
|
||||||
// all unworked holidays are already paid via the Svátek line and
|
|
||||||
// don't flow into Přesčas, so vcetne_svatku just equals the
|
|
||||||
// work + vacation + remainder.
|
|
||||||
const remainder = workedHoursRaw - odpracovano;
|
|
||||||
const holidayAdj = Math.min(freeHolidayHours, workedHolidayCount * 8);
|
|
||||||
const vcetneSv = odpracovano - holidayAdj + t.vacation_hours + remainder;
|
|
||||||
|
|
||||||
// Přesčas = vcetneSv − odpracovano (derived).
|
|
||||||
const prescas = vcetneSv - odpracovano;
|
|
||||||
|
|
||||||
// Legacy fields (kept so existing UI doesn't break).
|
// Legacy fields (kept so existing UI doesn't break).
|
||||||
const workedHours = Math.round(workedHoursRaw * 10) / 10;
|
const workedHours = Math.round(s.workedHoursRaw * 10) / 10;
|
||||||
const covered = Math.round((workedHours + t.vacation_hours) * 10) / 10;
|
const covered = Math.round((workedHours + t.vacation_hours) * 10) / 10;
|
||||||
|
|
||||||
t.fund = fund;
|
t.fund = s.fund;
|
||||||
t.business_days = rawBizDays;
|
t.business_days = s.businessDays;
|
||||||
t.worked_hours = workedHours;
|
t.worked_hours = workedHours;
|
||||||
t.covered = covered;
|
t.covered = covered;
|
||||||
t.missing = Math.max(0, Math.round((fund - covered) * 10) / 10);
|
t.missing = Math.max(0, Math.round((s.fund - covered) * 10) / 10);
|
||||||
t.overtime = Math.max(0, Math.round((covered - fund) * 10) / 10);
|
t.overtime = Math.max(0, Math.round((covered - s.fund) * 10) / 10);
|
||||||
|
|
||||||
t.worked_hours_raw = workedHoursRaw;
|
t.worked_hours_raw = s.workedHoursRaw;
|
||||||
t.odpracovano = odpracovano;
|
t.odpracovano = s.odpracovano;
|
||||||
t.vcetne_svatku = vcetneSv;
|
t.vcetne_svatku = s.vcetneSvatku;
|
||||||
t.prescas = prescas;
|
t.prescas = s.prescas;
|
||||||
t.svatek = freeHolidayHours;
|
t.svatek = s.svatekHours;
|
||||||
t.weekend_hours = weekendHours;
|
t.weekend_hours = s.weekendHours;
|
||||||
t.night_minutes = nightMinutes;
|
t.night_minutes = s.nightMinutes;
|
||||||
t.worked_holiday_hours = workedHolidayHours;
|
t.worked_holiday_hours = s.svatekHours;
|
||||||
}
|
}
|
||||||
|
|
||||||
return totals;
|
return totals;
|
||||||
@@ -390,10 +307,23 @@ export function buildUserSectionHtml(
|
|||||||
userData: UserTotal,
|
userData: UserTotal,
|
||||||
printData: PrintData,
|
printData: PrintData,
|
||||||
): string {
|
): string {
|
||||||
// Build a date-keyed lookup of the user's records.
|
// Build a date-keyed lookup of the user's records. A day may hold MULTIPLE
|
||||||
const recordsByDate = new Map<string, AttendanceRecord>();
|
// records (two shifts, or work + partial-day vacation) — keep them ALL; a
|
||||||
|
// single-record map silently dropped every shift but the last one.
|
||||||
|
const recordsByDate = new Map<string, AttendanceRecord[]>();
|
||||||
for (const r of userData.records || []) {
|
for (const r of userData.records || []) {
|
||||||
recordsByDate.set(normalizeDateStr(r.shift_date), r);
|
const key = normalizeDateStr(r.shift_date);
|
||||||
|
const list = recordsByDate.get(key);
|
||||||
|
if (list) list.push(r);
|
||||||
|
else recordsByDate.set(key, [r]);
|
||||||
|
}
|
||||||
|
// Stable in-day order: by arrival time (leave records without times last).
|
||||||
|
for (const list of recordsByDate.values()) {
|
||||||
|
list.sort((a, b) =>
|
||||||
|
String(a.arrival_time || "9999").localeCompare(
|
||||||
|
String(b.arrival_time || "9999"),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate one row per day of the month.
|
// Iterate one row per day of the month.
|
||||||
@@ -407,19 +337,14 @@ export function buildUserSectionHtml(
|
|||||||
const rows: string[] = [];
|
const rows: string[] = [];
|
||||||
for (let d = 1; d <= daysInMonth; d++) {
|
for (let d = 1; d <= daysInMonth; d++) {
|
||||||
const dateStr = shiftDateForMonth(yr, mo, d);
|
const dateStr = shiftDateForMonth(yr, mo, d);
|
||||||
const record = recordsByDate.get(dateStr);
|
const dayRecords = recordsByDate.get(dateStr);
|
||||||
const weekend = isWeekendDate(dateStr);
|
const weekend = isWeekendDate(dateStr);
|
||||||
const holiday = isHoliday(dateStr);
|
const holiday = isHoliday(dateStr);
|
||||||
const leaveType = (record?.leave_type as string) || "work";
|
|
||||||
const rowClasses = [
|
|
||||||
weekend && "weekend",
|
|
||||||
holiday && "holiday",
|
|
||||||
leaveType === "vacation" && "vacation",
|
|
||||||
]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(" ");
|
|
||||||
|
|
||||||
if (!record) {
|
if (!dayRecords || dayRecords.length === 0) {
|
||||||
|
const rowClasses = [weekend && "weekend", holiday && "holiday"]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(" ");
|
||||||
rows.push(`<tr class="${rowClasses}">
|
rows.push(`<tr class="${rowClasses}">
|
||||||
<td>${escapeHtml(formatDate(dateStr))}</td>
|
<td>${escapeHtml(formatDate(dateStr))}</td>
|
||||||
<td>${escapeHtml(getCzechWeekday(dateStr))}</td>
|
<td>${escapeHtml(getCzechWeekday(dateStr))}</td>
|
||||||
@@ -434,25 +359,37 @@ export function buildUserSectionHtml(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isLeave = leaveType !== "work";
|
// One row PER RECORD — a day can hold two shifts, or work + partial-day
|
||||||
const workMinutes = calculateWorkMinutesPrint(record);
|
// vacation. (A single row per day silently dropped the extra shifts.)
|
||||||
const hours = Math.floor(workMinutes / 60);
|
for (const record of dayRecords) {
|
||||||
const mins = workMinutes % 60;
|
const leaveType = (record.leave_type as string) || "work";
|
||||||
const breakCell =
|
const rowClasses = [
|
||||||
isLeave || !record.break_start || !record.break_end
|
weekend && "weekend",
|
||||||
? "—"
|
holiday && "holiday",
|
||||||
: `${escapeHtml(formatTimeOrDatetimePrint(record.break_start, record.shift_date))} - ${escapeHtml(formatTimeOrDatetimePrint(record.break_end, record.shift_date))}`;
|
leaveType === "vacation" && "vacation",
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
let hoursCell: string;
|
const isLeave = leaveType !== "work";
|
||||||
if (workMinutes > 0 && !isLeave) {
|
const workMinutes = calculateWorkMinutesPrint(record);
|
||||||
hoursCell = `${hours}:${String(mins).padStart(2, "0")} (${formatHoursDecimal(workMinutes)})`;
|
const hours = Math.floor(workMinutes / 60);
|
||||||
} else if (isLeave) {
|
const mins = workMinutes % 60;
|
||||||
hoursCell = formatHoursDecimal((Number(record.leave_hours) || 8) * 60);
|
const breakCell =
|
||||||
} else {
|
isLeave || !record.break_start || !record.break_end
|
||||||
hoursCell = "—";
|
? "—"
|
||||||
}
|
: `${escapeHtml(formatTimeOrDatetimePrint(record.break_start, record.shift_date))} - ${escapeHtml(formatTimeOrDatetimePrint(record.break_end, record.shift_date))}`;
|
||||||
|
|
||||||
rows.push(`<tr class="${rowClasses}">
|
let hoursCell: string;
|
||||||
|
if (workMinutes > 0 && !isLeave) {
|
||||||
|
hoursCell = `${hours}:${String(mins).padStart(2, "0")} (${formatHoursDecimal(workMinutes)})`;
|
||||||
|
} else if (isLeave) {
|
||||||
|
hoursCell = formatHoursDecimal((Number(record.leave_hours) || 8) * 60);
|
||||||
|
} else {
|
||||||
|
hoursCell = "—";
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.push(`<tr class="${rowClasses}">
|
||||||
<td>${escapeHtml(formatDate(dateStr))}</td>
|
<td>${escapeHtml(formatDate(dateStr))}</td>
|
||||||
<td>${escapeHtml(getCzechWeekday(dateStr))}</td>
|
<td>${escapeHtml(getCzechWeekday(dateStr))}</td>
|
||||||
<td>${escapeHtml(getLeaveTypeName(leaveType))}</td>
|
<td>${escapeHtml(getLeaveTypeName(leaveType))}</td>
|
||||||
@@ -463,110 +400,48 @@ export function buildUserSectionHtml(
|
|||||||
<td style="font-size:8px">${buildProjectLogsHtml(record)}</td>
|
<td style="font-size:8px">${buildProjectLogsHtml(record)}</td>
|
||||||
<td>${escapeHtml(record.notes || "")}</td>
|
<td>${escapeHtml(record.notes || "")}</td>
|
||||||
</tr>`);
|
</tr>`);
|
||||||
}
|
|
||||||
|
|
||||||
// ----- mzda-style summary numbers (computed here, not from userData,
|
|
||||||
// because the backend's getPrintData only returns the legacy fields). -----
|
|
||||||
|
|
||||||
// Worked minutes: sum of calculateWorkMinutesPrint across work records.
|
|
||||||
let workedMinutes = 0;
|
|
||||||
let weekendHoursRaw = 0;
|
|
||||||
let nightMinutes = 0;
|
|
||||||
for (const r of userRecords) {
|
|
||||||
const leaveType = r.leave_type || "work";
|
|
||||||
if (leaveType !== "work") continue;
|
|
||||||
const m = calculateWorkMinutesPrint(r);
|
|
||||||
workedMinutes += m;
|
|
||||||
if (isWeekendDate(r.shift_date)) {
|
|
||||||
weekendHoursRaw += m / 60;
|
|
||||||
}
|
}
|
||||||
nightMinutes += calculateNightMinutes(r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sum of vacation/sick/unpaid hours (in hours, not minutes). The
|
// Rekapitulace — single shared implementation (accountant spec 2026-07):
|
||||||
// "holiday" leave_type is auto-computed from Czech holidays further down
|
// Odpracováno = all worked hours EXCEPT hours worked on holidays
|
||||||
// and no longer selectable in the UI, so it's deliberately omitted here.
|
// Vč. svátků a přesč. = ALL worked hours (line2 − line1 = Svátek)
|
||||||
let vacationHours = 0;
|
// Přesčas = max(0, worked − fond)
|
||||||
for (const r of userRecords) {
|
// Svátek = hours actually worked on holiday dates
|
||||||
const lt = r.leave_type || "work";
|
const s = computeMzdaSummary(userRecords, printData.month);
|
||||||
if (lt === "work") continue;
|
|
||||||
const h = Number(r.leave_hours) || 8;
|
|
||||||
if (lt === "vacation") vacationHours += h;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Odpracováno: floor(worked / 8) × 8.
|
|
||||||
const workedHoursRaw = workedMinutes / 60;
|
|
||||||
const odpracovano = Math.floor(workedHoursRaw / 8) * 8;
|
|
||||||
|
|
||||||
// Free Svátek: 8h per holiday date the user did not work.
|
|
||||||
const holidayDates = holidaysInMonth(yr, mo);
|
|
||||||
const workedDates = new Set(
|
|
||||||
userRecords
|
|
||||||
.filter((r) => (r.leave_type || "work") === "work")
|
|
||||||
.map((r) => normalizeDateStr(r.shift_date))
|
|
||||||
.filter(Boolean),
|
|
||||||
);
|
|
||||||
let freeHolidayHours = 0;
|
|
||||||
let workedHolidayCount = 0;
|
|
||||||
for (const hd of holidayDates) {
|
|
||||||
if (workedDates.has(hd)) workedHolidayCount++;
|
|
||||||
else freeHolidayHours += 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fund: count Mon-Fri (incl. holidays) × 8h.
|
|
||||||
let rawBizDays = 0;
|
|
||||||
const cur = new Date(yr, mo - 1, 1);
|
|
||||||
while (cur.getMonth() === mo - 1) {
|
|
||||||
const dow = cur.getDay();
|
|
||||||
if (dow !== 0 && dow !== 6) rawBizDays++;
|
|
||||||
cur.setDate(cur.getDate() + 1);
|
|
||||||
}
|
|
||||||
const businessDays = rawBizDays;
|
|
||||||
const fund = businessDays * 8;
|
|
||||||
|
|
||||||
// Včetně svátků a přesčasů: floor(worked/8)*8 − worked_holiday*8 + vacation + remainder.
|
|
||||||
// (A worked holiday counts toward Odpracováno, but its 8h should not flow
|
|
||||||
// into Přesčas — so we subtract it here. Unworked holidays are paid via
|
|
||||||
// the fund and don't affect this line.)
|
|
||||||
const remainder = workedHoursRaw - odpracovano;
|
|
||||||
const vcetneSv =
|
|
||||||
odpracovano - workedHolidayCount * 8 + vacationHours + remainder;
|
|
||||||
|
|
||||||
// Přesčas = #2 - #1.
|
|
||||||
const prescas = vcetneSv - odpracovano;
|
|
||||||
|
|
||||||
const summaryHtml = `<div class="user-summary">
|
const summaryHtml = `<div class="user-summary">
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Odpracováno:</span>
|
<span class="summary-label">Odpracováno:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(odpracovano * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.odpracovano * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Odpracováno včetně svátků a přesčasů:</span>
|
<span class="summary-label">Odpracováno včetně svátků a přesčasů:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(vcetneSv * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.vcetneSvatku * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Dovolená:</span>
|
<span class="summary-label">Dovolená:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(vacationHours * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.vacationHours * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Přesčas:</span>
|
<span class="summary-label">Přesčas:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(prescas * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.prescas * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Svátek:</span>
|
<span class="summary-label">Svátek:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(freeHolidayHours * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.svatekHours * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">So/Ne:</span>
|
<span class="summary-label">So/Ne:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(weekendHoursRaw * 60)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.weekendHours * 60)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row">
|
<div class="summary-row">
|
||||||
<span class="summary-label">Práce v noci:</span>
|
<span class="summary-label">Práce v noci:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(nightMinutes)}h</span>
|
<span class="summary-value">${formatHoursDecimal(s.nightMinutes)}h</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="summary-row summary-fund">
|
<div class="summary-row summary-fund">
|
||||||
<span class="summary-label">Fond měsíce:</span>
|
<span class="summary-label">Fond měsíce:</span>
|
||||||
<span class="summary-value">${formatHoursDecimal(fund * 60)}h (${businessDays} dnů)</span>
|
<span class="summary-value">${formatHoursDecimal(s.fund * 60)}h (${s.businessDays} dnů)</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="legend">
|
<div class="legend">
|
||||||
<span class="legend-item"><span class="legend-swatch weekend"></span>Víkend (So/Ne)</span>
|
<span class="legend-item"><span class="legend-swatch weekend"></span>Víkend (So/Ne)</span>
|
||||||
|
|||||||
@@ -370,3 +370,94 @@ export const calculateFreeHolidayHours = (
|
|||||||
}
|
}
|
||||||
return free;
|
return free;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Mzda rekapitulace (single source — the print AND the admin screen consume
|
||||||
|
// this, so the two can never drift again; spec confirmed with the accountant
|
||||||
|
// 2026-07, see the summary lines below)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
export interface MzdaSummary {
|
||||||
|
/** All worked hours in the month: arrival→departure minus recorded breaks. */
|
||||||
|
workedHoursRaw: number;
|
||||||
|
/** Odpracováno: all worked hours EXCEPT hours worked on public holidays. */
|
||||||
|
odpracovano: number;
|
||||||
|
/** Odpracováno včetně svátků a přesčasů: ALL worked hours (line2 − line1 = Svátek). */
|
||||||
|
vcetneSvatku: number;
|
||||||
|
/** Dovolená: sum of vacation leave_hours (hour-based, partial days allowed). */
|
||||||
|
vacationHours: number;
|
||||||
|
/** Přesčas: worked hours above the monthly fund — max(0, worked − fond). */
|
||||||
|
prescas: number;
|
||||||
|
/** Svátek: hours actually WORKED on public-holiday dates. */
|
||||||
|
svatekHours: number;
|
||||||
|
/** So/Ne: worked hours on Saturdays and Sundays. */
|
||||||
|
weekendHours: number;
|
||||||
|
/** Práce v noci: worked minutes in the 22:00–06:00 window. */
|
||||||
|
nightMinutes: number;
|
||||||
|
/** Fond měsíce: Mon–Fri (public holidays included) × 8 h. */
|
||||||
|
fund: number;
|
||||||
|
businessDays: number;
|
||||||
|
/** 8 h per holiday the user did NOT work (fond is covered by these). */
|
||||||
|
freeHolidayHours: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Compute the payroll recap for one user's month of records. */
|
||||||
|
export const computeMzdaSummary = (
|
||||||
|
records: AttendanceRecord[],
|
||||||
|
monthStr: string,
|
||||||
|
): MzdaSummary => {
|
||||||
|
const [yearStr, monthNumStr] = monthStr.split("-");
|
||||||
|
const yr = parseInt(yearStr, 10);
|
||||||
|
const mo = parseInt(monthNumStr, 10);
|
||||||
|
|
||||||
|
const holidayDates = holidaysInMonth(yr, mo);
|
||||||
|
const holidaySet = new Set(holidayDates);
|
||||||
|
|
||||||
|
let workedMinutes = 0;
|
||||||
|
let svatekMinutes = 0;
|
||||||
|
let weekendMinutes = 0;
|
||||||
|
let nightMinutes = 0;
|
||||||
|
let vacationHours = 0;
|
||||||
|
|
||||||
|
for (const r of records) {
|
||||||
|
const lt = r.leave_type || "work";
|
||||||
|
if (lt !== "work") {
|
||||||
|
if (lt === "vacation") vacationHours += Number(r.leave_hours) || 8;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const m = calculateWorkMinutesPrint(r);
|
||||||
|
workedMinutes += m;
|
||||||
|
const day = normalizeDateStr(r.shift_date);
|
||||||
|
if (holidaySet.has(day)) svatekMinutes += m;
|
||||||
|
if (isWeekendDate(day)) weekendMinutes += m;
|
||||||
|
nightMinutes += calculateNightMinutes(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fond: Mon–Fri × 8 h; public holidays stay in the count (they are paid via
|
||||||
|
// the fond — an unworked holiday earns its 8 h, see freeHolidayHours).
|
||||||
|
let businessDays = 0;
|
||||||
|
const cur = new Date(yr, mo - 1, 1);
|
||||||
|
while (cur.getMonth() === mo - 1) {
|
||||||
|
const dow = cur.getDay();
|
||||||
|
if (dow !== 0 && dow !== 6) businessDays++;
|
||||||
|
cur.setDate(cur.getDate() + 1);
|
||||||
|
}
|
||||||
|
const fund = businessDays * 8;
|
||||||
|
|
||||||
|
const workedHoursRaw = workedMinutes / 60;
|
||||||
|
const svatekHours = svatekMinutes / 60;
|
||||||
|
|
||||||
|
return {
|
||||||
|
workedHoursRaw,
|
||||||
|
odpracovano: workedHoursRaw - svatekHours,
|
||||||
|
vcetneSvatku: workedHoursRaw,
|
||||||
|
vacationHours,
|
||||||
|
prescas: Math.max(0, workedHoursRaw - fund),
|
||||||
|
svatekHours,
|
||||||
|
weekendHours: weekendMinutes / 60,
|
||||||
|
nightMinutes,
|
||||||
|
fund,
|
||||||
|
businessDays,
|
||||||
|
freeHolidayHours: calculateFreeHolidayHours(records, holidayDates),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user