Files
SGF/scripts/stock-ledger-observer-entry.php
2026-11-04 09:37:20 +02:00

18 lines
4.4 KiB
PHP

<?php
$file=realpath($argv[1]??'');$run=json_decode(file_get_contents('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),true,32,JSON_THROW_ON_ERROR);if(!$file||!str_starts_with($file,realpath($run['root'].'/private').DIRECTORY_SEPARATOR))throw new LogicException('Owned observer QA state only');$s=json_decode(file_get_contents($file),true,64,JSON_THROW_ON_ERROR);$phase=$argv[2]??'';
if(!preg_match('/^[a-f0-9]{12}$/D',$s['tag'])||$s['database']!=='budget_core_sgf_'.$s['tag']||$s['role']!=='stock_ledger_sgf_'.$s['tag']||$s['run']!==$run['id'])throw new LogicException('Observer identity mismatch');
$env=['APP_ENV'=>'budget-testing','APP_DEBUG'=>'false','APP_KEY'=>$s['app_key'],'DB_CONNECTION'=>'pgsql','DB_HOST'=>'127.0.0.1','DB_PORT'=>'55439','DB_DATABASE'=>$s['database'],'DB_USERNAME'=>$s['role'],'DB_PASSWORD'=>$s['password'],'DB_URL'=>'','SESSION_DRIVER'=>'file','SESSION_COOKIE'=>'stock_observer_'.$s['tag'],'CACHE_STORE'=>'array','QUEUE_CONNECTION'=>'sync','BCRYPT_ROUNDS'=>'4','APP_CONFIG_CACHE'=>$s['storage'].'/cache/config.php','APP_ROUTES_CACHE'=>$s['storage'].'/cache/routes.php','APP_SERVICES_CACHE'=>$s['storage'].'/cache/services.php','APP_PACKAGES_CACHE'=>$s['storage'].'/cache/packages.php','VIEW_COMPILED_PATH'=>$s['storage'].'/framework/views','SGF_ISOLATED_REGRESSION_RUN'=>$s['run']];foreach($env as$k=>$v){putenv($k.'='.$v);$_ENV[$k]=$_SERVER[$k]=$v;}
require dirname(__DIR__).'/vendor/autoload.php';$app=require dirname(__DIR__).'/bootstrap/app.php';$app->useStoragePath($s['storage']);$app->addAbsoluteCachePathPrefix('C:');$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();if(isset($s['public'])){$app->usePublicPath($s['public']);app(Illuminate\Foundation\Vite::class)->useHotFile($s['storage'].'/no-hot');}
try{
if($phase==='setup'){
if(Illuminate\Support\Facades\DB::getSchemaBuilder()->hasTable('users'))throw new LogicException('Fresh observer base required');require dirname(__DIR__).'/packages/budget-control/tests/sgf-schema.php';require dirname(__DIR__).'/packages/budget-control/tests/host-support.php';$f=budgetQaFixture('sgf');
// Remove only fixture-created budget grants before narrowing the check.
Illuminate\Support\Facades\DB::table('permission_grants')->where('permission','like','purchasing_budget.%')->delete();
Illuminate\Support\Facades\DB::statement('ALTER TABLE permission_grants DROP CONSTRAINT permission_grants_scope_check');Illuminate\Support\Facades\DB::statement("ALTER TABLE permission_grants ADD CONSTRAINT permission_grants_scope_check CHECK ((unit_id IS NULL AND permission='company.view') OR (unit_id IS NOT NULL AND permission IN('unit.view','general_ledger.view','stock.view'))) ");
foreach([$f['actor']=>['general_ledger.view','stock.view'],$f['validator']=>['general_ledger.view']]as$id=>$rights)foreach($rights as$p)Illuminate\Support\Facades\DB::table('permission_grants')->insert($f['scope']+['user_id'=>$id,'permission'=>$p,'created_at'=>now(),'updated_at'=>now()]);
$native=json_decode(file_get_contents($s['native_root'].'/fixture.json'),true,32,JSON_THROW_ON_ERROR);$f['native_scope']=$native['scope'];$f['book_id']=$native['book_id'];file_put_contents($s['root'].'/fixture.json',json_encode($f,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));file_put_contents($s['root'].'/setup.json',json_encode(['passed'=>true,'new_sgf_identity_fixture'=>true,'no_sgf_physical_or_ledger_book'=>true,'native_scope_explicit'=>true],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));exit;
}
$f=json_decode(file_get_contents($s['root'].'/fixture.json'),true,32,JSON_THROW_ON_ERROR);$key=$f['tenant'].':'.$f['company'].':'.$f['unit'];config(['database.connections.stock_valuation_source'=>$s['source'],'stock_ledger.read_sources.'.$key=>['connection'=>'stock_valuation_source','native_scope'=>$f['native_scope'],'book_id'=>$f['book_id']],'modules.companies.'.$f['tenant'].':'.$f['company']=>['contracted'=>['stock','accounting'],'enabled'=>['stock','accounting']]]);
if($phase==='http')require __DIR__.'/stock-ledger-observer-http.php';elseif($phase==='proof')require __DIR__.'/stock-ledger-observer-proof.php';elseif($phase==='proof-final')require __DIR__.'/stock-ledger-observer-final.php';else throw new LogicException('Unknown observer phase');
}catch(Throwable $e){file_put_contents($s['root'].'/failure-'.time().'.json',json_encode(['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine()],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));fwrite(STDERR,get_class($e).': '.$e->getMessage()."\n");exit(1);}