*/ #[Locked] public array $quantities = []; public string $paymentMethod = 'cash'; public string $customerSearch = ''; #[Locked] public ?int $customerId = null; #[Locked] public string $customerName = ''; #[Locked] public string $cartMessage = ''; #[Locked] public ?int $savedSaleId = null; #[Locked] public string $savedMessage = ''; #[Locked] public string $hoursMessage = ''; public function mount(): void { $this->authorize('sales.use'); } /** * @return array By product id, in price list order. */ #[Computed] public function products(): array { $products = []; foreach (app(QuickSaleProducts::class)->sellable() as $product) { $products[$product->id] = $product; } return $products; } /** * The sale's lines with their products, in the order added. * * @return list */ #[Computed] public function lines(): array { $lines = []; foreach ($this->quantities as $productId => $quantity) { if (isset($this->products[$productId])) { $lines[] = ['product' => $this->products[$productId], 'quantity' => $quantity]; } } return $lines; } #[Computed] public function totalCents(): int { return array_sum(array_map(fn (array $line): int => $line['quantity'] * $line['product']->grossCents, $this->lines)); } /** * @return list */ #[Computed] public function customerResults(): array { $term = trim($this->customerSearch); return mb_strlen($term) < 2 ? [] : app(CustomerQueries::class)->search($term, 8); } public function addProduct(int $productId): void { $this->authorize('sales.use'); if (! isset($this->products[$productId])) { return; } $this->quantities[$productId] = min(QuickSaleLine::MAX_QUANTITY, ($this->quantities[$productId] ?? 0) + 1); $this->changed($productId); } public function decreaseProduct(int $productId): void { $this->authorize('sales.use'); if (! isset($this->quantities[$productId])) { return; } if ($this->quantities[$productId] <= 1) { $this->removeProduct($productId); return; } $this->quantities[$productId]--; $this->changed($productId); } /** * Sets a line's quantity typed in its field: 0 removes the line, and an * invalid value keeps the old quantity with an error. */ public function setQuantity(int $productId, string $value): void { $this->authorize('sales.use'); if (! isset($this->quantities[$productId])) { return; } $value = trim($value); if (preg_match('/^\d{1,3}$/', $value) !== 1) { $this->addError('quantities', __('sales.errors.quantity')); $this->showQuantity($productId, $this->quantities[$productId]); return; } $quantity = (int) $value; if ($quantity === 0) { $this->removeProduct($productId); return; } $this->quantities[$productId] = min(QuickSaleLine::MAX_QUANTITY, $quantity); $this->changed($productId); $this->showQuantity($productId, $this->quantities[$productId]); } public function removeProduct(int $productId): void { $this->authorize('sales.use'); $name = $this->products[$productId]->name ?? ''; unset($this->quantities[$productId]); $this->forgetLines(); $this->cartMessage = __('sales.sell.removed', ['product' => $name, 'total' => $this->money($this->totalCents)]); $this->js("document.getElementById('cart-heading')?.focus()"); } public function selectCustomer(int $customerId): void { $this->authorize('sales.use'); $customer = app(CustomerQueries::class)->findSummary($customerId); if ($customer !== null) { $this->customerId = $customer->id; $this->customerName = $customer->name; $this->customerSearch = ''; $this->js("document.getElementById('change-customer')?.focus()"); } } public function clearCustomer(): void { $this->customerId = null; $this->customerName = ''; $this->js("document.getElementById('customer-search')?.focus()"); } public function save(SellQuickSale $sellQuickSale): void { $this->authorize('sales.use'); $this->savedMessage = ''; $this->hoursMessage = ''; $this->savedSaleId = null; $this->validate(['paymentMethod' => ['required', Rule::enum(PaymentMethod::class)]]); try { $result = $sellQuickSale->execute((int) auth()->id(), new SaleData($this->quantities, $this->paymentMethod, $this->customerId)); } catch (QuickSaleEmpty) { $this->addError('quantities', __('sales.errors.empty')); return; } catch (InvalidQuantity) { $this->addError('quantities', __('sales.errors.quantity')); return; } catch (ProductNotSellable $exception) { unset($this->quantities[$exception->id], $this->products); $this->forgetLines(); $this->addError('quantities', __('sales.errors.product')); return; } catch (QuickSaleCustomerNotFound) { $this->addError('customerSearch', __('sales.errors.customer')); $this->clearCustomer(); return; } catch (CashDayClosed) { $this->addError('sale', __('sales.errors.day_closed')); return; } $this->savedSaleId = $result->saleId; $this->savedMessage = __('sales.sell.saved', [ 'number' => $result->saleId, 'total' => $this->money($result->totalCents), 'method' => __('sales.payment_methods.'.$this->paymentMethod), ]); $this->hoursMessage = trim( ($result->bookedQuarters > 0 ? __('sales.sell.hours_booked', ['hours' => $this->hours($result->bookedQuarters)]) : '') .' '.($result->unbookedQuarters > 0 ? __('sales.sell.hours_not_booked', ['hours' => $this->hours($result->unbookedQuarters)]) : ''), ); $this->reset('quantities', 'paymentMethod', 'customerSearch', 'customerId', 'customerName', 'cartMessage'); $this->forgetLines(); $this->js("document.getElementById('sale-saved')?.focus()"); } public function render(): mixed { return $this->view()->title(__('sales.sell.title')); } private function changed(int $productId): void { $this->forgetLines(); $this->resetErrorBag('quantities'); $this->cartMessage = __('sales.sell.changed', [ 'product' => $this->products[$productId]->name, 'quantity' => $this->quantities[$productId], 'total' => $this->money($this->totalCents), ]); } /** * Puts the saved quantity back in the field, which is not bound to a * property and so keeps what was typed (e.g. "03" or an invalid value). */ private function showQuantity(int $productId, int $quantity): void { $this->js("const field = document.getElementById('quantity-{$productId}'); if (field) { field.value = '{$quantity}'; }"); } private function forgetLines(): void { unset($this->lines, $this->totalCents); } }; ?>

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

