Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
12 changes: 9 additions & 3 deletions lib/Service/BalanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/Service/EmployeeJsonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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];
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/EmployeeJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []];
Expand Down
Loading