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:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user