{{ __('sales.sell.intro') }}

{{ __('sales.sell.to_list') }}

@if ($savedMessage !== '')

@if ($hoursMessage !== '')

{{ $hoursMessage }}

@endif @if ($savedSaleId !== null)

@endif
@endif
@error('sale') @enderror @if ($this->products === [])

{{ __('sales.sell.no_products') }}

@else

{{ __('sales.sell.products') }}

{{ __('sales.sell.products_help') }}

    @foreach ($this->products as $product)
  • @endforeach

{{ __('sales.sell.cart') }}

{{ $cartMessage }}

@error('quantities') @enderror @if ($this->lines === [])

{{ __('sales.sell.cart_empty') }}

@else
    @foreach ($this->lines as $line) @php($product = $line['product'])
  • {{ $product->name }}

    {{ __('sales.sell.line', ['quantity' => $line['quantity'], 'price' => $this->money($product->grossCents), 'total' => $this->money($line['quantity'] * $product->grossCents)]) }}

    @if ($product->durationQuarters > 0 && $product->workTypeId !== null)

    {{ __('sales.sell.line_hours', ['hours' => $this->hours($product->durationQuarters * $line['quantity'])]) }}

    @endif
  • @endforeach
@endif

{{ __('sales.sell.total', ['total' => $this->money($this->totalCents)]) }}

@if ($customerId !== null)

{{ __('sales.sell.customer') }}

{{ $customerName }}
@else @if (mb_strlen(trim($customerSearch)) >= 2)

{{ trans_choice('sales.sell.customer_results', count($this->customerResults), ['count' => count($this->customerResults)]) }}

    @foreach ($this->customerResults as $customer)
  • @endforeach
@endif @endif
@endif