nhance/app/Controllers/ICICILombardController.php
2025-09-24 12:25:15 +05:30

267 lines
8.4 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use Kint;
use Ramsey\Uuid\Uuid;
class ICICILombardController extends AdminController
{
public function generateAuthToken($scope = 'esbhealth')
{
helper('api');
$url = 'https://ilesbapigee.insurancearticlez.com/generate-jwt-token';
$method = 'POST';
$headers = [
'Content-Type: application/x-www-form-urlencoded'
];
$body = [
'grant_type' => 'password',
'username' => 'Nhanceins',
'password' => 'SSWW7bFNaLxxQyw',
'scope' => $scope,
'client_id' => 'Nhanceins',
'client_secret' => 'P4W0G6TAYMa0MV8bbVSx4pTAqbCcUIg48kCWa6PJykAhGWzPmpPr0iLuNWtz5wqN'
];
$response = call_third_party_api($url, $method, $headers, $body);
if($response['status'] != true){
return $this->response->setJSON([
'status' => false,
'message' => 'Token generation failed.',
'data' => $response
]);
}
return $response;
}
public function createEnrollmentBatch()
{
$client_id = $this->request->getGet('client_id');
$client_branch_id = $this->request->getGet('client_branch_id');
$policy_id = $this->request->getGet('policy_id');
$event = $this->request->getGet('event');
//fetch token
$tokenResponse = $this->generateAuthToken();
if (!$tokenResponse || empty($tokenResponse['data']['access_token'])) {
return $this->response->setJSON([
'status' => false,
'message' => 'Token generation failed.',
'data' => $tokenResponse
]);
}
$token = $tokenResponse['data']['access_token'];
$url = 'https://ilesbapigee.insurancearticlez.com/health/ilservices/health/v1/enrollment/batchcreation';
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
];
//Prepare body data
$db = \Config\Database::connect();
$data = $db->table('employee_polices ep')
->select('cp.policy_no as policyNumber,
e.id,e.emp_code as MemberEmpId, e.doj as DOJ, e.name as InsuredName, e.dob as DOB, e.relationship as Relationship,
e.gender as Gender, ep.date_coverage as DOC,ep.basic_cover_si as SumInsured, e.email_corporate as EmailId
')
->join('employees e', 'e.id = ep.employee_id')
->join('client_policy cp', 'ep.client_policy_id = cp.id')
->where('ep.client_policy_id', $policy_id)
->where('ep.status', 'active')
->where('ep.is_active', 1)
// ->where('ep.uhid', null)
->get()
->getResultArray();
$body = $this->formatPolicyData($data);
dd($body);
$response = call_third_party_api($url, 'POST', $headers, $body, true); // true = raw body mode
// print_rr(json_encode($response));die();
return $this->response->setJSON($response);
}
public function getEnrollmentBatchStatus()
{
helper('api');
//fetch token
$tokenResponse = $this->generateAuthToken();
if (!$tokenResponse || empty($tokenResponse['data']['access_token'])) {
return $this->response->setJSON([
'status' => false,
'message' => 'Token generation failed.',
'data' => $tokenResponse
]);
}
$token = $tokenResponse['data']['access_token'];
$url = 'https://ilesbapigee.insurancearticlez.com/health/ilservices/health/v1/enrollment/batchstatus';
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
];
// dd($headers);
$body = [
"PolicyNumber" => "4016/A/O/53077718/00/000",
"BatchId" => "3646145",
"CorrelationId" => "550e8400-e29b-41d4-a716-446655440001"
];
$response = call_third_party_api($url, 'POST', $headers, $body, true);
return $this->response->setJSON($response);
}
public function fetchUHIDDetails()
{
helper('api');
//fetch token
$tokenResponse = $this->generateAuthToken('esbgpauhid');
if (!$tokenResponse || empty($tokenResponse['data']['access_token'])) {
return $this->response->setJSON([
'status' => false,
'message' => 'Token generation failed.',
'data' => $tokenResponse
]);
}
$token = $tokenResponse['data']['access_token'];
// print_rr($token);
$url = 'https://ilesbapigee.insurancearticlez.com/health/ilservices/health/v1/enrollment/fetchuhid';
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
];
$body = [
"PolicyNumber" => "4016/A/51974976/00/000",
"IMID" => "2345617878",
"CorrelationId" => "86383c78-4c67-4e4d-9aa0-9f926fb0d55e"
];
$response = call_third_party_api($url, 'POST', $headers, $body, true);
return $this->response->setJSON($response);
}
public function formatPolicyData($data)
{
// Helper to format date
$formatDate = function ($date) {
return strtoupper(date('j-M-Y', strtotime($date))); // Example: 7-JUL-1983
};
// Generate UUID v4 for CorrelationId
$generateUUID = function () {
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
};
// Map MemberDetails
$memberDetails = array_map(function ($row) use ($formatDate) {
return [
"MemberEmpId" => $row['MemberEmpId'],
"DOJ" => $formatDate($row['DOJ']),
"InsuredName" => $row['InsuredName'],
"DOB" => $formatDate($row['DOB']),
"Relationship" => strtoupper($row['Relationship']),
"Gender" => strtoupper($row['Gender']),
"DOC" => $formatDate($row['DOC']),
"SumInsured" => $row['SumInsured'],
"EmailId" => $row['EmailId'],
"FlagStatus" => "A" // fixed value
];
}, $data);
// Final body
return [
"PolicyNumber" => $data[0]['policyNumber'] ?? null,
"CDBGAccountNumber" => "CD-MUM-0026",
"CorrelationId" => $generateUUID(),
"MemberDetails" => $memberDetails
];
}
// $body = [
// "PolicyNumber" => "4016/A/O/53077718/00/000",
// "CDBGAccountNumber" => "CD-MUM-0026",
// "CorrelationId" => "550e8400-e29b-41d4-a716-446655440001",
// "MemberDetails" => [
// [
// "MemberEmpId" => "EMPID3625557",
// "DOJ" => "21-MAR-2019",
// "InsuredName" => "Kumar",
// "DOB" => "7-JUL-1983",
// "Relationship" => "SELF",
// "Gender" => "MALE",
// "DOC" => "08-JUL-2025",
// "SumInsured" => "400000",
// "EmailId" => "KUMAR@GMAIL.COM",
// "FlagStatus" => "A"
// ],
// [
// "MemberEmpId" => "EMPID3625557",
// "DOJ" => "21-MAR-2019",
// "InsuredName" => "Saranya",
// "DOB" => "8-AUG-1970",
// "Relationship" => "MOTHER",
// "Gender" => "FEMALE",
// "DOC" => "08-JUL-2025",
// "SumInsured" => "400000",
// "EmailId" => "Saranya@GMAIL.COM",
// "FlagStatus" => "A"
// ],
// ]
// ];
}