Files
SGF/scripts/sourcing-host-setup.php
2026-11-04 09:37:20 +02:00

5 lines
2.1 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
$s=json_decode(file_get_contents(getenv('RECEIPT_COMPOSITION_STATE')),true,64,JSON_THROW_ON_ERROR);if(!($s['sourcing']??false)||file_exists($s['root'].'/sourcing-baseline.json'))throw new LogicException('Fresh sourcing composition required');require $s['app'].'/vendor/autoload.php';$app=require $s['app'].'/bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();App\Modules\Purchasing\BudgetAdapter::guard();$f=json_decode(file_get_contents($s['root'].'/fixture.json'),true);$n=json_decode(file_get_contents($s['root'].'/native-fixture.json'),true);$c=DB::selectOne("SELECT pg_get_constraintdef(oid) definition FROM pg_constraint WHERE conrelid='permission_grants'::regclass AND conname='permission_grants_scope_check'");if(!preg_match('/^CHECK\s*\((.*)\)$/s',$c->definition,$match))throw new LogicException('QA catalogue constraint not found');DB::statement('ALTER TABLE permission_grants DROP CONSTRAINT permission_grants_scope_check');DB::statement("ALTER TABLE permission_grants ADD CONSTRAINT permission_grants_scope_check CHECK (({$match[1]}) OR (unit_id IS NOT NULL AND permission IN ('purchasing_sourcing.view','purchasing_sourcing.prepare','purchasing_sourcing.evaluate','purchasing_sourcing.select','purchasing_sourcing.approve')))");
foreach(['actor'=>['view','prepare','evaluate','select'],'issuer'=>['view','evaluate','approve']]as$role=>$rights)foreach($rights as$right)DB::table('permission_grants')->insert($f['scope']+['user_id'=>$f[$role],'permission'=>'purchasing_sourcing.'.$right,'created_at'=>now(),'updated_at'=>now()]);$path=$s['root'].'/binding.json';$p=json_decode(file_get_contents($path),true);foreach($p['bindings']as&$b){$b['award_ids']=[];$b['supplier_ids']=$n['supplier_ids'];}unset($b);file_put_contents($path,json_encode($p,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));file_put_contents($s['root'].'/sourcing-baseline.json',json_encode(['passed'=>true,'quotes'=>0,'evaluations'=>0,'awards'=>0,'orders'=>0,'receipts'=>0,'movements'=>0,'initial_reserved_minor'=>($s['requests']??false)?0:10000,'initial_committed_minor'=>0],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));