Files
SGF/scripts/sgf-commercial-workflow.php
2026-11-11 09:00:17 +02:00

30 lines
3.7 KiB
PHP

<?php
use Illuminate\Support\Facades\{DB,Auth};
use Illuminate\Support\Str;
use App\Shared\Context\CompanyContext;
use App\Modules\Services\{ServiceWorkflow,ServiceInvoicing};
$book=$argv[1]??'regression';require __DIR__.'/sgf-single-bootstrap.php';
$profiles=json_decode(file_get_contents($s['root'].'/single-profiles.json'),true)['profiles'];
config(['sgf_single_installation.enabled'=>true,'sgf_single_installation.schema_sha256'=>$host->table('sgf_composition_schema')->value('source_hash')]);
$root=$s['root'].'/single-commercial.json';
if(is_file($root)){echo json_encode(['passed'=>true,'book'=>$book,'replayed'=>true,'new_business_commands'=>0]).PHP_EOL;exit;}
$actor=App\Models\User::findOrFail($profiles['commercial_prepare']['user_id']);$reviewer=App\Models\User::findOrFail($profiles['commercial_review']['user_id']);Auth::guard('web')->setUser($actor);
$request=Illuminate\Http\Request::create('/api/a/clients','POST');$request->attributes->set('sgf_module','commercial');$request->setUserResolver(fn()=>$actor);$session=app('session')->driver();$session->start();$session->put('company_context',$f['scope']+['module_id'=>'commercial','token'=>'fixture-commercial']);$request->setLaravelSession($session);$app->instance('request',$request);
$oldClientIds=$host->table('a_clients')->pluck('id')->all();$before=$host->table('a_clients')->whereIn('id',$oldClientIds)->orderBy('id')->get()->toJson();
$result=$host->transaction(function()use($actor,$reviewer,$f,$host){
require_once base_path('tests/Support/IdentityFixture.php');require_once base_path('tests/Support/InvoiceFixture.php');
$base=['tenant'=>$f['tenant'],'company'=>$f['company'],'unit'=>$f['unit'],'manager'=>$actor,'reader'=>$reviewer];
$fixture=Tests\Support\InvoiceFixture::create($base,'862.07');
app(CompanyContext::class)->selectFor($actor->id,$f['tenant'],$f['company'],$f['unit']);
$invoices=app(ServiceInvoicing::class);$review=$invoices->review($fixture['draft'],$fixture['series'],$fixture['rule'],$actor->id);
if(!$review['ready'])throw new LogicException('Test document is not ready: '.implode(' ',$review['payload']['blocks']??[]));
$key=(string)Str::uuid();$data=['series_id'=>$fixture['series'],'rule_id'=>$fixture['rule'],'review_hash'=>$review['review_hash']];
$issued=$invoices->issue($fixture['draft'],$actor->id,$data,$key);$retry=$invoices->issue($fixture['draft'],$actor->id,$data,$key);
if($issued['id']!==$retry['id'])throw new LogicException('Invoice duplicated');
$row=$host->table('si_invoices')->where('id',$issued['id'])->first();$snapshot=json_decode($row->snapshot,true);
if($snapshot['calculation']['total_document_proposed']!=='1000.00'||hash('sha256',base64_decode($row->pdf_base64,true))!==$row->pdf_sha256)throw new LogicException('Document arithmetic/PDF mismatch');
return array_intersect_key($fixture,array_flip(['client','service','source','draft','profile','temporal','calculation','series','rule','today','start','end']))+['invoice'=>$issued['id'],'issue_key'=>$key,'preparer'=>$actor->id,'reviewer'=>$reviewer->id,'pdf_sha256'=>$row->pdf_sha256,'total'=>'1000.00','synthetic_only'=>true];
});
if($before!==$host->table('a_clients')->whereIn('id',$oldClientIds)->orderBy('id')->get()->toJson())throw new LogicException('Existing client changed');
file_put_contents($root,json_encode($result,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));file_put_contents($work.'/commercial-result.json',json_encode(['passed'=>true,'invoice'=>$result['invoice'],'total'=>'1000.00','pdf_integrity'=>true,'replayed_without_duplicate'=>true,'old_clients_preserved'=>true,'synthetic_only'=>true],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'book'=>$book,'total'=>'1000.00','old_clients_preserved'=>true,'proof'=>$work.'/commercial-result.json']).PHP_EOL;