diff --git a/app/Config/Routes.php b/app/Config/Routes.php index cf17885b..b9988f4a 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -449,6 +449,8 @@ $routes->group("/util", ["filter" => "authMVC"], function ($routes) { $routes->post('insufficientCdBalanceHrMailSend', 'EmployeeController::insufficientCdBalanceHrMailSend'); $routes->get('getTpaClaimDumpErrorData/(:any)', 'TicketServiceController::getTpaClaimDumpErrorData/$1'); $routes->get('croneDailyActivityReport', 'DashboardController::croneDailyActivityReport'); + $routes->get('insertSampleTpaApiData/(:any)', 'TestingController::insertSampleTpaApiData/$1'); + $routes->get('listEmployeeCountByClientPolicy', 'TestingController::listEmployeeCountByClientPolicy'); }); $routes->post("policy_tranction/sendInstallmentRemainderMail","PolicyTransactionController::sendInstallmentRemainderMail"); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index df5dcfab..8d160ef5 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -2404,6 +2404,9 @@ class EmployeeRestController extends AdminController if ($this->request->is('get')) { + $client_id = $this->request->getGet('client_id') ?? null; + + $data['claim_status'] = $this->claimStatusModel ->select('id,ticket_type, display_name as claim_status') ->where('is_active', 1) @@ -2411,12 +2414,57 @@ class EmployeeRestController extends AdminController ->groupBy('display_name') ->findAll(); - $data['ticket_type'] = [ - ["ticket_type" => "1", "type_name" => "Claim-GMC"], - ["ticket_type" => "2", "type_name" => "Claim-GPA"], - ["ticket_type" => "3", "type_name" => "EDLI"], - ["ticket_type" => "4", "type_name" => "GTLI"], - ]; + if (!empty($client_id)) { + + if (is_string($client_id) && preg_match('/^[a-f0-9]{32}$/i', $client_id)) { + $client_data = $this->clientModel->where('MD5(id)', $client_id)->first(); + $client_id = $client_data['id'] ?? null; + } + + $client_data = $this->clientPolicyModel + ->where('client_id', $client_id) + ->where('is_active', 1) + ->groupBy('policy_type_id') + ->findAll(); + + $data['ticket_type'] = []; + $addedTypes = []; + + foreach ($client_data as $value) { + + if (in_array($value['policy_type_id'], [2,3,4,5]) && !in_array('1', $addedTypes)) { + $data['ticket_type'][] = ["ticket_type" => "1", "type_name" => "Claim-GMC"]; + $addedTypes[] = '1'; + + } elseif (in_array($value['policy_type_id'], [1]) && !in_array('2', $addedTypes)) { + $data['ticket_type'][] = ["ticket_type" => "2", "type_name" => "Claim-GPA"]; + $addedTypes[] = '2'; + + } elseif (in_array($value['policy_type_id'], [6]) && !in_array('3', $addedTypes)) { + $data['ticket_type'][] = ["ticket_type" => "3", "type_name" => "EDLI"]; + $addedTypes[] = '3'; + + } elseif (in_array($value['policy_type_id'], [7]) && !in_array('4', $addedTypes)) { + $data['ticket_type'][] = ["ticket_type" => "4", "type_name" => "GTLI"]; + $addedTypes[] = '4'; + + } elseif (in_array($value['policy_type_id'], [72]) && !in_array('72', $addedTypes)) { + $data['ticket_type'][] = ["ticket_type" => "72", "type_name" => "OPD"]; + $addedTypes[] = '72'; + } + } + + usort($data['ticket_type'], fn($a,$b) => $a['ticket_type'] <=> $b['ticket_type']); + + } else { + + $data['ticket_type'] = [ + ["ticket_type" => "1", "type_name" => "Claim-GMC"], + ["ticket_type" => "2", "type_name" => "Claim-GPA"], + ["ticket_type" => "3", "type_name" => "EDLI"], + ["ticket_type" => "4", "type_name" => "GTLI"], + ]; + } $claim_type = $this->ticketController->claimType; unset($claim_type[1][2]); diff --git a/app/Controllers/TestingController.php b/app/Controllers/TestingController.php index c83fe172..dce9636c 100644 --- a/app/Controllers/TestingController.php +++ b/app/Controllers/TestingController.php @@ -5,8 +5,11 @@ namespace App\Controllers; use App\Controllers\BaseController; use App\Models\EmployeePolicyModel; use App\Models\ClientPolicyModel; +use App\Models\TpaApiDataModel; +use App\Models\BatchFileModel; use App\Models\InsurerBranchModel; use App\Models\RFQModel; +use App\Models\EmployeeModel; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\API\ResponseTrait; use Dompdf\Dompdf; @@ -1120,4 +1123,185 @@ class TestingController extends BaseController 'metabaseUrl' => 'https://nsights.nhanceindia.in', ]); } + + /** + * Insert sample data into tpa_api_data for testing variance report (Not in NHANCE, Not in TPA, Need to Review). + * Uses client_policy (policy_status=1, is_active=1), employee_policies (active), and employees. + * + * @param int|null $client_policy_id Optional. If not provided, first eligible policy is used. + * @return \CodeIgniter\HTTP\ResponseInterface + */ + public function insertSampleTpaApiData($client_policy_id = null) + { + $db = \Config\Database::connect(); + $clientPolicyModel = new ClientPolicyModel(); + $employeePolicyModel = new EmployeePolicyModel(); + $employeeModel = new EmployeeModel(); + $tpaApiDataModel = new TpaApiDataModel(); + $batchFileModel = new BatchFileModel(); + + // 1. Get policies: policy_status = 1, is_active = 1 + $policyBuilder = $clientPolicyModel + ->where('policy_status', 1) + ->where('is_active', 1); + if ($client_policy_id !== null && $client_policy_id !== '') { + $policyBuilder->where('id', (int) $client_policy_id); + } + $policies = $policyBuilder->orderBy('id', 'ASC')->findAll(); + if (empty($policies)) { + return $this->respond([ + 'status' => false, + 'message' => 'No active client policy found (policy_status=1, is_active=1).', + 'data' => [], + ], 400); + } + $policy = $policies[0]; + $client_policy_id = (int) $policy['id']; + $client_id = (int) $policy['client_id']; + $client_branch_id = !empty($policy['client_branch_id']) ? (int) $policy['client_branch_id'] : 0; + + // 2. Get related employees from employee_policies (active) + employees + $empPolicies = $db->table('employee_polices ep') + ->select('ep.id AS emp_policy_id, ep.employee_id, ep.tpa_id, e.emp_code, e.name, e.dob, e.gender, e.relationship') + ->join('employees e', 'e.id = ep.employee_id') + ->where('ep.client_policy_id', $client_policy_id) + ->where('ep.is_active', 1) + ->whereIn('ep.status', ['active', 'expired']) + ->where('e.is_active', 1) + ->get() + ->getResultArray(); + if (empty($empPolicies)) { + return $this->respond([ + 'status' => false, + 'message' => 'No active employee policies found for this client policy.', + 'data' => ['client_policy_id' => $client_policy_id], + ], 400); + } + + // 3. Create a test batch file so we have a file_id for tpa_api_data + $createdBy = function_exists('get_session_userid') ? get_session_userid() : 1; + $batchCode = 'TPA_SAMPLE_' . date('YmdHis') . '_' . bin2hex(random_bytes(4)); + $batchFileId = $batchFileModel->insert([ + 'client_id' => $client_id, + 'client_policy_id' => $client_policy_id, + 'client_branch_id' => $client_branch_id, + 'batch_code' => $batchCode, + 'file_name' => 'sample_tpa_data_test_' . date('Y-m-d_His') . '.xlsx', + 'insurer_or_tpa' => 'tpa', + 'event_type' => 'api', + 'actions' => 'fetch', + 'status' => 'partially success', + 'count' => 0, + 'created_by' => $createdBy, + 'is_active' => 1, + ]); + if (!$batchFileId) { + return $this->respond([ + 'status' => false, + 'message' => 'Failed to create test batch file.', + 'data' => [], + ], 500); + } + $file_id = (int) $batchFileId; + + $tpaApiDataModel->skipValidation(true); + $inserted = ['not_in_nhance' => 0, 'need_to_review' => 0]; + $toInsert = []; + + // 4. Not in NHANCE: insert TPA records with emp_codes that do NOT exist in NHANCE for this policy + $fakeEmpCodes = ['TPA_SAMPLE_NOTINNHANCE_1', 'TPA_SAMPLE_NOTINNHANCE_2']; + foreach ($fakeEmpCodes as $i => $empCode) { + $toInsert[] = [ + 'file_id' => $file_id, + 'emp_code' => $empCode, + 'name' => 'Sample TPA Only ' . ($i + 1), + 'dob' => '1990-01-' . str_pad((string)(15 + $i), 2, '0', STR_PAD_LEFT), + 'relation' => 'Self', + 'gender' => ($i % 2 === 0) ? 'M' : 'F', + 'self' => 'Sample TPA Only ' . ($i + 1), + 'tpa_id' => 'TPA' . (1000 + $i), + 'age' => 32 + $i, + 'is_active' => 1, + 'desc' => 'Sample data – Not in NHANCE', + 'created_by'=> $createdBy, + ]; + $inserted['not_in_nhance']++; + } + + // 5. Need to Review: same employee as in NHANCE but with different name/dob/gender + $needReview = array_slice($empPolicies, 0, min(2, count($empPolicies))); + foreach ($needReview as $emp) { + $dob = $emp['dob']; + if (is_string($dob) && preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dob, $m)) { + $altDob = $m[1] . '-' . $m[2] . '-' . str_pad((string)((int)$m[3] + 1), 2, '0', STR_PAD_LEFT); + } else { + $altDob = '1995-06-15'; + } + $toInsert[] = [ + 'file_id' => $file_id, + 'emp_code' => $emp['emp_code'], + 'name' => '[TPA Altered] ' . ($emp['name'] ?? 'Unknown'), + 'dob' => $altDob, + 'relation' => $emp['relationship'] ?? 'Self', + 'gender' => (strtoupper($emp['gender'] ?? 'M') === 'M') ? 'F' : 'M', + 'self' => $emp['name'] ?? 'Unknown', + 'tpa_id' => $emp['tpa_id'] ?? ('T' . $emp['employee_id']), + 'age' => 30, + 'is_active' => 1, + 'desc' => 'Sample data – Need to Review (mismatch)', + 'created_by'=> $createdBy, + ]; + $inserted['need_to_review']++; + } + + foreach ($toInsert as $row) { + $tpaApiDataModel->insert($row); + } + + // Not in TPA: we do NOT insert those into tpa_api_data; NHANCE already has employees. So any employee + // we did not add to tpa_api_data will appear as "Not in TPA". We added only "Need to Review" and + // "Not in NHANCE" rows; the rest of NHANCE employees remain without TPA rows => they show as Not in TPA. + + return $this->respond([ + 'status' => true, + 'message' => 'Sample TPA API data inserted successfully.', + 'data' => [ + 'file_id' => $file_id, + 'client_policy_id' => $client_policy_id, + 'client_id' => $client_id, + 'batch_code' => $batchCode, + 'inserted' => $inserted, + 'not_in_tpa_note' => 'Employees in NHANCE that were not added to TPA data will appear as "Not in TPA" when you run the variance report for this file.', + ], + ], 200); + } + + /** + * List employee count per client policy. + * No input parameters. Checks all client_policy records and counts linked employee_policies per policy. + * + * @return \CodeIgniter\HTTP\ResponseInterface + */ + public function listEmployeeCountByClientPolicy() + { + $db = \Config\Database::connect(); + $rows = $db->table('client_policy cp') + ->select('cp.id AS client_policy_id, COUNT(ep.id) AS employee_policy_count', false) + ->join('employee_polices ep', 'ep.client_policy_id = cp.id', 'left') + ->groupBy('cp.id') + ->orderBy('cp.id', 'ASC') + ->get() + ->getResultArray(); + $list = array_map(function ($row) { + return [ + 'client_policy_id' => (int) $row['client_policy_id'], + 'employee_policy_count' => (int) $row['employee_policy_count'], + ]; + }, $rows); + return $this->respond([ + 'status' => true, + 'message' => 'Employee count per client policy.', + 'data' => $list, + ], 200); + } } diff --git a/app/Controllers/TicketController.php b/app/Controllers/TicketController.php index 3833e4e0..986579e6 100644 --- a/app/Controllers/TicketController.php +++ b/app/Controllers/TicketController.php @@ -1777,8 +1777,12 @@ class TicketController extends BaseController $ticket_id = $this->request->getPost('ticket_master_id'); $old_ticket_data = $this->ticketMasterModel->where('id', $ticket_id)->where('is_active', 1)->first(); $ticket_data['claim_status_id'] = $this->getLastMatchedStatus($ticket_data, $old_ticket_data); + $ticket_data['last_updated_by'] = 'USER'; // print_rr($ticket_data); die; + $this->myLogger->logme('error', "[UPDATE_CLAIM] Ticket Master ID: {data}", ['data' => $ticket_id]); + $this->myLogger->logme('error', "[UPDATE_CLAIM] Old Ticket Data: {data}", ['data' => json_encode($old_ticket_data, JSON_PRETTY_PRINT)]); + $this->myLogger->logme('error', "[UPDATE_CLAIM] New Ticket Data: {data}", ['data' => json_encode($ticket_data, JSON_PRETTY_PRINT)]); if ($ticket_data) { $return_value = $this->ticketMasterModel->where('id', $ticket_id)->set($ticket_data)->update(); @@ -2400,6 +2404,8 @@ class TicketController extends BaseController th.old_value, th.new_value, th.created_at, + th.updated_by, + CONCAT_WS(' ', creator.first_name, creator.last_name) AS modified_by, -- Claim Status old_status.claim_status as old_status_value, @@ -2414,7 +2420,7 @@ class TicketController extends BaseController new_insured_emp.name as new_insured_id_name, ticket_master.ticket_type_id - + FROM ticket_history th diff --git a/app/Models/TicketMasterModel.php b/app/Models/TicketMasterModel.php index 13682edb..d78402b6 100644 --- a/app/Models/TicketMasterModel.php +++ b/app/Models/TicketMasterModel.php @@ -94,6 +94,7 @@ class TicketMasterModel extends Model 'tpa_claim_type', 'tpa_ailments', 'claim_dump_ref_id', + 'last_updated_by', ]; diff --git a/app/Views/ticket_history.php b/app/Views/ticket_history.php index 384a638d..fe1fc222 100644 --- a/app/Views/ticket_history.php +++ b/app/Views/ticket_history.php @@ -20,7 +20,7 @@ '.$row['new_value']; ?> - +