insert($scope+['user_id'=>$f['actors']['prepare'],'action'=>'general_ledger.validate','person'=>'qa-stock-person-'.$f['actors']['prepare']]); $rate=DB::table('gl_stock_rates')->first();$refuse(fn()=>$enter('prepare')->stock->command('rate-validate',['rate_id'=>$rate->id,'version'=>$rate->version,'decision'=>'approved','reason'=>'Even with both rights the same person cannot review.'],(string)Str::uuid()),'same-person rate review refused with both actual rights'); $s2=$scope;$s2['tenant_id']=999999;$p=new StockValuation(DB::connection(),$s2,$f['actors']['read'],'qa',fn()=>null,fn()=>null);$refuse(fn()=>$p->source('stock.receipt',$f['movements'][0]['id']),'another tenant cannot resolve physical source'); $s2=$scope;$s2['company_id']=999999;$p=new StockValuation(DB::connection(),$s2,$f['actors']['read'],'qa',fn()=>null,fn()=>null);$refuse(fn()=>$p->source('stock.receipt',$f['movements'][0]['id']),'another company cannot resolve mapped book'); }); $case(function()use($enter,$f,$scope,$ok){ $j=(array)DB::table('gl_journals')->orderBy('id')->first();$p=(array)DB::table('gl_proposals')->where('id',$j['proposal_id'])->first();$r=(array)DB::table('gl_reviews')->where('id',$j['review_id'])->first();unset($p['id'],$r['id'],$j['id']);$p['effect_key']=hash('sha256',(string)Str::uuid());$error=null; try{DB::transaction(function()use($p,$r,$j){$id=DB::table('gl_proposals')->insertGetId($p);$r['proposal_id']=$id;$rid=DB::table('gl_reviews')->insertGetId($r);$j['proposal_id']=$id;$j['review_id']=$rid;$j['effect_key']=$p['effect_key'];DB::table('gl_journals')->insert($j);});}catch(Illuminate\Database\QueryException $e){$error=$e;} $ok($error&&($error->errorInfo[0]??null)==='23505'&&str_contains($error->getMessage(),'gl_stock_movement_once'),'SQL forbids a second stock journal even with a forged new effect key'); }); $case(function()use($enter,$f,$scope,$body,$command,$ok,$refuse){ Date::setTestNow('2026-10-29 12:30:00');$period=(array)DB::table('gl_periods')->orderByDesc('version')->first();unset($period['id']);$period['version']++;$period['status']='open';$period['reason']='Transaction-only test of typed stock constraints, rolled back.';DB::table('gl_periods')->insert($period); $r=App\Models\SupplyRecord::findOrFail($f['return_proposal']);$d=$r->data;$d['operation_key']=(string)Str::uuid();$d['quantity_milli']=250;$d['reason']='Actual quarter unit returned solely inside rollback constraint proof.';$u=App\Models\User::findOrFail($f['physical_actors']['issuer']);auth()->setUser($u);$p=app(App\Services\WarehouseReturns::class)->prepare($u,$d);$u=App\Models\User::findOrFail($f['physical_actors']['receiver']);auth()->setUser($u);$v=app(App\Services\WarehouseReturns::class)->confirm($u,$p['id'],['version'=>1,'received_in_warehouse'=>true,'reason'=>'Distinct physical return in transaction-only proof.']); $m=['id'=>$v['movement_id'],'movement_type'=>'workshop_return_in']; $fake=function($role)use($enter,$scope,$f){$c=$enter($role);$auth=(new ReflectionProperty($c,'authorize'))->getValue($c);$read=(new ReflectionProperty($c,'readAuthorize'))->getValue($c);return new PayableJournal(DB::connection(),$scope,$f['actors'][$role],'qa-stock-person-'.$f['actors'][$role],$auth,$read,fn()=>throw new DomainException('No supplier'),null,function($kind,$id)use($c){$source=$c->stock->source($kind,$id);$source['principal_minor']++;return$source;});}; $p=$fake('prepare')->command('effect-prepare',$body($m),(string)Str::uuid());$fake('validate')->command('validate',['proposal_id'=>$p['proposal_id'],'revision'=>$p['revision'],'decision'=>'approved','reason'=>'Adversarial source mutation tests SQL independently of PHP.'],(string)Str::uuid());$count=DB::table('gl_journals')->count();$error=null; try{DB::transaction(function()use($fake,$p){$fake('prepare')->command('post',['proposal_id'=>$p['proposal_id'],'revision'=>$p['revision'],'reason'=>'Forged extra cent must be refused by typed SQL source guard.'],(string)Str::uuid());DB::statement('SET CONSTRAINTS gl_stock_journal IMMEDIATE');});}catch(Illuminate\Database\QueryException $e){$error=$e;} $ok($error&&($error->errorInfo[0]??null)==='23514'&&str_contains($error->getMessage(),'GL_STOCK_JOURNAL_SOURCE_OR_LINES')&&DB::table('gl_journals')->count()===$count,'SQL rejects amount forged independently of reviewed PHP source'); $p=$command('prepare','effect-prepare',$body($m));DB::table('pa_access')->insert($scope+['user_id'=>$f['actors']['prepare'],'action'=>'general_ledger.validate','person'=>'qa-stock-person-'.$f['actors']['prepare']]);$refuse(fn()=>$command('prepare','validate',['proposal_id'=>$p['proposal_id'],'revision'=>$p['revision'],'decision'=>'approved','reason'=>'Both rights must not bypass distinct journal review.']),'same-person journal review refused with both actual rights'); }); file_put_contents($s['root'].'/extra.json',json_encode(['passed'=>true,'checks'=>$checks,'all_extra_physical_and_sql_changes_rolled_back'=>true,'existing_closed_book_preserved'=>true],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo count($checks)." additional isolation, identity and typed SQL checks passed.\n";