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]>
367 lines
15 KiB
PHP
367 lines
15 KiB
PHP
<?php
|
||
|
||
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\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;
|
||
use Livewire\Attributes\Computed;
|
||
use Livewire\Attributes\Locked;
|
||
use Livewire\Component;
|
||
|
||
/**
|
||
* A customer's page: details, contacts and billing units.
|
||
*/
|
||
new class extends Component
|
||
{
|
||
public const string REMOVE_DIALOG = 'remove-dialog';
|
||
|
||
#[Locked]
|
||
public int $customerId;
|
||
|
||
/**
|
||
* The row the remove dialog asks about: 'contact' or 'billing_unit'.
|
||
*/
|
||
#[Locked]
|
||
public ?string $removalType = null;
|
||
|
||
#[Locked]
|
||
public ?int $removalId = null;
|
||
|
||
#[Locked]
|
||
public string $removalName = '';
|
||
|
||
public string $statusMessage = '';
|
||
|
||
protected CustomerQueries $customerQueries;
|
||
|
||
public function boot(CustomerQueries $customerQueries): void
|
||
{
|
||
$this->customerQueries = $customerQueries;
|
||
}
|
||
|
||
public function mount(int $customer): void
|
||
{
|
||
$this->authorize('customers.use');
|
||
|
||
abort_if($this->customerQueries->findSummary($customer) === null, 404);
|
||
|
||
$this->customerId = $customer;
|
||
}
|
||
|
||
#[Computed]
|
||
public function customer(): CustomerSummary
|
||
{
|
||
return $this->customerQueries->findSummary($this->customerId) ?? abort(404);
|
||
}
|
||
|
||
/**
|
||
* The personal identity code as this user may see it: in full for admins
|
||
* and management, masked for others. Computed, so the full code is never
|
||
* kept in the component state sent to the browser.
|
||
*/
|
||
#[Computed]
|
||
public function personalIdentityCode(): ?string
|
||
{
|
||
if (Gate::allows('customers.view-personal-identity-code')) {
|
||
return $this->customerQueries->personalIdentityCodeOf($this->customerId);
|
||
}
|
||
|
||
return $this->customer->personalIdentityCodeMasked;
|
||
}
|
||
|
||
/**
|
||
* @return list<ContactSummary>
|
||
*/
|
||
#[Computed]
|
||
public function contacts(): array
|
||
{
|
||
return $this->customerQueries->contactsOf($this->customerId);
|
||
}
|
||
|
||
/**
|
||
* @return list<BillingUnitSummary>
|
||
*/
|
||
#[Computed]
|
||
public function billingUnits(): array
|
||
{
|
||
return $this->customerQueries->billingUnitsOf($this->customerId);
|
||
}
|
||
|
||
/**
|
||
* Opens the remove dialog for a contact or a billing unit of this customer.
|
||
*/
|
||
public function confirmRemoval(string $type, int $id): void
|
||
{
|
||
$this->authorize('customers.use');
|
||
|
||
$name = match ($type) {
|
||
'contact' => $this->customerQueries->findContact($this->customerId, $id)?->name,
|
||
'billing_unit' => $this->customerQueries->findBillingUnit($this->customerId, $id)?->name,
|
||
default => null,
|
||
};
|
||
|
||
if ($name === null) {
|
||
$this->statusMessage = __('customers.removal.already_removed');
|
||
|
||
return;
|
||
}
|
||
|
||
$this->removalType = $type;
|
||
$this->removalId = $id;
|
||
$this->removalName = $name;
|
||
$this->statusMessage = '';
|
||
|
||
$this->js("document.getElementById('".self::REMOVE_DIALOG."').showModal()");
|
||
}
|
||
|
||
public function remove(RemoveContact $removeContact, RemoveBillingUnit $removeBillingUnit): void
|
||
{
|
||
$this->authorize('customers.use');
|
||
|
||
if ($this->removalType === null || $this->removalId === null) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
match ($this->removalType) {
|
||
'contact' => $removeContact->execute($this->customerId, $this->removalId),
|
||
'billing_unit' => $removeBillingUnit->execute($this->customerId, $this->removalId),
|
||
};
|
||
|
||
$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.
|
||
$heading = $this->removalType === 'contact' ? 'contacts-heading' : 'billing-units-heading';
|
||
$this->js("document.getElementById('".self::REMOVE_DIALOG."').close(); document.getElementById('{$heading}').focus()");
|
||
|
||
$this->reset('removalType', 'removalId', 'removalName');
|
||
}
|
||
|
||
public function render(): mixed
|
||
{
|
||
return $this->view()->title($this->customer->name);
|
||
}
|
||
};
|
||
?>
|
||
|
||
<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>
|
||
|
||
<x-button
|
||
:href="route('customers.edit', $customerId)"
|
||
wire:navigate
|
||
subtle
|
||
color="black"
|
||
icon="pencil"
|
||
:text="__('customers.edit_details')"
|
||
/>
|
||
</div>
|
||
|
||
@if (session('status'))
|
||
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
|
||
@endif
|
||
|
||
<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) }}
|
||
@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>
|
||
<dd>{{ $this->customer->businessId }}</dd>
|
||
@endif
|
||
|
||
@if ($this->customer->type === CustomerType::Consumer)
|
||
<dt class="font-medium">{{ __('customers.fields.personal_identity_code') }}</dt>
|
||
<dd>
|
||
@if ($this->personalIdentityCode !== null)
|
||
{{ $this->personalIdentityCode }}
|
||
@else
|
||
<span class="inline-flex items-center gap-1">
|
||
<x-icon name="exclamation-triangle" class="size-4 shrink-0" aria-hidden="true" />
|
||
{{ __('customers.fields.personal_identity_code_missing') }}
|
||
</span>
|
||
@endif
|
||
</dd>
|
||
@endif
|
||
</dl>
|
||
|
||
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
|
||
</div>
|
||
|
||
<section aria-labelledby="contacts-heading" class="space-y-4">
|
||
<div class="flex flex-wrap items-center justify-between gap-4">
|
||
<h2 id="contacts-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">
|
||
{{ __('customers.contacts.title') }}
|
||
</h2>
|
||
|
||
<x-button
|
||
:href="route('customers.contacts.create', $customerId)"
|
||
wire:navigate
|
||
icon="plus"
|
||
:text="__('customers.contacts.new')"
|
||
/>
|
||
</div>
|
||
|
||
@if ($this->contacts === [])
|
||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('customers.contacts.empty') }}</p>
|
||
@else
|
||
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-200 dark:divide-zinc-700 dark:border-zinc-700">
|
||
@foreach ($this->contacts as $contact)
|
||
<li wire:key="contact-{{ $contact->id }}" class="flex flex-wrap items-start justify-between gap-4 p-4">
|
||
<div class="min-w-0 space-y-1 text-sm">
|
||
<h3 class="text-base font-medium break-words">{{ $contact->name }}</h3>
|
||
|
||
@if ($contact->title !== null)
|
||
<p>{{ $contact->title }}</p>
|
||
@endif
|
||
|
||
@if ($contact->phone !== null)
|
||
<p>
|
||
<span class="sr-only">{{ __('customers.contacts.fields.phone') }}:</span>
|
||
<x-link :href="'tel:'.preg_replace('/[^\d+]/', '', $contact->phone)">{{ $contact->phone }}</x-link>
|
||
</p>
|
||
@endif
|
||
|
||
@if ($contact->email !== null)
|
||
<p class="break-all">
|
||
<span class="sr-only">{{ __('customers.contacts.fields.email') }}:</span>
|
||
<x-link :href="'mailto:'.$contact->email">{{ $contact->email }}</x-link>
|
||
</p>
|
||
@endif
|
||
|
||
@if ($contact->note !== null)
|
||
<p class="whitespace-pre-line text-zinc-600 dark:text-zinc-300">{{ $contact->note }}</p>
|
||
@endif
|
||
</div>
|
||
|
||
<div class="flex flex-wrap gap-2">
|
||
<x-button
|
||
:href="route('customers.contacts.edit', [$customerId, $contact->id])"
|
||
wire:navigate
|
||
subtle
|
||
color="black"
|
||
sm
|
||
icon="pencil"
|
||
:text="__('customers.edit')"
|
||
:aria-label="__('customers.contacts.edit_named', ['name' => $contact->name])"
|
||
/>
|
||
<x-button
|
||
wire:click="confirmRemoval('contact', {{ $contact->id }})"
|
||
subtle
|
||
color="black"
|
||
sm
|
||
icon="trash"
|
||
:text="__('customers.remove')"
|
||
:aria-label="__('customers.contacts.remove_named', ['name' => $contact->name])"
|
||
/>
|
||
</div>
|
||
</li>
|
||
@endforeach
|
||
</ul>
|
||
@endif
|
||
</section>
|
||
|
||
<section aria-labelledby="billing-units-heading" class="space-y-4">
|
||
<div class="flex flex-wrap items-center justify-between gap-4">
|
||
<h2 id="billing-units-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">
|
||
{{ __('customers.billing_units.title') }}
|
||
</h2>
|
||
|
||
<x-button
|
||
:href="route('customers.billing-units.create', $customerId)"
|
||
wire:navigate
|
||
icon="plus"
|
||
:text="__('customers.billing_units.new')"
|
||
/>
|
||
</div>
|
||
|
||
@if ($this->billingUnits === [])
|
||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('customers.billing_units.empty') }}</p>
|
||
@else
|
||
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-200 dark:divide-zinc-700 dark:border-zinc-700">
|
||
@foreach ($this->billingUnits as $billingUnit)
|
||
<li wire:key="billing-unit-{{ $billingUnit->id }}" class="flex flex-wrap items-start justify-between gap-4 p-4">
|
||
<div class="min-w-0 space-y-1 text-sm">
|
||
<h3 class="text-base font-medium break-words">{{ $billingUnit->name }}</h3>
|
||
|
||
<dl class="grid gap-x-4 gap-y-1 sm:grid-cols-[max-content_1fr]">
|
||
@if ($billingUnit->billingAddress !== null)
|
||
<dt class="font-medium">{{ __('customers.billing_units.fields.billing_address') }}</dt>
|
||
<dd class="whitespace-pre-line">{{ $billingUnit->billingAddress }}</dd>
|
||
@endif
|
||
|
||
@if ($billingUnit->eInvoiceAddress !== null)
|
||
<dt class="font-medium">{{ __('customers.billing_units.fields.e_invoice_address') }}</dt>
|
||
<dd class="break-all">{{ $billingUnit->eInvoiceAddress }}</dd>
|
||
|
||
<dt class="font-medium">{{ __('customers.billing_units.fields.e_invoice_operator') }}</dt>
|
||
<dd class="break-all">{{ $billingUnit->eInvoiceOperator }}</dd>
|
||
@endif
|
||
|
||
@if ($billingUnit->reference !== null)
|
||
<dt class="font-medium">{{ __('customers.billing_units.fields.reference') }}</dt>
|
||
<dd class="break-words">{{ $billingUnit->reference }}</dd>
|
||
@endif
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="flex flex-wrap gap-2">
|
||
<x-button
|
||
:href="route('customers.billing-units.edit', [$customerId, $billingUnit->id])"
|
||
wire:navigate
|
||
subtle
|
||
color="black"
|
||
sm
|
||
icon="pencil"
|
||
:text="__('customers.edit')"
|
||
:aria-label="__('customers.billing_units.edit_named', ['name' => $billingUnit->name])"
|
||
/>
|
||
<x-button
|
||
wire:click="confirmRemoval('billing_unit', {{ $billingUnit->id }})"
|
||
subtle
|
||
color="black"
|
||
sm
|
||
icon="trash"
|
||
:text="__('customers.remove')"
|
||
:aria-label="__('customers.billing_units.remove_named', ['name' => $billingUnit->name])"
|
||
/>
|
||
</div>
|
||
</li>
|
||
@endforeach
|
||
</ul>
|
||
@endif
|
||
</section>
|
||
|
||
<x-confirm-dialog id="remove-dialog" :title="__('customers.removal.title')">
|
||
<p>
|
||
@if ($removalType !== null)
|
||
{{ __('customers.removal.question_'.$removalType, ['name' => $removalName]) }}
|
||
@endif
|
||
</p>
|
||
|
||
<x-slot:actions>
|
||
<x-button wire:click="remove" color="red" icon="trash" :text="__('customers.remove')" />
|
||
</x-slot:actions>
|
||
</x-confirm-dialog>
|
||
</div>
|