Files
Toiminnanohjaus/app/Modules/Customers/Presentation/Livewire/⚡show.blade.php
T
petriandClaude Opus 5.5 b4f2f1cfaa 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]>
2026-11-24 18:13:11 +02:00

431 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\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;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* A customer's page: details, contacts and billing units. A customer with
* nothing linked to it can be removed here.
*/
new class extends Component
{
public const string REMOVE_DIALOG = 'remove-dialog';
#[Locked]
public int $customerId;
/**
* What the remove dialog asks about: 'customer', 'contact' or 'billing_unit'.
*/
#[Locked]
public ?string $removalType = null;
#[Locked]
public ?int $removalId = null;
#[Locked]
public string $removalName = '';
public string $statusMessage = '';
protected CustomerQueries $customerQueries;
protected CustomerRemoval $customerRemoval;
public function boot(CustomerQueries $customerQueries, CustomerRemoval $customerRemoval): void
{
$this->customerQueries = $customerQueries;
$this->customerRemoval = $customerRemoval;
}
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;
}
/**
* 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>
*/
#[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 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,
};
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, RemoveCustomer $removeCustomer): void
{
$this->authorize('customers.use');
if ($this->removalType === null || $this->removalId === null) {
return;
}
if ($this->removalType === 'customer') {
$this->removeCustomer($removeCustomer);
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');
}
/**
* 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);
}
};
?>
<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 id="customer-heading" tabindex="-1" class="text-2xl font-semibold break-words focus:outline-none">{{ $this->customer->name }}</h1>
<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'))
<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>