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:
@@ -26,11 +26,12 @@ use Illuminate\Support\Carbon;
|
||||
* @property int $total_cents
|
||||
* @property int|null $credits_quick_sale_id
|
||||
* @property string|null $receipt_email
|
||||
* @property int|null $quick_sale_order_id
|
||||
* @property Carbon|null $receipt_sent_at
|
||||
* @property Carbon|null $receipt_send_failed_at
|
||||
*/
|
||||
#[Table('quick_sales')]
|
||||
#[Fillable(['user_id', 'customer_id', 'payment_method', 'sold_at', 'sold_on', 'total_cents', 'credits_quick_sale_id', 'receipt_email'])]
|
||||
#[Fillable(['user_id', 'customer_id', 'payment_method', 'sold_at', 'sold_on', 'total_cents', 'credits_quick_sale_id', 'receipt_email', 'quick_sale_order_id'])]
|
||||
#[Hidden(['receipt_email'])]
|
||||
#[UseFactory(QuickSaleFactory::class)]
|
||||
class QuickSaleModel extends Model
|
||||
@@ -67,6 +68,7 @@ class QuickSaleModel extends Model
|
||||
'total_cents' => 'integer',
|
||||
'credits_quick_sale_id' => 'integer',
|
||||
'receipt_email' => 'encrypted',
|
||||
'quick_sale_order_id' => 'integer',
|
||||
'receipt_sent_at' => 'datetime',
|
||||
'receipt_send_failed_at' => 'datetime',
|
||||
];
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Sales\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Sales Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $quick_sale_order_id
|
||||
* @property int $product_id
|
||||
* @property string $product_name
|
||||
* @property int $quantity
|
||||
* @property int $unit_price_cents
|
||||
* @property int $vat_basis_points
|
||||
* @property list<array{machineId?: ?int, durationQuarters?: int, workTypeId?: int, materials?: list<array{stockItemId: int, quantityMilli: int}>}> $production_steps
|
||||
* @property int|null $current_step
|
||||
* @property Carbon|null $made_at
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('quick_sale_order_lines')]
|
||||
#[Fillable(['quick_sale_order_id', 'product_id', 'product_name', 'quantity', 'unit_price_cents', 'vat_basis_points', 'production_steps', 'current_step', 'made_at', 'sort_order'])]
|
||||
class QuickSaleOrderLineModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'quick_sale_order_id' => 'integer',
|
||||
'product_id' => 'integer',
|
||||
'quantity' => 'integer',
|
||||
'unit_price_cents' => 'integer',
|
||||
'vat_basis_points' => 'integer',
|
||||
'production_steps' => 'array',
|
||||
'current_step' => 'integer',
|
||||
'made_at' => 'immutable_datetime',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Sales\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Sales Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $ordered_on Y-m-d
|
||||
* @property int $pickup_number
|
||||
* @property Carbon $ordered_at
|
||||
* @property int $user_id
|
||||
* @property int|null $customer_id
|
||||
* @property string|null $customer_name
|
||||
* @property string $status
|
||||
* @property Carbon|null $made_at
|
||||
* @property Carbon|null $paid_at
|
||||
* @property Carbon|null $cancelled_at
|
||||
* @property int|null $cancelled_by
|
||||
*/
|
||||
#[Table('quick_sale_orders')]
|
||||
#[Fillable(['ordered_on', 'pickup_number', 'ordered_at', 'user_id', 'customer_id', 'customer_name', 'status', 'made_at', 'paid_at', 'cancelled_at', 'cancelled_by'])]
|
||||
class QuickSaleOrderModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasMany<QuickSaleOrderLineModel, $this>
|
||||
*/
|
||||
public function lines(): HasMany
|
||||
{
|
||||
return $this->hasMany(QuickSaleOrderLineModel::class, 'quick_sale_order_id')->orderBy('sort_order');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasOne<QuickSaleModel, $this>
|
||||
*/
|
||||
public function sale(): HasOne
|
||||
{
|
||||
return $this->hasOne(QuickSaleModel::class, 'quick_sale_order_id')->whereNull('credits_quick_sale_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'pickup_number' => 'integer',
|
||||
'ordered_at' => 'immutable_datetime',
|
||||
'user_id' => 'integer',
|
||||
'customer_id' => 'integer',
|
||||
'made_at' => 'immutable_datetime',
|
||||
'paid_at' => 'immutable_datetime',
|
||||
'cancelled_at' => 'immutable_datetime',
|
||||
'cancelled_by' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
<?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,
|
||||
);
|
||||
})->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,
|
||||
);
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Sales\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Sales\Domain\Contracts\QuickSaleOrderRepository;
|
||||
use App\Modules\Sales\Domain\Entities\OrderLine;
|
||||
use App\Modules\Sales\Domain\Entities\QuickSaleOrder;
|
||||
use App\Modules\Sales\Domain\ValueObjects\OrderStep;
|
||||
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
|
||||
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderLineModel;
|
||||
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleOrderModel;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentQuickSaleOrderRepository implements QuickSaleOrderRepository
|
||||
{
|
||||
public function lockById(int $id): ?QuickSaleOrder
|
||||
{
|
||||
$model = QuickSaleOrderModel::query()->lockForUpdate()->with('lines')->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function lockByLineId(int $lineId): ?QuickSaleOrder
|
||||
{
|
||||
$orderId = QuickSaleOrderLineModel::query()->whereKey($lineId)->value('quick_sale_order_id');
|
||||
|
||||
return $orderId === null ? null : $this->lockById((int) $orderId);
|
||||
}
|
||||
|
||||
public function nextPickupNumber(DateTimeImmutable $day): int
|
||||
{
|
||||
$numbers = QuickSaleOrderModel::query()->where('ordered_on', $day->format('Y-m-d'))->lockForUpdate()->pluck('pickup_number');
|
||||
|
||||
return (int) $numbers->max() + 1;
|
||||
}
|
||||
|
||||
public function save(QuickSaleOrder $order): QuickSaleOrder
|
||||
{
|
||||
$model = $order->id() === null ? new QuickSaleOrderModel : QuickSaleOrderModel::query()->findOrFail($order->id());
|
||||
|
||||
$madeTimes = array_filter(array_map(fn (OrderLine $line): ?DateTimeImmutable => $line->madeAt(), $order->lines()));
|
||||
$madeAt = $order->isMade() && $madeTimes !== [] ? max($madeTimes) : null;
|
||||
|
||||
$model->fill([
|
||||
'ordered_on' => $order->orderedOn()->format('Y-m-d'),
|
||||
'pickup_number' => $order->pickupNumber(),
|
||||
'ordered_at' => $order->orderedAt()->format('Y-m-d H:i:s'),
|
||||
'user_id' => $order->userId(),
|
||||
'customer_id' => $order->customerId(),
|
||||
'customer_name' => $order->customerName(),
|
||||
'status' => $order->status()->value,
|
||||
'made_at' => $madeAt?->format('Y-m-d H:i:s'),
|
||||
'paid_at' => $order->paidAt()?->format('Y-m-d H:i:s'),
|
||||
'cancelled_at' => $order->cancelledAt()?->format('Y-m-d H:i:s'),
|
||||
'cancelled_by' => $order->cancelledBy(),
|
||||
])->save();
|
||||
|
||||
foreach ($order->lines() as $index => $line) {
|
||||
$lineModel = $line->id() === null ? new QuickSaleOrderLineModel(['quick_sale_order_id' => $model->id]) : QuickSaleOrderLineModel::query()->findOrFail($line->id());
|
||||
|
||||
$lineModel->fill([
|
||||
'product_id' => $line->sale()->productId,
|
||||
'product_name' => $line->sale()->productName,
|
||||
'quantity' => $line->sale()->quantity,
|
||||
'unit_price_cents' => $line->sale()->unitPriceCents,
|
||||
'vat_basis_points' => $line->sale()->vatBasisPoints,
|
||||
'production_steps' => array_map(fn (OrderStep $step): array => $step->toArray(), $line->steps()),
|
||||
'current_step' => $line->currentStepPosition(),
|
||||
'made_at' => $line->madeAt()?->format('Y-m-d H:i:s'),
|
||||
'sort_order' => $index + 1,
|
||||
])->save();
|
||||
}
|
||||
|
||||
return $this->lockById($model->id) ?? $order;
|
||||
}
|
||||
|
||||
private function toEntity(QuickSaleOrderModel $model): QuickSaleOrder
|
||||
{
|
||||
return QuickSaleOrder::reconstitute(
|
||||
$model->id,
|
||||
new DateTimeImmutable(substr($model->ordered_on, 0, 10)),
|
||||
$model->pickup_number,
|
||||
DateTimeImmutable::createFromInterface($model->ordered_at),
|
||||
$model->user_id,
|
||||
$model->customer_id,
|
||||
$model->customer_name,
|
||||
$model->lines->map(fn (QuickSaleOrderLineModel $line): OrderLine => new OrderLine(
|
||||
$line->id,
|
||||
new QuickSaleLine($line->product_id, $line->product_name, $line->quantity, $line->unit_price_cents, $line->vat_basis_points),
|
||||
array_map(fn (array $step): OrderStep => OrderStep::fromArray($step), $line->production_steps),
|
||||
$line->current_step,
|
||||
$line->made_at === null ? null : DateTimeImmutable::createFromInterface($line->made_at),
|
||||
))->values()->all(),
|
||||
$model->paid_at === null ? null : DateTimeImmutable::createFromInterface($model->paid_at),
|
||||
$model->cancelled_at === null ? null : DateTimeImmutable::createFromInterface($model->cancelled_at),
|
||||
$model->cancelled_by,
|
||||
);
|
||||
}
|
||||
}
|
||||
+2
@@ -30,6 +30,7 @@ class EloquentQuickSaleRepository implements QuickSaleRepository
|
||||
'total_cents' => $sale->totalCents(),
|
||||
'credits_quick_sale_id' => $sale->creditsSaleId(),
|
||||
'receipt_email' => $sale->receiptEmail()?->value,
|
||||
'quick_sale_order_id' => $sale->orderId(),
|
||||
]);
|
||||
|
||||
foreach ($sale->lines() as $index => $line) {
|
||||
@@ -101,6 +102,7 @@ class EloquentQuickSaleRepository implements QuickSaleRepository
|
||||
))->values()->all(),
|
||||
$model->credits_quick_sale_id,
|
||||
$model->receipt_email === null ? null : new EmailAddress($model->receipt_email),
|
||||
$model->quick_sale_order_id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user