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

6 lines
2.6 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['orders']??false)||file_exists($s['root'].'/order-baseline.json'))throw new LogicException('Fresh order 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);
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 ((unit_id IS NULL AND permission IN ('company.view','company.edit','unit.create','access.manage')) OR (unit_id IS NOT NULL AND (permission LIKE 'purchasing_budget.%' OR permission IN ('unit.view','unit.edit','purchasing_receipts.view','purchasing_receipts.confirm','purchasing_orders.view','purchasing_orders.issue'))))");DB::table('permission_grants')->where($f['scope'])->where('permission','purchasing_receipts.confirm')->delete();DB::table('permission_grants')->where($f['scope'])->where('permission','like','purchasing_budget.%')->delete();
foreach([$f['actor'],$f['issuer']]as$id)DB::table('permission_grants')->insert($f['scope']+['user_id'=>$id,'permission'=>'purchasing_orders.view','created_at'=>now(),'updated_at'=>now()]);foreach([[$f['actor'],'purchasing_orders.issue'],[$f['validator'],'purchasing_receipts.confirm']]as[$id,$right])DB::table('permission_grants')->insert($f['scope']+['user_id'=>$id,'permission'=>$right,'created_at'=>now(),'updated_at'=>now()]);$p=json_decode(file_get_contents($s['root'].'/binding.json'),true);$p['economic_manifest_sha256']=hash_file('sha256',$s['root'].'/economic/manifest.json');$p['bindings']=[];foreach(['actor'=>'buyer','validator'=>'receiver','issuer'=>'head']as$role=>$native)$p['bindings'][implode(':',array_values($f['scope'])).':'.$f[$role]]=['tenant_id'=>$n['tenant'],'actor_id'=>$n[$native],'order_ids'=>[],'award_ids'=>[$n['award']],'requisition_ids'=>[$n['requisition']],'warehouse_ids'=>[$n['warehouse']]];file_put_contents($s['root'].'/binding.json',json_encode($p,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));file_put_contents($s['root'].'/order-baseline.json',json_encode(['passed'=>true,'orders'=>0,'receipts'=>0,'movements'=>0,'reserve_minor'=>($s['requests']??false)?0:10000,'commit_minor'=>0,'qa_rights'=>['purchasing_orders.view','purchasing_orders.issue']],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));