Files
SGF/scripts/payable-page-fixture.php
2026-11-02 20:29:34 +02:00

29 lines
4.8 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
use Tempo\Finance\Payables\{Payables,HistoricalPayments};
// Benchmark clones only, explicitly different from the one native conferred document.
$f=json_decode(file_get_contents($s['root'].'/fixture.json'),true,64,JSON_THROW_ON_ERROR);$scope=$f['scope'];
$target=(int)($argv[3]??0);if(!in_array($target,[51,251,1000,5000,10000],true))throw new LogicException('Bounded QA volume required');
$count=DB::table('pa_imported_sources')->where($scope)->count();if($count>$target)throw new LogicException('Retained QA data is never reset');
$bundle=json_decode(file_get_contents($s['root'].'/source.json'),true,64,JSON_THROW_ON_ERROR);$base=$bundle['source'];unset($base['local_id']);$now=now();
for($start=$count;$start<$target;$start+=100){
$size=min(100,$target-$start);$ids=DB::select("SELECT nextval(pg_get_serial_sequence('pa_imported_sources','id')) AS local_id,nextval(pg_get_serial_sequence('pa_sources','id')) AS source_id,nextval(pg_get_serial_sequence('pa_proposals','id')) AS proposal_id,nextval(pg_get_serial_sequence('pa_obligations','id')) AS obligation_id FROM generate_series(1,?)",[$size]);
$rows=array_fill_keys(['pa_imported_sources','pa_source_histories','pa_sources','pa_proposals','pa_obligations','pa_history_imports','pa_imported_payments','pa_events'],[]);
foreach($ids as $offset=>$id){
$n=$start+$offset+1;$doc=array_replace($base,['document'=>100000+$n,'number'=>'BENCH-'.str_pad((string)$n,5,'0',STR_PAD_LEFT),'supplier_name'=>$n%2===0?'Synthetic supplier Alpha':'Synthetic supplier Beta']);$local=(int)$id->local_id;$source=(int)$id->source_id;$proposal=(int)$id->proposal_id;$obligation=(int)$id->obligation_id;
$payments=$bundle['capture']['payments'];foreach($payments as $i=>&$payment){$payment['id']=1000000+$n*3+$i;$payment['document']=$doc['document'];}unset($payment);$capture=HistoricalPayments::capture($doc,$payments);$key=Payables::identity($doc);$revision=Payables::revision($doc);
$receipt=['version'=>App\Modules\Purchasing\SupplierHistoryReceipts::VERSION,'receipt_version'=>1,'source_key'=>$key,'source_revision'=>$revision,'history_hash'=>$capture['history_hash'],'payments'=>$capture['payments']];
$rows['pa_imported_sources'][]=$scope+['id'=>$local,'payload'=>Payables::canonical($doc),'receipt_hash'=>hash('sha256',Payables::canonical($doc))];
$rows['pa_source_histories'][]=$scope+['local_source_id'=>$local,'version'=>1,'payload'=>Payables::canonical($receipt),'receipt_hash'=>hash('sha256',Payables::canonical($receipt)),'created_at'=>$now];
$rows['pa_sources'][]=$scope+['id'=>$source,'source_key'=>$key,'local_source_id'=>$local,'created_at'=>$now];
$rows['pa_proposals'][]=$scope+['id'=>$proposal,'source_id'=>$source,'source_revision'=>$revision,'capture_version'=>1,'snapshot'=>Payables::canonical($doc+['local_id'=>$local,'_history_import'=>$capture]),'missing'=>'[]','prepared_by'=>$f['actors']['prepare'],'prepared_person'=>'sgf-history-person-'.$f['actors']['prepare'],'reason'=>'Synthetic benchmark clone, not another native workflow execution.','created_at'=>$now];
$rows['pa_obligations'][]=$scope+['id'=>$obligation,'source_id'=>$source,'proposal_id'=>$proposal,'source_revision'=>$revision,'principal_minor'=>6000,'currency'=>'MZN','due_on'=>null,'recognised_by'=>$f['actors']['validate'],'recognised_person'=>'sgf-history-person-'.$f['actors']['validate'],'reason'=>'Synthetic benchmark recognised fixture.','created_at'=>$now];
$rows['pa_history_imports'][]=$scope+['obligation_id'=>$obligation,'source_id'=>$source,'source_key'=>$key,'history_hash'=>$capture['history_hash'],'confirmed_minor'=>3000,'pending_minor'=>1000,'rejected_minor'=>500,'snapshot'=>Payables::canonical($capture),'imported_by'=>$f['actors']['validate'],'created_at'=>$now];
foreach($capture['payments'] as $p)$rows['pa_imported_payments'][]=$scope+['obligation_id'=>$obligation,'source_id'=>$source,'payment_key'=>$p['payment_key'],'legacy_payment_id'=>$p['id'],'amount_minor'=>$p['amount_minor'],'status'=>$p['status'],'snapshot'=>Payables::canonical($p),'created_at'=>$now];
foreach(['payable.proposed','payable.legacy_history_imported','payable.recognised'] as $kind)$rows['pa_events'][]=$scope+['source_id'=>$source,'actor_id'=>$f['actors']['validate'],'kind'=>$kind,'payload'=>Payables::canonical(['benchmark_only'=>true,'proposal_id'=>$proposal,'obligation_id'=>$obligation]),'created_at'=>$now];
}
DB::transaction(function()use($rows){foreach($rows as $table=>$batch)DB::table($table)->insert($batch);});
}
file_put_contents($s['root'].'/page-volume.json',json_encode(['documents'=>$target,'benchmark_clones'=>$target-1,'native_workflow_documents'=>1,'chunk_size'=>100,'indices_added'=>false],JSON_PRETTY_PRINT));echo 'Owned synthetic page fixture volume '.$target." ready.\n";