113 lines
2.6 KiB
PHP
113 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Sales\Domain\Entities;
|
|
|
|
use App\Modules\Sales\Domain\Exceptions\OrderStepNotCurrent;
|
|
use App\Modules\Sales\Domain\ValueObjects\OrderStep;
|
|
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* A line of a quick sale order: a product with its price at ordering, and,
|
|
* for a product made before it is paid, its production steps and the step
|
|
* now being made. A line without steps is ready from the start.
|
|
*/
|
|
final class OrderLine
|
|
{
|
|
/**
|
|
* @param list<OrderStep> $steps
|
|
*/
|
|
public function __construct(
|
|
private ?int $id,
|
|
private QuickSaleLine $sale,
|
|
private array $steps,
|
|
private ?int $currentStep,
|
|
private ?DateTimeImmutable $madeAt,
|
|
) {}
|
|
|
|
/**
|
|
* @param list<OrderStep> $steps
|
|
*/
|
|
public static function ordered(QuickSaleLine $sale, array $steps, DateTimeImmutable $at): self
|
|
{
|
|
return new self(null, $sale, $steps, $steps === [] ? null : 0, $steps === [] ? $at : null);
|
|
}
|
|
|
|
/**
|
|
* Completes the current step; the line moves to its next step or is made.
|
|
*
|
|
* @return OrderStep|null The next step, null when the line is made.
|
|
*
|
|
* @throws OrderStepNotCurrent
|
|
*/
|
|
public function completeStep(int $position, DateTimeImmutable $at): ?OrderStep
|
|
{
|
|
if ($this->currentStep !== $position) {
|
|
throw new OrderStepNotCurrent;
|
|
}
|
|
|
|
$next = $position + 1;
|
|
|
|
if (isset($this->steps[$next])) {
|
|
$this->currentStep = $next;
|
|
|
|
return $this->steps[$next];
|
|
}
|
|
|
|
$this->currentStep = null;
|
|
$this->madeAt = $at;
|
|
|
|
return null;
|
|
}
|
|
|
|
public function currentStep(): ?OrderStep
|
|
{
|
|
return $this->currentStep === null ? null : $this->steps[$this->currentStep];
|
|
}
|
|
|
|
public function currentStepPosition(): ?int
|
|
{
|
|
return $this->currentStep;
|
|
}
|
|
|
|
public function isMade(): bool
|
|
{
|
|
return $this->madeAt !== null;
|
|
}
|
|
|
|
public function isInProduction(): bool
|
|
{
|
|
return $this->steps !== [] && $this->currentStep !== null;
|
|
}
|
|
|
|
public function hasStarted(): bool
|
|
{
|
|
return $this->steps !== [] && ($this->currentStep === null || $this->currentStep > 0);
|
|
}
|
|
|
|
public function id(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function sale(): QuickSaleLine
|
|
{
|
|
return $this->sale;
|
|
}
|
|
|
|
/**
|
|
* @return list<OrderStep>
|
|
*/
|
|
public function steps(): array
|
|
{
|
|
return $this->steps;
|
|
}
|
|
|
|
public function madeAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->madeAt;
|
|
}
|
|
}
|