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 $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); }); } }