Files
Toiminnanohjaus/app/Modules/Sales/Domain/Entities/QuickSaleOrder.php
T
petriandClaude Sonnet 5.5 701f05780f Change a quick sale order before making starts
An unpaid order whose lines have no completed step can be reopened in the
cart from the open orders list; saving replaces its lines with current
prices and processes, moves the machine work list rows and keeps the
pickup number. Keyboard browser tests cover changing and cancelling an
order and moving production steps.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 09:11:36 +02:00

242 lines
6.3 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\QuickSaleOrderInProduction;
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;
}
/**
* Whether no step has been completed on any line yet.
*/
public function canBeChanged(): bool
{
if ($this->paidAt !== null || $this->cancelledAt !== null) {
return false;
}
foreach ($this->lines as $line) {
if ($line->hasStarted()) {
return false;
}
}
return true;
}
/**
* Replaces the products and the customer before making has started; the
* pickup number stays.
*
* @param list<OrderLine> $lines
*
* @throws QuickSaleOrderClosed
* @throws QuickSaleOrderInProduction
* @throws QuickSaleEmpty
*/
public function change(?int $customerId, ?string $customerName, array $lines): void
{
if ($this->paidAt !== null || $this->cancelledAt !== null) {
throw new QuickSaleOrderClosed;
}
if (! $this->canBeChanged()) {
throw new QuickSaleOrderInProduction;
}
if ($lines === []) {
throw new QuickSaleEmpty;
}
$customerName = $customerName === null ? null : mb_substr(trim($customerName), 0, 150);
$this->customerId = $customerId;
$this->customerName = $customerName === '' ? null : $customerName;
$this->lines = $lines;
}
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;
}
}