diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 1ffcd736..25ae65cd 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -909,9 +909,12 @@ current data of every employee, *Download* saves the same text, and forgotten key never wipes data. Employees not in the list are left alone. - `vacation` is optional: the annual leave base days (§6.4) of each year it lists, from two years back to next year, in whole days. Years it leaves out keep their - days. The export lists the current year (the default while HR set nothing) and next - year once set. Each change goes through `EntitlementService`, so the entitlement - history and the activity stream name the HR member. + days. The export lists the current year (while HR set nothing, what the balance + shows: the default, or 0 in a year the contract does not cover, §6.1) and next + year once set. A listed year is compared with the same figure, so the default of a + year outside the contract is a change. Each change goes through + `EntitlementService`, so the entitlement history and the activity stream name the + HR member. - Region codes are read as everywhere else (`de_by` is `DE-BY`); `*` is for holidays, not people. Dates are `YYYY-MM-DD`, weekdays 1 (Monday) to 7, percent 1 to 100 or `null`. diff --git a/lib/Service/BalanceService.php b/lib/Service/BalanceService.php index 20179951..014fc024 100644 --- a/lib/Service/BalanceService.php +++ b/lib/Service/BalanceService.php @@ -54,9 +54,15 @@ public function isEmployedIn(string $employeeUid, int $year): bool { * the annual type in a year the person is employed in, 0 otherwise (§6.1). */ private function defaultBaseDays(string $employeeUid, int $year, LeaveType $type): float { - return $type->getKey() === 'annual' && $this->isEmployedIn($employeeUid, $year) - ? $this->config->getDefaultEntitlement() - : 0.0; + return $type->getKey() === 'annual' ? $this->defaultAnnualDays($employeeUid, $year) : 0.0; + } + + /** + * The annual base days a year without a stored entitlement stands for: the + * configured default in a year the person is employed in, 0 otherwise (§6.1). + */ + public function defaultAnnualDays(string $employeeUid, int $year): float { + return $this->isEmployedIn($employeeUid, $year) ? $this->config->getDefaultEntitlement() : 0.0; } /** diff --git a/lib/Service/EmployeeJsonService.php b/lib/Service/EmployeeJsonService.php index 430ea338..a2d9f58d 100644 --- a/lib/Service/EmployeeJsonService.php +++ b/lib/Service/EmployeeJsonService.php @@ -68,12 +68,11 @@ public function export(): array { $regions = $this->config->getRegionCodes(); $year = $this->balances->currentYear(); $vacation = $this->vacationDays([$year, $year + 1]); - $default = $this->config->getDefaultEntitlement(); $entries = []; foreach ($uids as $uid) { $contract = $this->employees->getContract($uid); - // The current year always, with the default while HR set nothing; the next one once set - $days = [$year => $vacation[$uid][$year] ?? $default]; + // The current year always, with what the balance shows while HR set nothing; the next one once set + $days = [$year => $vacation[$uid][$year] ?? $this->balances->defaultAnnualDays($uid, $year)]; if (isset($vacation[$uid][$year + 1])) { $days[$year + 1] = $vacation[$uid][$year + 1]; } @@ -367,7 +366,6 @@ private function compare(array $wanted, ?string $actor): array { $regions = $this->config->getRegionCodes(); $years = array_values(array_unique(array_merge([], ...array_map(static fn (array $want): array => array_keys($want['after']['vacation']), array_values($wanted))))); $vacation = $years === [] ? [] : $this->vacationDays($years); - $default = $this->config->getDefaultEntitlement(); $changes = []; $writes = []; @@ -391,8 +389,8 @@ private function compare(array $wanted, ?string $actor): array { } $days = []; foreach ($after['vacation'] as $year => $wantedDays) { - // No row yet means the default, which is what the balance shows - $current = $vacation[$uid][$year] ?? $default; + // No row yet means what the balance shows + $current = $vacation[$uid][$year] ?? $this->balances->defaultAnnualDays($uid, $year); if ($current !== $wantedDays) { $days[$year] = $wantedDays; $fields[] = ['field' => 'vacation', 'year' => $year, 'before' => $current, 'after' => $wantedDays]; diff --git a/tests/Integration/EmployeeJsonTest.php b/tests/Integration/EmployeeJsonTest.php index 9f7dc8cf..98b679c9 100644 --- a/tests/Integration/EmployeeJsonTest.php +++ b/tests/Integration/EmployeeJsonTest.php @@ -163,6 +163,31 @@ public function testVacationDaysAreSetForTheYearsListedOnly(): void { self::assertSame([$year => 24.0, $year + 1 => 22.0], $this->entry($this->service->export(), self::ROBIN)['vacation']); } + public function testAYearOutsideTheContractHasNoDefaultDays(): void { + $year = $this->year(); + $default = Server::get(ConfigService::class)->getDefaultEntitlement(); + $start = ($year + 1) . '-03-01'; + $entry = ['user' => self::SAM, 'region' => null, 'start' => $start, 'end' => null, + 'contract' => [['from' => $start, 'weekdays' => [1, 2, 3, 4, 5], 'percent' => 100]]]; + Server::get(EmployeeService::class)->saveContract(self::SAM, $start, null, $entry['contract'], self::HR); + $balance = array_values(array_filter( + Server::get(BalanceService::class)->getBalance(self::SAM, $year)['balances'], + static fn (array $balance): bool => $balance['typeKey'] === 'annual', + )); + + // Like the balance, which gives no default before the start date + self::assertSame(0.0, $balance[0]['entitlement']); + $entries = $this->service->export(); + self::assertSame([$year => 0.0], $this->entry($entries, self::SAM)['vacation']); + self::assertSame([], $this->service->plan(EmployeeJsonService::format($entries))['changes']); + + // The default days are a change for that year, not what is there + self::assertSame( + [['field' => 'vacation', 'year' => $year, 'before' => 0.0, 'after' => $default]], + $this->service->plan($this->json([[...$entry, 'vacation' => [(string)$year => (int)$default]]]))['changes'][0]['fields'], + ); + } + public function testHrDoesNotSetTheirOwnVacationDays(): void { $year = $this->year(); $entry = ['user' => self::HR, 'region' => null, 'start' => null, 'end' => null, 'contract' => []];