Files
ladill-pos/tests/Feature/PosRestaurantTest.php
T
isaaccladandClaude Opus 4.8 50b170b0af
Deploy Ladill POS / deploy (push) Successful in 35s
Restaurant mode Phase 2: menu depth — categories, stations, modifiers, courses
Builds on Phase 1 with the menu structure restaurants need:
- Menu setup page (restaurant mode): categories, kitchen stations, and modifier
  groups with options (price deltas).
- Products gain category, kitchen station, default course, and attached modifier
  groups (managed on the product form).
- Ordering: the ticket groups the catalog by category and, when a product has
  modifier groups, opens a picker (respects min/max select) for options + notes +
  course before adding. Effective price = base + modifier deltas (resolved
  server-side; client prices are never trusted).
- Fire-by-course: send the whole tab or just one course to the kitchen.
- KDS: filter by station; each item shows its station, course, modifiers and notes.

Schema additive (pos_categories, pos_stations, pos_modifier_groups, pos_modifiers,
product↔group pivot, pos_sale_line_modifiers; category/station/course columns on
products and lines). PosRestaurantTest covers modifiers, course and station routing;
suite green (11 passed).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 19:58:57 +00:00

175 lines
6.6 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\PosLocation;
use App\Models\PosModifier;
use App\Models\PosModifierGroup;
use App\Models\PosProduct;
use App\Models\PosSale;
use App\Models\PosSaleLine;
use App\Models\PosStation;
use App\Models\PosTable;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PosRestaurantTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
$this->withoutVite();
}
private function user(): User
{
return User::create([
'public_id' => 'u-'.uniqid(),
'name' => 'Server',
'email' => uniqid().'@example.com',
]);
}
private function restaurant(User $user): array
{
$location = PosLocation::create([
'owner_ref' => $user->public_id,
'name' => 'Café',
'currency' => 'GHS',
'service_style' => PosLocation::STYLE_RESTAURANT,
]);
$table = PosTable::create([
'owner_ref' => $user->public_id,
'location_id' => $location->id,
'label' => 'T1',
'seats' => 4,
'status' => PosTable::STATUS_FREE,
]);
$product = PosProduct::create([
'owner_ref' => $user->public_id,
'name' => 'Latte',
'price_minor' => 2500,
'currency' => 'GHS',
'is_active' => true,
]);
return [$location, $table, $product];
}
public function test_dine_in_ticket_lifecycle(): void
{
$user = $this->user();
[, $table, $product] = $this->restaurant($user);
// Open a dine-in tab on the table.
$this->actingAs($user)->post(route('pos.tickets.open'), [
'order_type' => 'dine_in',
'table_id' => $table->id,
])->assertRedirect();
$sale = PosSale::where('owner_ref', $user->public_id)->firstOrFail();
$this->assertSame(PosSale::ORDER_DINE_IN, $sale->order_type);
$this->assertSame(PosSale::STATUS_PENDING, $sale->status);
$this->assertTrue($sale->table_id === $table->id);
$this->assertSame(PosTable::STATUS_OCCUPIED, $table->fresh()->status);
// Add an item to the tab.
$this->actingAs($user)->postJson(route('pos.tickets.lines.add', $sale), [
'product_id' => $product->id,
'name' => $product->name,
'unit_price_minor' => $product->price_minor,
'quantity' => 2,
])->assertOk()->assertJsonPath('total_minor', 5000);
// Fire it to the kitchen.
$this->actingAs($user)->postJson(route('pos.tickets.send', $sale))
->assertOk()->assertJsonPath('fired', 1);
$line = $sale->lines()->firstOrFail();
$this->assertSame(PosSaleLine::KITCHEN_QUEUED, $line->fresh()->kitchen_state);
$this->assertSame(PosSale::KITCHEN_ACTIVE, $sale->fresh()->kitchen_status);
// Kitchen feed shows the active ticket.
$this->actingAs($user)->getJson(route('pos.kitchen.feed'))
->assertOk()->assertJsonCount(1, 'tickets');
// Bump the line through to served.
foreach (['preparing', 'ready', 'served'] as $expected) {
$this->actingAs($user)->postJson(route('pos.kitchen.bump', $line))
->assertOk()->assertJsonPath('state', $expected);
}
$this->assertSame(PosSale::KITCHEN_SERVED, $sale->fresh()->kitchen_status);
// Settle in cash — ticket paid, table freed.
$this->actingAs($user)->post(route('pos.tickets.settle', $sale), [
'payment_method' => 'cash',
])->assertRedirect(route('pos.sales.show', $sale));
$this->assertSame(PosSale::STATUS_PAID, $sale->fresh()->status);
$this->assertNotNull($sale->fresh()->closed_at);
$this->assertSame(PosTable::STATUS_FREE, $table->fresh()->status);
$this->assertNull($table->fresh()->current_sale_id);
}
public function test_modifiers_course_and_station_routing(): void
{
$user = $this->user();
[, , $product] = $this->restaurant($user);
$station = PosStation::create(['owner_ref' => $user->public_id, 'name' => 'Bar']);
$group = PosModifierGroup::create(['owner_ref' => $user->public_id, 'name' => 'Size', 'min_select' => 1, 'max_select' => 1]);
$large = PosModifier::create(['modifier_group_id' => $group->id, 'owner_ref' => $user->public_id, 'name' => 'Large', 'price_delta_minor' => 1000]);
$product->update(['station_id' => $station->id, 'course' => 'drink']);
$product->modifierGroups()->sync([$group->id]);
// Open a takeaway ticket.
$this->actingAs($user)->post(route('pos.tickets.open'), ['order_type' => 'takeaway'])->assertRedirect();
$sale = PosSale::where('owner_ref', $user->public_id)->firstOrFail();
// Add the product with the Large modifier — price = base 2500 + 1000.
$this->actingAs($user)->postJson(route('pos.tickets.lines.add', $sale), [
'product_id' => $product->id,
'modifier_ids' => [$large->id],
'quantity' => 1,
])->assertOk()->assertJsonPath('total_minor', 3500);
$line = $sale->lines()->firstOrFail();
$this->assertSame(3500, $line->unit_price_minor);
$this->assertSame($station->id, $line->station_id);
$this->assertSame('drink', $line->course);
$this->assertSame('Large', $line->modifiers()->value('name'));
// Fire only the "drink" course.
$this->actingAs($user)->postJson(route('pos.tickets.send', $sale), ['course' => 'drink'])
->assertOk()->assertJsonPath('fired', 1);
// KDS feed carries the station + modifier.
$this->actingAs($user)->getJson(route('pos.kitchen.feed'))
->assertOk()
->assertJsonPath('tickets.0.lines.0.station', 'Bar')
->assertJsonPath('tickets.0.lines.0.modifiers.0', 'Large');
}
public function test_cannot_open_two_tabs_on_one_table(): void
{
$user = $this->user();
[, $table] = $this->restaurant($user);
$this->actingAs($user)->post(route('pos.tickets.open'), [
'order_type' => 'dine_in', 'table_id' => $table->id,
])->assertRedirect();
$this->actingAs($user)->post(route('pos.tickets.open'), [
'order_type' => 'dine_in', 'table_id' => $table->id,
])->assertRedirect();
$this->assertSame(1, PosSale::where('owner_ref', $user->public_id)->count());
}
}