Files
SGF/scripts/sgf-composed-period-proof.php
T
2026-11-11 09:00:17 +02:00

19 lines
4.5 KiB
PHP

<?php
use Illuminate\Support\Str;
$book='8085';require __DIR__.'/sgf-ledger-proof-bootstrap.php';config(['sales_ledger.synthetic_enabled'=>true,'cost_ledger.synthetic_enabled'=>true,'industrial_ledger.synthetic_enabled'=>true,'cost_ledger.correction_enabled'=>true,'industrial_ledger.correction_enabled'=>true]);
$scope=['tenant_id'=>1,'company_id'=>2,'unit_id'=>2];$journals=$native->table('gl_journals')->where($scope)->orderBy('id')->get()->toJson();$host->beginTransaction();$native->beginTransaction();
try{
$p=$call('accounting_prepare','period_prepare',fn($e)=>$e->command('period-prepare',['period_key'=>'QA-2026-INTEGRADO','reason'=>'Conferência integral das origens efectivas na mesma8085; ensaio revertido para manter o período de apresentação aberto.'],(string)Str::uuid()));
$snapshot=json_decode($native->table('gl_period_checks')->where('id',$p['check_id'])->value('snapshot'),true);$ok($snapshot['pending']===0&&$snapshot['debit_minor']===$snapshot['credit_minor'],'all supplier, sales, costs, corrections and physical origins reconcile with zero pending facts');
$review=['check_id'=>$p['check_id'],'revision'=>$p['revision'],'decision'=>'approved','reason'=>'Revisão independente do conjunto integral de origens e montantes QA.'];$call('accounting_review','period_validate',fn($e)=>$e->command('period-validate',$review,(string)Str::uuid()));
$native->beginTransaction();try{$rule=(array)$native->table('gl_rules')->where($scope)->where('rule_key','sales.invoice')->first();unset($rule['id']);$rule['version']=2;$rule['reason']='Versão adicional apenas no ensaio de recusa de conferência antiga.';$native->table('gl_rules')->insert($rule);$bad=false;try{$call('accounting_prepare','period_close',fn($e)=>$e->command('period-close',['check_id'=>$p['check_id'],'revision'=>$p['revision'],'reason'=>'Não fechar com regras alteradas.'],(string)Str::uuid()));}catch(DomainException $e){$bad=str_contains($e->getMessage(),'mudaram');}$ok($bad,'rule version changed after review refuses closure and requires fresh conference');}finally{$native->rollBack();}
$key=(string)Str::uuid();$body=['check_id'=>$p['check_id'],'revision'=>$p['revision'],'reason'=>'Fecho QA integral apenas dentro da transacção de prova, sem reabrir depois.'];$closed=$call('accounting_prepare','period_close',fn($e)=>$e->command('period-close',$body,$key));$again=$call('accounting_prepare','period_close',fn($e)=>$e->command('period-close',$body,$key));
$ok($closed['state']==='closed'&&$closed['closure_id']===$again['closure_id'],'distinctly reviewed close and replay retain one closure');
$ok($native->table('gl_periods')->where($scope)->where('period_key','QA-2026-INTEGRADO')->orderByDesc('version')->value('status')==='closed','closure appends actual closed period version');
$physical=json_decode(file_get_contents($s['root'].'/single-stock-physical-input.json'),true);$receiptId=(int)$native->table('stock_movements')->where('stock_item_id',$physical['stock_item'])->where('movement_type','receipt_in')->value('id');$bad=false;
try{$call('accounting_prepare','prepare',fn($e)=>$e->command('effect-prepare',['effect_type'=>'stock.receipt','source_id'=>$receiptId,'journal_on'=>'2026-11-10','period_key'=>'QA-2026-INTEGRADO','rule_key'=>'stock.receipt','reason'=>'Não criar captura num período fechado.'],(string)Str::uuid()));}catch(DomainException $e){$bad=str_contains($e->getMessage(),'fechado');}$ok($bad,'closed QA period refuses further capture');
$ok($journals===$native->table('gl_journals')->where($scope)->orderBy('id')->get()->toJson(),'closure and refusals preserve every journal present before this proof');$native->statement('SET CONSTRAINTS ALL IMMEDIATE');
}finally{while($native->transactionLevel()>0)$native->rollBack();while($host->transactionLevel()>0)$host->rollBack();}
$ok($native->table('gl_periods')->where($scope)->where('period_key','QA-2026-INTEGRADO')->orderByDesc('version')->value('status')==='open','proof rolls back instead of reopening; actual8085 presentation period remains open');
file_put_contents($work.'/period-composition-result.json',json_encode(['passed'=>true,'checks'=>$checks,'facts'=>$snapshot['facts'],'pending'=>0,'journals'=>count(json_decode($journals,true)),'close_executed_in_rolled_back_transaction'=>true,'period_reopened'=>false,'presentation_period_open'=>true,'browser_visual_review'=>false],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'facts'=>$snapshot['facts'],'proof'=>$work.'/period-composition-result.json']).PHP_EOL;