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

26 lines
4.0 KiB
PHP

<?php
use Illuminate\Support\Facades\{Auth,DB};
use Illuminate\Support\Str;
use App\Shared\Context\CompanyContext;
use App\Modules\Industry\IndustrialCosts;
$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')]);
$path=$s['root'].'/single-industrial.json';
if(is_file($path))$result=json_decode(file_get_contents($path),true);else{
require_once base_path('tests/Support/IdentityFixture.php');require_once base_path('tests/Support/IndustrialCostFixture.php');
$manager=App\Models\User::findOrFail($profiles['finance_prepare']['user_id']);$reader=App\Models\User::findOrFail($profiles['finance_review']['user_id']);
$r=Illuminate\Http\Request::create('/api/industrial-costs','POST');$r->setUserResolver(fn()=>$manager);$session=app('session')->driver();$session->start();$session->put('company_context',$f['scope']+['module_id'=>'finance','token'=>'industrial-setup']);$r->setLaravelSession($session);$app->instance('request',$r);Auth::guard('web')->setUser($manager);
$users=$host->table('users')->count();$result=$host->transaction(function()use($f,$manager,$reader){
$fixture=Tests\Support\IndustrialCostFixture::create(['tenant'=>$f['tenant'],'company'=>$f['company'],'unit'=>$f['unit'],'manager'=>$manager,'reader'=>$reader]);
$engine=app(IndustrialCosts::class);app(CompanyContext::class)->selectFor($manager->id,$f['tenant'],$f['company'],$f['unit']);
$input=['currency'=>'MZN','version'=>1,'previous_id'=>null,'refs'=>$fixture['refs']];$key=(string)Str::uuid();$calculation=$engine->calculate($fixture['lot'],$manager->id,$input,$key);$retry=$engine->calculate($fixture['lot'],$manager->id,$input,$key);if($retry['id']!==$calculation['id'])throw new LogicException('Duplicate industrial result');
app(CompanyContext::class)->selectFor($reader->id,$f['tenant'],$f['company'],$f['unit']);$engine->decide($fixture['lot'],$reader->id,['type'=>'result','id'=>$calculation['id'],'version'=>1,'decision'=>'approved','reason'=>'Revisão distinta de material1000, trabalho500 e CIF300; residual300 separado.'],(string)Str::uuid());
$snap=json_decode(DB::table('ic_results')->where('id',$calculation['id'])->value('snapshot'),true);if($snap['known_cost_exact']!=='1800'||$snap['residual_exact']!=='300'||$snap['good_units']!=='10')throw new LogicException('Industrial arithmetic mismatch');
return ['lot'=>$fixture['lot'],'refs'=>$fixture['refs'],'criterionData'=>$fixture['criterionData'],'calculation'=>$calculation,'total'=>'1800','residual'=>'300','units'=>'10','synthetic_only'=>true];
});if($users!==$host->table('users')->count())throw new LogicException('No additional accounts authorized');file_put_contents($path,json_encode($result,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));
}
$origins=[];foreach(['material','labour','pool','machine','production'] as $kind)$origins[$kind]=$host->table('ic_inputs')->where('id',$result['refs'][$kind]['id'])->value('source_ref');
$native->transaction(function()use($native,$result,$origins,$nf){$scope=['tenant_id'=>1,'company_id'=>2,'unit_id'=>2];if(!$native->table('gl_industrial_sources')->where($scope)->where('lot_id',$result['lot'])->exists())$native->table('gl_industrial_sources')->insert($scope+['lot_id'=>$result['lot'],'origins'=>json_encode($origins,JSON_THROW_ON_ERROR),'period_key'=>'QA-2026-INTEGRADO','created_by'=>$nf['finance'],'reason'=>'Perímetro individual duplamente validado do lote sintético1800.','created_at'=>now()]);});
file_put_contents($work.'/industrial-workflow-result.json',json_encode(['passed'=>true,'book'=>$book,'lot'=>$result['lot'],'total'=>'1800','residual'=>'300','units'=>'10','new_accounts'=>0,'distinct_review'=>true],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'total'=>'1800','proof'=>$work.'/industrial-workflow-result.json']).PHP_EOL;