feat: enhance raw material stock filtering in index view, adding tests for low stock and out of stock scenarios
This commit is contained in:
parent
fe5dfd382f
commit
fa4f6d4737
@ -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]);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -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 ───────────────────────────────────────────────
|
||||
|
||||
Loading…
Reference in New Issue
Block a user