Files
GSB-Construction/tests/Feature/EstimatedVsUnestimatedRequisitionTest.php

246 lines
9.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\MasterData\Models\Material;
use Modules\MaterialLogistics\Models\MaterialRequisition;
use Modules\MaterialLogistics\Models\Warehouse;
use Modules\ProjectManagement\Enums\ProjectStatus;
use Modules\ProjectManagement\Models\Project;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;
class EstimatedVsUnestimatedRequisitionTest extends TestCase
{
use RefreshDatabase;
protected User $admin;
protected Project $project;
protected Material $cement;
protected Material $sealant;
protected Warehouse $warehouse;
protected function setUp(): void
{
parent::setUp();
$role = Role::firstOrCreate(['name' => 'Super Admin', 'guard_name' => 'web']);
Permission::firstOrCreate(['name' => 'projects.access', 'guard_name' => 'web']);
Permission::firstOrCreate(['name' => 'materials-catalog.access', 'guard_name' => 'web']);
Permission::firstOrCreate(['name' => 'inventory.access', 'guard_name' => 'web']);
$this->admin = User::factory()->create([
'status' => 'active',
'user_type' => 'admin',
]);
$this->admin->assignRole($role);
$this->cement = Material::create([
'name' => 'Portland Cement Type 1',
'sku' => 'CEM-POR-001',
'unit' => 'bag',
'unit_cost' => 300.00,
'category' => 'cement',
'type' => 'raw',
'status' => 'active',
]);
$this->sealant = Material::create([
'name' => 'Polyurethane Sealant',
'sku' => 'SLN-PLY-002',
'unit' => 'gallon',
'unit_cost' => 1000.00,
'category' => 'chemicals',
'type' => 'raw',
'status' => 'active',
]);
$this->project = Project::create([
'name' => 'Highrise Horizon Tower',
'code' => 'PRJ-HRZ-2026',
'status' => ProjectStatus::InProgress,
'project_type' => 'standard',
'is_unprofitable' => false,
'contract_value' => 5000000.00,
'total_capitalization' => 0.00,
'current_wizard_step' => 8,
]);
// Add 100 bags of cement to baseline estimation
$this->project->materialsEstimates()->create([
'material_id' => $this->cement->id,
'estimated_qty' => 100,
'unit_cost' => 300.00,
]);
$this->warehouse = Warehouse::create([
'name' => 'Horizon Project Site Warehouse',
'code' => 'WH-HRZ-001',
'project_id' => $this->project->id,
'is_active' => true,
]);
}
public function test_estimated_materials_populate_remaining_quantities_and_lock_when_exhausted(): void
{
// 1. Initial MR Create check -> 100 bags remaining, hasRemainingEstimates is true
$initialCreateResp = $this->actingAs($this->admin)->get(route('requisitions.create', [
'project_ulid' => $this->project->ulid,
]));
$initialCreateResp->assertOk();
$this->assertTrue($initialCreateResp->viewData('page')['props']['hasRemainingEstimates']);
$prefilled = $initialCreateResp->viewData('page')['props']['prefilledItems'];
$this->assertCount(1, $prefilled);
$this->assertEquals(100, $prefilled[0]['remaining_qty']);
// 2. Request all 100 bags in first MR
$storeMR1Resp = $this->actingAs($this->admin)->post(route('requisitions.store'), [
'project_ulid' => $this->project->ulid,
'requisition_type' => 'estimated',
'needed_by_date' => now()->addDays(5)->format('Y-m-d'),
'purpose' => 'Foundation slab concrete pouring',
'notes' => 'First batch: all 100 bags of cement.',
'items' => [
[
'material_ulid' => $this->cement->ulid,
'quantity' => 100,
'unit_cost' => 300.00,
'is_unestimated' => false,
],
],
]);
$storeMR1Resp->assertRedirect();
$mr1 = MaterialRequisition::where('project_id', $this->project->id)->firstOrFail();
$this->assertEquals('estimated', $mr1->requisition_type);
$this->assertFalse($mr1->items->first()->is_unestimated);
// 3. Second MR Create check -> All estimated materials requested -> hasRemainingEstimates is false (LOCKED)
$secondCreateResp = $this->actingAs($this->admin)->get(route('requisitions.create', [
'project_ulid' => $this->project->ulid,
]));
$secondCreateResp->assertOk();
$this->assertFalse($secondCreateResp->viewData('page')['props']['hasRemainingEstimates']);
$this->assertEmpty($secondCreateResp->viewData('page')['props']['prefilledItems']);
}
public function test_unestimated_materials_prices_are_added_to_project_total_capitalization(): void
{
$this->project->refresh();
$this->assertEquals(0.00, (float) $this->project->total_capitalization);
// 1. Create MR with unestimated materials (15 gallons of sealant @ 1,000 PHP = 15,000 PHP)
$unestimatedMRResp = $this->actingAs($this->admin)->post(route('requisitions.store'), [
'project_ulid' => $this->project->ulid,
'requisition_type' => 'unestimated',
'needed_by_date' => now()->addDays(3)->format('Y-m-d'),
'purpose' => 'Basement waterproofing seal',
'notes' => 'Urgent missed sealant required for joint waterproofing.',
'items' => [
[
'material_ulid' => $this->sealant->ulid,
'quantity' => 15,
'unit_cost' => 1000.00,
'is_unestimated' => true,
],
],
]);
$unestimatedMRResp->assertRedirect();
$mr = MaterialRequisition::where('requisition_type', 'unestimated')->firstOrFail();
$this->assertTrue($mr->items->first()->is_unestimated);
$this->assertEquals(15000.00, (float) $mr->total_cost);
// 2. Verify that the project total_capitalization was automatically increased by 15,000 PHP
$this->project->refresh();
$this->assertEquals(15000.00, (float) $this->project->total_capitalization);
// 3. Verify that Project Show page reflects the updated capitalization
$showResp = $this->actingAs($this->admin)->get(route('projects.show', $this->project));
$showResp->assertOk();
$this->assertEquals(15000.00, (float) $showResp->viewData('page')['props']['project']['total_capitalization']);
}
public function test_cannot_request_estimated_materials_exceeding_project_estimates(): void
{
// Baseline estimate is 100 bags of cement. Attempting to request 120 bags in Estimated mode.
$response = $this->actingAs($this->admin)->post(route('requisitions.store'), [
'project_ulid' => $this->project->ulid,
'requisition_type' => 'estimated',
'needed_by_date' => now()->addDays(5)->format('Y-m-d'),
'purpose' => 'Foundation slab pouring',
'notes' => 'Attempting over-requisition of estimated cement.',
'items' => [
[
'material_ulid' => $this->cement->ulid,
'quantity' => 120,
'unit_cost' => 300.00,
'is_unestimated' => false,
],
],
]);
$response->assertSessionHas('error');
$this->assertStringContainsString('exceeds the project\'s remaining estimate', session('error'));
$this->assertDatabaseMissing('material_requisitions', [
'project_id' => $this->project->id,
'notes' => 'Attempting over-requisition of estimated cement.',
]);
}
public function test_cannot_request_material_not_in_project_estimates_under_estimated_mode(): void
{
// Sealant is not in project materials baseline estimates
$response = $this->actingAs($this->admin)->post(route('requisitions.store'), [
'project_ulid' => $this->project->ulid,
'requisition_type' => 'estimated',
'needed_by_date' => now()->addDays(5)->format('Y-m-d'),
'purpose' => 'Joint waterproofing',
'notes' => 'Attempting to request unestimated sealant as estimated item.',
'items' => [
[
'material_ulid' => $this->sealant->ulid,
'quantity' => 5,
'unit_cost' => 1000.00,
'is_unestimated' => false,
],
],
]);
$response->assertSessionHas('error');
$this->assertStringContainsString('has no remaining estimated quantity on this project', session('error'));
}
public function test_can_request_additional_or_unestimated_materials_under_unestimated_mode(): void
{
// Baseline estimate is 100 bags. User requests 50 supplemental bags via unestimated mode.
$response = $this->actingAs($this->admin)->post(route('requisitions.store'), [
'project_ulid' => $this->project->ulid,
'requisition_type' => 'unestimated',
'needed_by_date' => now()->addDays(5)->format('Y-m-d'),
'purpose' => 'Supplemental works',
'notes' => 'Supplemental cement requested via unestimated mode.',
'items' => [
[
'material_ulid' => $this->cement->ulid,
'quantity' => 50,
'unit_cost' => 300.00,
'is_unestimated' => true,
],
],
]);
$response->assertRedirect();
$this->assertDatabaseHas('material_requisition_items', [
'material_id' => $this->cement->id,
'quantity' => 50,
'is_unestimated' => true,
]);
}
}