|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\Finance\Models\PriceList; |
| 6 | +use App\Modules\Finance\Models\PriceListItem; |
| 7 | +use App\Modules\Inventory\Models\Product; |
| 8 | +use Database\Seeders\RolePermissionSeeder; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->seed(RolePermissionSeeder::class); |
| 12 | + $this->tenant = Tenant::create(['name' => 'Price Co', 'slug' => 'price-co-' . uniqid()]); |
| 13 | + $this->user = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 14 | + $this->user->assignRole('super-admin'); |
| 15 | + $this->token = $this->user->createToken('test')->plainTextToken; |
| 16 | + app()->instance('tenant', $this->tenant); |
| 17 | +}); |
| 18 | + |
| 19 | +function makePriceProduct(array $attrs = []): Product |
| 20 | +{ |
| 21 | + return Product::create([ |
| 22 | + 'tenant_id' => test()->tenant->id, |
| 23 | + 'name' => 'Item ' . uniqid(), |
| 24 | + 'sku' => 'SKU-PL-' . uniqid(), |
| 25 | + 'sale_price' => 100.00, |
| 26 | + 'cost_price' => 50.00, |
| 27 | + 'is_active' => true, |
| 28 | + ...$attrs, |
| 29 | + ]); |
| 30 | +} |
| 31 | + |
| 32 | +test('can create a price list', function () { |
| 33 | + $this->withToken($this->token) |
| 34 | + ->postJson('/api/v1/price-lists', [ |
| 35 | + 'name' => 'VIP Pricing', |
| 36 | + 'discount_percent' => 15.0, |
| 37 | + 'currency_code' => 'USD', |
| 38 | + ]) |
| 39 | + ->assertStatus(201) |
| 40 | + ->assertJsonPath('data.name', 'VIP Pricing'); |
| 41 | +}); |
| 42 | + |
| 43 | +test('can list price lists', function () { |
| 44 | + PriceList::create([ |
| 45 | + 'tenant_id' => $this->tenant->id, |
| 46 | + 'name' => 'Standard', |
| 47 | + 'is_active' => true, |
| 48 | + 'is_default' => true, |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->withToken($this->token) |
| 52 | + ->getJson('/api/v1/price-lists') |
| 53 | + ->assertStatus(200) |
| 54 | + ->assertJsonStructure(['data']); |
| 55 | +}); |
| 56 | + |
| 57 | +test('can view price list with items', function () { |
| 58 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Retail', 'is_active' => true, 'is_default' => false]); |
| 59 | + $product = makePriceProduct(); |
| 60 | + |
| 61 | + PriceListItem::create([ |
| 62 | + 'tenant_id' => $this->tenant->id, |
| 63 | + 'price_list_id' => $pl->id, |
| 64 | + 'product_id' => $product->id, |
| 65 | + 'unit_price' => 85.00, |
| 66 | + 'min_quantity' => 1, |
| 67 | + ]); |
| 68 | + |
| 69 | + $response = $this->withToken($this->token) |
| 70 | + ->getJson("/api/v1/price-lists/{$pl->id}") |
| 71 | + ->assertStatus(200) |
| 72 | + ->assertJsonStructure(['data' => ['id', 'name', 'items']]); |
| 73 | + |
| 74 | + expect(count($response->json('data.items')))->toBe(1); |
| 75 | +}); |
| 76 | + |
| 77 | +test('can add a product to a price list', function () { |
| 78 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Wholesale', 'is_active' => true, 'is_default' => false]); |
| 79 | + $product = makePriceProduct(); |
| 80 | + |
| 81 | + $this->withToken($this->token) |
| 82 | + ->postJson("/api/v1/price-lists/{$pl->id}/items", [ |
| 83 | + 'product_id' => $product->id, |
| 84 | + 'unit_price' => 75.00, |
| 85 | + 'min_quantity' => 10, |
| 86 | + ]) |
| 87 | + ->assertStatus(201) |
| 88 | + ->assertJsonPath('data.unit_price', '75.0000'); |
| 89 | +}); |
| 90 | + |
| 91 | +test('can remove a product from a price list', function () { |
| 92 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Temp PL', 'is_active' => true, 'is_default' => false]); |
| 93 | + $product = makePriceProduct(); |
| 94 | + |
| 95 | + $item = PriceListItem::create([ |
| 96 | + 'tenant_id' => $this->tenant->id, |
| 97 | + 'price_list_id' => $pl->id, |
| 98 | + 'product_id' => $product->id, |
| 99 | + 'unit_price' => 90.00, |
| 100 | + 'min_quantity' => 1, |
| 101 | + ]); |
| 102 | + |
| 103 | + $this->withToken($this->token) |
| 104 | + ->deleteJson("/api/v1/price-lists/{$pl->id}/items/{$item->id}") |
| 105 | + ->assertStatus(200); |
| 106 | + |
| 107 | + expect(PriceListItem::find($item->id))->toBeNull(); |
| 108 | +}); |
| 109 | + |
| 110 | +test('can lookup product price from a price list', function () { |
| 111 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Lookup PL', 'is_active' => true, 'is_default' => false]); |
| 112 | + $product = makePriceProduct(); |
| 113 | + |
| 114 | + PriceListItem::create([ |
| 115 | + 'tenant_id' => $this->tenant->id, |
| 116 | + 'price_list_id' => $pl->id, |
| 117 | + 'product_id' => $product->id, |
| 118 | + 'unit_price' => 80.00, |
| 119 | + 'min_quantity' => 1, |
| 120 | + ]); |
| 121 | + |
| 122 | + $response = $this->withToken($this->token) |
| 123 | + ->postJson('/api/v1/price-lists/lookup', [ |
| 124 | + 'price_list_id' => $pl->id, |
| 125 | + 'product_id' => $product->id, |
| 126 | + 'quantity' => 1, |
| 127 | + ]) |
| 128 | + ->assertStatus(200); |
| 129 | + |
| 130 | + expect((float) $response->json('data.unit_price'))->toBe(80.0); |
| 131 | + expect($response->json('data.has_override'))->toBeTrue(); |
| 132 | +}); |
| 133 | + |
| 134 | +test('lookup returns null for products not on price list', function () { |
| 135 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Empty PL', 'is_active' => true, 'is_default' => false]); |
| 136 | + $product = makePriceProduct(); |
| 137 | + |
| 138 | + $response = $this->withToken($this->token) |
| 139 | + ->postJson('/api/v1/price-lists/lookup', [ |
| 140 | + 'price_list_id' => $pl->id, |
| 141 | + 'product_id' => $product->id, |
| 142 | + ]) |
| 143 | + ->assertStatus(200); |
| 144 | + |
| 145 | + expect($response->json('data.unit_price'))->toBeNull(); |
| 146 | + expect($response->json('data.has_override'))->toBeFalse(); |
| 147 | +}); |
| 148 | + |
| 149 | +test('setting default clears other defaults', function () { |
| 150 | + $pl1 = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'PL1', 'is_active' => true, 'is_default' => true]); |
| 151 | + $pl2 = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'PL2', 'is_active' => true, 'is_default' => false]); |
| 152 | + |
| 153 | + $this->withToken($this->token) |
| 154 | + ->putJson("/api/v1/price-lists/{$pl2->id}", ['is_default' => true]) |
| 155 | + ->assertStatus(200); |
| 156 | + |
| 157 | + expect($pl1->fresh()->is_default)->toBeFalse(); |
| 158 | + expect($pl2->fresh()->is_default)->toBeTrue(); |
| 159 | +}); |
| 160 | + |
| 161 | +test('can soft delete a price list', function () { |
| 162 | + $pl = PriceList::create(['tenant_id' => $this->tenant->id, 'name' => 'Delete Me', 'is_active' => true, 'is_default' => false]); |
| 163 | + |
| 164 | + $this->withToken($this->token) |
| 165 | + ->deleteJson("/api/v1/price-lists/{$pl->id}") |
| 166 | + ->assertStatus(200); |
| 167 | + |
| 168 | + expect(PriceList::withTrashed()->find($pl->id)?->deleted_at)->not->toBeNull(); |
| 169 | +}); |
| 170 | + |
| 171 | +test('requires authentication', function () { |
| 172 | + $this->getJson('/api/v1/price-lists')->assertStatus(401); |
| 173 | +}); |
0 commit comments