Make quick sale products on machines as orders paid when ready

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 17:11:54 +02:00
co-authored by Claude Sonnet 5.5
parent 920fecdeb2
commit 38b8075a9c
58 changed files with 3435 additions and 26 deletions
@@ -18,6 +18,9 @@ use App\Modules\Machines\Application\DTOs\QueueRow;
use App\Modules\Machines\Application\UseCases\MachineMaintenance;
use App\Modules\Machines\Application\UseCases\MachineWorkList;
use App\Modules\Machines\Presentation\Livewire\Concerns\DescribesMaintenance;
use App\Modules\Sales\Application\Contracts\QuickSaleOrderQueries;
use App\Modules\Sales\Application\DTOs\OrderLineSummary;
use App\Modules\Sales\Application\UseCases\QuickSaleOrders;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
use App\Modules\Users\Application\Contracts\Clock;
@@ -97,6 +100,18 @@ new #[Layout('layouts.console')] class extends Component
#[Locked]
public string $refreshMessage = '';
#[Locked]
public ?int $completingEntryId = null;
/**
* Material used per stock item while completing an order's step.
*
* @var array<int, string>
*/
public array $orderUsed = [];
public string $moveToMachineId = '';
public function mount(int $machine): void
{
$this->authorize('machines.use');
@@ -131,6 +146,109 @@ new #[Layout('layouts.console')] class extends Component
return app(MachineQueries::class)->rowsOn($this->machineId, $this->day(), $this->today);
}
/**
* The quick sale order lines on the list, by line id.
*
* @return array<int, OrderLineSummary>
*/
#[Computed]
public function orderLines(): array
{
$ids = array_values(array_filter(array_map(fn (QueueRow $row): ?int => $row->orderLineId, $this->rows)));
return app(QuickSaleOrderQueries::class)->lines($ids);
}
/**
* Machines of the same type to move an order's step to.
*
* @return list<MachineSummary>
*/
#[Computed]
public function sameTypeMachines(): array
{
return array_values(array_filter(
app(MachineQueries::class)->machines(),
fn (MachineSummary $machine): bool => $machine->type === $this->machine->type && $machine->id !== $this->machineId,
));
}
public function startOrderComplete(int $entryId): void
{
$this->authorize('sales.use');
$row = $this->row($entryId);
$line = $this->orderLines[$row->orderLineId] ?? abort(404);
$this->completingEntryId = $entryId;
$this->orderUsed = [];
foreach ($line->currentMaterials as $material) {
$this->orderUsed[$material['stockItemId']] = StockQuantity::inputValue($material['quantityMilli'] * $line->quantity);
}
$this->js("setTimeout(() => document.getElementById('order-complete-{$entryId}')?.focus())");
}
public function changeOrderUsed(int $itemId, int $step): void
{
$current = StockQuantity::parse($this->orderUsed[$itemId] ?? '0')?->milli ?? 0;
$this->orderUsed[$itemId] = StockQuantity::inputValue(max(0, $current + $step * 1000));
}
public function completeOrder(QuickSaleOrders $orders): void
{
$this->authorize('sales.use');
$row = $this->row((int) $this->completingEntryId);
$line = $this->orderLines[$row->orderLineId] ?? abort(404);
$used = [];
foreach ($this->orderUsed as $itemId => $value) {
try {
$used[(int) $itemId] = StockQuantity::parse($value)?->milli ?? 0;
} catch (DomainException) {
$this->addError('orderUsed.'.$itemId, __('inventory.errors.quantity'));
return;
}
}
try {
$orders->completeStep($line->id, (int) $row->stepPosition, (int) auth()->id(), $used);
} catch (DomainException) {
$this->message = __('sales.orders.errors.step');
$this->completingEntryId = null;
$this->reload();
return;
}
$this->message = __('machines.console.order_done', ['number' => $line->pickupNumber, 'product' => $line->productName]);
$this->completingEntryId = null;
$this->reload();
$this->js("document.getElementById('console-status')?.focus()");
}
public function moveOrder(int $entryId, MachineWorkList $workList): void
{
$this->authorize('machines.use');
$row = $this->row($entryId);
$line = $this->orderLines[$row->orderLineId] ?? abort(404);
try {
$workList->moveOrderLine($entryId, (int) $this->moveToMachineId);
} catch (DomainException) {
$this->addError('moveToMachineId', __('machines.errors.machine'));
return;
}
$this->message = __('machines.console.order_moved', ['number' => $line->pickupNumber, 'machine' => app(MachineQueries::class)->findMachine((int) $this->moveToMachineId)->name ?? '']);
$this->moveToMachineId = '';
$this->reload();
}
#[Computed]
public function selectedRow(): ?QueueRow
{
@@ -455,7 +573,7 @@ new #[Layout('layouts.console')] class extends Component
*/
private function reload(): int
{
unset($this->rows, $this->maintenance, $this->items, $this->selectedRow, $this->machine);
unset($this->rows, $this->maintenance, $this->items, $this->selectedRow, $this->machine, $this->orderLines);
$ids = array_map(fn (QueueRow $row): int => $row->entryId, $this->rows);
$new = count(array_diff($ids, $this->knownEntryIds));
@@ -544,7 +662,61 @@ new #[Layout('layouts.console')] class extends Component
<p class="text-base font-semibold">{{ __('machines.console.next') }}</p>
@endif
@if ($row->isOrder())
<p class="font-semibold">{{ __('machines.show.order') }}</p>
@php($line = $this->orderLines[$row->orderLineId] ?? null)
<p class="flex items-center gap-2 font-semibold">
<x-icon name="user" class="size-5" aria-hidden="true" />
{{ __('machines.show.order') }}
</p>
@if ($line !== null)
<p class="text-4xl font-bold">{{ __('sales.orders.pickup', ['number' => $line->pickupNumber]) }}</p>
<p class="text-xl font-semibold">{{ $line->quantity }} × {{ $line->productName }}</p>
<p class="text-base">
{{ $line->customerName ?? __('sales.anonymous') }} · {{ __('machines.console.ordered_at', ['time' => substr($line->orderedAt, 11)]) }}
@if (count($line->stepMachineIds) > 1)
· {{ __('machines.console.order_step', ['step' => (int) $row->stepPosition + 1, 'steps' => count($line->stepMachineIds)]) }}
@endif
</p>
@if (! $nextDay)
@if ($completingEntryId === $row->entryId)
@php($items = app(\App\Modules\Inventory\Application\Contracts\StockQueries::class)->itemsByIds(array_map(fn ($material) => $material['stockItemId'], $line->currentMaterials)))
<form wire:submit="completeOrder" class="space-y-3 rounded-lg border border-zinc-300 p-3 dark:border-zinc-600" novalidate>
<p id="order-complete-{{ $row->entryId }}" tabindex="-1" class="font-semibold focus:outline-none">{{ __('machines.console.order_materials') }}</p>
@foreach ($line->currentMaterials as $material)
@php($item = $items[$material['stockItemId']] ?? null)
<div wire:key="order-material-{{ $material['stockItemId'] }}" class="flex flex-wrap items-end gap-2">
<button type="button" wire:click="changeOrderUsed({{ $material['stockItemId'] }}, -1)" class="{{ $secondary }} text-2xl" aria-label="{{ __('machines.console.less_of', ['item' => $item->name ?? '']) }}">−</button>
<div class="w-40">
<x-input id="order-used-{{ $material['stockItemId'] }}" wire:model="orderUsed.{{ $material['stockItemId'] }}" inputmode="decimal" :label="($item->name ?? '').' ('.__('inventory.units.'.($item->unit->value ?? 'piece')).')'" autocomplete="off" />
</div>
<button type="button" wire:click="changeOrderUsed({{ $material['stockItemId'] }}, 1)" class="{{ $secondary }} text-2xl" aria-label="{{ __('machines.console.more_of', ['item' => $item->name ?? '']) }}">+</button>
</div>
@endforeach
<p class="text-base">{{ __('sales.orders.complete_hint') }}</p>
<button type="submit" class="{{ $primary }} w-full text-xl">{{ __('machines.console.confirm_order') }}</button>
</form>
@else
<div class="flex flex-wrap items-end gap-2">
<button type="button" wire:click="startOrderComplete({{ $row->entryId }})" class="{{ $primary }} text-xl" aria-label="{{ __('machines.console.order_finish_named', ['number' => $line->pickupNumber]) }}">
<x-icon name="check" class="size-6" aria-hidden="true" />
{{ __('machines.console.finish') }}
</button>
@if ($this->sameTypeMachines !== [])
<form wire:submit="moveOrder({{ $row->entryId }})" class="flex flex-wrap items-end gap-2">
<div class="min-w-40">
<x-select.native id="move-order-{{ $row->entryId }}" wire:model="moveToMachineId" :label="__('machines.console.move_to')" :aria-label="__('machines.console.move_to_named', ['number' => $line->pickupNumber])">
<option value="">{{ __('machines.task.choose_machine') }}</option>
@foreach ($this->sameTypeMachines as $other)
<option value="{{ $other->id }}">{{ $other->name }}</option>
@endforeach
</x-select.native>
</div>
<button type="submit" class="{{ $secondary }}">{{ __('machines.console.move') }}</button>
</form>
@endif
</div>
@endif
@endif
@endif
@elseif ($task !== null)
<p class="text-xl font-semibold break-words">{{ $task->title }}</p>
<p class="text-base">{{ $task->jobNumber }} {{ $task->jobName }} · {{ $task->customerName }}</p>