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_guest_table_qr_order_fires_to_kitchen(): void { $user = $this->user(); [, $table, $product] = $this->restaurant($user); $code = $table->ensureShortCode(); // Public menu loads with no auth. $this->get(route('pos.table.menu', $code))->assertOk()->assertSee($product->name); // Guest submits an order from the table. $this->postJson(route('pos.table.order', $code), [ 'customer_name' => 'Ama', 'items' => [ ['product_id' => $product->id, 'quantity' => 2, 'modifier_ids' => [], 'notes' => 'extra hot'], ], ])->assertOk()->assertJsonStructure(['redirect']); $sale = PosSale::where('owner_ref', $user->public_id)->firstOrFail(); $this->assertSame(PosSale::ORDER_DINE_IN, $sale->order_type); $this->assertSame($table->id, $sale->table_id); $this->assertSame('Ama', $sale->customer_name); $this->assertSame(PosSale::KITCHEN_ACTIVE, $sale->kitchen_status); $this->assertSame(PosTable::STATUS_OCCUPIED, $table->fresh()->status); $line = $sale->lines()->firstOrFail(); $this->assertSame('guest', $line->source); $this->assertSame(2, $line->quantity); $this->assertSame('extra hot', $line->notes); $this->assertSame(PosSaleLine::KITCHEN_QUEUED, $line->kitchen_state); } public function test_split_bill_settles_over_multiple_cash_payments(): void { $user = $this->user(); [, $table, $product] = $this->restaurant($user); // Latte 2500 $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->actingAs($user)->postJson(route('pos.tickets.lines.add', $sale), ['product_id' => $product->id, 'quantity' => 1])->assertOk(); // Partial cash payment — ticket stays open with a balance. $this->actingAs($user)->post(route('pos.tickets.settle', $sale), ['payment_method' => 'cash', 'amount' => '10.00'])->assertRedirect(); $sale->refresh(); $this->assertSame(PosSale::STATUS_PENDING, $sale->status); $this->assertSame(1000, $sale->paidMinor()); $this->assertSame(1500, $sale->balanceMinor()); $this->assertSame(PosTable::STATUS_OCCUPIED, $table->fresh()->status); // Pay the rest — settles and frees the table. $this->actingAs($user)->post(route('pos.tickets.settle', $sale), ['payment_method' => 'cash', 'amount' => '15.00']) ->assertRedirect(route('pos.sales.show', $sale)); $sale->refresh(); $this->assertSame(PosSale::STATUS_PAID, $sale->status); $this->assertSame('split', $sale->payment_method); $this->assertSame(0, $sale->balanceMinor()); $this->assertSame(PosTable::STATUS_FREE, $table->fresh()->status); } public function test_external_online_order_ingests_to_kitchen(): void { $user = $this->user(); $this->restaurant($user); config(['pos.kitchen_api_keys' => ['merchant' => 'test-key']]); $payload = [ 'owner' => $user->public_id, 'reference' => 'M-123', 'customer_name' => 'Kofi', 'items' => [['name' => 'Pizza', 'quantity' => 2, 'unit_price_minor' => 4000, 'notes' => 'thin crust']], ]; // Rejected without a valid service key. $this->postJson(route('api.kitchen.orders'), $payload)->assertStatus(401); // Accepted with the key — creates a paid, fired kitchen ticket. $this->withHeader('Authorization', 'Bearer test-key') ->postJson(route('api.kitchen.orders'), $payload) ->assertStatus(201)->assertJsonStructure(['id', 'reference']); $sale = PosSale::where('owner_ref', $user->public_id)->where('external_ref', 'M-123')->firstOrFail(); $this->assertSame(PosSale::STATUS_PAID, $sale->status); $this->assertSame('online', $sale->order_type); $this->assertSame(PosSale::KITCHEN_ACTIVE, $sale->kitchen_status); $this->assertSame(8000, $sale->total_minor); $line = $sale->lines()->firstOrFail(); $this->assertSame('online', $line->source); $this->assertSame(PosSaleLine::KITCHEN_QUEUED, $line->kitchen_state); // Idempotent by reference. $this->withHeader('Authorization', 'Bearer test-key')->postJson(route('api.kitchen.orders'), $payload)->assertStatus(201); $this->assertSame(1, PosSale::where('external_ref', 'M-123')->count()); // Shows on the KDS feed (payment-agnostic). $this->actingAs($user)->getJson(route('pos.kitchen.feed'))->assertOk()->assertJsonCount(1, 'tickets'); } public function test_register_mode_toggle_switches_service_style(): void { $user = $this->user(); // Switch to restaurant from the register — lands on the floor. $this->actingAs($user)->post(route('pos.mode.set'), ['style' => 'restaurant']) ->assertRedirect(route('pos.floor')); $this->assertSame('restaurant', PosLocation::owned($user->public_id)->first()->service_style); // Back to retail — stays on the register. $this->actingAs($user)->post(route('pos.mode.set'), ['style' => 'retail']) ->assertRedirect(route('pos.register')); $this->assertSame('retail', PosLocation::owned($user->public_id)->first()->service_style); } public function test_pending_ticket_can_be_cancelled_then_deleted(): void { $user = $this->user(); [, $table, $product] = $this->restaurant($user); $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->actingAs($user)->postJson(route('pos.tickets.lines.add', $sale), ['product_id' => $product->id, 'quantity' => 1])->assertOk(); // Cancel — frees the table. $this->actingAs($user)->post(route('pos.sales.cancel', $sale))->assertRedirect(route('pos.sales.show', $sale)); $this->assertSame(PosSale::STATUS_CANCELLED, $sale->fresh()->status); $this->assertSame(PosTable::STATUS_FREE, $table->fresh()->status); // Delete — gone (cascades lines). $this->actingAs($user)->delete(route('pos.sales.destroy', $sale))->assertRedirect(route('pos.sales.index')); $this->assertNull(PosSale::find($sale->id)); $this->assertSame(0, $sale->lines()->count()); } public function test_paid_sale_cannot_be_deleted(): void { $user = $this->user(); $sale = PosSale::create([ 'owner_ref' => $user->public_id, 'reference' => 'POS-PAID1', 'status' => PosSale::STATUS_PAID, 'payment_method' => 'cash', 'subtotal_minor' => 1000, 'total_minor' => 1000, 'currency' => 'GHS', 'paid_at' => now(), ]); $this->actingAs($user)->delete(route('pos.sales.destroy', $sale)); $this->assertNotNull(PosSale::find($sale->id)); } 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()); } }