*/ public array $used = []; #[Locked] public ?int $payingOrderId = null; public string $paymentMethod = 'cash'; public string $receiptEmail = ''; #[Locked] public ?int $cancelOrderId = null; /** * Orders already seen as ready, to announce newly ready ones. * * @var list */ #[Locked] public array $knownReady = []; #[Locked] public string $message = ''; #[Locked] public ?int $paidSaleId = null; public function mount(): void { $this->authorize('sales.use'); $this->knownReady = $this->readyIds(); } /** * @return list */ #[Computed] public function orders(): array { return app(QuickSaleOrderQueries::class)->openOrders(); } /** * @return array */ #[Computed] public function machineNames(): array { $names = []; foreach (app(MachineQueries::class)->machines(includeInactive: true) as $machine) { $names[$machine->id] = $machine->name; } return $names; } #[On('quick-sale-orders-changed')] public function reloadOrders(): void { unset($this->orders); $this->knownReady = array_values(array_unique([...$this->knownReady, ...$this->readyIds()])); } public function refreshList(): void { $this->authorize('sales.use'); unset($this->orders); $ready = $this->readyIds(); $new = array_diff($ready, $this->knownReady); if ($new !== []) { $numbers = array_map(fn (OrderSummary $order): int => $order->pickupNumber, array_filter($this->orders, fn (OrderSummary $order): bool => in_array($order->id, $new, true))); $this->message = trans_choice('sales.orders.became_ready', count($numbers), ['numbers' => implode(', ', $numbers)]); } $this->knownReady = array_values(array_unique([...$this->knownReady, ...$ready])); } public function refresh(Clock $clock): void { $this->refreshList(); if ($this->message === '') { $this->message = __('sales.orders.refreshed', ['time' => $clock->now()->setTimezone(new DateTimeZone((string) config('app.business_timezone')))->format('H.i')]); } } public function startComplete(int $lineId): void { $this->authorize('sales.use'); $line = $this->line($lineId); $this->completingLineId = $lineId; $this->used = []; foreach ($line->currentMaterials as $material) { $this->used[$material['stockItemId']] = StockQuantity::inputValue($material['quantityMilli'] * $line->quantity); } $this->js("setTimeout(() => document.getElementById('complete-line-{$lineId}')?.focus())"); } public function complete(QuickSaleOrders $orders): void { $this->authorize('sales.use'); $line = $this->line((int) $this->completingLineId); $usedMilli = []; foreach ($this->used as $itemId => $value) { try { $usedMilli[(int) $itemId] = StockQuantity::parse($value)?->milli ?? 0; } catch (DomainException) { $this->addError('used.'.$itemId, __('inventory.errors.quantity')); return; } } try { $made = $orders->completeStep($line->id, (int) $line->currentStep, (int) auth()->id(), $usedMilli); } catch (DomainException) { $this->message = __('sales.orders.errors.step'); $this->completingLineId = null; unset($this->orders); return; } $this->message = $made ? __('sales.orders.line_made', ['number' => $line->pickupNumber, 'product' => $line->productName]) : __('sales.orders.step_done', ['number' => $line->pickupNumber, 'product' => $line->productName]); $this->completingLineId = null; $this->reloadOrders(); $this->js("document.getElementById('orders-status')?.focus()"); } public function startPay(int $orderId): void { $this->authorize('sales.use'); $order = $this->order($orderId); $this->payingOrderId = $order->id; $this->paymentMethod = 'cash'; $this->receiptEmail = ''; $this->resetErrorBag(); $this->js("setTimeout(() => document.getElementById('pay-order-{$orderId}')?.focus())"); } public function pay(QuickSaleOrders $orders): void { $this->authorize('sales.use'); $order = $this->order((int) $this->payingOrderId); $this->validate([ 'paymentMethod' => ['required', Rule::enum(PaymentMethod::class)], 'receiptEmail' => ['nullable', 'string', 'email', 'max:255'], ], attributes: ['receiptEmail' => __('sales.sell.receipt_email')]); try { $result = $orders->pay($order->id, (int) auth()->id(), $this->paymentMethod, $this->receiptEmail); } catch (InvalidEmailAddress) { $this->addError('receiptEmail', __('sales.errors.receipt_email')); return; } catch (CashDayClosed) { $this->addError('paymentMethod', __('sales.errors.day_closed')); return; } catch (DomainException) { $this->message = __('sales.orders.errors.closed'); $this->payingOrderId = null; unset($this->orders); return; } $this->paidSaleId = $result->saleId; $this->message = __('sales.orders.paid', ['number' => $order->pickupNumber, 'total' => $this->money($result->totalCents), 'method' => __('sales.payment_methods.'.$this->paymentMethod)]) .($result->receiptEmail === null ? '' : ' '.__('sales.sell.receipt_queued', ['email' => $result->receiptEmail])); $this->payingOrderId = null; unset($this->orders); $this->js("document.getElementById('orders-status')?.focus()"); } public function confirmCancel(int $orderId): void { $this->authorize('sales.use'); $this->cancelOrderId = $this->order($orderId)->id; $this->js("document.getElementById('cancel-order-dialog').showModal()"); } public function cancel(QuickSaleOrders $orders): void { $this->authorize('sales.use'); $order = $this->order((int) $this->cancelOrderId); try { $orders->cancel($order->id, (int) auth()->id()); $this->message = __('sales.orders.cancelled', ['number' => $order->pickupNumber]); } catch (DomainException) { $this->message = __('sales.orders.errors.closed'); } $this->cancelOrderId = null; unset($this->orders); $this->js("document.getElementById('cancel-order-dialog').close(); document.getElementById('orders-status').focus()"); } /** * @return array */ public function itemsOf(OrderLineSummary $line): array { return app(StockQueries::class)->itemsByIds(array_map(fn (array $material): int => $material['stockItemId'], $line->currentMaterials)); } public function lineState(OrderLineSummary $line): string { if ($line->isMade) { return __('sales.orders.line_ready'); } if (! $line->hasProduction()) { return __('sales.orders.line_no_production'); } $machineId = $line->currentMachineId(); return __('sales.orders.line_step', [ 'step' => (int) $line->currentStep + 1, 'steps' => count($line->stepMachineIds), 'where' => $machineId === null ? __('sales.orders.by_hand') : ($this->machineNames[$machineId] ?? ''), ]); } /** * @return list */ private function readyIds(): array { return array_values(array_map( fn (OrderSummary $order): int => $order->id, array_filter($this->orders, fn (OrderSummary $order): bool => $order->status->value === 'ready'), )); } private function order(int $orderId): OrderSummary { return collect($this->orders)->firstWhere('id', $orderId) ?? abort(404); } private function line(int $lineId): OrderLineSummary { foreach ($this->orders as $order) { foreach ($order->lines as $line) { if ($line->id === $lineId) { return $line; } } } abort(404); } }; ?>

