Files
SGF/scripts/ledger-reports-reconcile.php
T
2026-11-04 09:37:20 +02:00

10 lines
2.2 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
use Tempo\Finance\Payables\Payables;
$output=$s['root'].'/ledger-reports-reconcile.json';if(file_exists($output))throw new LogicException('Completed integration report proof preserved');
$tables=['gl_journals','gl_lines','gl_proposals','gl_reviews','gl_events','gl_periods','gl_period_checks','gl_period_reviews','gl_period_closures'];
$tables=array_values(array_filter($tables,fn($t)=>DB::getSchemaBuilder()->hasTable($t)));$fingerprint=function()use($tables){$h=hash_init('sha256');foreach($tables as $t){hash_update($h,$t);foreach(DB::table($t)->orderBy('id')->cursor() as $r)hash_update($h,Payables::canonical((array)$r));}return hash_final($h);};$before=$fingerprint();$checks=0;$ok=function($v,$m)use(&$checks){$checks++;if(!$v)throw new LogicException($m);};
$r=$ledger->report($enter('read'),'trial-balance',['from_on'=>'2026-10-01','to_on'=>'2026-10-31','per_page'=>100]);$sum=DB::table('gl_lines')->where($scope)->selectRaw('SUM(debit_minor) d,SUM(credit_minor) c')->first();$ok($r['totals']['debit_minor']===(int)$sum->d&&$r['totals']['credit_minor']===(int)$sum->c,'New report equals all actual posted lines');
foreach($r['rows'] as $account){$entries=$ledger->report($enter('read'),'account-ledger',['from_on'=>'2026-10-01','to_on'=>'2026-10-31','account_id'=>$account['account_id'],'per_page'=>100]);$ok($entries['closing_net_minor']===$account['closing_net_minor']&&end($entries['rows'])['balance_minor']===$account['closing_net_minor'],'Each account reconciles to its complete actual entry stream');}
$ok($fingerprint()===$before,'Existing accepted source evidence and closed period not changed');file_put_contents($output,json_encode(['passed'=>true,'checks'=>$checks,'read_only_commands_only'=>true,'actual_posted_journals'=>DB::table('gl_journals')->count(),'actual_posted_lines'=>DB::table('gl_lines')->count(),'debit_minor'=>$r['totals']['debit_minor'],'credit_minor'=>$r['totals']['credit_minor'],'accounts'=>count($r['rows']),'all_account_balances_reconcile'=>true,'ledger_and_closed_period_fingerprint_unchanged'=>true],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo "Actual-source report reconciliation: $checks checks; original ledger/closed period evidence unchanged.\n";