Files
ladill-pos/app/Services/Pos/PosSaleService.php
T
isaaccladandClaude Opus 4.8 d4f4821d96
Deploy Ladill POS / deploy (push) Successful in 23s
Restaurant mode Phase 3: table-QR self-ordering into the kitchen
The "links" slice — a guest scans a table's QR, browses the menu (with
modifiers), and submits an order that lands on that table's open tab and fires
straight to the Kitchen Display.

- Public, auth-free flow scoped by an unguessable table short_code:
  GET /t/{code} (menu + client cart), POST /t/{code}/order (throttled),
  GET /t/{code}/done. Orders open/append the table's dine-in tab, add lines as
  source=guest, and send to the kitchen.
- Staff print a per-table QR (Settings → table → QR; renders client-side to the
  public menu URL). short_code is generated lazily.
- Guest lines are badged on the ticket and the KDS so staff can tell them apart;
  staff still settle the tab as usual (cash / Ladill Pay).
- Extracted PosSaleService::buildProductLine as the single product+modifier
  price resolver, now shared by staff and guest ordering (client prices never
  trusted).

Schema additive: pos_tables.short_code, pos_sale_lines.source. New
PosRestaurantTest covers the guest order firing to the kitchen; suite green (12).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 20:13:28 +00:00

400 lines
14 KiB
PHP

