Files
SGF/scripts/sgf-iva-setup.php
2026-11-11 09:00:17 +02:00

6 lines
2.7 KiB
PHP

<?php
$book=$argv[1]??'regression';require __DIR__.'/sgf-single-bootstrap.php';$p=json_decode(file_get_contents($repo.'/docs/integration/sgf-iva-assessment-access-proposal.json'),true);if(($p['approved']??false)!==true||($p['authorization']['decision']??null)!=='Autorizar o livro/mapa IVA QA e os direitos apresentados'||count($p['grants_per_book'])!==14)throw new LogicException('Specific authorization required');
$count=0;$host->transaction(function()use($host,$p,&$count){$old=$host->selectOne("SELECT pg_get_constraintdef(oid) d FROM pg_constraint WHERE conrelid='permission_grants'::regclass AND conname='permission_grants_scope_check'")->d;if(!str_contains($old,'fiscal_assessments.view')){if(!preg_match('/^CHECK \((.*)\)$/s',$old,$m))throw new LogicException('Unknown check');$host->statement('ALTER TABLE permission_grants DROP CONSTRAINT permission_grants_scope_check');$names=implode(',',array_map(fn($s)=>"'".$s."'",$p['new_right_names']));$host->statement('ALTER TABLE permission_grants ADD CONSTRAINT permission_grants_scope_check CHECK (('.$m[1].') OR (unit_id IS NOT NULL AND permission IN('.$names.')))');}foreach($p['grants_per_book']as$g){if(!in_array($g['user_id'],[13,14,15,16,17],true)||array_intersect_key($g,array_flip(['tenant_id','company_id','unit_id']))!==['tenant_id'=>1,'company_id'=>2,'unit_id'=>2])throw new LogicException('Exact QA recipients');if($host->table('permission_grants')->where($g)->exists())continue;$host->table('permission_grants')->insert($g);App\Modules\Identity\Services\Audit::record('access.granted_on_creation',null,1,2,2,$g['user_id'],['permission'=>$g['permission'],'reason'=>$p['authorization']]);$count++;}if(!$host->getSchemaBuilder()->hasTable('fx_versions'))$host->unprepared(file_get_contents(dirname(__DIR__).'/database/sql/fiscal_assessment.sql'));});
$cash=json_decode(file_get_contents($s['root'].'/cash-binding.json'),true);$path=$s['root'].'/fiscal-binding.json';$binding=['composition'=>$tag,'scope'=>$f['scope'],'process_key'=>'a4c81438-5ff9-4a42-a036-696e154749b0','source_output_id'=>$cash['source_output_id'],'invoice_id'=>$cash['invoice_id'],'receipt_id'=>$cash['receipt_id'],'period'=>'2026-11','contract'=>'SGF-IVA-ASSESSMENT-FORM-QA-v1'];if(is_file($path)&&json_decode(file_get_contents($path),true)!==$binding)throw new LogicException('Preserve different fiscal binding');if(!is_file($path))file_put_contents($path,json_encode($binding,JSON_PRETTY_PRINT));
file_put_contents($work.'/iva-setup.json',json_encode(['passed'=>true,'book'=>$book,'new_grants'=>$count,'new_right_names'=>6,'new_pg_roles'=>0,'new_demo'=>false,'binding_sha256'=>hash_file('sha256',$path)],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'book'=>$book,'new_grants'=>$count,'proof'=>$work.'/iva-setup.json']).PHP_EOL;