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
@@ -3,7 +3,11 @@
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Sales\Application\Contracts\QuickSaleProducts;
use App\Modules\Sales\Application\Contracts\ProductionProcesses;
use App\Modules\Sales\Application\DTOs\OrderData;
use App\Modules\Sales\Application\DTOs\SaleData;
use App\Modules\Sales\Application\UseCases\QuickSaleOrders;
use App\Modules\Sales\Domain\Exceptions\OrderHasNoProduction;
use App\Modules\Sales\Application\DTOs\SellableProduct;
use App\Modules\Sales\Application\UseCases\SellQuickSale;
use App\Modules\Sales\Domain\Enums\PaymentMethod;
@@ -65,6 +69,11 @@ new class extends Component
#[Locked]
public string $hoursMessage = '';
#[Locked]
public ?int $savedPickupNumber = null;
public string $orderCustomerName = '';
public function mount(): void
{
$this->authorize('sales.use');
@@ -223,12 +232,36 @@ new class extends Component
$this->js("document.getElementById('customer-search')?.focus()");
}
public function save(SellQuickSale $sellQuickSale): void
/**
* Products with a production process: an order of any of them is made
* before it is paid (spec 19.7).
*
* @return list<int>
*/
#[Computed]
public function madeProductIds(): array
{
return array_keys(app(ProductionProcesses::class)->stepsOf(array_keys($this->products)));
}
public function isOrder(): bool
{
return array_intersect(array_keys($this->quantities), $this->madeProductIds) !== [];
}
public function save(SellQuickSale $sellQuickSale, QuickSaleOrders $orders): void
{
$this->authorize('sales.use');
$this->savedMessage = '';
$this->hoursMessage = '';
$this->savedSaleId = null;
$this->savedPickupNumber = null;
if ($this->isOrder()) {
$this->sendOrder($orders);
return;
}
$this->validate([
'paymentMethod' => ['required', Rule::enum(PaymentMethod::class)],
@@ -293,6 +326,43 @@ new class extends Component
return $this->view()->title(__('sales.sell.title'));
}
/**
* Sends the sale to production as an order with the day's pickup number;
* it is paid on the open orders list once made (or in advance).
*/
private function sendOrder(QuickSaleOrders $orders): void
{
$this->validate(['orderCustomerName' => ['nullable', 'string', 'max:150']]);
try {
$result = $orders->send((int) auth()->id(), new OrderData($this->quantities, $this->customerId, $this->orderCustomerName));
} catch (QuickSaleEmpty|InvalidQuantity|OrderHasNoProduction) {
$this->addError('quantities', __('sales.errors.empty'));
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;
}
$this->savedPickupNumber = $result->pickupNumber;
$this->savedMessage = __('sales.orders.sent', ['number' => $result->pickupNumber, 'total' => $this->money($result->totalCents)]);
$this->reset('quantities', 'paymentMethod', 'customerSearch', 'customerId', 'customerName', 'cartMessage', 'receiptEmail', 'orderCustomerName');
$this->forgetLines();
$this->dispatch('quick-sale-orders-changed');
$this->js("document.getElementById('sale-saved')?.focus()");
}
private function changed(int $productId): void
{
$this->forgetLines();
@@ -352,6 +422,9 @@ new class extends Component
<x-icon name="check-circle" class="size-5 shrink-0" aria-hidden="true" />
{{ $savedMessage }}
</p>
@if ($savedPickupNumber !== null)
<p class="text-4xl font-bold" aria-hidden="true">{{ $savedPickupNumber }}</p>
@endif
@if ($hoursMessage !== '')
<p>{{ $hoursMessage }}</p>
@endif
@@ -397,6 +470,12 @@ new class extends Component
>
<span class="font-medium break-words">{{ $product->name }}</span>
<span class="text-sm text-zinc-700 dark:text-zinc-300">{{ $this->money($product->grossCents) }} / {{ __('catalog.units.'.$product->unit) }}</span>
@if (in_array($product->id, $this->madeProductIds, true))
<span class="inline-flex items-center gap-1 text-xs">
<x-icon name="printer" class="size-4" aria-hidden="true" />
{{ __('sales.orders.made') }}
</span>
@endif
</button>
</li>
@endforeach
@@ -457,15 +536,25 @@ new class extends Component
</p>
</section>
@php($isOrder = $this->isOrder())
<form wire:submit="save" class="space-y-6" novalidate>
<x-radio.group
wire:model="paymentMethod"
:label="__('sales.sell.payment_method')"
:options="[
['label' => __('sales.payment_methods.cash'), 'value' => PaymentMethod::Cash->value],
['label' => __('sales.payment_methods.card'), 'value' => PaymentMethod::Card->value],
]"
/>
@if ($isOrder)
<p class="flex items-start gap-2 rounded-lg border border-zinc-400 p-3 text-sm dark:border-zinc-500">
<x-icon name="printer" class="size-5 shrink-0" aria-hidden="true" />
{{ __('sales.orders.order_note') }}
</p>
<x-input id="order-customer-name" wire:model="orderCustomerName" :label="__('sales.orders.customer_name')" :hint="__('sales.orders.customer_name_hint')" autocomplete="off" maxlength="150" />
@else
<x-radio.group
wire:model="paymentMethod"
:label="__('sales.sell.payment_method')"
:options="[
['label' => __('sales.payment_methods.cash'), 'value' => PaymentMethod::Cash->value],
['label' => __('sales.payment_methods.card'), 'value' => PaymentMethod::Card->value],
]"
/>
@endif
<div class="space-y-2">
@if ($customerId !== null)
@@ -516,16 +605,20 @@ new class extends Component
@endif
</div>
<x-input
wire:model="receiptEmail"
type="email"
inputmode="email"
autocomplete="off"
:label="__('sales.sell.receipt_email')"
:hint="__('sales.sell.receipt_email_hint')"
/>
@unless ($isOrder)
<x-input
wire:model="receiptEmail"
type="email"
inputmode="email"
autocomplete="off"
:label="__('sales.sell.receipt_email')"
:hint="__('sales.sell.receipt_email_hint')"
/>
@endunless
<x-button submit lg class="w-full" :text="__('sales.sell.save')" />
<x-button submit lg class="w-full" :text="$isOrder ? __('sales.orders.send') : __('sales.sell.save')" />
</form>
@endif
<livewire:sales::orders />
</div>