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