diff --git a/app/Services/Master/RawMaterialService.php b/app/Services/Master/RawMaterialService.php index 2173b37..eff3251 100644 --- a/app/Services/Master/RawMaterialService.php +++ b/app/Services/Master/RawMaterialService.php @@ -53,17 +53,17 @@ public function paginateForIndex(array $tableQuery, string $isActive, string $st }) ->when($stockStatus === 'low_stock', function (Builder $query): void { $minStock = [ - RawMaterialUnit::YARD => 20, - RawMaterialUnit::METER => 10, - RawMaterialUnit::KILOGRAM => 5, + RawMaterialUnit::YARD->value => 20, + RawMaterialUnit::METER->value => 10, + RawMaterialUnit::KILOGRAM->value => 5, ]; $query->whereHas('prices', function (Builder $priceQuery) use ($minStock): void { $priceQuery->where('stock', '>', 0)->where(function (Builder $priceQuery) use ($minStock): void { foreach (RawMaterialUnit::cases() as $unit) { $priceQuery->orWhere(function (Builder $priceQuery) use ($unit, $minStock): void { - $priceQuery->whereHas('rawMaterial', fn (Builder $rawMaterialQuery) => $rawMaterialQuery->where('unit', $unit)) - ->where('stock', '<', $minStock[$unit]); + $priceQuery->whereHas('rawMaterial', fn (Builder $rawMaterialQuery) => $rawMaterialQuery->where('unit', $unit->value)) + ->where('stock', '<', $minStock[$unit->value]); }); } }); diff --git a/tests/Feature/Admin/Master/RawMaterialTest.php b/tests/Feature/Admin/Master/RawMaterialTest.php index 479f63f..0768ea9 100644 --- a/tests/Feature/Admin/Master/RawMaterialTest.php +++ b/tests/Feature/Admin/Master/RawMaterialTest.php @@ -141,6 +141,44 @@ function createRawMaterialVerifierUser(): User ->get(route('admin.master.raw_materials.index', ['is_active' => '1'])) ->assertOk(); }); + + test('index can filter by stock status (out of stock)', function () { + $user = createRawMaterialUserWithPermission(PermissionEnum::RAW_MATERIALS_VIEW); + + $inStock = RawMaterial::factory()->create(); + RawMaterialPrice::factory()->create(['raw_material_id' => $inStock->id, 'stock' => 10]); + + $outOfStock = RawMaterial::factory()->create(); + RawMaterialPrice::factory()->create(['raw_material_id' => $outOfStock->id, 'stock' => 0]); + + $response = $this->actingAs($user) + ->get(route('admin.master.raw_materials.index', ['stock_status' => 'out_of_stock'])); + + $response->assertOk(); + $response->assertInertia(fn ($page) => $page + ->has('rawMaterials.data', 1) + ->where('rawMaterials.data.0.id', $outOfStock->id) + ); + }); + + test('index can filter by stock status (low stock)', function () { + $user = createRawMaterialUserWithPermission(PermissionEnum::RAW_MATERIALS_VIEW); + + $yardNormal = RawMaterial::factory()->create(['unit' => RawMaterialUnit::YARD->value]); + RawMaterialPrice::factory()->create(['raw_material_id' => $yardNormal->id, 'stock' => 25]); + + $yardLow = RawMaterial::factory()->create(['unit' => RawMaterialUnit::YARD->value]); + RawMaterialPrice::factory()->create(['raw_material_id' => $yardLow->id, 'stock' => 15]); + + $response = $this->actingAs($user) + ->get(route('admin.master.raw_materials.index', ['stock_status' => 'low_stock'])); + + $response->assertOk(); + $response->assertInertia(fn ($page) => $page + ->has('rawMaterials.data', 1) + ->where('rawMaterials.data.0.id', $yardLow->id) + ); + }); }); // ─── Create ───────────────────────────────────────────────