Files
Toiminnanohjaus/app/Modules/Sales/Domain/Entities/QuickSaleOrder.php
T
2026-11-26 17:11:54 +02:00

193 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Entities;
use App\Modules\Sales\Domain\Enums\OrderStatus;
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderClosed;
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderPaid;
use DateTimeImmutable;
/**
* A quick sale order (tilaus, spec 19.7): products made on machines before
* the customer pays. It gets the day's running pickup number, is made step
* by step, and is paid as an ordinary quick sale, before or after it is
* ready. It ends when it is both paid and made; an unpaid order can be
* cancelled. It is not tied to a device or a seller.
*/
final class QuickSaleOrder
{
/**
* @param list<OrderLine> $lines
*/
private function __construct(
private ?int $id,
private DateTimeImmutable $orderedOn,
private int $pickupNumber,
private DateTimeImmutable $orderedAt,
private int $userId,
private ?int $customerId,
private ?string $customerName,
private array $lines,
private ?DateTimeImmutable $paidAt,
private ?DateTimeImmutable $cancelledAt,
private ?int $cancelledBy,
) {
if ($lines === []) {
throw new QuickSaleEmpty;
}
}
/**
* @param list<OrderLine> $lines
*
* @throws QuickSaleEmpty
*/
public static function place(DateTimeImmutable $orderedOn, int $pickupNumber, DateTimeImmutable $orderedAt, int $userId, ?int $customerId, ?string $customerName, array $lines): self
{
$customerName = $customerName === null ? null : mb_substr(trim($customerName), 0, 150);
return new self(null, $orderedOn, $pickupNumber, $orderedAt, $userId, $customerId, $customerName === '' ? null : $customerName, $lines, null, null, null);
}
/**
* @param list<OrderLine> $lines
*/
public static function reconstitute(int $id, DateTimeImmutable $orderedOn, int $pickupNumber, DateTimeImmutable $orderedAt, int $userId, ?int $customerId, ?string $customerName, array $lines, ?DateTimeImmutable $paidAt, ?DateTimeImmutable $cancelledAt, ?int $cancelledBy): self
{
return new self($id, $orderedOn, $pickupNumber, $orderedAt, $userId, $customerId, $customerName, $lines, $paidAt, $cancelledAt, $cancelledBy);
}
public function status(): OrderStatus
{
return match (true) {
$this->cancelledAt !== null => OrderStatus::Cancelled,
$this->isMade() && $this->paidAt !== null => OrderStatus::Completed,
$this->isMade() => OrderStatus::Ready,
default => OrderStatus::InProduction,
};
}
public function isMade(): bool
{
foreach ($this->lines as $line) {
if (! $line->isMade()) {
return false;
}
}
return true;
}
/**
* @throws QuickSaleOrderClosed
*/
public function ensureOpen(): void
{
if ($this->cancelledAt !== null || $this->status() === OrderStatus::Completed) {
throw new QuickSaleOrderClosed;
}
}
/**
* @throws QuickSaleOrderClosed
*/
public function pay(DateTimeImmutable $at): void
{
if ($this->paidAt !== null || $this->cancelledAt !== null) {
throw new QuickSaleOrderClosed;
}
$this->paidAt = $at;
}
/**
* @throws QuickSaleOrderPaid
* @throws QuickSaleOrderClosed
*/
public function cancel(int $userId, DateTimeImmutable $at): void
{
if ($this->paidAt !== null) {
throw new QuickSaleOrderPaid;
}
if ($this->cancelledAt !== null) {
throw new QuickSaleOrderClosed;
}
$this->cancelledAt = $at;
$this->cancelledBy = $userId;
}
public function lineById(int $lineId): ?OrderLine
{
foreach ($this->lines as $line) {
if ($line->id() === $lineId) {
return $line;
}
}
return null;
}
public function id(): ?int
{
return $this->id;
}
public function orderedOn(): DateTimeImmutable
{
return $this->orderedOn;
}
public function pickupNumber(): int
{
return $this->pickupNumber;
}
public function orderedAt(): DateTimeImmutable
{
return $this->orderedAt;
}
public function userId(): int
{
return $this->userId;
}
public function customerId(): ?int
{
return $this->customerId;
}
public function customerName(): ?string
{
return $this->customerName;
}
/**
* @return list<OrderLine>
*/
public function lines(): array
{
return $this->lines;
}
public function paidAt(): ?DateTimeImmutable
{
return $this->paidAt;
}
public function cancelledAt(): ?DateTimeImmutable
{
return $this->cancelledAt;
}
public function cancelledBy(): ?int
{
return $this->cancelledBy;
}
}