Change a quick sale order before making starts

An unpaid order whose lines have no completed step can be reopened in the
cart from the open orders list; saving replaces its lines with current
prices and processes, moves the machine work list rows and keeps the
pickup number. Keyboard browser tests cover changing and cancelling an
order and moving production steps.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-29 09:11:36 +02:00
co-authored by Claude Sonnet 5.5
parent acf87aa3cd
commit 701f05780f
13 changed files with 364 additions and 19 deletions
@@ -29,6 +29,7 @@ use App\Modules\Sales\Domain\Exceptions\ProductNotSellable;
use App\Modules\Sales\Domain\Exceptions\QuickSaleCustomerNotFound;
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderClosed;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderInProduction;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderNotFound;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderPaid;
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
@@ -72,6 +73,70 @@ final class QuickSaleOrders
* @throws OrderHasNoProduction
*/
public function send(int $sellerId, OrderData $data): OrderResult
{
$lines = $this->lines($data);
$now = $this->clock->now();
$today = $this->clock->today();
return $this->transactions->run(function () use ($sellerId, $data, $lines, $now, $today): OrderResult {
$order = QuickSaleOrder::place($today, $this->orders->nextPickupNumber($today), $now, $sellerId, $data->customerId, $data->customerName, $lines);
$order = $this->orders->save($order);
$this->enqueueFirstSteps($order);
return $this->result($order);
});
}
/**
* Changes an order's products and customer before making has started
* on any line (spec 19.7): the lines are replaced with the products'
* current prices and processes, and the work lists follow. The pickup
* number stays.
*
* @throws QuickSaleOrderNotFound
* @throws QuickSaleOrderClosed
* @throws QuickSaleOrderInProduction
* @throws ProductNotSellable
* @throws InvalidQuantity
* @throws QuickSaleEmpty
* @throws QuickSaleCustomerNotFound
* @throws OrderHasNoProduction
*/
public function change(int $orderId, OrderData $data): OrderResult
{
$lines = $this->lines($data);
return $this->transactions->run(function () use ($orderId, $data, $lines): OrderResult {
$order = $this->orders->lockById($orderId) ?? throw new QuickSaleOrderNotFound;
$oldLines = $order->lines();
$order->change($data->customerId, $data->customerName, $lines);
foreach ($oldLines as $line) {
if ($line->isInProduction()) {
$this->queue->remove((int) $line->id());
}
}
$order = $this->orders->save($order);
$this->enqueueFirstSteps($order);
return $this->result($order);
});
}
/**
* New order lines of the products with their current prices and
* production processes.
*
* @return list<OrderLine>
*
* @throws ProductNotSellable
* @throws InvalidQuantity
* @throws QuickSaleCustomerNotFound
* @throws OrderHasNoProduction
*/
private function lines(OrderData $data): array
{
$sellable = [];
foreach ($this->products->sellable() as $product) {
@@ -89,7 +154,6 @@ final class QuickSaleOrders
}
$now = $this->clock->now();
$today = $this->clock->today();
$lines = [];
foreach ($data->quantities as $productId => $quantity) {
@@ -101,22 +165,25 @@ final class QuickSaleOrders
);
}
return $this->transactions->run(function () use ($sellerId, $data, $lines, $now, $today): OrderResult {
$order = QuickSaleOrder::place($today, $this->orders->nextPickupNumber($today), $now, $sellerId, $data->customerId, $data->customerName, $lines);
$order = $this->orders->save($order);
return $lines;
}
foreach ($order->lines() as $line) {
$step = $line->currentStep();
private function enqueueFirstSteps(QuickSaleOrder $order): void
{
foreach ($order->lines() as $line) {
$step = $line->currentStep();
if ($step !== null && $step->machineId !== null) {
$this->queue->enqueue((int) $line->id(), 0, $step->machineId, $today);
}
if ($step !== null && $step->machineId !== null) {
$this->queue->enqueue((int) $line->id(), 0, $step->machineId, $order->orderedOn());
}
}
}
$total = array_sum(array_map(fn (OrderLine $line): int => $line->sale()->totalCents(), $order->lines()));
private function result(QuickSaleOrder $order): OrderResult
{
$total = array_sum(array_map(fn (OrderLine $line): int => $line->sale()->totalCents(), $order->lines()));
return new OrderResult((int) $order->id(), $order->pickupNumber(), $total);
});
return new OrderResult((int) $order->id(), $order->pickupNumber(), $total);
}
/**