96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
/**
|
|
* Claims Collection dashboard backed by claim_report with ticket_master fallback.
|
|
* Reuses V2 KPI SQL; claim fact tables are rewritten at query time.
|
|
*/
|
|
class ClaimReportDashboardModel extends ClaimsCollectionV2DashboardModel
|
|
{
|
|
/** @var array<int, string> */
|
|
protected array $claimsTableCache = [];
|
|
|
|
/**
|
|
* Resolve claim fact table for a policy.
|
|
* Uses claim_report when TPA dump table exists and claim_report has rows; else ticket_master.
|
|
*/
|
|
public function resolveClaimsTable(int $policyId): string
|
|
{
|
|
if (isset($this->claimsTableCache[$policyId])) {
|
|
return $this->claimsTableCache[$policyId];
|
|
}
|
|
|
|
$table = 'ticket_master';
|
|
$db = \Config\Database::connect($this->DBGroup);
|
|
|
|
if (!$db->tableExists('claim_report') || $policyId <= 0) {
|
|
return $this->claimsTableCache[$policyId] = $table;
|
|
}
|
|
|
|
$policy = $db->table('client_policy')
|
|
->select('tpa_id')
|
|
->where('id', $policyId)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$tpaId = (int) ($policy['tpa_id'] ?? 0);
|
|
$dumpTable = $this->getTpaDumpTableMap()[$tpaId] ?? null;
|
|
|
|
if ($dumpTable === null || !$db->tableExists($dumpTable)) {
|
|
return $this->claimsTableCache[$policyId] = $table;
|
|
}
|
|
|
|
$hasReportRows = $db->table('claim_report')
|
|
->where('client_policy_id', $policyId)
|
|
->where('is_active', 1)
|
|
->countAllResults() > 0;
|
|
|
|
if ($hasReportRows) {
|
|
$table = 'claim_report';
|
|
}
|
|
|
|
return $this->claimsTableCache[$policyId] = $table;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
protected function getTpaDumpTableMap(): array
|
|
{
|
|
return [
|
|
(int) env('VIDAL_PRIMARY_KEY_CONSTANT') => 'claims_dump_vidal',
|
|
(int) env('ABHI_PRIMARY_KEY_CONSTANT') => 'claims_dump_abhi',
|
|
(int) env('MEDI_ASSIST_PRIMARY_KEY_CONSTANT') => 'claims_dump_medi_assist',
|
|
(int) env('FHPL_PRIMARY_KEY_CONSTANT') => 'claims_dump_fhpl',
|
|
(int) env('R_CARE_PRIMARY_KEY_CONSTANT') => 'claims_dump_reliance',
|
|
(int) env('ICICI_PRIMARY_KEY_CONSTANT') => 'claims_dump_icici',
|
|
];
|
|
}
|
|
|
|
protected function runKpiQuery(string $sql, int $policyId): array
|
|
{
|
|
$sql = str_replace(['\\t', '\\n', '\\r'], ["\t", "\n", "\r"], $sql);
|
|
|
|
$claimsTable = $this->resolveClaimsTable($policyId);
|
|
if ($claimsTable !== 'ticket_master') {
|
|
$sql = preg_replace('/\bticket_master\b/', $claimsTable, $sql) ?? $sql;
|
|
}
|
|
|
|
$db = \Config\Database::connect($this->DBGroup);
|
|
$query = $db->query($sql, ['policy_id' => $policyId]);
|
|
|
|
return $query->getResultArray();
|
|
}
|
|
|
|
public function getAllKpis(int $policyId): array
|
|
{
|
|
$out = parent::getAllKpis($policyId);
|
|
$out['_meta'] = [
|
|
'claims_source' => $this->resolveClaimsTable($policyId),
|
|
];
|
|
|
|
return $out;
|
|
}
|
|
}
|