f = receiptFixture(); $this->author = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::Requester]); $this->actingAs($this->author); $this->payload = ['operation_key' => (string) Str::uuid(), 'cost_center_id' => $this->f['r']->cost_center_id, 'priority' => 'normal', 'needed_by' => today()->addWeek()->toDateString(), 'justification' => 'Pedido sintético para classificação explícita.', 'items' => [['description' => 'Material sintético', 'category' => 'Categoria anterior', 'purchase_type' => 'consumable', 'quantity' => '2.000', 'unit' => 'cx', 'unit_price' => '10.00', 'tax_rate' => '16.00']]]; }); test('office classification and its explicit text justification persist through the native HTTP workflow and audit', function () { $this->payload['items'][0] += ['procurement_category' => 'office_equipment', 'category_reason' => 'Artigo de escritório ainda sem entrada no catálogo.']; $this->postJson('/requisicoes', $this->payload)->assertRedirect(); $request = Requisition::query()->where('requester_id', $this->author->id)->sole(); $line = $request->items()->sole(); expect($request->draft_input)->toBeNull()->and($line->category)->toBe('Categoria anterior') ->and($line->category_context)->toBe(['version' => 1, 'category' => 'office_equipment', 'reason' => $this->payload['items'][0]['category_reason']]) ->and($request->total_minor)->toBe(2320) ->and(DB::table('budget_entries')->where('requisition_id', $request->id)->count())->toBe(0); $event = DB::table('audit_logs')->where('requisition_id', $request->id)->where('action', 'requisition.created')->sole(); expect($event)->not->toBeNull(); }); test('unfinished category requirements are saved as an incomplete draft and cannot reserve or submit', function () { $this->payload['items'][0]['procurement_category'] = 'office_equipment'; $request = app(RequisitionWorkflow::class)->save($this->author, $this->payload); expect($request->draft_input['incomplete'])->toBeTrue()->and($request->items()->count())->toBe(0) ->and($request->draft_input['values']['items'][0]['procurement_category'])->toBe('office_equipment'); $this->postJson('/requisicoes/'.$request->id.'/submeter', ['operation_key' => (string) Str::uuid(), 'process_version' => $request->process_version])->assertUnprocessable(); expect($request->fresh()->status)->toBe('draft')->and(DB::table('budget_entries')->where('requisition_id', $request->id)->count())->toBe(0); }); test('production inputs use the native published stock catalogue and retain its authoritative descriptors', function () { $stock = $this->f['stock']->fresh(); $this->actingAs($this->f['receiver']); app(RequisitionCatalogue::class)->save($this->f['receiver'], $stock->id, ['version' => 0, 'stock_basis' => app(RequisitionCatalogue::class)->basis($stock), 'category' => 'Material catalogado', 'purchase_type' => 'consumable', 'published' => true, 'reason' => 'Publicação sintética para a classificação.']); $entry = DB::table('requisition_catalogue')->where('stock_item_id', $stock->id)->sole(); $this->actingAs($this->author); $this->payload['items'] = [['catalogue_entry_id' => $entry->id, 'catalogue_version' => $entry->version, 'procurement_category' => 'production_input', 'quantity' => '2.000', 'unit_price' => '10.00', 'tax_rate' => '16.00']]; $request = app(RequisitionWorkflow::class)->save($this->author, $this->payload); expect($request->draft_input)->toBeNull()->and($request->items()->sole()->category_context['category'])->toBe('production_input') ->and($request->items()->sole()->catalogue_snapshot['stock_item_id'])->toBe($stock->id) ->and($request->items()->sole()->category)->toBe('Material catalogado'); }); test('unclassified legacy lines remain unchanged and adding an empty context preserves the accepted technical basis', function () { $request = app(RequisitionWorkflow::class)->save($this->author, $this->payload); expect($request->items()->sole()->category_context)->toBeNull(); $withEmptyColumn = app(EquipmentWorkflow::class)->basis($request); Schema::table('requisition_items', fn ($table) => $table->dropColumn('category_context')); expect(app(EquipmentWorkflow::class)->basis($request->fresh()))->toBe($withEmptyColumn); $migration = require database_path('migrations/2026_11_22_000005_add_requisition_category_context.php'); $migration->up(); expect(app(EquipmentWorkflow::class)->basis($request->fresh()))->toBe($withEmptyColumn) ->and(fn () => $migration->down())->toThrow(LogicException::class); }); test('unknown classification and a missing schema are rejected without silently discarding inputs', function () { $this->payload['items'][0]['procurement_category'] = 'unknown'; $this->postJson('/requisicoes', $this->payload)->assertUnprocessable(); Schema::table('requisition_items', fn ($table) => $table->dropColumn('category_context')); $this->payload['items'][0]['procurement_category'] = 'office_equipment'; $this->payload['items'][0]['category_reason'] = 'Razão explícita de texto livre.'; $this->postJson('/requisicoes', $this->payload)->assertUnprocessable(); expect(Requisition::query()->where('requester_id', $this->author->id)->count())->toBe(0); }); test('capital classification retains the native equipment controls and requires an explicit investment reason', function () { $this->payload['items'][0] += ['procurement_category' => 'capital_equipment', 'category_reason' => 'Investimento sintético para ampliar a capacidade.', 'production_area' => 'Área sintética']; $this->payload['items'][0]['purchase_type'] = 'equipment'; $request = app(RequisitionWorkflow::class)->save($this->author, $this->payload); expect($request->draft_input)->toBeNull()->and($request->items()->sole()->purchase_type)->toBe('equipment') ->and($request->items()->sole()->category_context['category'])->toBe('capital_equipment'); $this->postJson('/requisicoes/'.$request->id.'/submeter', ['operation_key' => (string) Str::uuid(), 'process_version' => $request->process_version])->assertUnprocessable(); expect($request->fresh()->status)->toBe('draft'); }); test('an older client preserves the exact classification by native line identity and only an explicit clear removes it', function () { $this->payload['items'][0] += ['procurement_category' => 'office_equipment', 'category_reason' => 'Necessidade sintética sem entrada no catálogo.']; $request = app(RequisitionWorkflow::class)->save($this->author, $this->payload); $original = $request->items()->sole()->category_context; $this->payload['items'][0]['source_line_id'] = $request->items()->sole()->id; unset($this->payload['items'][0]['procurement_category'], $this->payload['items'][0]['category_reason']); $this->payload['operation_key'] = (string) Str::uuid(); $this->payload['process_version'] = $request->process_version; $saved = app(RequisitionWorkflow::class)->save($this->author, $this->payload, $request); expect($saved->items()->sole()->category_context)->toBe($original); $this->payload['items'][0] += ['procurement_category' => '', 'category_reason' => '']; $this->payload['operation_key'] = (string) Str::uuid(); $this->payload['process_version'] = $saved->process_version; $saved = app(RequisitionWorkflow::class)->save($this->author, $this->payload, $saved); expect($saved->items()->sole()->category_context)->toBeNull(); });