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
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\Contracts;
use App\Modules\Machines\Application\DTOs\ProductionStepSummary;
/**
* Production processes of quick sale products (spec 19.7). The Sales
* module reads them through this contract when an order is sent.
*/
interface ProductionProcessQueries
{
/**
* @return list<ProductionStepSummary> In order; empty without a process.
*/
public function processOf(int $productId): array;
/**
* @param list<int> $productIds
* @return array<int, list<ProductionStepSummary>> By product id; products without a process are left out.
*/
public function processesOf(array $productIds): array;
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A production step as entered: the machine (null: by hand), the duration
* per piece in hours as typed ("0,25"), the work type its hours go to, and
* materials per piece with quantities as typed.
*/
final readonly class ProductionStepData
{
/**
* @param list<array{stockItemId: int, quantity: string}> $materials
*/
public function __construct(
public ?int $machineId,
public string $duration,
public int $workTypeId,
public array $materials = [],
) {}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A step of a product's production process as stored. $position counts
* from 0. Materials are per piece, quantities in thousandths of the item's
* unit (the unit key in $unit).
*/
final readonly class ProductionStepSummary
{
/**
* @param list<array{stockItemId: int, itemName: string, unit: string, quantityMilli: int}> $materials
*/
public function __construct(
public int $position,
public ?int $machineId,
public ?string $machineName,
public int $durationQuarters,
public int $workTypeId,
public string $workTypeName,
public array $materials,
) {}
}
@@ -153,6 +153,82 @@ final class MachineWorkList
}
}
/**
* Puts a step of a quick sale order line on the machine's list for the
* day (spec 19.7); customer waiting rows are listed first.
*/
public function enqueueOrderStep(int $orderLineId, int $stepPosition, int $machineId, DateTimeImmutable $day): void
{
$this->transactions->run(function () use ($orderLineId, $stepPosition, $machineId, $day): void {
$open = $this->queue->openEntryForOrderLine($orderLineId);
if ($open !== null) {
$this->queue->delete($open);
}
$this->queue->save(QueueEntry::forOrderLine($machineId, $orderLineId, $stepPosition, $day, 0));
});
}
/**
* Closes an order line's open row as made.
*/
public function completeOrderLine(int $orderLineId, ?int $actorUserId): void
{
$this->transactions->run(function () use ($orderLineId, $actorUserId): void {
$entry = $this->queue->openEntryForOrderLine($orderLineId);
if ($entry !== null) {
$entry->complete($actorUserId, $this->clock->now());
$this->queue->save($entry);
}
});
}
/**
* Takes an order line's open row off its list (a cancelled order).
*/
public function removeOrderLine(int $orderLineId): void
{
$this->transactions->run(function () use ($orderLineId): void {
$entry = $this->queue->openEntryForOrderLine($orderLineId);
if ($entry !== null) {
$this->queue->delete($entry);
}
});
}
/**
* Moves an order line's open row to another machine of the same type.
*
* @throws QueueEntryNotFound
* @throws MachineNotFound
* @throws MachineInactive
* @throws MachineNotForTask when the machine is of another type
*/
public function moveOrderLine(int $entryId, int $machineId): void
{
$this->transactions->run(function () use ($entryId, $machineId): void {
$entry = $this->queue->findById($entryId) ?? throw new QueueEntryNotFound;
if (! $entry->isOrderLine()) {
throw new QueueEntryNotFound;
}
$current = $this->machines->findById($entry->machineId()) ?? throw new MachineNotFound;
$target = $this->machines->findById($machineId) ?? throw new MachineNotFound;
$target->ensureActive();
if ($target->type() !== $current->type()) {
throw new MachineNotForTask;
}
$entry->plan($machineId, $entry->plannedOn(), 0);
$this->queue->save($entry);
});
}
/**
* Closes the open row of a task that was marked done elsewhere.
*/
@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\UseCases;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Machines\Application\DTOs\ProductionStepData;
use App\Modules\Machines\Domain\Contracts\MachineRepository;
use App\Modules\Machines\Domain\Contracts\ProductionProcessRepository;
use App\Modules\Machines\Domain\Exceptions\InvalidProductionStep;
use App\Modules\Machines\Domain\Exceptions\InvalidReading;
use App\Modules\Machines\Domain\Exceptions\ProductionMachineNotAllowed;
use App\Modules\Machines\Domain\ValueObjects\ProductionStep;
use App\Modules\Machines\Domain\ValueObjects\Reading;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Saves a consumer product's production process (management, spec 19.7).
* A step's machine must be in use and belong to the service unit of the
* product's work type (any unit using stock when the product has none).
* Orders already sent keep their own copy of the process.
*/
final class SaveProductionProcess
{
public function __construct(
private ProductionProcessRepository $processes,
private MachineRepository $machines,
private PriceListQueries $priceList,
private CatalogQueries $catalog,
private TransactionManager $transactions,
) {}
/**
* @param list<ProductionStepData> $steps
*
* @throws InvalidProductionStep with nothing saved
* @throws ProductionMachineNotAllowed
*/
public function execute(int $productId, array $steps): void
{
$product = $this->priceList->findProduct($productId) ?? throw new InvalidProductionStep;
$allowedUnits = $this->allowedUnits($product->workTypeId);
$parsed = [];
foreach ($steps as $step) {
if ($this->catalog->findWorkType($step->workTypeId) === null) {
throw new InvalidProductionStep;
}
if ($step->machineId !== null) {
$machine = $this->machines->findById($step->machineId);
if ($machine === null || ! $machine->isActive() || ! $machine->servesAnyOf($allowedUnits)) {
throw new ProductionMachineNotAllowed;
}
}
$parsed[] = new ProductionStep($step->machineId, $this->quarters($step->duration), $step->workTypeId, $this->materials($step->materials));
}
$this->transactions->run(fn () => $this->processes->replace($productId, $parsed));
}
/**
* @return list<int>
*/
private function allowedUnits(?int $workTypeId): array
{
$stockUnits = $this->catalog->stockServiceUnitIds();
$workType = $workTypeId === null ? null : $this->catalog->findWorkType($workTypeId);
$unit = $workType === null ? null : $this->catalog->findServiceUnit($workType->serviceUnitId);
return $unit === null || $unit->isShared ? $stockUnits : array_values(array_intersect([$unit->id], $stockUnits));
}
/**
* @throws InvalidProductionStep
*/
private function quarters(string $duration): int
{
$duration = str_replace(',', '.', trim($duration));
if ($duration === '') {
return 0;
}
if (! is_numeric($duration) || fmod((float) $duration * 4, 1.0) !== 0.0) {
throw new InvalidProductionStep;
}
return (int) round((float) $duration * 4);
}
/**
* @param list<array{stockItemId: int, quantity: string}> $materials
* @return list<array{stockItemId: int, quantityMilli: int}>
*
* @throws InvalidProductionStep
*/
private function materials(array $materials): array
{
$parsed = [];
foreach ($materials as $material) {
try {
$parsed[] = ['stockItemId' => $material['stockItemId'], 'quantityMilli' => Reading::parse($material['quantity']) ?? 0];
} catch (InvalidReading) {
throw new InvalidProductionStep;
}
}
return $parsed;
}
}