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

29 lines
5.3 KiB
PHP

<?php
use Illuminate\Support\Str;
use App\Shared\Context\CompanyContext;
use App\Modules\Industry\IndustrialCosts;
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]);
$i=json_decode(file_get_contents($s['root'].'/single-industrial.json'),true);$lot=$i['lot'];$original=$host->table('ic_results')->where('id',$i['calculation']['id'])->value('snapshot');$host->beginTransaction();$native->beginTransaction();
try{
$p=$call('accounting_prepare','prepare',fn($e)=>$e->command('effect-prepare',['effect_type'=>'industry.cost.validated','source_id'=>$lot,'journal_on'=>'2026-11-10','period_key'=>'QA-2026-INTEGRADO','rule_key'=>'industry.cost.validated','reason'=>'Resultado industrial1800 efectivamente validado na origem SGF.'],(string)Str::uuid()));
$call('accounting_review','validate',fn($e)=>$e->command('validate',['proposal_id'=>$p['proposal_id'],'revision'=>$p['revision'],'decision'=>'approved','reason'=>'Revisão contabilística distinta do lote1800.'],(string)Str::uuid()));
$j=$call('accounting_prepare','post',fn($e)=>$e->command('post',['proposal_id'=>$p['proposal_id'],'revision'=>$p['revision'],'reason'=>'Aplicação única do custo industrial seleccionado.'],(string)Str::uuid()));
$snapshot=json_decode($native->table('gl_journals')->where('id',$j['journal_id'])->value('snapshot'),true);$ok($snapshot['source']['principal_minor']===180000&&$snapshot['source']['residual_exact']==='300','same native ledger recognises1800 while retaining separate residual300');
$set=function($role)use($profiles,$f){$id=$profiles[$role]['user_id'];Illuminate\Support\Facades\Auth::guard('web')->setUser(App\Models\User::findOrFail($id));app(CompanyContext::class)->selectFor($id,$f['tenant'],$f['company'],$f['unit']);return $id;};
$ic=app(IndustrialCosts::class);$old=$host->table('ic_inputs')->where('id',$i['refs']['material_rate']['id'])->first();$data=json_decode($old->data,true);$data['amount']='51';$prepare=$set('finance_prepare');
$rate=$ic->input($lot,$prepare,['kind'=>'material_rate','source_ref'=>$old->source_ref,'version'=>2,'previous_id'=>$old->id,'data'=>$data,'evidence'=>'Taxa sucessora51 para20KG, mantendo os restantes pressupostos.'],(string)Str::uuid());
$review=$set('finance_review');$ic->decide($lot,$review,['type'=>'input','id'=>$rate['id'],'version'=>2,'decision'=>'approved','reason'=>'Conferência distinta da taxa artificial51.'],(string)Str::uuid());
$refs=$i['refs'];$refs['material_rate']=['id'=>$rate['id'],'version'=>2];$set('finance_prepare');$r=$ic->calculate($lot,$prepare,['currency'=>'MZN','version'=>2,'previous_id'=>$i['calculation']['id'],'refs'=>$refs],(string)Str::uuid());
$set('finance_review');$ic->decide($lot,$review,['type'=>'result','id'=>$r['id'],'version'=>2,'decision'=>'approved','reason'=>'Novo resultado1820 conferido, residual300 permanece separado.'],(string)Str::uuid());
$snap=json_decode($host->table('ic_results')->where('id',$r['id'])->value('snapshot'),true);$ok($snap['known_cost_exact']==='1820'&&$snap['residual_exact']==='300','validated successor changes material only,1800 to1820');
$p=$call('accounting_prepare','correct_prepare',fn($e)=>$e->command('correct-prepare',['journal_id'=>$j['journal_id'],'journal_on'=>'2026-11-10','period_key'=>'QA-2026-INTEGRADO','reason'=>'Par indivisível de correcção industrial1800 para1820.'],(string)Str::uuid()));
$call('accounting_review','correct_validate',fn($e)=>$e->command('correct-validate',['correction_id'=>$p['correction_id'],'revision'=>$p['revision'],'decision'=>'approved','reason'=>'Revisão independente do par industrial exacto.'],(string)Str::uuid()));
$body=['correction_id'=>$p['correction_id'],'revision'=>$p['revision'],'reason'=>'Aplicação única e atómica do par industrial.'];$key=(string)Str::uuid();$pair=$call('accounting_prepare','correct_post',fn($e)=>$e->command('correct-post',$body,$key));$again=$call('accounting_prepare','correct_post',fn($e)=>$e->command('correct-post',$body,$key));
$ok($pair['reverse_journal_id']===$again['reverse_journal_id']&&$pair['replacement_journal_id']===$again['replacement_journal_id'],'industrial pair replay has no duplicated journal');
$ok((int)$native->table('gl_journals')->where('id',$pair['reverse_journal_id'])->value('debit_minor')===180000&&(int)$native->table('gl_journals')->where('id',$pair['replacement_journal_id'])->value('debit_minor')===182000,'indivisible pair retains1800 reversal and1820 replacement');
$ok($original===$host->table('ic_results')->where('id',$i['calculation']['id'])->value('snapshot'),'original industrial result remains immutable');$native->statement('SET CONSTRAINTS ALL IMMEDIATE');
}finally{while($native->transactionLevel()>0)$native->rollBack();while($host->transactionLevel()>0)$host->rollBack();}
$ok($host->table('ic_results')->where('lot_id',$lot)->count()===1,'successor and journal proofs roll back without changing original regression result');
file_put_contents($work.'/industrial-ledger-result.json',json_encode(['passed'=>true,'checks'=>$checks,'rolled_back'=>true,'same_native_book'=>true],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/industrial-ledger-result.json']).PHP_EOL;