From f763c60b3378e13cf5d2195b7aba0b08b83bbbbf Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Fri, 7 Nov 2025 17:10:26 +0530 Subject: [PATCH] FIX_MEDI_QUERY_UPDATE --- app/Controllers/ApiServiceController.php | 23 ++++- app/Controllers/ClientController.php | 6 +- app/Controllers/MediAssistApiController.php | 108 +++++++++++++++----- app/Helpers/utility_helper.php | 19 +++- 4 files changed, 123 insertions(+), 33 deletions(-) diff --git a/app/Controllers/ApiServiceController.php b/app/Controllers/ApiServiceController.php index a2740632..5b32cec1 100644 --- a/app/Controllers/ApiServiceController.php +++ b/app/Controllers/ApiServiceController.php @@ -157,6 +157,27 @@ class ApiServiceController extends BaseController return $this->respond(['status' => false, 'code' => 200,'message' => 'TPA initiation is in progress.','data' => [] ]); } + $employeePolicyModel = new EmployeePolicyModel(); + $employeePolicyData = $employeePolicyModel + ->select(' + employees.*, + employee_polices.id as emp_policy_id, + employee_polices.client_policy_id, + ') + ->join('employees', 'employees.id = employee_polices.employee_id') + ->where('employee_polices.is_active', 1) + ->where('employee_polices.status', 'active') + ->where('employees.is_active', 1) + ->where('employees.emp_status', 'active') + ->where('employee_polices.tpa_id IS NULL') + ->where('employee_polices.client_policy_id', $policy_id) + ->countAllResults(); + + if($employeePolicyData == 0){ + log_message('error', "No Employee Policy found with null TPA ID. TPA ID is already updated."); + return $this->respond(['status' => false, 'code' => 200,'message' => 'TPA ID Already updated','data' => [] ]); + } + if (empty($policy_no)) { log_message('error', 'GetBenefDetails: policy_no missing in request'); return $this->respond(['status' => false, 'message' => 'policy_no required']); @@ -181,7 +202,7 @@ class ApiServiceController extends BaseController log_message('error', "Files table inserted successfully, File id : {$file_id}"); // $mediAssistController = new MediAssistApiController(); // $mediAssistController->GetBenefDetails( [ 'polict_no' => $policy_no, 'file_id' =>$file_id ] ); - $r = Jobs::addJob(['job_name' => 'GetBenefDetails', 'payload' => ['policy_no' => $policy_no, 'file_id' => $file_id, 'return_type' => 'job']]); + $r = Jobs::addJob(['job_name' => 'GetBenefDetails', 'payload' => ['policy_no' => $policy_no, 'file_id' => $file_id, 'client_policy_id' => $policy_id, 'return_type' => 'job']]); log_message('error', "GetBenefDetails job pushed successfully."); return $this->respond(['status' => true, 'code' => 200, 'message' => 'Action Triggered','data' => [] ]); diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index d2d2e288..73149423 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -5654,10 +5654,10 @@ class ClientController extends AdminController // $ticket_id = 515; // $apiServiceController = new ApiServiceController(); // $apiServiceController->pushClaims($ticket_id); die; - + // echo change_date_format('13/10/1998 00:00:00','d/m/Y H:i:s');die(); // $medi_assist = new MediAssistApiController(); - // $medi_assist->GetBenefDetails(['policy_no' => '97000034240400000030', 'file_id' => 389, 'return_type' => 'job' ]); - // die; + // $res = $medi_assist->GetBenefDetails(['policy_no' => '97000034240400000030', 'file_id' => 389, 'return_type' => 'job', 'client_policy_id' => 6192 ]); + // dd($res); // $employeeController = new EmployeeController(); // $employeeController->truncateFileData('633'); diff --git a/app/Controllers/MediAssistApiController.php b/app/Controllers/MediAssistApiController.php index 34ec932e..d40e8939 100644 --- a/app/Controllers/MediAssistApiController.php +++ b/app/Controllers/MediAssistApiController.php @@ -4,6 +4,7 @@ namespace App\Controllers; use App\Controllers\BaseController; use App\Models\BatchFileModel; +use App\Models\EmployeePolicyModel; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; @@ -216,6 +217,7 @@ class MediAssistApiController extends BaseController ]; $policyNo = $requestData['policy_no'] ?? null; + $client_policy_id = $requestData['client_policy_id'] ?? null; if (empty($policyNo)) { log_message('error', 'GetBenefDetails: policy_no missing in request'); @@ -226,14 +228,48 @@ class MediAssistApiController extends BaseController } } + if (empty($client_policy_id)) { + log_message('error', 'GetBenefDetails: client_policy_id missing in request'); + if($function_calling_type == "job"){ + return ['status' => false, 'message' => 'client_policy_id required']; + }else{ + return $this->respond(['status' => false, 'message' => 'client_policy_id required']); + } + } + log_message('error', "GetBenefDetails called for policy_no: {$policyNo}"); + log_message('error', "GetBenefDetails called for client_policy_id: {$client_policy_id}"); + + $employeePolicyModel = new EmployeePolicyModel(); + $employeePolicyData = $employeePolicyModel + ->select(' + employees.*, + employee_polices.id as emp_policy_id, + employee_polices.client_policy_id, + ') + ->join('employees', 'employees.id = employee_polices.employee_id') + ->where('employee_polices.is_active', 1) + ->where('employee_polices.status', 'active') + ->where('employees.is_active', 1) + ->where('employees.emp_status', 'active') + ->where('employee_polices.tpa_id IS NULL') + ->where('employee_polices.client_policy_id', $client_policy_id) + ->findAll(); + + if (empty($employeePolicyData)) { + log_message('error', 'GetBenefDetails: employeePolicyData is empty'); + if($function_calling_type == "job"){ + return ['status' => false, 'message' => 'employeePolicyData not found']; + }else{ + return $this->respond(['status' => false, 'message' => 'employeePolicyData not found']); + } + } $allBenef = []; $startIndex = 0; $range = 100; $totalCount = 0; - do { $body = [ "policyNo" => $policyNo, @@ -246,6 +282,7 @@ class MediAssistApiController extends BaseController ]; log_message('error', "GetBenefDetails API Request (startIndex={$startIndex}): " . json_encode($body)); + log_message('error', "GetBenefDetails API parems " . json_encode([$url, $method, $headers, $body])); $response = call_third_party_api($url, $method, $headers, $body); @@ -291,35 +328,56 @@ class MediAssistApiController extends BaseController $db = \Config\Database::connect(); $updated = 0; + foreach ($employeePolicyData as $policy_data) { + foreach ($allBenef as $row) { - foreach ($allBenef as $row) { + // log_message( + // "error", + // "POLICY MATCH CHECK: " . json_encode([ + // 'policy_data' => [ + // 'name' => $policy_data['name'] ?? null, + // 'emp_code' => $policy_data['emp_code'] ?? null, + // 'relationship' => $policy_data['relationship'] ?? null, + // 'gender' => $policy_data['gender'] ?? null, + // 'dob' => $policy_data['dob'] ?? null, + // ], + // 'row_data' => [ + // 'benefName' => $row['benefName'] ?? null, + // 'priBenefEmpCode' => $row['priBenefEmpCode'] ?? null, + // 'relName' => $row['relName'] ?? null, + // 'benefSex' => $row['benefSex'] ?? null, + // 'benefDOB' => $row['benefDOB'] ?? null, + // 'benefDOB_fmt' => change_date_format($row['benefDOB'],'d/m/Y H:i:s') ?? null, + // ], + // ]) + // ); - // $update = $db->table('employee_polices') - // ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id AND client_policy.policy_no = ' . $db->escape($row['polNo']), 'left') - // ->join( - // 'employees', - // 'employee_polices.employee_id = employees.id - // AND employees.emp_code = ' . $db->escape($row['priBenefEmpCode']) . ' - // AND employees.relationship = ' . $db->escape($row['relName']), - // 'left' - // ) - // ->where('employee_polices.tpa_id IS NULL') - // ->update(['employee_polices.tpa_id' => $row['benefMediAssistID']]); + if ( + strtolower(trim($policy_data['name'] ?? '')) == strtolower(trim($row['benefName'] ?? '')) && + ($policy_data['emp_code'] ?? '') == ($row['priBenefEmpCode'] ?? '') && + strtolower(trim($policy_data['relationship'] ?? '')) == strtolower(trim($row['relName'] ?? '')) && + ($policy_data['gender'] ?? '') == ($row['benefSex'] ?? '') && + ($policy_data['dob'] ?? '') == (change_date_format($row['benefDOB'], 'd/m/Y H:i:s') ?? '') + ) { + + log_message('error', "✅ Match found: emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); - $update = $db->table('employee_polices') - ->join('client_policy', 'employee_polices.client_policy_id = client_policy.id', 'left') - ->join('employees', 'employee_polices.employee_id = employees.id', 'left') - ->where('client_policy.policy_no', $row['polNo']) - ->where('employees.emp_code', $row['priBenefEmpCode']) - ->where('employees.relationship', $row['relName']) - ->where('employee_polices.tpa_id IS NULL', null, false) - ->update(['employee_polices.tpa_id' => $row['benefMediAssistID']]); + $sql = "UPDATE employee_polices + SET tpa_id = ? + WHERE id = ?"; + $db->query($sql, [$row['benefMediAssistID'], $policy_data['emp_policy_id']]); + + if ($db->affectedRows() > 0) { + $updated++; + log_message('error', "✅ Updated tpa_id={$row['benefMediAssistID']} for emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); + } else { + log_message('error', "⚠️ No update (already set or not matched) for emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); + } - - if ($db->affectedRows() > 0) { - $updated++; - log_message('error', "Updated tpa_id for emp_code={$row['priBenefEmpCode']}, policy={$row['polNo']}"); + }else{ + log_message('error', "❌ Not matched: emp_code={$row['priBenefEmpCode']} policy={$row['polNo']}"); + } } } diff --git a/app/Helpers/utility_helper.php b/app/Helpers/utility_helper.php index dd4de7df..9095986c 100755 --- a/app/Helpers/utility_helper.php +++ b/app/Helpers/utility_helper.php @@ -647,7 +647,9 @@ if (!function_exists('change_date_format')) { if ($source_format !== null && $output_format !== null) { $date = DateTime::createFromFormat($source_format, $date_str); if (!$date) { - throw new Exception("Invalid date string for source format: $source_format"); + // throw new Exception("Invalid date string for source format: $source_format"); + log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + return null; } return $date->format($output_format); } @@ -656,7 +658,9 @@ if (!function_exists('change_date_format')) { if ($source_format !== null && $output_format === null) { $date = DateTime::createFromFormat($source_format, $date_str); if (!$date) { - throw new Exception("Invalid date string for source format: $source_format"); + // throw new Exception("Invalid date string for source format: $source_format"); + log_message('error', "❌ Date format error : Invalid date string | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + return null; } return $date->format('Y-m-d'); // MySQL default format } @@ -671,10 +675,17 @@ if (!function_exists('change_date_format')) { } // If no format matches, throw an exception $allowed_placeholders = implode(', ', $allowed_formats); - throw new Exception("Invalid date string format. Allowed formats: $allowed_placeholders"); + // throw new Exception("Invalid date string format. Allowed formats: $allowed_placeholders"); + log_message( + 'error', + "❌ Date format error: Invalid date string. Allowed formats: {$allowed_placeholders} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}" + ); + return null; } } catch (Exception $e) { - return "Error: " . $e->getMessage(); + // return "Error: " . $e->getMessage(); + log_message('error', "❌ Date format error: {$e->getMessage()} | Params => date_str: {$date_str}, source_format: {$source_format}, output_format: {$output_format}"); + return null; } return null;