<?php
namespace App\Services\Pos;
use App\Models\PosProduct;
use App\Models\PosSale;
use App\Models\PosSaleLine;
use App\Models\PosSaleLineModifier;
use App\Models\PosTable;
use App\Models\User;
use App\Services\Pay\PayClient;
use Illuminate\Support\Str;
use RuntimeException;
class PosSaleService
{
public function __construct(
private PayClient $pay,
private PosTimelineService $timeline,
) {}
/**
* @param list<array{product_id?: ?int, name: string, unit_price_minor: int, quantity: int}> $lines
* @param array{customer_name?: ?string, customer_email?: ?string, customer_phone?: ?string, crm_customer_id?: ?int, location_id?: ?int, currency?: string} $meta
*/
public function createSale(User $merchant, array $lines, array $meta = []): PosSale
{
$normalized = $this->normalizeLines($lines);
if ($normalized === []) {
throw new RuntimeException('Add at least one item to the sale.');
}
$subtotal = collect($normalized)->sum('line_total_minor');
if ($subtotal <= 0) {
throw new RuntimeException('Sale total must be greater than zero.');
}
$currency = strtoupper((string) ($meta['currency'] ?? config('pos.default_currency', 'GHS')));
$sale = PosSale::create([
'owner_ref' => $merchant->public_id,
'location_id' => $meta['location_id'] ?? null,
'reference' => 'POS-'.strtoupper(Str::random(12)),
'status' => PosSale::STATUS_PENDING,
'payment_method' => PosSale::METHOD_PAY,
'customer_name' => $meta['customer_name'] ?? null,
'customer_email' => $meta['customer_email'] ?? null,
'customer_phone' => $meta['customer_phone'] ?? null,
'crm_customer_id' => $meta['crm_customer_id'] ?? null,
'subtotal_minor' => $subtotal,
'total_minor' => $subtotal,
'currency' => $currency,
]);
foreach ($normalized as $index => $line) {
PosSaleLine::create([
'pos_sale_id' => $sale->id,
'product_id' => $line['product_id'] ?? null,
'name' => $line['name'],
'unit_price_minor' => $line['unit_price_minor'],
'quantity' => $line['quantity'],
'line_total_minor' => $line['line_total_minor'],
'position' => $index,
]);
}
return $sale->fresh('lines');
}
/**
* Open an empty restaurant ticket (a tab) that stays pending while items are
* added. Occupies the table for dine-in orders.
*
* @param array{order_type?: string, table_id?: ?int, location_id?: ?int, currency?: string, covers?: ?int, customer_name?: ?string} $meta
*/
public function openTicket(User $merchant, array $meta = []): PosSale
{
$orderType = in_array($meta['order_type'] ?? '', [PosSale::ORDER_DINE_IN, PosSale::ORDER_TAKEAWAY, PosSale::ORDER_COUNTER], true)
? $meta['order_type']
: PosSale::ORDER_COUNTER;
$table = null;
if ($orderType === PosSale::ORDER_DINE_IN && ! empty($meta['table_id'])) {
$table = PosTable::owned($merchant->public_id)->find($meta['table_id']);
if ($table && ! $table->isFree()) {
throw new RuntimeException('That table already has an open ticket.');
}
}
$sale = PosSale::create([
'owner_ref' => $merchant->public_id,
'location_id' => $meta['location_id'] ?? null,
'table_id' => $table?->id,
'reference' => 'POS-'.strtoupper(Str::random(12)),
'status' => PosSale::STATUS_PENDING,
'payment_method' => PosSale::METHOD_PAY,
'order_type' => $orderType,
'kitchen_status' => PosSale::KITCHEN_NONE,
'covers' => $meta['covers'] ?? null,
'customer_name' => $meta['customer_name'] ?? null,
'subtotal_minor' => 0,
'total_minor' => 0,
'currency' => strtoupper((string) ($meta['currency'] ?? config('pos.default_currency', 'GHS'))),
'opened_at' => now(),
]);
if ($table) {
$table->forceFill([
'status' => PosTable::STATUS_OCCUPIED,
'current_sale_id' => $sale->id,
])->save();
}
return $sale->fresh('lines');
}
/**
* Resolve a catalog product (owner-scoped) + chosen modifiers into an addLine
* payload. Effective price = base + modifier deltas; null if product unknown.
*
* @param list<int|string> $modifierIds
* @return array<string,mixed>|null
*/
public function buildProductLine(string $owner, int $productId, array $modifierIds = [], int $quantity = 1, ?string $notes = null, ?string $course = null, string $source = 'staff'): ?array
{
$product = PosProduct::owned($owner)->with('modifierGroups.modifiers')->find($productId);
if (! $product) {
return null;
}
$allowed = $product->modifierGroups->flatMap->modifiers->keyBy('id');
$chosen = collect($modifierIds)->map(fn ($id) => $allowed->get((int) $id))->filter()->values();
return [
'product_id' => $product->id,
'name' => $product->name,
'station_id' => $product->station_id,
'course' => $course ?: $product->course,
'quantity' => max(1, $quantity),
'notes' => $notes,
'source' => $source,
'unit_price_minor' => max(1, $product->price_minor + (int) $chosen->sum('price_delta_minor')),
'modifiers' => $chosen->map(fn ($m) => [
'modifier_id' => $m->id, 'name' => $m->name, 'price_delta_minor' => $m->price_delta_minor,
])->all(),
];
}
/**
* @param array{product_id?: ?int, station_id?: ?int, course?: ?string, source?: string, name: string, unit_price_minor: int, quantity?: int, notes?: ?string, modifiers?: list<array{modifier_id?: ?int, name: string, price_delta_minor?: int}>} $line
*/
public function addLine(PosSale $sale, array $line): PosSaleLine
{
$name = trim((string) ($line['name'] ?? ''));
$unit = max(0, (int) ($line['unit_price_minor'] ?? 0));
$qty = max(1, (int) ($line['quantity'] ?? 1));
if ($name === '' || $unit <= 0) {
throw new RuntimeException('Choose an item with a price.');
}
$position = (int) $sale->lines()->max('position') + 1;
$created = PosSaleLine::create([
'pos_sale_id' => $sale->id,
'product_id' => $line['product_id'] ?? null,
'station_id' => $line['station_id'] ?? null,
'name' => $name,
'unit_price_minor' => $unit,
'quantity' => $qty,
'line_total_minor' => $unit * $qty,
'position' => $position,
'kitchen_state' => PosSaleLine::KITCHEN_NEW,
'source' => $line['source'] ?? 'staff',
'notes' => isset($line['notes']) ? trim((string) $line['notes']) ?: null : null,
'course' => $line['course'] ?? null,
]);
foreach (($line['modifiers'] ?? []) as $mod) {
$modName = trim((string) ($mod['name'] ?? ''));
if ($modName === '') {
continue;
}
PosSaleLineModifier::create([
'pos_sale_line_id' => $created->id,
'modifier_id' => $mod['modifier_id'] ?? null,
'name' => $modName,
'price_delta_minor' => (int) ($mod['price_delta_minor'] ?? 0),
]);
}
$this->recomputeTotals($sale);
return $created;
}
public function updateLine(PosSaleLine $line, int $quantity, ?string $notes = null): void
{
if ($quantity <= 0) {
$this->removeLine($line);
return;
}
$line->forceFill([
'quantity' => $quantity,
'line_total_minor' => $line->unit_price_minor * $quantity,
'notes' => $notes !== null ? (trim($notes) ?: null) : $line->notes,
])->save();
$this->recomputeTotals($line->sale);
}
public function removeLine(PosSaleLine $line): void
{
$sale = $line->sale;
$line->delete();
$this->recomputeTotals($sale);
}
public function recomputeTotals(PosSale $sale): PosSale
{
$subtotal = (int) $sale->lines()->sum('line_total_minor');
$sale->forceFill([
'subtotal_minor' => $subtotal,
'total_minor' => $subtotal,
])->save();
return $sale;
}
/**
* Fire not-yet-sent lines to the kitchen. Pass a course to fire just that course.
*
* @return int number of lines fired
*/
public function sendToKitchen(PosSale $sale, ?string $course = null): int
{
$query = $sale->lines()->where('kitchen_state', PosSaleLine::KITCHEN_NEW);
if ($course !== null && $course !== '') {
$query->where('course', $course);
}
$fired = $query->update(['kitchen_state' => PosSaleLine::KITCHEN_QUEUED]);
if ($fired > 0) {
$sale->forceFill([
'kitchen_status' => PosSale::KITCHEN_ACTIVE,
'kitchen_sent_at' => $sale->kitchen_sent_at ?? now(),
])->save();
}
return $fired;
}
/**
* Close a ticket once it's settled: free its table and mark kitchen done.
*/
public function closeTicket(PosSale $sale): void
{
$sale->forceFill([
'kitchen_status' => $sale->kitchen_status === PosSale::KITCHEN_NONE ? PosSale::KITCHEN_NONE : PosSale::KITCHEN_SERVED,
'closed_at' => now(),
])->save();
$sale->lines()
->whereNotIn('kitchen_state', [PosSaleLine::KITCHEN_NEW, PosSaleLine::KITCHEN_SERVED])
->update(['kitchen_state' => PosSaleLine::KITCHEN_SERVED]);
if ($sale->table_id) {
PosTable::where('id', $sale->table_id)
->where('current_sale_id', $sale->id)
->update(['status' => PosTable::STATUS_FREE, 'current_sale_id' => null]);
}
}
/**
* @return array{sale: PosSale, checkout_url: string}
*/
public function initiatePayCheckout(PosSale $sale, User $merchant): array
{
if ($sale->isPaid()) {
throw new RuntimeException('This sale is already paid.');
}
$sale->load('lines');
$callbackUrl = route('pos.sales.callback', $sale);
$payOrder = $this->pay->createCheckout([
'merchant' => $merchant->public_id,
'fee_tier' => 'sales',
'source_service' => 'pos',
'source_ref' => (string) $sale->id,
'callback_url' => $callbackUrl,
'customer_name' => $sale->customer_name,
'customer_email' => $sale->customer_email,
'customer_phone' => $sale->customer_phone,
'line_items' => $sale->lines->map(fn (PosSaleLine $line) => [
'name' => $line->name,
'unit_price_minor' => $line->unit_price_minor,
'quantity' => $line->quantity,
])->all(),
'metadata' => [
'pos_sale_id' => $sale->id,
'pos_reference' => $sale->reference,
],
]);
$checkoutUrl = (string) ($payOrder['checkout_url'] ?? '');
if ($checkoutUrl === '') {
throw new RuntimeException('Could not start checkout. Please try again.');
}
$sale->forceFill([
'payment_method' => PosSale::METHOD_PAY,
'pay_order_id' => $payOrder['id'] ?? null,
'payment_reference' => $payOrder['reference'] ?? null,
])->save();
return [
'sale' => $sale->fresh('lines'),
'checkout_url' => $checkoutUrl,
];
}
public function recordCashPayment(PosSale $sale): PosSale
{
if ($sale->isPaid()) {
throw new RuntimeException('This sale is already paid.');
}
$sale->forceFill([
'payment_method' => PosSale::METHOD_CASH,
'status' => PosSale::STATUS_PAID,
'paid_at' => now(),
])->save();
$this->closeTicket($sale);
$sale = $sale->fresh('lines');
$this->timeline->pushPaidSale($sale);
return $sale;
}
public function completePayCheckout(string $paymentReference): PosSale
{
$sale = PosSale::query()
->where('payment_reference', $paymentReference)
->where('status', PosSale::STATUS_PENDING)
->firstOrFail();
$payOrder = $this->pay->verify($paymentReference);
$sale->forceFill([
'status' => PosSale::STATUS_PAID,
'total_minor' => (int) ($payOrder['amount_minor'] ?? $sale->total_minor),
'pay_order_id' => $payOrder['id'] ?? $sale->pay_order_id,
'paid_at' => now(),
])->save();
$this->closeTicket($sale);
$sale = $sale->fresh('lines');
$this->timeline->pushPaidSale($sale);
return $sale;
}
/**
* @param list<array{product_id?: ?int, name: string, unit_price_minor: int, quantity: int}> $lines
* @return list<array{product_id?: ?int, name: string, unit_price_minor: int, quantity: int, line_total_minor: int}>
*/
private function normalizeLines(array $lines): array
{
$out = [];
foreach ($lines as $line) {
$qty = max(1, (int) ($line['quantity'] ?? 1));
$unit = max(0, (int) ($line['unit_price_minor'] ?? 0));
$name = trim((string) ($line['name'] ?? ''));
if ($name === '' || $unit <= 0) {
continue;
}
$out[] = [
'product_id' => $line['product_id'] ?? null,
'name' => $name,
'unit_price_minor' => $unit,
'quantity' => $qty,
'line_total_minor' => $unit * $qty,
];
}
return $out;
}
}