f = receiptFixture(); supplyGrant($this->f['issuer'], ['warehouse_request_prepare']); $this->actingAs($this->f['issuer']); $this->data = ['operation_key' => (string) Str::uuid(), 'warehouse_id' => $this->f['warehouse']->id, 'cost_center_id' => $this->f['r']->cost_center_id, 'destination' => 'Oficina da unidade', 'reason' => 'Material para manutenção da unidade.', 'priority' => 'normal', 'lines' => [['stock_item_id' => $this->f['stock']->id, 'quantity' => '2.500', 'use' => 'internal', 'equipment_id' => null]]]; }); test('warehouse list and detail agree when an owner no longer has preparation or department review authority', function () { $this->withoutVite(); $this->postJson('/stock/requisicoes', $this->data)->assertRedirect(); $record = WarehouseRequest::query()->sole(); $actor = $this->f['issuer']; $desk = app(WarehouseWorkdesk::class); expect($desk->canView($actor, $record))->toBeTrue(); DB::table('organization_memberships')->where('user_id', $actor->id)->update(['expires_at' => now()->subSecond()]); supplyGrant($actor, ['warehouse_request_deliver']); expect($desk->canView($actor, $record))->toBeFalse(); $this->get('/stock/requisicoes/'.$record->id)->assertForbidden(); expect($desk->visible($actor)->whereKey($record->id)->exists())->toBeFalse(); $this->get('/stock/requisicoes')->assertOk()->assertInertia(fn (Assert $page) => $page->where('records.total', 0)); supplyGrant($actor, ['warehouse_request_read']); expect($desk->canView($actor, $record))->toBeTrue(); $this->get('/stock/requisicoes')->assertOk()->assertInertia(fn (Assert $page) => $page->where('records.total', 1)); }); test('global warehouse search uses native references event state literal terms and pagination without effects', function () { $this->withoutVite(); config(['procurement_search.warehouse_requests_metadata_enabled' => true]); $this->postJson('/stock/requisicoes', [...$this->data, 'destination' => 'GLOBAL-WAREHOUSE%_QA'])->assertRedirect(); $record = WarehouseRequest::query()->sole(); $this->postJson('/stock/requisicoes/'.$record->id.'/eventos', ['operation_key' => (string) Str::uuid(), 'version' => app(WarehouseFulfilment::class)->progress($record)['version'], 'action' => 'submit', 'reason' => 'Submissão sintética para conferir a projecção de pesquisa'])->assertRedirect(); foreach (range(1, 16) as $index) { $this->postJson('/stock/requisicoes', [...$this->data, 'operation_key' => (string) Str::uuid(), 'destination' => 'PAGE-WAREHOUSE-'.$index])->assertRedirect(); } $name = $record->snapshot['requester']['name']; $this->f['issuer']->update(['name' => 'Current name changed after request']); $before = []; foreach (['warehouse_requests', 'warehouse_request_events', 'audit_logs', 'stock_movements', 'requisitions', 'budget_entries'] as $table) { $before[$table] = DB::table($table)->orderBy('id')->get()->toJson(); } $this->get('/compras/pesquisa?q=RA-'.$record->id)->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private') ->assertViewHas('page', fn ($page) => $page['encryptHistory'] === true)->assertInertia(fn (Assert $page) => $page ->where('warehouseRequestSearchAvailable', true)->where('groups.1.key', 'warehouse_requests')->where('groups.1.records.total', 1) ->where('groups.1.records.data.0.title', 'RA-'.$record->id)->where('groups.1.records.data.0.detail', 'submitted') ->where('groups.1.records.data.0.href', '/stock/requisicoes/'.$record->id) ->missing('groups.1.records.data.0.snapshot')->missing('groups.1.records.data.0.destination')); $this->get('/compras/pesquisa?q='.urlencode('%_'))->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 1)); $this->get('/compras/pesquisa?q='.urlencode($name))->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 17)); $this->get('/compras/pesquisa?q=PAGE-WAREHOUSE&warehouse_requests_page=2')->assertInertia(fn (Assert $page) => $page ->where('groups.1.records.total', 16)->where('groups.1.records.current_page', 2)->has('groups.1.records.data', 1)); $this->get('/compras/pesquisa?q=PAGE-WAREHOUSE&warehouse_requests_page=0')->assertSessionHasErrors('warehouse_requests_page'); foreach ($before as $table => $rows) { expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows); } $this->get('/stock/requisicoes/'.$record->id)->assertOk(); config(['procurement_search.warehouse_requests_metadata_enabled' => false]); $this->get('/compras/pesquisa?q=RA-'.$record->id)->assertInertia(fn (Assert $page) => $page ->where('warehouseRequestSearchAvailable', false)->has('groups', 1)); }); test('global warehouse search and native read predicate agree across departments companies and withdrawn authority', function () { $this->withoutVite(); config(['procurement_search.warehouse_requests_metadata_enabled' => true]); $own = $this->f; $this->postJson('/stock/requisicoes', [...$this->data, 'destination' => 'GLOBAL-SCOPE-OWN'])->assertRedirect(); $record = WarehouseRequest::query()->sole(); $department = Department::query()->create(['tenant_id' => $own['tenant']->id, 'name' => 'Other synthetic department']); $centre = CostCenter::query()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $department->id, 'code' => 'SCOPE-QA', 'name' => 'Other synthetic centre']); $otherOwner = User::factory()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $department->id]); supplyGrant($otherOwner, ['warehouse_request_prepare']); $this->actingAs($otherOwner)->postJson('/stock/requisicoes', [...$this->data, 'operation_key' => (string) Str::uuid(), 'cost_center_id' => $centre->id, 'destination' => 'GLOBAL-SCOPE-OTHER-DEPARTMENT'])->assertRedirect(); $foreign = receiptFixture(); supplyGrant($foreign['issuer'], ['warehouse_request_prepare']); $this->actingAs($foreign['issuer'])->postJson('/stock/requisicoes', [...$this->data, 'operation_key' => (string) Str::uuid(), 'warehouse_id' => $foreign['warehouse']->id, 'cost_center_id' => $foreign['r']->cost_center_id, 'destination' => 'GLOBAL-SCOPE-OTHER-COMPANY', 'lines' => [['stock_item_id' => $foreign['stock']->id, 'quantity' => '1.000', 'use' => 'internal', 'equipment_id' => null]]])->assertRedirect(); $this->actingAs($own['issuer'])->get('/compras/pesquisa?q=GLOBAL-SCOPE')->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 1)); $reviewer = User::factory()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $own['department']->id]); supplyGrant($reviewer, ['warehouse_request_review']); $this->actingAs($reviewer)->get('/compras/pesquisa?q=GLOBAL-SCOPE')->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 1)); supplyGrant($reviewer, ['warehouse_request_read']); $this->get('/compras/pesquisa?q=GLOBAL-SCOPE')->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 2)); $this->actingAs($own['issuer']); DB::table('organization_memberships')->where('user_id', $own['issuer']->id)->update(['expires_at' => now()->subSecond()]); supplyGrant($own['issuer'], ['warehouse_request_deliver']); $this->get('/compras/pesquisa?q=GLOBAL-SCOPE')->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 0)); expect(app(WarehouseWorkdesk::class)->canView($own['issuer'], $record))->toBeFalse(); }); test('warehouse draft freezes catalogue and destination once without stock budget or purchase effects', function () { $moves = DB::table('stock_movements')->count(); $purchases = DB::table('requisitions')->count(); $this->postJson('/stock/requisicoes', $this->data)->assertRedirect(); $this->postJson('/stock/requisicoes', $this->data)->assertRedirect(); $record = WarehouseRequest::query()->sole(); expect($record->snapshot['lines'][0]['quantity_milli'])->toBe(2500)->and($record->snapshot['lines'][0]['item']['unit'])->toBe($this->f['stock']->unit) ->and($record->snapshot['destination'])->toBe($this->data['destination']) ->and(DB::table('stock_movements')->count())->toBe($moves)->and(DB::table('requisitions')->count())->toBe($purchases) ->and(DB::table('budget_entries')->count())->toBe(0); $this->f['stock']->update(['name' => 'Nome futuro do artigo']); expect($record->fresh()->snapshot['lines'][0]['item']['name'])->not->toBe('Nome futuro do artigo'); $this->postJson('/stock/requisicoes', [...$this->data, 'destination' => 'Outro local'])->assertUnprocessable(); $this->get('/stock/requisicoes')->assertOk()->assertInertia(fn (Assert $p) => $p->component('stock/requests')->where('records.total', 1)->missing('records.data.0.input_hash')); }); test('equipment destination is conditional and selected from the permitted catalogue', function () { $this->data['lines'][0]['use'] = 'equipment'; $this->postJson('/stock/requisicoes', $this->data)->assertUnprocessable(); $level = CriticalityLevel::query()->create(['tenant_id' => $this->f['tenant']->id, 'created_by' => $this->f['issuer']->id, 'code' => 'NORMAL', 'name' => 'Normal', 'criteria' => 'Critério de teste.', 'is_critical' => false, 'created_at' => now()]); $equipment = Equipment::query()->create(['tenant_id' => $this->f['tenant']->id, 'created_by' => $this->f['issuer']->id, 'department_id' => $this->f['issuer']->department_id, 'criticality_level_id' => $level->id, 'code' => 'EQ-1', 'description' => 'Máquina da oficina', 'area' => 'Oficina', 'status' => 'active']); $this->data['lines'][0]['equipment_id'] = $equipment->id; $this->postJson('/stock/requisicoes', $this->data)->assertRedirect(); expect(WarehouseRequest::query()->sole()->snapshot['lines'][0]['equipment']['code'])->toBe('EQ-1'); $this->data['operation_key'] = (string) Str::uuid(); $this->data['lines'][0]['use'] = 'internal'; $this->postJson('/stock/requisicoes', $this->data)->assertUnprocessable(); $this->data['lines'][0]['use'] = 'sale'; $this->postJson('/stock/requisicoes', $this->data)->assertUnprocessable(); }); test('production input destination freezes a declared reference without requiring an external production order or creating a purchase', function () { $body = $this->data; $body['lines'][0] = [...$body['lines'][0], 'use' => 'production', 'production_reference' => 'OP-QA-001']; $purchases = DB::table('requisitions')->count(); $this->postJson('/stock/requisicoes', $body)->assertRedirect(); $this->postJson('/stock/requisicoes', $body)->assertRedirect(); $record = WarehouseRequest::query()->sole(); expect($record->snapshot['lines'][0]['production'])->toBe(['reference' => 'OP-QA-001', 'verification' => 'user_supplied']) ->and(DB::table('requisitions')->count())->toBe($purchases)->and(DB::table('stock_movements')->count())->toBe(0); $body['operation_key'] = (string) Str::uuid(); $body['lines'][0]['production_reference'] = null; $this->postJson('/stock/requisicoes', $body)->assertRedirect(); $body['operation_key'] = (string) Str::uuid(); $body['lines'][0]['use'] = 'internal'; $body['lines'][0]['production_reference'] = 'OP-QA-002'; $this->postJson('/stock/requisicoes', $body)->assertUnprocessable(); }); test('warehouse draft rejects another company sources and revoked access', function () { $foreign = receiptFixture(); $this->actingAs($this->f['issuer'])->postJson('/stock/requisicoes', [...$this->data, 'warehouse_id' => $foreign['warehouse']->id])->assertNotFound(); DB::table('organization_memberships')->where('user_id', $this->f['issuer']->id)->update(['expires_at' => now()->subMinute()]); $this->postJson('/stock/requisicoes', $this->data)->assertForbidden(); expect(DB::table('warehouse_requests')->count())->toBe(0); }); test('warehouse draft audit failure leaves no partial document', function () { $this->mock(RequisitionWorkflow::class)->shouldReceive('audit')->once()->andThrow(new RuntimeException('Injected draft audit failure')); $this->withoutExceptionHandling(); expect(fn () => $this->postJson('/stock/requisicoes', $this->data))->toThrow(RuntimeException::class); expect(DB::table('warehouse_requests')->count())->toBe(0); });