21 lines
6.2 KiB
PHP
21 lines
6.2 KiB
PHP
<?php
|
|
use App\Modules\Services\BillingRegister;use Illuminate\Support\Str;
|
|
require __DIR__.'/sgf-service-documents-proof-bootstrap.php';$actor=$select('finance_review');$service=app(BillingRegister::class);$before=[];foreach(['si_invoices','sd_outputs','cq_versions','sr_records','sr_effects','a_billing_drafts'] as $t)$before[$t]=$host->table($t)->orderBy('id')->get()->toJson();$host->beginTransaction();
|
|
try{
|
|
$state=$service->listing($actor,['per_page'=>100]);$ok($state['documents']->perPage()===100,'server register accepts100 rows');$fr=$host->table('sd_outputs')->where('kind','invoice_receipt')->firstOrFail();$rows=collect($state['documents']->items());$ok($rows->where('kind','invoice_receipt')->where('invoice_id',$fr->invoice_id)->count()===1&&$rows->where('kind','invoice')->where('invoice_id',$fr->invoice_id)->count()===0,'invoice-receipt appears once without its technical invoice duplicated');
|
|
$row=$rows->first(fn($r)=>$r->kind==='invoice_receipt');$ok($row->balance_available&&bccomp($row->origin_invoice_balance,'0.00',2)===0,'bounded projection reconciles settled invoice-receipt balance');
|
|
$series=explode('/',$row->reference,2)[0];$r=$service->listing($actor,['series'=>$series,'kind'=>'invoice_receipt']);$ok($r['documents']->total()===1,'exact series filter retained on server');$r=$service->listing($actor,['kind'=>'invoice_receipt','author'=>$row->actor_id]);$ok($r['documents']->total()===1,'recorded author filter retained on server');$r=$service->listing($actor,['currency'=>'USD']);$ok($r['documents']->total()===0,'currency filter does not invent conversions');
|
|
foreach($rows->where('kind','draft') as $draft){$data=json_decode($host->table('a_billing_drafts')->where('id',$draft->source_id)->value('data'),true);$ok($draft->base===App\Modules\Services\DraftValues::base($data),'page draft'.$draft->source_id.' preserves canonical base without fiscal total');}
|
|
$foreign=(array)$fr;unset($foreign['id']);$foreign['tenant_id']=1;$foreign['company_id']=1;$foreign['unit_id']=1;$foreign['document_key']=(string)Str::uuid();$foreign['version_id']=$host->table('sd_versions')->where(['document_key'=>$fr->document_key,'version'=>1])->value('id');$foreign['invoice_id']=$rows->first(fn($r)=>$r->kind==='invoice')->invoice_id;$foreign['number']=999999;$host->table('sd_outputs')->insert($foreign);
|
|
$afterForeign=$service->listing($actor,['per_page'=>100]);$ok($afterForeign['documents']->total()===$state['documents']->total()&&collect($afterForeign['documents']->items())->where('kind','invoice')->where('invoice_id',$foreign['invoice_id'])->count()===1,'foreign output fixture cannot appear or suppress an authorised invoice');
|
|
$base=(array)$host->table('cq_versions')->where('action','prepare')->firstOrFail();unset($base['id']);$deps=json_decode($base['dependencies'],true);
|
|
for($i=0;$i<127;$i++){$data=$base;$data['quote_key']=(string)Str::uuid();$data['previous_id']=null;$data['version']=1;$data['state']='draft';$data['action']='prepare';$data['prepared_by']=$data['actor_id']=$profiles['commercial_prepare']['user_id'];$data['reason']='Fixture de consulta apenas; não decisão comercial.';$deps['client']['name']='QA-REGISTO-%_!-'.$i;$data['dependencies']=json_encode($deps);$host->table('cq_versions')->insert($data);}
|
|
$keys=[];$last=null;for($page=1;$page<=6;$page++){$host->flushQueryLog();$host->enableQueryLog();$result=$service->listing($actor,['kind'=>'quote','search'=>'QA-REGISTO-%_!-','per_page'=>25,'page'=>$page]);$sql=array_filter($host->getQueryLog(),fn($q)=>str_contains($q['query'],'document_register')||str_contains($q['query'],'register_ob'));$host->disableQueryLog();$ok(count($sql)===2,'quote page'.$page.' uses two bounded register queries');foreach($result['documents']->items() as $r)$keys[]=$r->source_id;$last=$result;}
|
|
$ok(count($keys)===127&&count(array_unique($keys))===127&&$last['documents']->total()===127,'six pages cover127 literal-search fixtures without loss or duplication');$r=$service->listing($actor,['kind'=>'quote','search'=>'QA-REGISTO-%_!-','per_page'=>100,'page'=>1]);$ok(count($r['documents']->items())===100,'first100-row page contains exact100 fixtures');$r=$service->listing($actor,['kind'=>'quote','search'=>'QA-REGISTO-%_!-','per_page'=>100,'page'=>2]);$ok(count($r['documents']->items())===27,'second100-row page contains exact27 remainder');
|
|
$r=$service->listing($actor,['kind'=>'quote','search'=>'QA-REGISTO-%_!-','from'=>'2026-01-01']);$ok($r['documents']->total()===0,'emission filter excludes unissued quotes');$service->listing($actor,['to'=>'2026-12-31']);$ok(true,'standalone upper date filter accepted');$reject(fn()=>$service->listing($actor,['from'=>'2026-12-31','to'=>'2026-01-01']),422,'reversed emission range refused');$reject(fn()=>$service->listing($actor,['company_id'=>1]),422,'caller cannot override company context');
|
|
$host->beginTransaction();try{$host->table('permission_grants')->where(['user_id'=>$actor,'permission'=>'service_receivables.view'])->delete();$r=$service->listing($actor,['per_page'=>100]);$ok(!$r['financial_access']&&collect($r['documents']->items())->every(fn($r)=>$r->origin_invoice_balance===null&&!$r->balance_available),'missing financial right hides all balances rather than exposing zero');$host->table('permission_grants')->where(['user_id'=>$actor,'permission'=>'service_documents.view'])->delete();$r=$service->listing($actor,['per_page'=>100]);$ok(collect($r['documents']->items())->every(fn($r)=>!in_array($r->kind,['receipt','credit_note','invoice_receipt'],true)),'missing additional-document right hides those families and counts');}finally{$host->rollBack();}
|
|
request()->attributes->set('sgf_module','finance');$reject(fn()=>$service->listing($actor,[]),403,'other module cannot invoke Commercial register');request()->attributes->set('sgf_module','commercial');
|
|
}finally{while($host->transactionLevel()>0)$host->rollBack();}
|
|
foreach($before as $t=>$rows)$ok($rows===$host->table($t)->orderBy('id')->get()->toJson(),'read proof preserves original '.$t);
|
|
file_put_contents($work.'/billing-register-result.json',json_encode(['passed'=>true,'checks'=>$checks,'fixtures_rolled_back'=>127,'new_rights'=>0,'browser_visual_review'=>false],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/billing-register-result.json']).PHP_EOL;
|