Set the calendar's daily capacity in the company details

The task calendar's daily maximum moves from the hidden setting
jobs.daily_capacity_hours to shared.daily_capacity_hours, edited by the
admin on the company details page. An empty field means 7.5 hours.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 11:00:35 +02:00
co-authored by Claude Sonnet 5.5
parent e0da863cf7
commit 7c605014c7
16 changed files with 245 additions and 23 deletions
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\Modules\Shared\Application\UseCases;
use App\Modules\Shared\Application\Contracts\Settings;
use App\Modules\Shared\Domain\Exceptions\InvalidDailyCapacity;
/**
* The most hours the task calendar books for one person on one working
* day, set on the company details page (setting `shared.daily_capacity_hours`,
* 7.5 h when empty). Who may save is checked by the caller (admin).
*/
final class DailyCapacity
{
public const string KEY = 'shared.daily_capacity_hours';
public const int DEFAULT_MINUTES = 450;
public function __construct(private Settings $settings) {}
public function minutes(): int
{
return self::minutesOf($this->settings->get(self::KEY)) ?? self::DEFAULT_MINUTES;
}
/**
* The saved hours in Finnish form (e.g. "7,5"), or null when not set.
*/
public function hours(): ?string
{
$minutes = self::minutesOf($this->settings->get(self::KEY));
return $minutes === null ? null : self::format($minutes);
}
/**
* Saves the hours ("7,5" or "7.5"); empty removes the setting.
*
* @throws InvalidDailyCapacity
*/
public function save(?string $hours): void
{
$hours = trim((string) $hours);
if ($hours === '') {
$this->settings->put(self::KEY, null);
return;
}
$minutes = self::minutesOf($hours) ?? throw new InvalidDailyCapacity;
$this->settings->put(self::KEY, str_replace(',', '.', self::format($minutes)));
}
/**
* Whether save() accepts the hours: empty, or more than 0 and at most 24.
*/
public static function isValid(?string $hours): bool
{
return trim((string) $hours) === '' || self::minutesOf($hours) !== null;
}
private static function minutesOf(?string $hours): ?int
{
$hours = str_replace(',', '.', trim((string) $hours));
if (! is_numeric($hours)) {
return null;
}
$minutes = (int) round((float) $hours * 60);
return $minutes > 0 && $minutes <= 24 * 60 ? $minutes : null;
}
private static function format(int $minutes): string
{
return str_replace('.', ',', rtrim(rtrim(number_format($minutes / 60, 2, '.', ''), '0'), '.'));
}
}