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,245 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Sales\Application\UseCases;
|
||||
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Sales\Application\Contracts\OrderMaterials;
|
||||
use App\Modules\Sales\Application\Contracts\ProductionProcesses;
|
||||
use App\Modules\Sales\Application\Contracts\ProductionQueue;
|
||||
use App\Modules\Sales\Application\Contracts\QuickSaleHours;
|
||||
use App\Modules\Sales\Application\Contracts\QuickSaleProducts;
|
||||
use App\Modules\Sales\Application\Contracts\ReceiptMailer;
|
||||
use App\Modules\Sales\Application\DTOs\OrderData;
|
||||
use App\Modules\Sales\Application\DTOs\OrderResult;
|
||||
use App\Modules\Sales\Application\DTOs\SaleResult;
|
||||
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
|
||||
use App\Modules\Sales\Domain\Contracts\QuickSaleOrderRepository;
|
||||
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
|
||||
use App\Modules\Sales\Domain\Entities\OrderLine;
|
||||
use App\Modules\Sales\Domain\Entities\QuickSale;
|
||||
use App\Modules\Sales\Domain\Entities\QuickSaleOrder;
|
||||
use App\Modules\Sales\Domain\Enums\PaymentMethod;
|
||||
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
|
||||
use App\Modules\Sales\Domain\Exceptions\InvalidQuantity;
|
||||
use App\Modules\Sales\Domain\Exceptions\OrderHasNoProduction;
|
||||
use App\Modules\Sales\Domain\Exceptions\OrderStepNotCurrent;
|
||||
use App\Modules\Sales\Domain\Exceptions\ProductNotSellable;
|
||||
use App\Modules\Sales\Domain\Exceptions\QuickSaleCustomerNotFound;
|
||||
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
|
||||
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderClosed;
|
||||
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderNotFound;
|
||||
use App\Modules\Sales\Domain\Exceptions\QuickSaleOrderPaid;
|
||||
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* Quick sale orders of products made before they are paid (spec 19.7).
|
||||
* Sending an order snapshots the prices and the products' production
|
||||
* processes and puts each made line's first step on its machine's work
|
||||
* list. Completing a step records its materials (anything over the plan
|
||||
* as a reprint write-off) and hours on whoever completed it, and moves the
|
||||
* line to its next step. Paying makes an ordinary quick sale on the day's
|
||||
* cash day without hours (they were booked in production). An unpaid
|
||||
* order can be cancelled; what was used stays recorded.
|
||||
*/
|
||||
final class QuickSaleOrders
|
||||
{
|
||||
public function __construct(
|
||||
private QuickSaleOrderRepository $orders,
|
||||
private QuickSaleRepository $sales,
|
||||
private CashDayRepository $cashDays,
|
||||
private QuickSaleProducts $products,
|
||||
private ProductionProcesses $processes,
|
||||
private ProductionQueue $queue,
|
||||
private OrderMaterials $materials,
|
||||
private QuickSaleHours $hours,
|
||||
private ReceiptMailer $mailer,
|
||||
private CustomerQueries $customers,
|
||||
private TransactionManager $transactions,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws ProductNotSellable
|
||||
* @throws InvalidQuantity
|
||||
* @throws QuickSaleEmpty
|
||||
* @throws QuickSaleCustomerNotFound
|
||||
* @throws OrderHasNoProduction
|
||||
*/
|
||||
public function send(int $sellerId, OrderData $data): OrderResult
|
||||
{
|
||||
$sellable = [];
|
||||
foreach ($this->products->sellable() as $product) {
|
||||
$sellable[$product->id] = $product;
|
||||
}
|
||||
|
||||
$steps = $this->processes->stepsOf(array_map('intval', array_keys($data->quantities)));
|
||||
|
||||
if ($steps === []) {
|
||||
throw new OrderHasNoProduction;
|
||||
}
|
||||
|
||||
if ($data->customerId !== null && $this->customers->findSummary($data->customerId) === null) {
|
||||
throw new QuickSaleCustomerNotFound($data->customerId);
|
||||
}
|
||||
|
||||
$now = $this->clock->now();
|
||||
$today = $this->clock->today();
|
||||
$lines = [];
|
||||
|
||||
foreach ($data->quantities as $productId => $quantity) {
|
||||
$product = $sellable[$productId] ?? throw new ProductNotSellable($productId);
|
||||
$lines[] = OrderLine::ordered(
|
||||
QuickSaleLine::sold($product->id, $product->name, $quantity, $product->grossCents, $product->vatBasisPoints),
|
||||
$steps[$productId] ?? [],
|
||||
$now,
|
||||
);
|
||||
}
|
||||
|
||||
return $this->transactions->run(function () use ($sellerId, $data, $lines, $now, $today): OrderResult {
|
||||
$order = QuickSaleOrder::place($today, $this->orders->nextPickupNumber($today), $now, $sellerId, $data->customerId, $data->customerName, $lines);
|
||||
$order = $this->orders->save($order);
|
||||
|
||||
foreach ($order->lines() as $line) {
|
||||
$step = $line->currentStep();
|
||||
|
||||
if ($step !== null && $step->machineId !== null) {
|
||||
$this->queue->enqueue((int) $line->id(), 0, $step->machineId, $today);
|
||||
}
|
||||
}
|
||||
|
||||
$total = array_sum(array_map(fn (OrderLine $line): int => $line->sale()->totalCents(), $order->lines()));
|
||||
|
||||
return new OrderResult((int) $order->id(), $order->pickupNumber(), $total);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes a line's current step. $usedMilli gives the material used
|
||||
* per stock item (by default quantity × the step's amount per piece);
|
||||
* anything over the plan is written off as a reprint.
|
||||
*
|
||||
* @param array<int, int> $usedMilli By stock item id.
|
||||
* @return bool Whether the line is now made.
|
||||
*
|
||||
* @throws QuickSaleOrderNotFound
|
||||
* @throws QuickSaleOrderClosed
|
||||
* @throws OrderStepNotCurrent
|
||||
*/
|
||||
public function completeStep(int $lineId, int $stepPosition, int $actorUserId, array $usedMilli = []): bool
|
||||
{
|
||||
$today = $this->clock->today();
|
||||
|
||||
return $this->transactions->run(function () use ($lineId, $stepPosition, $actorUserId, $usedMilli, $today): bool {
|
||||
$order = $this->orders->lockByLineId($lineId) ?? throw new QuickSaleOrderNotFound;
|
||||
|
||||
if ($order->cancelledAt() !== null) {
|
||||
throw new QuickSaleOrderClosed;
|
||||
}
|
||||
|
||||
$line = $order->lineById($lineId) ?? throw new QuickSaleOrderNotFound;
|
||||
$step = $line->currentStep() ?? throw new OrderStepNotCurrent;
|
||||
$quantity = $line->sale()->quantity;
|
||||
|
||||
foreach ($step->materials as $material) {
|
||||
$planned = $material['quantityMilli'] * $quantity;
|
||||
$used = max(0, $usedMilli[$material['stockItemId']] ?? $planned);
|
||||
|
||||
if (min($planned, $used) > 0) {
|
||||
$this->materials->use($material['stockItemId'], min($planned, $used), $lineId, $step->machineId, $actorUserId);
|
||||
}
|
||||
|
||||
if ($used > $planned) {
|
||||
$this->materials->writeOff($material['stockItemId'], $used - $planned, $lineId, $step->machineId, $actorUserId, 'reprint');
|
||||
}
|
||||
}
|
||||
|
||||
if ($step->durationQuarters > 0) {
|
||||
$this->hours->bookForOrder($actorUserId, $today, $step->workTypeId, $step->durationQuarters * $quantity, $order->pickupNumber());
|
||||
}
|
||||
|
||||
if ($step->machineId !== null) {
|
||||
$this->queue->complete($lineId, $actorUserId);
|
||||
}
|
||||
|
||||
$next = $line->completeStep($stepPosition, $this->clock->now());
|
||||
|
||||
if ($next !== null && $next->machineId !== null) {
|
||||
$this->queue->enqueue($lineId, (int) $line->currentStepPosition(), $next->machineId, $today);
|
||||
}
|
||||
|
||||
$this->orders->save($order);
|
||||
|
||||
return $line->isMade();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the payment: an ordinary quick sale of the order's lines on
|
||||
* today's cash day, its receipt emailed when an address is given.
|
||||
*
|
||||
* @throws QuickSaleOrderNotFound
|
||||
* @throws QuickSaleOrderClosed
|
||||
* @throws CashDayClosed
|
||||
* @throws InvalidEmailAddress
|
||||
*/
|
||||
public function pay(int $orderId, int $actorUserId, string $paymentMethod, ?string $receiptEmail = null): SaleResult
|
||||
{
|
||||
$email = $receiptEmail === null || trim($receiptEmail) === '' ? null : new EmailAddress($receiptEmail);
|
||||
$today = $this->clock->today();
|
||||
$now = $this->clock->now();
|
||||
|
||||
$result = $this->transactions->run(function () use ($orderId, $actorUserId, $paymentMethod, $email, $today, $now): SaleResult {
|
||||
$order = $this->orders->lockById($orderId) ?? throw new QuickSaleOrderNotFound;
|
||||
$this->cashDays->lockForDay($today)->ensureOpen();
|
||||
|
||||
$order->pay($now);
|
||||
$sale = QuickSale::sell(
|
||||
$actorUserId,
|
||||
$order->customerId(),
|
||||
PaymentMethod::from($paymentMethod),
|
||||
array_map(fn (OrderLine $line): QuickSaleLine => $line->sale(), $order->lines()),
|
||||
$now,
|
||||
$today,
|
||||
$email,
|
||||
$orderId,
|
||||
);
|
||||
$saleId = (int) $this->sales->add($sale)->id();
|
||||
$this->orders->save($order);
|
||||
|
||||
return new SaleResult($saleId, $sale->totalCents(), 0, 0, $email?->value);
|
||||
});
|
||||
|
||||
if ($result->receiptEmail !== null) {
|
||||
$this->mailer->send($result->saleId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws QuickSaleOrderNotFound
|
||||
* @throws QuickSaleOrderPaid
|
||||
* @throws QuickSaleOrderClosed
|
||||
*/
|
||||
public function cancel(int $orderId, int $actorUserId): void
|
||||
{
|
||||
$this->transactions->run(function () use ($orderId, $actorUserId): void {
|
||||
$order = $this->orders->lockById($orderId) ?? throw new QuickSaleOrderNotFound;
|
||||
$order->cancel($actorUserId, $this->clock->now());
|
||||
|
||||
foreach ($order->lines() as $line) {
|
||||
if ($line->isInProduction()) {
|
||||
$this->queue->remove((int) $line->id());
|
||||
}
|
||||
}
|
||||
|
||||
$this->orders->save($order);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user