|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\Inventory\Models\Backorder; |
| 6 | +use App\Modules\Inventory\Models\Product; |
| 7 | +use App\Modules\Inventory\Models\Warehouse; |
| 8 | +use Database\Seeders\RolePermissionSeeder; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->seed(RolePermissionSeeder::class); |
| 12 | + $this->tenant = Tenant::create(['name' => 'BOcorp', 'slug' => 'bo-corp-' . uniqid()]); |
| 13 | + $this->admin = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 14 | + $this->admin->assignRole('super-admin'); |
| 15 | + $this->actingAs($this->admin); |
| 16 | + app()->instance('tenant', $this->tenant); |
| 17 | +}); |
| 18 | + |
| 19 | +function makeBOWarehouse(): Warehouse |
| 20 | +{ |
| 21 | + return Warehouse::create([ |
| 22 | + 'tenant_id' => test()->tenant->id, |
| 23 | + 'name' => 'BO-WH-' . uniqid(), |
| 24 | + ]); |
| 25 | +} |
| 26 | + |
| 27 | +function makeBOProduct(): Product |
| 28 | +{ |
| 29 | + return Product::create([ |
| 30 | + 'tenant_id' => test()->tenant->id, |
| 31 | + 'name' => 'BO Product ' . uniqid(), |
| 32 | + 'sku' => 'BP-' . strtoupper(substr(uniqid(), -6)), |
| 33 | + 'is_active' => true, |
| 34 | + ]); |
| 35 | +} |
| 36 | + |
| 37 | +function makeBackorder(array $attrs = []): Backorder |
| 38 | +{ |
| 39 | + $wh = makeBOWarehouse(); |
| 40 | + $prod = makeBOProduct(); |
| 41 | + return Backorder::create([ |
| 42 | + 'tenant_id' => test()->tenant->id, |
| 43 | + 'product_id' => $prod->id, |
| 44 | + 'warehouse_id' => $wh->id, |
| 45 | + 'quantity_ordered' => 10, |
| 46 | + ...$attrs, |
| 47 | + ]); |
| 48 | +} |
| 49 | + |
| 50 | +it('index requires authentication', function () { |
| 51 | + $this->post('/logout'); |
| 52 | + $this->get('/inventory/backorders')->assertRedirect('/login'); |
| 53 | +}); |
| 54 | + |
| 55 | +it('admin can list backorders', function () { |
| 56 | + makeBackorder(); |
| 57 | + $this->get('/inventory/backorders')->assertOk(); |
| 58 | +}); |
| 59 | + |
| 60 | +it('store creates a backorder', function () { |
| 61 | + $wh = makeBOWarehouse(); |
| 62 | + $prod = makeBOProduct(); |
| 63 | + |
| 64 | + $this->post('/inventory/backorders', [ |
| 65 | + 'product_id' => $prod->id, |
| 66 | + 'warehouse_id' => $wh->id, |
| 67 | + 'quantity_ordered' => 5, |
| 68 | + 'expected_date' => now()->addDays(7)->toDateString(), |
| 69 | + ])->assertRedirect(); |
| 70 | + |
| 71 | + expect(Backorder::where('product_id', $prod->id)->where('quantity_ordered', 5)->exists())->toBeTrue(); |
| 72 | +}); |
| 73 | + |
| 74 | +it('store validates required fields', function () { |
| 75 | + $this->postJson('/inventory/backorders', [])->assertStatus(422)->assertJsonValidationErrors(['product_id', 'warehouse_id', 'quantity_ordered']); |
| 76 | +}); |
| 77 | + |
| 78 | +it('show displays a backorder', function () { |
| 79 | + $bo = makeBackorder(); |
| 80 | + $this->get("/inventory/backorders/{$bo->id}")->assertOk(); |
| 81 | +}); |
| 82 | + |
| 83 | +it('fulfill updates quantity and status to partial', function () { |
| 84 | + $bo = makeBackorder(['quantity_ordered' => 10]); |
| 85 | + expect($bo->is_pending)->toBeTrue(); |
| 86 | + |
| 87 | + $this->post("/inventory/backorders/{$bo->id}/fulfill", ['quantity' => 4])->assertRedirect(); |
| 88 | + |
| 89 | + $bo->refresh(); |
| 90 | + expect($bo->status)->toBe('partial'); |
| 91 | + expect((float) $bo->quantity_fulfilled)->toBe(4.0); |
| 92 | + expect((float) $bo->quantity_remaining)->toBe(6.0); |
| 93 | +}); |
| 94 | + |
| 95 | +it('fulfill marks as fulfilled when quantity meets order', function () { |
| 96 | + $bo = makeBackorder(['quantity_ordered' => 5]); |
| 97 | + |
| 98 | + $this->post("/inventory/backorders/{$bo->id}/fulfill", ['quantity' => 5])->assertRedirect(); |
| 99 | + |
| 100 | + $bo->refresh(); |
| 101 | + expect($bo->is_fulfilled)->toBeTrue(); |
| 102 | + expect($bo->backorder_number)->not->toBeNull(); |
| 103 | +}); |
| 104 | + |
| 105 | +it('cancel marks as cancelled', function () { |
| 106 | + $bo = makeBackorder(); |
| 107 | + $this->post("/inventory/backorders/{$bo->id}/cancel")->assertRedirect(); |
| 108 | + $bo->refresh(); |
| 109 | + expect($bo->status)->toBe('cancelled'); |
| 110 | +}); |
| 111 | + |
| 112 | +it('quantity_remaining accessor is correct', function () { |
| 113 | + $bo = makeBackorder(['quantity_ordered' => 10, 'quantity_fulfilled' => 3]); |
| 114 | + expect((float) $bo->quantity_remaining)->toBe(7.0); |
| 115 | +}); |
| 116 | + |
| 117 | +it('destroy soft-deletes the backorder', function () { |
| 118 | + $bo = makeBackorder(); |
| 119 | + $this->delete("/inventory/backorders/{$bo->id}")->assertRedirect(); |
| 120 | + expect(Backorder::find($bo->id))->toBeNull(); |
| 121 | + expect(Backorder::withTrashed()->find($bo->id))->not->toBeNull(); |
| 122 | +}); |
0 commit comments