Files
Toiminnanohjaus/app/Modules/Sales/Infrastructure/Persistence/Repositories/EloquentQuickSaleOrderQueries.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

130 lines
5.6 KiB
PHP

<?php
namespace App\Modules\Sales\Infrastructure\Persistence\Repositories;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Sales\Application\Contracts\QuickSaleOrderQueries;
use App\Modules\Sales\Application\DTOs\OrderLineSummary;
use App\Modules\Sales\Application\DTOs\OrderSummary;
use App\Modules\Sales\Domain\Enums\OrderStatus;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderLineModel;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderModel;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
class EloquentQuickSaleOrderQueries implements QuickSaleOrderQueries
{
public function __construct(
private UserAccountQueries $users,
private CustomerQueries $customers,
) {}
public function openOrders(): array
{
return $this->summaries(QuickSaleOrderModel::query()
->with(['lines', 'sale:id,quick_sale_order_id,credits_quick_sale_id'])
->whereIn('status', [OrderStatus::InProduction->value, OrderStatus::Ready->value])
->orderBy('ordered_at')
->orderBy('id')
->get());
}
public function findOrder(int $id): ?OrderSummary
{
$model = QuickSaleOrderModel::query()->with(['lines', 'sale:id,quick_sale_order_id,credits_quick_sale_id'])->find($id);
return $model === null ? null : $this->summaries(collect([$model]))[0];
}
public function lines(array $lineIds): array
{
if ($lineIds === []) {
return [];
}
$lines = QuickSaleOrderLineModel::query()->whereIn('id', $lineIds)->get();
$orders = QuickSaleOrderModel::query()->whereIn('id', $lines->pluck('quick_sale_order_id')->unique())->get()->keyBy('id');
$result = [];
foreach ($lines as $line) {
$order = $orders->get($line->quick_sale_order_id);
if ($order !== null) {
$result[$line->id] = $this->lineSummary($line, $order, $this->customerName($order));
}
}
return $result;
}
public function unpaidCount(): int
{
return QuickSaleOrderModel::query()->whereNull('paid_at')->whereNull('cancelled_at')->count();
}
/**
* @param Collection<int, QuickSaleOrderModel> $models
* @return list<OrderSummary>
*/
private function summaries(Collection $models): array
{
$sellers = $this->users->namesByIds($models->pluck('user_id')->unique()->values()->all());
$customerNames = $this->customers->namesByIds($models->pluck('customer_id')->filter()->unique()->values()->all());
return $models->map(function (QuickSaleOrderModel $model) use ($sellers, $customerNames): OrderSummary {
$customerName = $model->customer_name ?? ($model->customer_id === null ? null : ($customerNames[$model->customer_id] ?? null));
$lines = $model->lines->map(fn (QuickSaleOrderLineModel $line): OrderLineSummary => $this->lineSummary($line, $model, $customerName))->values()->all();
return new OrderSummary(
id: $model->id,
pickupNumber: $model->pickup_number,
orderedOn: substr($model->ordered_on, 0, 10),
orderedAt: $this->localTime($model->ordered_at),
sellerName: $sellers[$model->user_id] ?? '',
customerId: $model->customer_id,
customerName: $customerName,
status: OrderStatus::from($model->status),
saleId: $model->sale?->id,
totalCents: array_sum(array_map(fn (OrderLineSummary $line): int => $line->totalCents, $lines)),
lines: $lines,
canBeChanged: $model->paid_at === null && $model->cancelled_at === null && $model->lines->every(
fn (QuickSaleOrderLineModel $line): bool => $line->production_steps === [] || $line->current_step === 0,
),
);
})->values()->all();
}
private function lineSummary(QuickSaleOrderLineModel $line, QuickSaleOrderModel $order, ?string $customerName): OrderLineSummary
{
$steps = $line->production_steps;
$current = $line->current_step === null ? null : ($steps[$line->current_step] ?? null);
return new OrderLineSummary(
id: $line->id,
orderId: $order->id,
pickupNumber: $order->pickup_number,
customerName: $customerName,
orderedAt: $this->localTime($order->ordered_at),
productName: $line->product_name,
quantity: $line->quantity,
totalCents: $line->quantity * $line->unit_price_cents,
stepMachineIds: array_map(fn (array $step): ?int => isset($step['machineId']) ? (int) $step['machineId'] : null, $steps),
currentStep: $line->current_step,
currentMaterials: array_map(fn (array $material): array => ['stockItemId' => (int) $material['stockItemId'], 'quantityMilli' => (int) $material['quantityMilli']], $current['materials'] ?? []),
isMade: $line->made_at !== null,
productId: $line->product_id,
);
}
private function customerName(QuickSaleOrderModel $order): ?string
{
return $order->customer_name ?? ($order->customer_id === null ? null : ($this->customers->namesByIds([$order->customer_id])[$order->customer_id] ?? null));
}
private function localTime(\DateTimeInterface $moment): string
{
return Carbon::instance($moment)->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i');
}
}