Add the task calendar, holidays, workflow templates and customer removal

Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 18:13:11 +02:00
co-authored by Claude Opus 5.5
parent dfe4941f02
commit b4f2f1cfaa
134 changed files with 6930 additions and 45 deletions
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Application\Contracts;
/**
* Whether another module has rows that refer to a customer (customer jobs,
* quick sales, invoices), so the customer cannot be removed. Each such module
* tags its own implementation with `customers.references`.
*/
interface CustomerReferences
{
public const string TAG = 'customers.references';
public function refersTo(int $customerId): bool;
}
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Application\Services;
use App\Modules\Customers\Application\Contracts\CustomerReferences;
use App\Modules\Customers\Domain\Contracts\BillingUnitRepository;
use App\Modules\Customers\Domain\Contracts\CustomerContactRepository;
use App\Modules\Customers\Domain\Contracts\CustomerRepository;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\Exceptions\InternalCustomerCannotBeRemoved;
/**
* Whether a customer may be removed: it is not the internal customer and
* nothing is linked to it (no contacts, no billing units, no rows of other
* modules).
*/
final class CustomerRemoval
{
public function __construct(
private CustomerRepository $customers,
private CustomerContactRepository $contacts,
private BillingUnitRepository $billingUnits,
private CustomerReferences $references,
) {}
public function canRemove(int $customerId): bool
{
try {
$this->ensureCanRemove($customerId);
return true;
} catch (CustomerNotFound|InternalCustomerCannotBeRemoved|CustomerInUse) {
return false;
}
}
/**
* @throws CustomerNotFound
* @throws InternalCustomerCannotBeRemoved
* @throws CustomerInUse
*/
public function ensureCanRemove(int $customerId): Customer
{
$customer = $this->customers->findById($customerId)
?? throw new CustomerNotFound($customerId);
if ($customer->isInternal()) {
throw new InternalCustomerCannotBeRemoved;
}
if ($this->contacts->existsForCustomer($customerId)
|| $this->billingUnits->existsForCustomer($customerId)
|| $this->references->refersTo($customerId)) {
throw new CustomerInUse;
}
return $customer;
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Application\UseCases;
use App\Modules\Customers\Application\Services\CustomerRemoval;
use App\Modules\Customers\Domain\Contracts\CustomerRepository;
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\Exceptions\InternalCustomerCannotBeRemoved;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Deletes a customer that has nothing but its own details: no contacts,
* no billing units, and nothing in other modules (customer jobs, quick
* sales, invoices) that refers to it.
*/
final class RemoveCustomer
{
public function __construct(
private CustomerRepository $customers,
private CustomerRemoval $removal,
private TransactionManager $transactions,
) {}
/**
* @throws CustomerNotFound
* @throws InternalCustomerCannotBeRemoved
* @throws CustomerInUse
*/
public function execute(int $customerId): void
{
$this->transactions->run(function () use ($customerId): void {
$this->customers->delete($this->removal->ensureCanRemove($customerId));
});
}
}
@@ -20,4 +20,6 @@ interface BillingUnitRepository
public function save(BillingUnit $billingUnit): BillingUnit;
public function delete(BillingUnit $billingUnit): void;
public function existsForCustomer(int $customerId): bool;
}
@@ -20,4 +20,6 @@ interface CustomerContactRepository
public function save(CustomerContact $contact): CustomerContact;
public function delete(CustomerContact $contact): void;
public function existsForCustomer(int $customerId): bool;
}
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Modules\Customers\Domain\Contracts;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
@@ -18,6 +19,11 @@ interface CustomerRepository
*/
public function save(Customer $customer): Customer;
/**
* @throws CustomerInUse when other rows refer to the customer
*/
public function delete(Customer $customer): void;
public function isBusinessIdTaken(BusinessId $businessId, ?int $exceptCustomerId = null): bool;
public function isPersonalIdentityCodeTaken(PersonalIdentityCode $code, ?int $exceptCustomerId = null): bool;
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The customer has contacts, billing units, or rows of other modules
* (customer jobs, quick sales, invoices) refer to it.
*/
final class CustomerInUse extends DomainException
{
public function __construct()
{
parent::__construct('The customer has rows that refer to it.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InternalCustomerCannotBeRemoved extends DomainException
{
public function __construct()
{
parent::__construct('The internal customer cannot be removed.');
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Modules\Customers\Infrastructure;
use App\Modules\Customers\Application\Contracts\CustomerReferences;
/**
* Asks every module that tagged a `CustomerReferences` implementation.
* Without any, nothing refers to customers.
*/
class AnyModuleRefersToCustomer implements CustomerReferences
{
/**
* @param iterable<CustomerReferences> $references
*/
public function __construct(private iterable $references) {}
public function refersTo(int $customerId): bool
{
foreach ($this->references as $references) {
if ($references->refersTo($customerId)) {
return true;
}
}
return false;
}
}
@@ -40,6 +40,11 @@ class EloquentBillingUnitRepository implements BillingUnitRepository
BillingUnitModel::query()->whereKey($billingUnit->id())->delete();
}
public function existsForCustomer(int $customerId): bool
{
return BillingUnitModel::query()->where('customer_id', $customerId)->exists();
}
private function toEntity(BillingUnitModel $model): BillingUnit
{
return BillingUnit::reconstitute(
@@ -40,6 +40,11 @@ class EloquentCustomerContactRepository implements CustomerContactRepository
CustomerContactModel::query()->whereKey($contact->id())->delete();
}
public function existsForCustomer(int $customerId): bool
{
return CustomerContactModel::query()->where('customer_id', $customerId)->exists();
}
private function toEntity(CustomerContactModel $model): CustomerContact
{
return CustomerContact::reconstitute(
@@ -4,11 +4,13 @@ namespace App\Modules\Customers\Infrastructure\Persistence\Repositories;
use App\Modules\Customers\Domain\Contracts\CustomerRepository;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Customers\Infrastructure\Persistence\PersonalIdentityCodeHasher;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
use Illuminate\Database\QueryException;
class EloquentCustomerRepository implements CustomerRepository
{
@@ -38,6 +40,24 @@ class EloquentCustomerRepository implements CustomerRepository
return $this->toEntity($model);
}
/**
* Customer jobs, quick sales and invoices refer to customers with
* restricting foreign keys, so the database refuses the delete while
* any of them exists. That keeps this module unaware of their tables.
*/
public function delete(Customer $customer): void
{
try {
CustomerModel::query()->whereKey($customer->id())->delete();
} catch (QueryException $exception) {
if (str_starts_with((string) $exception->getCode(), '23')) {
throw new CustomerInUse;
}
throw $exception;
}
}
public function isBusinessIdTaken(BusinessId $businessId, ?int $exceptCustomerId = null): bool
{
return CustomerModel::query()
@@ -4,10 +4,12 @@ namespace App\Modules\Customers\Infrastructure\Providers;
use App\Models\User;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\Contracts\CustomerReferences;
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
use App\Modules\Customers\Domain\Contracts\BillingUnitRepository;
use App\Modules\Customers\Domain\Contracts\CustomerContactRepository;
use App\Modules\Customers\Domain\Contracts\CustomerRepository;
use App\Modules\Customers\Infrastructure\AnyModuleRefersToCustomer;
use App\Modules\Customers\Infrastructure\NothingUsesCustomerRows;
use App\Modules\Customers\Infrastructure\Persistence\Repositories\EloquentBillingUnitRepository;
use App\Modules\Customers\Infrastructure\Persistence\Repositories\EloquentCustomerContactRepository;
@@ -36,6 +38,15 @@ class CustomersServiceProvider extends ServiceProvider
CustomerRowUsage::class => NothingUsesCustomerRows::class,
];
public function register(): void
{
// Jobs, Sales and Billing tag their own checks of rows that refer to a customer.
$this->app->bind(
CustomerReferences::class,
fn ($app): CustomerReferences => new AnyModuleRefersToCustomer($app->tagged(CustomerReferences::TAG)),
);
}
public function boot(): void
{
$presentationPath = dirname(__DIR__, 2).'/Presentation';
@@ -4,12 +4,17 @@ use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\BillingUnitSummary;
use App\Modules\Customers\Application\DTOs\ContactSummary;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Application\Services\CustomerRemoval;
use App\Modules\Customers\Application\UseCases\RemoveBillingUnit;
use App\Modules\Customers\Application\UseCases\RemoveContact;
use App\Modules\Customers\Application\UseCases\RemoveCustomer;
use App\Modules\Customers\Domain\Exceptions\BillingUnitInUse;
use App\Modules\Customers\Domain\Exceptions\BillingUnitNotFound;
use App\Modules\Customers\Domain\Exceptions\ContactInUse;
use App\Modules\Customers\Domain\Exceptions\ContactNotFound;
use App\Modules\Customers\Domain\Exceptions\CustomerInUse;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\Exceptions\InternalCustomerCannotBeRemoved;
use App\Modules\Customers\Domain\Enums\CustomerType;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
@@ -17,7 +22,8 @@ use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* A customer's page: details, contacts and billing units.
* A customer's page: details, contacts and billing units. A customer with
* nothing linked to it can be removed here.
*/
new class extends Component
{
@@ -27,7 +33,7 @@ new class extends Component
public int $customerId;
/**
* The row the remove dialog asks about: 'contact' or 'billing_unit'.
* What the remove dialog asks about: 'customer', 'contact' or 'billing_unit'.
*/
#[Locked]
public ?string $removalType = null;
@@ -42,9 +48,12 @@ new class extends Component
protected CustomerQueries $customerQueries;
public function boot(CustomerQueries $customerQueries): void
protected CustomerRemoval $customerRemoval;
public function boot(CustomerQueries $customerQueries, CustomerRemoval $customerRemoval): void
{
$this->customerQueries = $customerQueries;
$this->customerRemoval = $customerRemoval;
}
public function mount(int $customer): void
@@ -77,6 +86,15 @@ new class extends Component
return $this->customer->personalIdentityCodeMasked;
}
/**
* Whether nothing is linked to this customer, so the remove button is shown.
*/
#[Computed]
public function canBeRemoved(): bool
{
return $this->customerRemoval->canRemove($this->customerId);
}
/**
* @return list<ContactSummary>
*/
@@ -96,13 +114,15 @@ new class extends Component
}
/**
* Opens the remove dialog for a contact or a billing unit of this customer.
* Opens the remove dialog for this customer, or for one of its contacts
* or billing units.
*/
public function confirmRemoval(string $type, int $id): void
{
$this->authorize('customers.use');
$name = match ($type) {
'customer' => $id === $this->customerId ? $this->customerQueries->findSummary($id)?->name : null,
'contact' => $this->customerQueries->findContact($this->customerId, $id)?->name,
'billing_unit' => $this->customerQueries->findBillingUnit($this->customerId, $id)?->name,
default => null,
@@ -122,7 +142,7 @@ new class extends Component
$this->js("document.getElementById('".self::REMOVE_DIALOG."').showModal()");
}
public function remove(RemoveContact $removeContact, RemoveBillingUnit $removeBillingUnit): void
public function remove(RemoveContact $removeContact, RemoveBillingUnit $removeBillingUnit, RemoveCustomer $removeCustomer): void
{
$this->authorize('customers.use');
@@ -130,6 +150,12 @@ new class extends Component
return;
}
if ($this->removalType === 'customer') {
$this->removeCustomer($removeCustomer);
return;
}
try {
match ($this->removalType) {
'contact' => $removeContact->execute($this->customerId, $this->removalId),
@@ -150,6 +176,32 @@ new class extends Component
$this->reset('removalType', 'removalId', 'removalName');
}
/**
* Removes this customer and returns to the customer list, or tells why
* it was kept and moves focus back to the page heading.
*/
private function removeCustomer(RemoveCustomer $removeCustomer): void
{
try {
$removeCustomer->execute($this->customerId);
session()->flash('status', __('customers.removal.removed_customer', ['name' => $this->removalName]));
$this->redirectRoute('customers.index', navigate: true);
return;
} catch (CustomerNotFound) {
$this->statusMessage = __('customers.removal.already_removed');
} catch (InternalCustomerCannotBeRemoved) {
$this->statusMessage = __('customers.removal.internal_customer');
} catch (CustomerInUse) {
$this->statusMessage = __('customers.removal.in_use_customer', ['name' => $this->removalName]);
}
$this->js("document.getElementById('".self::REMOVE_DIALOG."').close(); document.getElementById('customer-heading').focus()");
$this->reset('removalType', 'removalId', 'removalName');
}
public function render(): mixed
{
return $this->view()->title($this->customer->name);
@@ -160,16 +212,28 @@ new class extends Component
<div class="mx-auto max-w-5xl space-y-8 p-4 sm:p-6">
<div class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h1 class="text-2xl font-semibold break-words">{{ $this->customer->name }}</h1>
<h1 id="customer-heading" tabindex="-1" class="text-2xl font-semibold break-words focus:outline-none">{{ $this->customer->name }}</h1>
<x-button
:href="route('customers.edit', $customerId)"
wire:navigate
subtle
color="black"
icon="pencil"
:text="__('customers.edit_details')"
/>
<div class="flex flex-wrap gap-2">
<x-button
:href="route('customers.edit', $customerId)"
wire:navigate
subtle
color="black"
icon="pencil"
:text="__('customers.edit_details')"
/>
@if ($this->canBeRemoved)
<x-button
wire:click="confirmRemoval('customer', {{ $customerId }})"
subtle
color="black"
icon="trash"
:text="__('customers.remove_customer')"
/>
@endif
</div>
</div>
@if (session('status'))