177 lines
5.5 KiB
PHP
177 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\ClaimsCollectionV2DashboardModel;
|
|
use App\Models\ClaimDumpFileModel;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
/**
|
|
* Claims Collection V2 KPI API (Metabase SQL as model methods).
|
|
*/
|
|
class ClaimsCollectionV2DashboardController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
|
|
/**
|
|
* Resolve policy id from request; optional $fallback (e.g. route default for debug only).
|
|
*/
|
|
protected function resolvePolicyId(?int $fallback = null): int
|
|
{
|
|
$id = (int) (
|
|
$this->request->getGet('client_policy')
|
|
?? $this->request->getGet('client_policy_id')
|
|
?? $this->request->getPost('client_policy')
|
|
?? $this->request->getPost('client_policy_id')
|
|
?? 0
|
|
);
|
|
|
|
if ($id > 0) {
|
|
return $id;
|
|
}
|
|
|
|
if ($fallback !== null && $fallback > 0) {
|
|
return $fallback;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Resolve KPI by Metabase numeric id or method slug.
|
|
*/
|
|
protected function resolveKpiMethod(string $kpiKey): ?string
|
|
{
|
|
$kpiKey = trim($kpiKey);
|
|
if ($kpiKey === '') {
|
|
return null;
|
|
}
|
|
|
|
if (in_array($kpiKey, ClaimsCollectionV2DashboardModel::KPI_MAP, true)) {
|
|
return $kpiKey;
|
|
}
|
|
|
|
if (ctype_digit($kpiKey)) {
|
|
$id = (int) $kpiKey;
|
|
return ClaimsCollectionV2DashboardModel::KPI_MAP[$id] ?? null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* JSON: single KPI by slug (e.g. incurred_ratio) or Metabase id (e.g. 207).
|
|
* Requires client_policy or client_policy_id.
|
|
*/
|
|
public function kpi(string $kpiMethod = '')
|
|
{
|
|
$policyId = $this->resolvePolicyId();
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'client_policy or client_policy_id is required.',
|
|
], 422);
|
|
}
|
|
|
|
$kpiMethod = $this->resolveKpiMethod($kpiMethod);
|
|
if ($kpiMethod === null) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'Unknown KPI. Pass Metabase id or method slug.',
|
|
'allowed' => ClaimsCollectionV2DashboardModel::KPI_MAP,
|
|
], 404);
|
|
}
|
|
|
|
$model = new ClaimsCollectionV2DashboardModel();
|
|
$metabaseId = array_search($kpiMethod, ClaimsCollectionV2DashboardModel::KPI_MAP, true);
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'kpi_id' => $metabaseId !== false ? (int) $metabaseId : null,
|
|
'kpi' => $kpiMethod,
|
|
'label' => ClaimsCollectionV2DashboardModel::KPI_LABELS[$kpiMethod] ?? $kpiMethod,
|
|
'rows' => $model->getKpi($kpiMethod, $policyId),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* JSON: all KPIs. Requires client_policy or client_policy_id.
|
|
*/
|
|
public function all()
|
|
{
|
|
$policyId = $this->resolvePolicyId();
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'client_policy or client_policy_id is required.',
|
|
], 422);
|
|
}
|
|
|
|
$generatedAt = (new ClaimDumpFileModel())->getGeneratedAtForPolicy($policyId);
|
|
if ($generatedAt === null) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'claim_dump_date is mandatory. No claim dump date found for this policy.',
|
|
], 422);
|
|
}
|
|
|
|
$model = new ClaimsCollectionV2DashboardModel();
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'generated_at' => $generatedAt,
|
|
'data' => $model->getAllKpis($policyId),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Admin preview UI (authMVC only) — KPI grid for manual testing.
|
|
* Default policy id only in method signature; override via query or /preview/{id}.
|
|
*/
|
|
public function preview(int $policyId = 4687)
|
|
{
|
|
$policyId = $this->resolvePolicyId($policyId);
|
|
$path = $this->request->getUri()->getPath();
|
|
$isJwt = stripos($path, 'employeeRest') !== false;
|
|
$prefix = $isJwt ? 'employeeRest/claims-collection-v2' : 'util/claims-collection-v2';
|
|
|
|
return view('claims_collection_v2_dashboard', [
|
|
'policy_id' => $policyId,
|
|
'kpi_map' => ClaimsCollectionV2DashboardModel::KPI_MAP,
|
|
'kpi_labels' => ClaimsCollectionV2DashboardModel::KPI_LABELS,
|
|
'api_all_url' => base_url($prefix . '/all'),
|
|
'api_kpi_url' => base_url($prefix . '/kpi'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Admin check only: raw JSON on screen (no dashboard UI).
|
|
* Default policy id only here; override via ?client_policy= or /debug/{id}.
|
|
*/
|
|
public function debug(int $policyId = 4687)
|
|
{
|
|
$policyId = $this->resolvePolicyId($policyId);
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->response
|
|
->setStatusCode(422)
|
|
->setBody('client_policy or client_policy_id is required.');
|
|
}
|
|
|
|
$model = new ClaimsCollectionV2DashboardModel();
|
|
$body = json_encode([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'data' => $model->getAllKpis($policyId),
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
return $this->response
|
|
->setHeader('Content-Type', 'application/json; charset=UTF-8')
|
|
->setBody($body);
|
|
}
|
|
}
|