'Super Admin']); Permission::firstOrCreate(['name' => 'projects.access']); $this->user = User::factory()->create([ 'status' => 'active', 'user_type' => 'admin', ]); $this->user->assignRole($role); // Standard Material $this->material = Material::create([ 'name' => 'Standard Portland Cement', 'sku' => 'CEM-POR-001', 'unit' => 'bag', 'unit_cost' => 350.00, 'category' => 'cement', 'type' => 'raw', 'status' => 'active', ]); // Create Project (defaults to planning state because no estimates are defined) $this->project = Project::create([ 'name' => 'Terminal A Expansion', 'code' => 'PRJ-2026-999', 'status' => ProjectStatus::Planning, 'project_type' => 'standard', 'is_unprofitable' => false, 'contract_value' => 100000.00, ]); } public function test_mr_creation_blocked_when_project_is_in_planning(): void { $response = $this->actingAs($this->user)->post(route('requisitions.store'), [ 'project_ulid' => $this->project->ulid, 'notes' => 'Urgent materials', 'items' => [ [ 'material_ulid' => $this->material->ulid, 'quantity' => 10, 'unit_cost' => 350.00, ] ] ]); $response->assertSessionHas('error'); $this->assertDatabaseMissing('material_requisitions', [ 'project_id' => $this->project->id, ]); } public function test_mr_creation_allowed_when_project_has_estimates(): void { // Add materials estimate to project $this->project->materialsEstimates()->create([ 'material_id' => $this->material->id, 'estimated_qty' => 50, 'unit_cost' => 350.00, ]); // Add a dummy task so it progresses from 'estimated' to 'scheduled' state $this->project->tasks()->create([ 'name' => 'Foundation Pouring', 'status' => 'pending', 'labor_cost' => 0, 'estimated_hours' => 10, 'actual_hours' => 0, 'completion_percentage' => 0, 'sort_order' => 1, ]); $this->project->update(['current_wizard_step' => 8]); $response = $this->actingAs($this->user)->post(route('requisitions.store'), [ 'project_ulid' => $this->project->ulid, 'notes' => 'Allowed materials', 'items' => [ [ 'material_ulid' => $this->material->ulid, 'quantity' => 10, 'unit_cost' => 350.00, ] ] ]); $response->assertRedirect(); $this->assertDatabaseHas('material_requisitions', [ 'project_id' => $this->project->id, 'notes' => 'Allowed materials', ]); } }