identifiers();$unit=$ctx->unitId();$host=DB::connection();$version=Catalogue::active()->source_version;$p=app(Permissions::class);$actor=$ctx->userId(); $read=function(string $provider,int $unitKey,string $module,\Closure $names,?int $only=null)use($host,$s,$kind,$filters,$version){ $query=$host->table('geo_locations')->where($s)->where(['unit_key'=>$unitKey,'module_key'=>$module,'kind'=>$kind,'provider'=>$provider])->whereNotNull('selection')->where('selection->version',$version);if($only!==null)$query->where('entity_id',$only);foreach(Cartography::FIELDS as$f)if(isset($filters[$f]))$query->where('selection->codes->'.$f,$filters[$f]);$after=(int)($filters['after']??0);$out=[]; do{$rows=(clone $query)->where('entity_id','>',$after)->orderBy('entity_id')->limit(100)->get();if($rows->isEmpty())break;$allowed=$names($rows->pluck('entity_id')->all());foreach($rows as$row){$after=(int)$row->entity_id;if(isset($allowed[$after]))$out[]=['entity_id'=>$after,'name'=>$allowed[$after],'location'=>json_decode($row->selection,true),'revision'=>(int)$row->revision];if(count($out)===26)break 2;}}while($rows->count()===100); $more=count($out)>25;if($more)array_pop($out);return ['version'=>$version,'data'=>$out,'next_after'=>$more?end($out)['entity_id']:null,'page_size'=>25,'population'=>'explicit_segment_locations']; }; if($kind==='supplier')return app(ReceiptRuntime::class)->run('purchasing_suppliers.view',function($native,$binding,$fingerprint,$origin)use($read,$unit){abort_unless($native->hasFunctions(['admin','procurement','finance','department_head']),403);return $read(hash('sha256',DB::connection()->getDatabaseName()),$unit,'purchasing',fn($ids)=>SupplierAccess::query($binding,$origin['sgf_context_key'])->whereIn('id',$ids)->pluck('name','id')->all());}); if($kind==='client'){$p->require($actor,$s['tenant_id'],$s['company_id'],'clients.view');abort_unless(ModuleRecords::active()==='commercial',403);return $read('sgf',0,'commercial',fn($ids)=>ModuleRecords::query('a_clients')->where($s)->whereNull('unit_id')->whereIn('id',$ids)->pluck('name','id')->all());} if($kind==='unit'){abort_unless($unit!==null,403);$p->require($actor,$s['tenant_id'],$s['company_id'],'unit.view',$unit);return $read('sgf',$unit,'general',fn($ids)=>$host->table('units')->where($s)->whereIn('id',$ids)->pluck('name','id')->all(),$unit);} return $read('sgf',0,'general',fn($ids)=>$host->table('companies')->where('tenant_id',$s['tenant_id'])->where('id',$s['company_id'])->whereIn('id',$ids)->pluck('name','id')->all(),$s['company_id']); } }