{{ __('sales.orders.title') }}

{{ $message }}

@if ($paidSaleId !== null)

@endif
@if ($this->orders === [])

{{ __('sales.orders.none') }}

@else
    @foreach ($this->orders as $order)
  • {{ __('sales.orders.pickup', ['number' => $order->pickupNumber]) }}

    {{ $order->customerName ?? __('sales.anonymous') }} · {{ $this->dateLabel($order->orderedAt) }} · {{ $this->money($order->totalCents) }}

      @foreach ($order->lines as $line)
    • {{ $line->quantity }} × {{ $line->productName }} · {{ $this->lineState($line) }}

      @if (! $line->isMade && $line->hasProduction()) @if ($completingLineId === $line->id)

      {{ __('sales.orders.complete_title', ['product' => $line->productName]) }}

      @php($items = $this->itemsOf($line)) @foreach ($line->currentMaterials as $material) @php($item = $items[$material['stockItemId']] ?? null)
      @endforeach

      {{ __('sales.orders.complete_hint') }}

      @else
      @if ($line->currentMachineId() !== null) {{ __('sales.orders.open_console', ['machine' => $this->machineNames[$line->currentMachineId()] ?? '']) }} @endif
      @endif @endif
    • @endforeach
    @unless ($order->isPaid()) @if ($payingOrderId === $order->id)

    {{ __('sales.orders.pay_title', ['number' => $order->pickupNumber, 'total' => $this->money($order->totalCents)]) }}

    @else
    @endif @endunless
  • @endforeach
@endif

{{ __('sales.orders.cancel_text') }}