selectOne('SELECT current_database() AS database, inet_server_port() AS port'); if ($target !== ['database'=>'stability_consumers_20261011_084534_test','port'=>55441]) { throw new RuntimeException('QA isolada obrigatória.'); } $db->beginTransaction(); $db->statement('SET TRANSACTION READ ONLY'); try { $record = App\Models\Opportunity::findOrFail(32); $context = $record->workflow_context ?? []; $currentValues = app(App\Services\OpportunityManagement::class)->values($record); $cutoff = new DateTimeImmutable('2026-10-13T22:30:15+00:00'); $events = array_values(array_filter($context['events'] ?? [], fn ($e)=> isset($e['at']) && new DateTimeImmutable($e['at']) > $cutoff && in_array($e['action'] ?? '', ['request_correction','submit'], true))); $events = array_map(fn ($e)=>array_intersect_key($e,array_flip(['action','actor_id','at','from','to','review_cycle','content_revision'])), $events); $audits = $db->select("SELECT id,created_at,source_system,action,actor_user_id, after_data->>'version' AS saved_version, after_data->'before'->>'status' AS state_before, after_data->'after'->>'status' AS state_after, after_data->'after'->>'version' AS workflow_saved_version FROM audit_events WHERE after_data->>'record_id'='32' AND source_system IN ('portal-profiles-v1','opportunity-management') AND action IN ('workflow_request_correction','draft_updated','workflow_submit') AND created_at >= ?::timestamptz ORDER BY created_at", [$cutoff->format('c')]); $saveAudit = $db->table('audit_events')->where('source_system','opportunity-management') ->where('action','draft_updated')->where('actor_user_id',21) ->whereRaw("after_data->>'record_id' = ?",['32']) ->where('created_at','>=',$cutoff->format('c'))->latest('created_at')->first(['id','before_data','after_data']); $comparison = ['QA21_save_present'=>$saveAudit!==null]; if ($saveAudit) { $beforeEnvelope = json_decode($saveAudit->before_data,true,512,JSON_THROW_ON_ERROR); $afterEnvelope = json_decode($saveAudit->after_data,true,512,JSON_THROW_ON_ERROR); $beforeAttributes = json_decode(Illuminate\Support\Facades\Crypt::decryptString($beforeEnvelope['snapshot']),true,512,JSON_THROW_ON_ERROR); $afterAttributes = json_decode(Illuminate\Support\Facades\Crypt::decryptString($afterEnvelope['snapshot']),true,512,JSON_THROW_ON_ERROR); $old = new App\Models\Opportunity(); $old->setRawAttributes($beforeAttributes,true); $saved = new App\Models\Opportunity(); $saved->setRawAttributes($afterAttributes,true); $management = app(App\Services\OpportunityManagement::class); $oldValues = $management->values($old); $savedValues = $management->values($saved); $oldFlat = Illuminate\Support\Arr::dot($oldValues['canonical'] ?? []); $savedFlat = Illuminate\Support\Arr::dot($savedValues['canonical'] ?? []); $changedPaths = array_values(array_filter(array_unique([...array_keys($oldFlat),...array_keys($savedFlat)]),fn ($key)=>($oldFlat[$key]??null)!==($savedFlat[$key]??null))); $comparison += ['audit_id'=>$saveAudit->id,'saved_version'=>(int)$afterEnvelope['version'], 'changed_canonical_paths'=>$changedPaths, 'description_changed'=>($oldValues['description']??null)!==($savedValues['description']??null), 'canonical_demand_statement_changed'=>($oldValues['canonical']['demand_statement']??null)!==($savedValues['canonical']['demand_statement']??null), 'land_contract_unchanged'=>($oldValues['canonical']['land_rights']??null)===($savedValues['canonical']['land_rights']??null), 'organization_unchanged'=>$old->organization_id===$saved->organization_id, 'author_unchanged'=>$old->owner_user_id===$saved->owner_user_id]; $oldContext = $old->workflow_context ?? []; $comparison['submission_resource_snapshot_unchanged'] = ($oldContext['submission']['resources']??[])===($context['submission']['resources']??[]); } $submission = $context['submission'] ?? []; $materiality = app(App\Services\OpportunityReviewMateriality::class); $gateResults = []; foreach (app(App\Services\OpportunityCanonicalContract::class)->gates($record) as $gate) { $review = $context['reviews'][$gate] ?? []; $accepted = ($review['hash']??'')===($submission['hash']??'') || ($review['accepted_submission_hash']??'')===($submission['hash']??''); $gateResults[] = ['gate'=>$gate,'decision'=>$review['decision']??null, 'actor_QA_account'=>$review['actor_id']??null,'accepted_for_current_submission'=>$accepted, 'scope_matches'=>$review!==[] && $materiality->matches($submission,$gate,$review), 'requires_new_review'=>($review['decision']??null)!=='approved'||!$accepted, 'evidence_documents_count'=>count($review['evidence_document_ids']??[])]; } $receipt = ['at'=>gmdate('c'),'base_review'=>'396c691930bc','target'=>$target,'SQL_read_only'=>true, 'user_statement'=>'acç\\ao realizada','scope_from_context'=>'Percurso3, pedido de correções e passagem para QA21.', 'opportunity_id'=>32,'working_state'=>app(App\Services\PortalProfileAccess::class)->state($record), 'management_version'=>(int)$record->management_version,'review_cycle'=>$context['review_cycle']??null, 'content_revision'=>$context['content_revision']??null, 'stored_current_text_length'=>mb_strlen($currentValues['canonical']['demand_statement']??''), 'submitted_text_length'=>mb_strlen($context['submission']['payload']['demand_statement']??''), 'current_text_matches_last_submission'=>($currentValues['canonical']['demand_statement']??null)===($context['submission']['payload']['demand_statement']??null), 'events'=>$events,'audits'=>$audits, 'save_comparison'=>$comparison,'gates'=>$gateResults, 'materiality'=>array_intersect_key($context['materiality']??[],array_flip(['rule','reopened','retained','forced'])), 'open_conditions_count'=>count(array_filter($context['conditions']??[],fn ($c)=>($c['status']??'')==='open')), 'QA24_available_actions'=>app(App\Services\OpportunityProfileWorkflow::class)->actions(App\Models\User::findOrFail(24),$record), 'private_contents_and_authentication_not_exported'=>true, 'UI_feedback_and_focus_not_inferred'=>true,'QA_database_changes'=>0,'active_changes'=>0]; file_put_contents($proofPath,json_encode($receipt,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode(['case'=>32,'state'=>$receipt['working_state'],'review_cycle'=>$receipt['review_cycle'], 'current_text_length'=>$receipt['stored_current_text_length'], 'current_text_matches_last_submission'=>$receipt['current_text_matches_last_submission'], 'events'=>$events,'save_comparison'=>$comparison,'gates'=>$gateResults,'materiality'=>$receipt['materiality'], 'open_conditions'=>$receipt['open_conditions_count'],'QA24_actions'=>$receipt['QA24_available_actions'],'active_changes'=>0],JSON_THROW_ON_ERROR).PHP_EOL; } finally { $db->rollBack(); }