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,32 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Contracts;
use App\Modules\Sales\Domain\Entities\QuickSaleOrder;
use DateTimeImmutable;
interface QuickSaleOrderRepository
{
/**
* The order, locked until the end of the transaction.
*/
public function lockById(int $id): ?QuickSaleOrder;
/**
* The order a line belongs to, locked until the end of the transaction.
*/
public function lockByLineId(int $lineId): ?QuickSaleOrder;
/**
* The day's next pickup number, starting from 1 each day. The day's
* orders are locked so that two orders never get the same number.
*/
public function nextPickupNumber(DateTimeImmutable $day): int;
/**
* Inserts or updates the order and its lines; returns the stored order.
*/
public function save(QuickSaleOrder $order): QuickSaleOrder;
}
@@ -0,0 +1,112 @@
<?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;
}
}
@@ -32,6 +32,7 @@ final class QuickSale
private array $lines,
private ?int $creditsSaleId,
private ?EmailAddress $receiptEmail = null,
private ?int $orderId = null,
) {
if ($lines === []) {
throw new QuickSaleEmpty;
@@ -44,9 +45,9 @@ final class QuickSale
*
* @throws QuickSaleEmpty
*/
public static function sell(int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, array $lines, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, ?EmailAddress $receiptEmail = null): self
public static function sell(int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, array $lines, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, ?EmailAddress $receiptEmail = null, ?int $orderId = null): self
{
return new self(null, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, null, $receiptEmail);
return new self(null, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, null, $receiptEmail, $orderId);
}
/**
@@ -74,9 +75,9 @@ final class QuickSale
/**
* @param list<QuickSaleLine> $lines
*/
public static function reconstitute(int $id, int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, array $lines, ?int $creditsSaleId, ?EmailAddress $receiptEmail = null): self
public static function reconstitute(int $id, int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, array $lines, ?int $creditsSaleId, ?EmailAddress $receiptEmail = null, ?int $orderId = null): self
{
return new self($id, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, $creditsSaleId, $receiptEmail);
return new self($id, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, $creditsSaleId, $receiptEmail, $orderId);
}
public function totalCents(): int
@@ -136,4 +137,12 @@ final class QuickSale
{
return $this->receiptEmail;
}
/**
* The quick sale order this sale paid, if any.
*/
public function orderId(): ?int
{
return $this->orderId;
}
}
@@ -0,0 +1,192 @@
<?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;
}
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Enums;
/**
* Where a quick sale order is (spec 19.7): being made, made and waiting to
* be paid, made and paid (ended), or cancelled.
*/
enum OrderStatus: string
{
case InProduction = 'in_production';
case Ready = 'ready';
case Completed = 'completed';
case Cancelled = 'cancelled';
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class OrderHasNoProduction extends DomainException
{
public function __construct()
{
parent::__construct('None of the products has a production process.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class OrderStepNotCurrent extends DomainException
{
public function __construct()
{
parent::__construct('The production step is not the current step of the line.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class QuickSaleOrderClosed extends DomainException
{
public function __construct()
{
parent::__construct('The quick sale order is already paid or cancelled.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class QuickSaleOrderNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The quick sale order was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class QuickSaleOrderPaid extends DomainException
{
public function __construct()
{
parent::__construct('A paid order cannot be cancelled; credit the sale instead.');
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Modules\Sales\Domain\ValueObjects;
/**
* A production step copied to an order line when the order is sent (spec
* 19.7): the machine (null: by hand), the duration and materials per
* piece and the work type the hours go to. Later changes to the product's
* process do not touch the copy.
*/
final readonly class OrderStep
{
/**
* @param list<array{stockItemId: int, quantityMilli: int}> $materials
*/
public function __construct(
public ?int $machineId,
public int $durationQuarters,
public int $workTypeId,
public array $materials = [],
) {}
/**
* @return array{machineId: ?int, durationQuarters: int, workTypeId: int, materials: list<array{stockItemId: int, quantityMilli: int}>}
*/
public function toArray(): array
{
return ['machineId' => $this->machineId, 'durationQuarters' => $this->durationQuarters, 'workTypeId' => $this->workTypeId, 'materials' => $this->materials];
}
/**
* @param array{machineId?: ?int, durationQuarters?: int, workTypeId?: int, materials?: list<array{stockItemId: int, quantityMilli: int}>} $data
*/
public static function fromArray(array $data): self
{
return new self(
isset($data['machineId']) ? (int) $data['machineId'] : null,
(int) ($data['durationQuarters'] ?? 0),
(int) ($data['workTypeId'] ?? 0),
array_map(fn (array $material): array => ['stockItemId' => (int) $material['stockItemId'], 'quantityMilli' => (int) $material['quantityMilli']], $data['materials'] ?? []),
);
}
}