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
@@ -7,7 +7,7 @@ namespace App\Modules\Jobs\Application\Services;
use App\Modules\Jobs\Domain\Contracts\TaskBookingRepository;
use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\Services\TaskScheduler;
use App\Modules\Shared\Application\Contracts\Settings;
use App\Modules\Shared\Application\UseCases\DailyCapacity;
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
use DateInterval;
use DateTimeImmutable;
@@ -15,29 +15,23 @@ use DateTimeImmutable;
/**
* Books a saved task's estimate from its assignee's days with the
* TaskScheduler, using the holidays in use and the daily maximum
* (setting `jobs.daily_capacity_hours`, 8 h by default). When the days
* are full, the task's planned end moves later.
* (Shared's DailyCapacity, set in the company details, 7.5 h by default).
* When the days are full, the task's planned end moves later.
*/
final class TaskPlanner
{
public const string CAPACITY_KEY = 'jobs.daily_capacity_hours';
private const int DEFAULT_CAPACITY_HOURS = 8;
/** Days past the start in which holidays and other bookings are looked up. */
private const int LOOKAHEAD_DAYS = 400;
public function __construct(
private TaskBookingRepository $bookings,
private HolidayCalendar $holidays,
private Settings $settings,
private DailyCapacity $capacity,
) {}
public function capacityMinutes(): int
{
$hours = (float) str_replace(',', '.', $this->settings->get(self::CAPACITY_KEY) ?? '');
return (int) round(($hours > 0 && $hours <= 24 ? $hours : self::DEFAULT_CAPACITY_HOURS) * 60);
return $this->capacity->minutes();
}
/**
@@ -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'), '.'));
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\Modules\Shared\Domain\Exceptions;
final class InvalidDailyCapacity extends DomainException
{
public function __construct()
{
parent::__construct('The daily capacity must be more than 0 and at most 24 hours.');
}
}
@@ -2,15 +2,18 @@
use App\Modules\Shared\Application\DTOs\CompanyDetails;
use App\Modules\Shared\Application\UseCases\CompanyDetailsStore;
use App\Modules\Shared\Application\UseCases\DailyCapacity;
use App\Modules\Shared\Domain\Exceptions\CompanyNameRequired;
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Shared\Domain\Exceptions\InvalidDailyCapacity;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* The company's own details (Yritystiedot) for receipts and other
* documents. Only admins change settings (spec 2).
* documents, and the task calendar's daily capacity. Only admins change
* settings (spec 2).
*/
new class extends Component
{
@@ -26,13 +29,17 @@ new class extends Component
public string $email = '';
public string $daily_capacity_hours = '';
#[Locked]
public string $statusMessage = '';
public function mount(CompanyDetailsStore $store): void
public function mount(CompanyDetailsStore $store, DailyCapacity $capacity): void
{
$this->authorize('settings.manage');
$this->daily_capacity_hours = $capacity->hours() ?? '';
$details = $store->get();
if ($details !== null) {
@@ -45,7 +52,7 @@ new class extends Component
}
}
public function save(CompanyDetailsStore $store): void
public function save(CompanyDetailsStore $store, DailyCapacity $capacity): void
{
$this->authorize('settings.manage');
$this->statusMessage = '';
@@ -57,6 +64,7 @@ new class extends Component
'address' => ['nullable', 'string', 'max:500'],
'phone' => ['nullable', 'string', 'max:50'],
'email' => ['nullable', 'string', 'max:255'],
'daily_capacity_hours' => ['nullable', 'string', 'max:10'],
], [
'name.required' => __('company.errors.name'),
'name.max' => __('company.errors.name'),
@@ -66,10 +74,22 @@ new class extends Component
'address.max' => __('company.errors.address'),
'phone.max' => __('company.errors.phone'),
'email.max' => __('company.errors.email'),
'daily_capacity_hours.max' => __('company.errors.daily_capacity_hours'),
]);
if (! DailyCapacity::isValid($this->daily_capacity_hours)) {
$this->addError('daily_capacity_hours', __('company.errors.daily_capacity_hours'));
return;
}
try {
$store->save(new CompanyDetails($this->name, $this->business_id, $this->address, $this->phone, $this->email, $this->menu_name));
$capacity->save($this->daily_capacity_hours);
} catch (InvalidDailyCapacity) {
$this->addError('daily_capacity_hours', __('company.errors.daily_capacity_hours'));
return;
} catch (CompanyNameRequired) {
$this->addError('name', __('company.errors.name'));
@@ -85,6 +105,7 @@ new class extends Component
}
$this->business_id = (string) $store->get()?->businessId;
$this->daily_capacity_hours = $capacity->hours() ?? '';
$this->statusMessage = __('company.saved');
}
@@ -111,6 +132,9 @@ new class extends Component
<x-input wire:model="phone" type="tel" :label="__('company.fields.phone')" autocomplete="tel" />
<x-input wire:model="email" type="email" :label="__('company.fields.email')" autocomplete="email" />
<h2 class="pt-2 text-lg font-semibold">{{ __('company.calendar_heading') }}</h2>
<x-input wire:model="daily_capacity_hours" inputmode="decimal" :label="__('company.fields.daily_capacity_hours')" :hint="__('company.fields.daily_capacity_hours_hint')" autocomplete="off" />
<x-button submit :text="__('company.save')" />
</form>
</div>