Add customer jobs with numbering, members, tags, links and history

Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:57:18 +02:00
co-authored by Claude Opus 5.5
parent 51b2d4a252
commit 70e107f6e2
95 changed files with 4631 additions and 26 deletions
@@ -17,6 +17,14 @@ interface CustomerQueries
{
public function findSummary(int $id): ?CustomerSummary;
/**
* Customer names for many ids at once (for lists in other modules).
*
* @param list<int> $ids
* @return array<int, string>
*/
public function namesByIds(array $ids): array;
/**
* The full personal identity code. Sensitive: call only after checking
* the customers.view-personal-identity-code ability, and never log it.
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Application\Contracts;
/**
* Whether other modules refer to a contact or billing unit, so it is not
* removed while in use. The Jobs module provides the real answer; without
* it nothing is in use.
*/
interface CustomerRowUsage
{
public function isContactInUse(int $contactId): bool;
public function isBillingUnitInUse(int $billingUnitId): bool;
}
@@ -21,6 +21,7 @@ final readonly class CustomerSummary
public CustomerType $type,
public ?string $businessId,
public ?string $personalIdentityCodeMasked = null,
public bool $isInternal = false,
) {}
public function hasPersonalIdentityCode(): bool
@@ -4,23 +4,26 @@ declare(strict_types=1);
namespace App\Modules\Customers\Application\UseCases;
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
use App\Modules\Customers\Domain\Contracts\BillingUnitRepository;
use App\Modules\Customers\Domain\Exceptions\BillingUnitInUse;
use App\Modules\Customers\Domain\Exceptions\BillingUnitNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Deletes a billing unit. Nothing refers to billing units yet; once customer
* jobs and invoices do, a billing unit in use cannot be removed.
* Deletes a billing unit that no customer job has chosen.
*/
final class RemoveBillingUnit
{
public function __construct(
private BillingUnitRepository $billingUnits,
private CustomerRowUsage $usage,
private TransactionManager $transactions,
) {}
/**
* @throws BillingUnitNotFound
* @throws BillingUnitInUse
*/
public function execute(int $customerId, int $billingUnitId): void
{
@@ -28,6 +31,10 @@ final class RemoveBillingUnit
$billingUnit = $this->billingUnits->findForCustomer($customerId, $billingUnitId)
?? throw new BillingUnitNotFound($customerId, $billingUnitId);
if ($this->usage->isBillingUnitInUse($billingUnitId)) {
throw new BillingUnitInUse;
}
$this->billingUnits->delete($billingUnit);
});
}
@@ -4,23 +4,26 @@ declare(strict_types=1);
namespace App\Modules\Customers\Application\UseCases;
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
use App\Modules\Customers\Domain\Contracts\CustomerContactRepository;
use App\Modules\Customers\Domain\Exceptions\ContactInUse;
use App\Modules\Customers\Domain\Exceptions\ContactNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Deletes a contact. Nothing refers to contacts yet; once customer jobs do,
* a contact in use cannot be removed.
* Deletes a contact that no customer job has chosen.
*/
final class RemoveContact
{
public function __construct(
private CustomerContactRepository $contacts,
private CustomerRowUsage $usage,
private TransactionManager $transactions,
) {}
/**
* @throws ContactNotFound
* @throws ContactInUse
*/
public function execute(int $customerId, int $contactId): void
{
@@ -28,6 +31,10 @@ final class RemoveContact
$contact = $this->contacts->findForCustomer($customerId, $contactId)
?? throw new ContactNotFound($customerId, $contactId);
if ($this->usage->isContactInUse($contactId)) {
throw new ContactInUse;
}
$this->contacts->delete($contact);
});
}
@@ -7,6 +7,7 @@ namespace App\Modules\Customers\Domain\Entities;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
use App\Modules\Customers\Domain\Exceptions\InternalCustomerMustBeCompany;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
@@ -25,8 +26,10 @@ final class Customer
private CustomerType $type,
private ?BusinessId $businessId,
private ?PersonalIdentityCode $personalIdentityCode,
private bool $internal = false,
) {
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
self::ensureInternalIsCompany($internal, $type);
}
/**
@@ -50,8 +53,9 @@ final class Customer
CustomerType $type,
?BusinessId $businessId,
?PersonalIdentityCode $personalIdentityCode = null,
bool $internal = false,
): self {
return new self($id, $name, $type, $businessId, $personalIdentityCode);
return new self($id, $name, $type, $businessId, $personalIdentityCode, $internal);
}
/**
@@ -69,6 +73,7 @@ final class Customer
}
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
self::ensureInternalIsCompany($this->internal, $type);
$this->name = $name;
$this->type = $type;
@@ -106,6 +111,22 @@ final class Customer
return $this->personalIdentityCode;
}
/**
* The company itself, whose jobs hold internal work (administration,
* sales, training). Created by a migration, never through the UI.
*/
public function isInternal(): bool
{
return $this->internal;
}
private static function ensureInternalIsCompany(bool $internal, CustomerType $type): void
{
if ($internal && $type !== CustomerType::Company) {
throw new InternalCustomerMustBeCompany;
}
}
private static function ensureIdentifiersFitType(
CustomerType $type,
?BusinessId $businessId,
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class BillingUnitInUse extends DomainException
{
public function __construct()
{
parent::__construct('The billing unit is chosen on a customer job.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ContactInUse extends DomainException
{
public function __construct()
{
parent::__construct('The contact is chosen on a customer job.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InternalCustomerMustBeCompany extends DomainException
{
public function __construct()
{
parent::__construct('The internal customer is the company itself and cannot be a consumer.');
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Modules\Customers\Infrastructure;
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
/**
* Default when no module refers to contacts or billing units.
*/
class NothingUsesCustomerRows implements CustomerRowUsage
{
public function isContactInUse(int $contactId): bool
{
return false;
}
public function isBillingUnitInUse(int $billingUnitId): bool
{
return false;
}
}
@@ -22,6 +22,7 @@ use Illuminate\Database\Eloquent\Model;
* @property string|null $business_id
* @property string|null $personal_identity_code Decrypted by the cast; sensitive.
* @property string|null $personal_identity_code_hash
* @property bool $is_internal
*/
#[Table('customers')]
#[Fillable(['name', 'type', 'business_id', 'personal_identity_code', 'personal_identity_code_hash'])]
@@ -40,6 +41,7 @@ class CustomerModel extends Model
return [
'type' => CustomerType::class,
'personal_identity_code' => 'encrypted',
'is_internal' => 'boolean',
];
}
}
@@ -22,6 +22,11 @@ class EloquentCustomerQueries implements CustomerQueries
return $model === null ? null : $this->toSummary($model);
}
public function namesByIds(array $ids): array
{
return $ids === [] ? [] : CustomerModel::query()->whereIn('id', $ids)->pluck('name', 'id')->all();
}
public function personalIdentityCodeOf(int $customerId): ?string
{
return CustomerModel::query()->find($customerId)?->personal_identity_code;
@@ -106,7 +111,7 @@ class EloquentCustomerQueries implements CustomerQueries
? null
: (new PersonalIdentityCode($model->personal_identity_code))->masked();
return new CustomerSummary($model->id, $model->name, $model->type, $model->business_id, $masked);
return new CustomerSummary($model->id, $model->name, $model->type, $model->business_id, $masked, (bool) $model->is_internal);
}
private function toContactSummary(CustomerContactModel $model): ContactSummary
@@ -62,6 +62,7 @@ class EloquentCustomerRepository implements CustomerRepository
$model->type,
$model->business_id === null ? null : new BusinessId($model->business_id),
$model->personal_identity_code === null ? null : new PersonalIdentityCode($model->personal_identity_code),
(bool) $model->is_internal,
);
}
}
@@ -4,9 +4,11 @@ namespace App\Modules\Customers\Infrastructure\Providers;
use App\Models\User;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
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\NothingUsesCustomerRows;
use App\Modules\Customers\Infrastructure\Persistence\Repositories\EloquentBillingUnitRepository;
use App\Modules\Customers\Infrastructure\Persistence\Repositories\EloquentCustomerContactRepository;
use App\Modules\Customers\Infrastructure\Persistence\Repositories\EloquentCustomerQueries;
@@ -30,6 +32,8 @@ class CustomersServiceProvider extends ServiceProvider
CustomerQueries::class => EloquentCustomerQueries::class,
CustomerContactRepository::class => EloquentCustomerContactRepository::class,
BillingUnitRepository::class => EloquentBillingUnitRepository::class,
// Replaced by the Jobs module, which knows which rows its jobs use.
CustomerRowUsage::class => NothingUsesCustomerRows::class,
];
public function boot(): void
@@ -8,6 +8,7 @@ use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\BusinessIdAlreadyInUse;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
use App\Modules\Customers\Domain\Exceptions\InternalCustomerMustBeCompany;
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Customers\Domain\Exceptions\InvalidCustomerName;
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
@@ -71,6 +72,7 @@ class CustomerForm extends Form
BusinessIdAlreadyInUse::class => ['business_id', 'customers.errors.business_id_taken'],
InvalidPersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.invalid_personal_identity_code'],
CompanyCannotHavePersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.company_personal_identity_code'],
InternalCustomerMustBeCompany::class => ['type', 'customers.errors.internal_must_be_company'],
PersonalIdentityCodeAlreadyInUse::class => ['personal_identity_code', 'customers.errors.personal_identity_code_taken'],
default => throw $exception,
};
@@ -90,6 +90,9 @@ new class extends Component
<x-table :headers="$this->headers()" :rows="$this->customers" paginate>
@interact('column_name', $customer)
<x-link :href="route('customers.show', $customer->id)" navigate class="font-medium">{{ $customer->name }}</x-link>
@if ($customer->isInternal)
<span class="ml-1 text-sm">({{ __('customers.internal') }})</span>
@endif
@endinteract
@interact('column_type', $customer)
@@ -6,7 +6,9 @@ use App\Modules\Customers\Application\DTOs\ContactSummary;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Application\UseCases\RemoveBillingUnit;
use App\Modules\Customers\Application\UseCases\RemoveContact;
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\Enums\CustomerType;
use Illuminate\Support\Facades\Gate;
@@ -137,6 +139,8 @@ new class extends Component
$this->statusMessage = __('customers.removal.removed_'.$this->removalType, ['name' => $this->removalName]);
} catch (ContactNotFound|BillingUnitNotFound) {
$this->statusMessage = __('customers.removal.already_removed');
} catch (ContactInUse|BillingUnitInUse) {
$this->statusMessage = __('customers.removal.in_use_'.$this->removalType, ['name' => $this->removalName]);
}
// The removed row's button is gone, so focus moves to its section heading.
@@ -174,7 +178,12 @@ new class extends Component
<dl class="grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('customers.fields.type') }}</dt>
<dd>{{ __('customers.types.'.$this->customer->type->value) }}</dd>
<dd>
{{ __('customers.types.'.$this->customer->type->value) }}
@if ($this->customer->isInternal)
– {{ __('customers.internal') }}. {{ __('customers.internal_description') }}
@endif
</dd>
@if ($this->customer->businessId !== null)
<dt class="font-medium">{{ __('customers.fields.business_id') }}</dt>