true,'cost_ledger.synthetic_enabled'=>true,'industrial_ledger.synthetic_enabled'=>true,'cost_ledger.correction_enabled'=>true,'industrial_ledger.correction_enabled'=>true]);set_exception_handler(function($e){echo json_encode(['passed'=>false,'type'=>get_class($e),'message'=>$e->getMessage(),'frames'=>array_map(fn($f)=>[basename($f['file']??''),$f['line']??0,$f['function']??''],array_slice($e->getTrace(),0,12))]).PHP_EOL;exit(1);});$pointer=json_decode(file_get_contents($s['root'].'/stock-final-backup-pointer.json'),true);$before=json_decode(file_get_contents($pointer['root'].'/preservation-private.json'),true);$checks=[]; foreach(['clients'=>'a_clients','service_drafts'=>'a_service_drafts','billing_drafts'=>'a_billing_drafts'] as $key=>$table){if($before[$key]!==$host->table($table)->orderBy('id')->get()->toJson())throw new LogicException('Preserve all existing client/draft records and associations');$checks[]=$table.' all records unchanged';} $old=json_decode($before['journals'],true);$oldIds=array_column($old,'id');if($before['journals']!==$native->table('gl_journals')->whereIn('id',$oldIds)->orderBy('id')->get()->toJson())throw new LogicException('Preserve sixteen existing journals');$checks[]='all16 existing journals byte-identical'; if($before['native_binding_sha256']!==hash_file('sha256',$s['root'].'/binding.json'))throw new LogicException('Preserve frozen Purchasing binding');$checks[]='original Purchasing binding unchanged'; $scope=$f['scope'];$stock=$native->table('gl_accounts')->where($scope)->where('code','QA-STOCK')->value('id');$amount=(int)$native->table('gl_lines')->where($scope)->where('account_id',$stock)->selectRaw('sum(debit_minor-credit_minor) balance')->value('balance');if($amount!==14500)throw new LogicException('Expected stock value145MZN');$checks[]='native stock account145MZN'; $expense=$native->table('gl_accounts')->where($scope)->where('code','QA-DIFERENCA-INVENTARIO')->value('id');if((int)$native->table('gl_lines')->where($scope)->where('account_id',$expense)->sum('debit_minor')!==1000)throw new LogicException('Inventory difference10MZN');$checks[]='native inventory difference10MZN'; $journals=$native->table('gl_journals')->where($scope)->count();if($journals!==18)throw new LogicException('Only two additional journals expected');$checks[]='18 balanced journals, no transfer journal'; $host->beginTransaction();$native->beginTransaction();try{ $check=$call('accounting_prepare','period_prepare',fn($e)=>$e->command('period-prepare',['period_key'=>'QA-2026-INTEGRADO','reason'=>'ConferĂȘncia final das origens e dos movimentos fora do armazĂ©m de origem.'],(string)Illuminate\Support\Str::uuid()));$cut=json_decode($native->table('gl_period_checks')->where('id',$check['check_id'])->value('snapshot'),true);if($cut['pending']!==0)throw new LogicException('Current period must reconcile');$checks[]='current extended period zero pending facts'; $policy=json_decode(file_get_contents($s['root'].'/stock-final-binding.json'),true);$book=$native->table('gl_stock_books')->where($scope)->where('stock_item_id',$policy['transfer_item_id'])->first();$ownership=(new Tempo\Finance\GeneralLedger\StockTransitEvidence($native,$scope))->ownership($book);if($ownership['owned_quantity_milli']!==1500||$ownership['in_transit_milli']!==0)throw new LogicException('Owned original1.5UN conserved');$checks[]='owned original article1.5UN, in transit0'; $native->table('stock_movements')->insert(['tenant_id'=>1,'stock_item_id'=>$policy['transfer_item_id'],'warehouse_id'=>2,'moved_by'=>6,'movement_type'=>'transfer_in','quantity_milli'=>1,'snapshot'=>'{}','moved_at'=>'2026-11-10 12:00:00']); $blocked=false;try{(new Tempo\Finance\GeneralLedger\StockTransitEvidence($native,$scope))->ownership($book);}catch(DomainException){$blocked=true;}if(!$blocked)throw new LogicException('Unknown outside movement must not be silently counted as owned transfer');$checks[]='unknown out-of-book movement blocks ownership reconciliation'; $physical=$call('accounting_prepare','view',fn($e,$a,$b,$o,$s,$stock)=>$stock->period(['period_key'=>'QA-2026-INTEGRADO','starts_on'=>'2026-01-01','ends_on'=>'2026-12-31']));if($physical['pending']<1)throw new LogicException('Unknown outside movement must prevent period closure');$checks[]='unknown outside movement becomes a pending period fact'; }finally{while($native->transactionLevel()>0)$native->rollBack();while($host->transactionLevel()>0)$host->rollBack();} file_put_contents($work.'/stock-final-preservation-result.json',json_encode(['passed'=>true,'checks'=>$checks,'original_journals'=>16,'current_journals'=>18,'stock_value_minor'=>14500,'other_databases_connected'=>false,'cache_cleared'=>false,'test_changes_rolled_back'=>true],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/stock-final-preservation-result.json']).PHP_EOL;