table('permission_grants')->where(['tenant_id'=>1,'company_id'=>2])->where(fn($q)=>$q->whereNull('unit_id')->orWhere('unit_id',2))->whereIn('permission',$rights)->orderBy('user_id')->orderBy('permission')->get(['user_id','permission','unit_id'])->map(fn($r)=>(array)$r)->all();$missing=array_values(array_diff($rights,array_column($rows,'permission')));if($missing)throw new LogicException('Additional powers need explicit proposal '.implode(',',$missing));file_put_contents($work.'/multiline-invoice-existing-access-private.json',json_encode(['passed'=>true,'book'=>$book,'existing_grants'=>$rows,'required_names'=>$rights,'missing'=>[],'database_writes'=>0,'new_grants'=>0],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'book'=>$book,'existing_permission_names'=>count($rights),'new_grants'=>0,'proof'=>$work.'/multiline-invoice-existing-access-private.json']).PHP_EOL;