commit melhorias sgf

This commit is contained in:
2026-11-11 09:00:17 +02:00
parent 0b05a1192c
commit 1d11a5e57b
736 changed files with 40746 additions and 210 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
$repo=dirname(__DIR__);$sgc='C:/laragon/www/compras';
$run=json_decode(file_get_contents('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),true);
if($run['id']!=='2576427d1939'||$run['port']!==55439)throw new LogicException('Existing regression cluster only');
$root=getenv('SGC_DISPATCH_PG_ROOT');if(!$root||dirname(str_replace('\\','/',$root))!==str_replace('\\','/',$run['root'].'/private')||!preg_match('/^sgc-dispatch-pg-[a-f0-9]{12}$/D',basename($root)))throw new LogicException('Private proof root');
$source=json_decode(file_get_contents($run['root'].'/private/receipt-composition-b4093c6f6707/state.json'),true);
$n=$source['nativeConnection'];if($n['database']!=='compras_receipts_test_b4093c6f6707'||$n['username']!=='payable_history_native_b4093c6f6707')throw new LogicException('Source regression book only');
$private=json_decode(file_get_contents($root.'/settings.json'),true);
foreach(['APP_BASE_PATH'=>$sgc,'APP_ENV'=>'testing','APP_KEY'=>$private['app_key'],'APP_DEBUG'=>'false','DB_CONNECTION'=>'pgsql','DB_HOST'=>'127.0.0.1','DB_PORT'=>'55439','DB_DATABASE'=>$n['database'],'DB_USERNAME'=>$n['username'],'DB_PASSWORD'=>$n['password'],'DB_URL'=>'',
'SESSION_DRIVER'=>'array','CACHE_STORE'=>'array','QUEUE_CONNECTION'=>'sync','COMMON_PAYABLES_ENABLED'=>'false','COMMON_PAYABLES_HISTORY_ENABLED'=>'false','COMMON_PAYABLES_HISTORY_DECISIONS_ENABLED'=>'false',
'APP_CONFIG_CACHE'=>$root.'/config.php','APP_SERVICES_CACHE'=>$root.'/services.php','APP_PACKAGES_CACHE'=>$root.'/packages.php','APP_ROUTES_CACHE'=>$root.'/routes.php']as$k=>$v){putenv($k.'='.$v);$_ENV[$k]=$v;$_SERVER[$k]=$v;}
foreach(['logs','framework/views','framework/sessions','framework/cache/data']as$d)if(!is_dir($root.'/storage/'.$d))mkdir($root.'/storage/'.$d,0700,true);
require $sgc.'/vendor/autoload.php';$app=require $sgc.'/bootstrap/app.php';$app->addAbsoluteCachePathPrefix('C:');$app->useStoragePath($root.'/storage');$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$db=Illuminate\Support\Facades\DB::connection();$identity=$db->selectOne("SELECT current_database() db,current_user role,current_setting('cluster_name') cluster,rolsuper OR rolcreatedb OR rolcreaterole OR rolreplication OR rolbypassrls elevated FROM pg_roles WHERE rolname=current_user");
if($identity->db!==$n['database']||$identity->role!==$n['username']||$identity->cluster!=='sgf_modular_regression_'.$run['id']||$identity->elevated)throw new LogicException('Physical source identity refused');
$actor=App\Models\User::where('tenant_id',1)->where('purchasing_role','finance')->findOrFail(4);auth()->guard('web')->setUser($actor);
$receiverRoot=json_decode(file_get_contents($run['root'].'/private/sgc-invoice-current.json'),true)['root'];
$receiverSecret=json_decode(file_get_contents($receiverRoot.'/client.json'),true);$fixture=json_decode(file_get_contents($receiverRoot.'/source-fixture.json'),true);
$localOrder=$db->table('purchase_orders')->where('tenant_id',1)->first();
$mapping=['id'=>'QA-INTERAPP','version'=>1,'source_scope'=>['tenant_id'=>1],'target_scope'=>['tenant_id'=>1,'company_id'=>2,'unit_id'=>2],'authority'=>'sgf_only','order_id'=>$fixture['order'],'lines'=>['QA-SOURCE-LINE'=>$fixture['line']]];
config(['sgf_invoice.workspace_enabled'=>true,'sgf_invoice.enabled'=>true,'sgf_invoice.client_package'=>$repo.'/packages/sgc-invoice-client/src/InvoiceClient.php','sgf_invoice.client_package_sha256'=>hash_file('sha256',$repo.'/packages/sgc-invoice-client/src/InvoiceClient.php'),
'sgf_invoice.connections'=>['qa'=>['enabled'=>true,'client_id'=>'synthetic','token'=>$receiverSecret['token'],'context_version'=>$receiverSecret['context'],'origin'=>'http://127.0.0.1:8085','source_scope'=>['tenant_id'=>1],
'target_scope'=>['tenant_id'=>1,'company_id'=>2,'unit_id'=>2],'mappings'=>['map'=>['approved'=>true,'label'=>'QA bridge only','source_supplier_id'=>(int)$localOrder->supplier_id,'mapping'=>$mapping]]]]]);
Illuminate\Support\Facades\Http::preventStrayRequests();$bridgeCalls=0;
Illuminate\Support\Facades\Http::fake(function(Illuminate\Http\Client\Request $r)use($receiverRoot,$receiverSecret,$repo,$root,&$bridgeCalls){
if(!str_starts_with($r->url(),'http://127.0.0.1:8085/api/v1/integration/sgc/invoices'))throw new LogicException('Unexpected bridge destination');
$headers=[];foreach(['Authorization','X-SGC-Client','X-SGC-Context','X-Idempotency-Key']as$h)$headers[$h]=$r->header($h)[0]??null;
if($headers['Authorization']!=='Bearer '.$receiverSecret['token']||$headers['X-SGC-Context']!==$receiverSecret['context']||$headers['X-SGC-Client']!=='synthetic')return Illuminate\Support\Facades\Http::response([],401);
if(($r['phase']??null)==='confirm'&&getenv('SGC_BRIDGE_DELAY')==='true'){file_put_contents($root.'/sending-ready','ready');usleep(1800000);}
$name='source-'.substr(basename($root),-12).'-'.getmypid().'-'.(++$bridgeCalls);$job=$name.'.json';$out=$name.'-result.json';
file_put_contents($receiverRoot.'/'.$job,json_encode(['method'=>$r->method(),'path'=>parse_url($r->url(),PHP_URL_PATH),'body'=>$r->data(),'headers'=>$headers],JSON_THROW_ON_ERROR));
$p=new Symfony\Component\Process\Process(['C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',$repo.'/scripts/sgc-dispatch-receiver.php',$job,$out],$repo,['SGC_INVOICE_PROOF_ROOT'=>$receiverRoot],null,30);$p->run();
if(!$p->isSuccessful()||!is_file($receiverRoot.'/'.$out)){file_put_contents($root.'/receiver-error.log',$p->getOutput().$p->getErrorOutput());throw new RuntimeException('Receiver kernel incomplete');}
$response=json_decode(file_get_contents($receiverRoot.'/'.$out),true);
if(($r['phase']??null)==='confirm'&&getenv('SGC_BRIDGE_LOSE')==='true'){putenv('SGC_BRIDGE_LOSE=false');throw new Illuminate\Http\Client\ConnectionException('Synthetic source response lost after receiver commit');}
return Illuminate\Support\Facades\Http::response($response['data'],$response['status']);
});
$service=app(App\Services\SgfInvoiceDispatches::class);