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,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;
}
}