Files
Investidor-V2/tools/investmoz-v2-opportunity-origin.php
T

37 lines
3.7 KiB
PHP

<?php
// Reuse the protected reader; no seeder, Laravel kernel or write to the active database.
require __DIR__.'/investmoz-v2-territory-audit.php';
$directory = dirname(__DIR__).'/storage/app/private/investmoz-v2';
$audit = json_decode(file_get_contents($directory.'/territory-audit.json'), true, 512, JSON_THROW_ON_ERROR);
$source = file_get_contents(dirname(__DIR__).'/database/seeders/OpportunitySeeder.php');
preg_match_all("/'reference' => '([^']+)', 'title' => '([^']+)', 'province' => '([^']+)', 'maturity' => ([0-9]+), 'amount' => ([0-9]+), 'financing' => '([^']+)', 'impact' => '([^']+)'/", $source, $matches, PREG_SET_ORDER);
if (count($matches) !== 6) {
throw new RuntimeException('Seeder format changed; individual review required.');
}
$comparisons = [];
foreach ($audit['records'] as $entity) {
if ($entity['kind'] !== 'opportunities') {
continue;
}
$record = $entity['record'];
$seed = array_values(array_filter($matches, fn ($match) => $match[1] === $record['reference']))[0] ?? null;
if (! $seed) {
throw new RuntimeException('Opportunity not covered by reviewed seeder.');
}
$fields = ['reference' => $seed[1], 'title' => $seed[2], 'province' => $seed[3], 'maturity_level' => (int) $seed[4], 'estimated_investment_amount' => $seed[5], 'financing_modality' => $seed[6], 'description' => $seed[7], 'promoter_name' => 'Promotor em validação'];
$checks = [];
foreach ($fields as $field => $value) {
$checks[$field] = $field === 'estimated_investment_amount' ? (float) $record[$field] === (float) $value : (string) $record[$field] === (string) $value;
}
$comparisons[] = ['id' => $record['id'], 'reference' => $record['reference'], 'title' => $record['title'], 'created_at' => $record['created_at'], 'status' => $record['status'], 'checks' => $checks, 'code_origin' => 'database/seeders/OpportunitySeeder.php', 'real_world_status' => 'not_established_by_seeder', 'classification_decision' => 'pending_responsible'];
}
$pdo->exec('BEGIN TRANSACTION READ ONLY');
$events = $pdo->query("SELECT action,source_system,actor_user_id,occurred_at,after_data->>'reference' AS reference,after_data->>'opportunity_id' AS opportunity_id FROM audit_events WHERE entity_type IN ('opportunity','opportunities') OR after_data->>'reference' IN ('INV-MZ-AGR-001','INV-MZ-ENE-002','INV-MZ-TUR-003','INV-MZ-INF-004','INV-MZ-BLUE-005','INV-MZ-FIN-006') ORDER BY occurred_at,id")->fetchAll(PDO::FETCH_ASSOC);
$importTables = $pdo->query("SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema NOT IN ('pg_catalog','information_schema') AND (table_name ILIKE '%import%' OR table_name ILIKE '%seed%') ORDER BY table_schema,table_name")->fetchAll(PDO::FETCH_ASSOC);
$classificationEvents = (int) $pdo->query("SELECT count(*) FROM audit_events WHERE source_system='opportunity-origin-review'")->fetchColumn();
$pdo->exec('ROLLBACK');
$proof = ['captured_at' => $audit['captured_at'], 'identity' => $audit['identity'], 'seeder_sha256' => hash('sha256', $source), 'comparisons' => $comparisons, 'available_opportunity_audit_events' => $events, 'import_registry_tables_found' => $importTables, 'active_classification_events' => $classificationEvents, 'creation_execution_proven' => false, 'official_origin_revalidation' => false, 'active_writes' => 0];
file_put_contents($directory.'/opportunity-origin-confirmation.json', json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode(['matching_records' => count($comparisons), 'matched_fields' => count(array_filter($comparisons, fn ($record) => ! in_array(false, $record['checks'], true))), 'available_audit_events' => count($events), 'real_world_status' => 'requires_responsible_confirmation', 'active_writes' => 0], JSON_THROW_ON_ERROR).PHP_EOL;