'; // Set version to 0100 $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // Set bits 6-7 to 10 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // Output the 36 character UUID. $uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); return $uuid; } } if (!function_exists('change_date_format2')) { function change_date_format2($data, $source_format, $output_format) { $data = trim($data); // echo $data, $source_format, $output_format;return true; try { // Create DateTime object with the source format $dateTime = DateTime::createFromFormat($source_format, $data); // Check if the DateTime object is created successfully if ($dateTime === false) { $errors = DateTime::getLastErrors(); throw new Exception('Invalid date or format' . implode(', ', $errors['errors'])); } // Format the DateTime object with the output format $formattedDate = $dateTime->format($output_format); return $formattedDate; } catch (Exception $e) { // Handle the exception (e.g., log it, show a user-friendly message) return "Error: " . $e->getMessage(); return $data; } } } if (!function_exists('file_Upload')) { function file_Upload($fileToUpload, $filepath) { if ($fileToUpload !== null && $fileToUpload->isValid() && !$fileToUpload->hasMoved()) { $fileToUpload->move($filepath); $fileName = $fileToUpload->getName(); return $fileName; } else { return ""; } } } if (!function_exists('file_unlink')) { function file_unlink($filepath) { if (is_file($filepath) && file_exists($filepath)) { unlink($filepath); } } } if (!function_exists('compressImage')) { function compressImage($file, $destinationPath, $newWidth = 100, $newHeight = 100) { // Load the image manipulation library $image = \Config\Services::image(); // Resize and compress the image $image->withFile($file) ->fit($newWidth, $newHeight, 'center') ->save($destinationPath); return true; } } if (!function_exists('fancy_date_time_format')) { function fancy_date_time_format($datetime,$return_type = 'fancy') { date_default_timezone_set('Asia/Kolkata'); $currentDateTime = new DateTime(); $passedDateTime = new DateTime($datetime); // Calculate the interval between the current time and the passed datetime $interval = $currentDateTime->diff($passedDateTime); // If the interval is more than 1 month or the year is different, return the original datetime if ($interval->m > 1 || $interval->y != 0) { if ($return_type == 'fancy') { return change_date_format($datetime, 'Y-m-d H:i:s', 'd M Y h:i a'); } return $datetime; } elseif ($interval->m == 1 && $interval->y == 0) { return "1 month ago"; } elseif ($interval->d >= 7) { $weeks = floor($interval->d / 7); return $weeks == 1 ? "1 week ago" : "$weeks weeks ago"; } elseif ($interval->d >= 1) { return $interval->d == 1 ? "1 day ago" : $interval->d . " days ago"; } elseif ($interval->h >= 1) { return $interval->h == 1 ? "1 hour ago" : $interval->h . " hours ago"; } elseif ($interval->i >= 1) { return $interval->i == 1 ? "1 minute ago" : $interval->i . " minutes ago"; } else { return "Just now"; } } } if(!function_exists('check_string_date')){ function check_string_date($str) { if (DateTime::createFromFormat('Y-m-d H:i:s', $str) !== false) { return true; } return false; } } if (!function_exists('generate_download_link')) { function generate_download_link($rand_string) { // Generate the link using provided emp_code and client_policy_id $link = htmlspecialchars(base_url('download-e-card/' . $rand_string)); // Return the link wrapped in anchor tag // return 'Click To '; return $link; } } if (!function_exists('generate_random_alphanumeric')) { function generate_random_alphanumeric($length = 6) { $random_string = ''; for ($i = 0; $i < $length; $i++) { $random_ascii = rand(0, 61); if ($random_ascii < 10) { $random_character = chr($random_ascii + 48); } elseif ($random_ascii < 36) { $random_character = chr($random_ascii + 55); } else { $random_character = chr($random_ascii + 61); } $random_string .= $random_character; } return $random_string; } } if (!function_exists('get_base64_image')) { function get_base64_image($path) { // Get file extension $type = pathinfo($path, PATHINFO_EXTENSION); // Read file content $dataContent = file_get_contents($path); // Encode as base64 $base64Image = 'data:image/' . $type . ';base64,' . base64_encode($dataContent); return $base64Image; } } if (!function_exists('format_indian_number')) { function format_indian_number($number) { // Round the number to two decimal places $number = isset($number) ? $number : 0; $number = round($number, 2); // Split the number into integer and decimal parts $numberParts = explode('.', number_format($number, 2, '.', '')); $integerPart = $numberParts[0]; $decimalPart = isset($numberParts[1]) ? $numberParts[1] : '00'; // Format the integer part with commas $length = strlen($integerPart); $formattedStr = ''; $counter = 0; for ($i = $length - 1; $i >= 0; $i--) { $formattedStr = $integerPart[$i] . $formattedStr; $counter++; if ($counter == 3 && $i != 0) { $formattedStr = ',' . $formattedStr; $counter = 0; } elseif ($counter == 2 && $i != 0 && $length - $i > 3) { $formattedStr = ',' . $formattedStr; $counter = 0; } } // Combine the integer and decimal parts return $formattedStr . '.' . $decimalPart; } } if (!function_exists('get_username')) { function get_username($user_id) { // Connect to the database $db = \Config\Database::connect(); // Query the database $query = $db->table('user_profiles') ->select('first_name') ->where('id', $user_id) ->get(); // Get the result $result = $query->getRow(); // Return the username if found, otherwise return null return $result ? $result->first_name : null; } } if (!function_exists('get_role_id')) { function get_role_id() { $role_id = isset(get_session_userdata()->role) ? get_session_userdata()->role : null; return $role_id; } } if (!function_exists('teams')) { function teams() { // Load the UserTeamsModel $teamModel = new \App\Models\UserTeamsModel(); // Get the user ID from the session $user_id = get_session_userid(); // Fetch the team IDs associated with the user $user_teams = $teamModel->select('team_id')->where('user_id', $user_id)->findAll(); // Debugging: Log or print the query result to check its structure // var_dump($user_teams); // You can use this temporarily for testing // log_message('info', 'User Teams: ' . json_encode($user_teams)); // Optionally log it // Check if the result is not empty if (!empty($user_teams)) { // Extract only the 'team_id' values $team_ids = array_column($user_teams, 'team_id'); return $team_ids; // Return array of team IDs } else { return []; // Return empty array if no teams found } } } if (!function_exists('generate_client_code')) { function generate_client_code($string = 'GC') { $clientModel = new \App\Models\ClientModel(); $latestClient = $clientModel->select('id')->orderBy('id', 'DESC')->first(); $id = $latestClient ? $latestClient['id'] : 0; $year = date('y'); $newId = $id + 1; $client_code = $year . $string . $newId; return $client_code; } } if (!function_exists('generate_tsi_code')) { function generate_tsi_code($type) { $PolicyTransactionModel = new \App\Models\PolicyTransactionModel(); $latestClient = $PolicyTransactionModel->select('id')->orderBy('id', 'DESC')->first(); $id = $latestClient ? $latestClient['id'] : 0; $year = date('y'); $month = date('m'); $string = 'P'; if($type == 2){ $string2 = 'R'; }else{ $string2 = 'F'; } $newId = $id + 1; $tsi_code = $string2 . $month . $year . $string . $newId; return $tsi_code; } } if (!function_exists('generateRandomCode')) { function generateRandomCode($prefix = 'RTL-', $length = 6) { // Generate a random number with the specified length $randomNumber = str_pad(mt_rand(0, pow(10, $length)-1), $length, '0', STR_PAD_LEFT); // Return the code with the prefix return $prefix . $randomNumber; } } if (!function_exists('excelFileGDriveUpload')) { function excelFileGDriveUpload($file_id, $table_name) { $doc_type = "UPLOADS"; $uploadFilePath = WRITEPATH . 'uploads/' . ($table_name == 'batch_file' ? 'import_excel' : 'excel'); $models = [ 'batch_file' => [ 'model' => new BatchFileModel(), 'select' => "client_id, client_policy_id, file_name" ], 'files' => [ 'model' => new FileModel(), 'select' => "client_id, policy_id as client_policy_id, file_name" ], ]; if (!array_key_exists($table_name, $models)) { return; } // Retrieve data $data = $models[$table_name]['model'] ->select($models[$table_name]['select']) ->where('id', $file_id) ->where('is_active', 1) ->first(); if ($data) { $uploadFilePath .= '/' . $data['file_name']; // dd($data, $uploadFilePath); $GoogleDriveController = new GoogleDriveController(); $result = $GoogleDriveController->uploadFiletoGdrive( // client_id : $data['client_id'], client_policy_id: $data['client_policy_id'], doc_type : $doc_type, file_path : $uploadFilePath, file_name : $data['file_name'] ); // dd($result); return true; }else{ return false; } } } if(!function_exists('checkFamilyFloaters')){ function checkFamilyFloaters($policy_premium_data, $client_policy_data, $emp_data){ if($policy_premium_data['premium_type'] == 1){ //only family floater if ($emp_data['relationship'] == 'Self') { return true; } if($client_policy_data['policy_type_id'] == 3 && $client_policy_data['is_addon'] == 3){ if($emp_data['rata_premimum'] > 0){ return true; }else{ return false; } } return false; }else{ //individual return true; } } } if (!function_exists('numberToWords')) { function numberToWords($number) { $words = array( '0' => 'Zero', '1' => 'One', '2' => 'Two', '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six', '7' => 'Seven', '8' => 'Eight', '9' => 'Nine', '10' => 'Ten', '11' => 'Eleven', '12' => 'Twelve', '13' => 'Thirteen', '14' => 'Fourteen', '15' => 'Fifteen', '16' => 'Sixteen', '17' => 'Seventeen', '18' => 'Eighteen', '19' => 'Nineteen', '20' => 'Twenty', '30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty', '60' => 'Sixty', '70' => 'Seventy', '80' => 'Eighty', '90' => 'Ninety' ); if ($number < 21) { return $words[$number]; } if ($number < 100) { $tens = (int)($number / 10) * 10; $units = $number % 10; return $words[$tens] . ($units ? ' ' . $words[$units] : ''); } if ($number < 1000) { $hundreds = (int)($number / 100); $remainder = $number % 100; return $words[$hundreds] . ' Hundred' . ($remainder ? ' and ' . numberToWords($remainder) : ''); } $levels = array('', ' Thousand', ' Million', ' Billion', ' Trillion', ' Quadrillion', ' Quintillion'); for ($i = 0, $unit = 1; $i < count($levels); $i++, $unit *= 1000) { if ($number < $unit * 1000) { $current = (int)($number / $unit); $remainder = $number % $unit; return numberToWords($current) . $levels[$i] . ($remainder ? ' ' . numberToWords($remainder) : ''); } } return $number; // Fallback for numbers beyond the supported range } } if (!function_exists('print_rr')) { function print_rr($data) { echo "
";
print_r($data);
echo "";
}
}
if (!function_exists('get_server_details')) {
/**
* Get the hostname and server name.
*
* @return array
*/
function get_server_details(): array
{
$hostname = gethostname(); // Get the hostname of the server
$serverName = $_SERVER['SERVER_NAME'] ?? 'Unknown'; // Get the server name
return [
'hostname' => $hostname,
'server_name' => $serverName,
];
}
}
if (!function_exists('isJsonString')) {
function isJsonString($input)
{
json_decode($input); // Decode the string
return (json_last_error() === JSON_ERROR_NONE); // Check if the last JSON error is "no error"
}
}
if (!function_exists('isValidDate')) {
function isValidDate($date, $format) {
$parsed_date = DateTime::createFromFormat($format, $date);
return $parsed_date && $parsed_date->format($format) === $date;
}
}
if (!function_exists('change_date_format')) {
function change_date_format($date_str, $source_format = null, $output_format = 'Y-m-d') {
$date_str = trim($date_str);
// Allowed date formats
$allowed_formats = [
// Day Month Year (clear unambiguous formats)
'd M Y', // 01 Dec 2024
'd-M-Y', // 01-Dec-2024
'd/M/Y', // 01/Dec/2024
'd.M.Y', // 01.Dec.2024
'd,M,Y', // 01,Dec,2024
// Month Day Year (clear unambiguous formats)
'M d Y', // Dec 01 2024
'M-d-Y', // Dec-01-2024
'M/d/Y', // Dec/01/2024
'M.d.Y', // Dec.01.2024
'M,d,Y', // Dec,01,2024
// Year Month Day (clear unambiguous formats)
'Y M d', // 2024 Dec 01
'Y-M-d', // 2024-Dec-01
'Y/M/d', // 2024/Dec/01
'Y.M.d', // 2024.Dec.01
'Y,M,d', // 2024,Dec,01
// Year Numeric Month Numeric Day
'Y-m-d', // 2024-12-01
'Y/m/d', // 2024/12/01
'Y.m.d', // 2024.12.01
'Y,m,d', // 2024,12,01
'd/m/Y', // 01/01/2025
'd-m-Y', // 01-01-2025
];
try {
// Case 1: Source and Output formats are provided
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");
}
return $date->format($output_format);
}
// Case 2: Source format is provided, Output format is null
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");
}
return $date->format('Y-m-d'); // MySQL default format
}
// Case 3: Source format is null, auto-detect format
if ($source_format === null) {
foreach ($allowed_formats as $format) {
if (isValidDate($date_str, $format)) {
$date = DateTime::createFromFormat($format, $date_str);
return $date->format($output_format); // Default is MySQL format
}
}
// If no format matches, throw an exception
$allowed_placeholders = implode(', ', $allowed_formats);
throw new Exception("Invalid date string format. Allowed formats: $allowed_placeholders");
}
} catch (Exception $e) {
return "Error: " . $e->getMessage();
}
return null;
}
}
// if (!function_exists('check_pay_by_employee_or_company'))
// {
// function check_pay_by_employee_or_company($is_payable_employee = null, $relationship = null) {
// $relationship = strtolower(str_replace(" ", "_", $relationship));
// $result = 0;
// if($relationship == 'self'){
// $result = $is_payable_employee['self'] == 1 ? 1 : 0;
// }else if($relationship == 'spouse'){
// $result = $is_payable_employee['spouse'] == 1 ? 1 : 0;
// }else if(in_array($relationship, ['son', 'daughter'])){
// $result = $is_payable_employee['childern'] == 1 ? 1 : 0;
// }else if(in_array($relationship, ['father', 'mother', 'father_in_law', 'mother_in_law'])){
// $result = $is_payable_employee['elders'] == 1 ? 1 : 0;
// }
// return $result;
// }
// }
if (!function_exists('check_pay_by_employee_or_company')) {
function check_pay_by_employee_or_company($policy_terms = null, $relationship = null)
{
// dd($policy_terms, $relationship);
if (!$policy_terms || !($policy_terms = json_decode($policy_terms, true))) {
return 0;
}
// dd($policy_terms, $relationship);
if (!isset($policy_terms['is_payable_employee'])) {
return 0;
}
$is_payable_employee = $policy_terms['is_payable_employee'];
$relationship = strtolower(str_replace(" ", "_", $relationship));
// dd($is_payable_employee, $relationship);
$relationshipMap = [
'self' => 'self',
'spouse' => 'spouse',
'son' => 'childern',
'daughter' => 'childern',
'father' => 'elders',
'mother' => 'elders',
'father_in_law' => 'elders',
'mother_in_law' => 'elders'
];
if (array_key_exists($relationship, $relationshipMap)) {
$key = $relationshipMap[$relationship];
return isset($is_payable_employee[$key]) && $is_payable_employee[$key] == 1 ? 1 : 0;
}
return 0;
}
}
if (!function_exists('is_json_string')) {
function is_json_string($string)
{
if (!is_string($string)) {
return false;
}
$decoded = json_decode($string, true);
return (json_last_error() === JSON_ERROR_NONE && is_array($decoded));
}
}
if (!function_exists('check_cd_entry_exist')) {
function check_cd_entry_exist($params)
{
$db = db_connect();
$client_id = $params['client_id'];
$client_policy_id = $params['client_policy_id'];
$insurer_id = $params['insurer_id'];
$cd_ac_pk = $params['cd_ac_pk'];
$event_name = $params['event_name'];
// Check if truncated entry (sub_type = 8) exists
$has_truncated = $db->table('cash_deposit')
->where('client_id', $client_id)
->where('insurer_id', $insurer_id)
->where('cd_ac_pk', $cd_ac_pk)
->where('client_policy_id', $client_policy_id)
->where('event_name', $event_name)
->where('sub_type', 8)
->where('is_active', 1)
->countAllResults();
if ($has_truncated) {
echo "has_truncated";
// Get all entries with same details (including truncated)
$entries = $db->table('cash_deposit')
->where('client_id', $client_id)
->where('insurer_id', $insurer_id)
->where('cd_ac_pk', $cd_ac_pk)
->where('client_policy_id', $client_policy_id)
->where('event_name', $event_name)
->where('is_active', 1)
->where('sub_type !=', 8)
->get()
->getResultArray();
if (count($entries) > 1) {
// Only one entry found (truncated)
return true;
}else{
return false;
}
} else {
// Check if any other entry exists
$entry = $db->table('cash_deposit')
->where('client_id', $client_id)
->where('insurer_id', $insurer_id)
->where('cd_ac_pk', $cd_ac_pk)
->where('client_policy_id', $client_policy_id)
->where('event_name', $event_name)
->where('is_active', 1)
->get()
->getRowArray();
if (!empty($entry)) {
return true;
}
}
return false;
}
}
if (!function_exists('expected_amount_calc')) {
function expected_amount_calc($data, $index)
{
$agreed_amount = (float) ($data['agreed_amount'][$index] ?? 0);
$agreed_bp_per = (float) ($data['agreed_bp'][$index] ?? 0);
$agreed_tp_per = (float) ($data['agreed_tp'][$index] ?? 0);
$agreed_tep_per = (float) ($data['agreed_ter'][$index] ?? 0);
$standard_bp_per = (float) ($data['standard_bp'][$index] ?? 0);
$standard_tp_per = (float) ($data['standard_tp'][$index] ?? 0);
$standard_tep_per = (float) ($data['standard_ter'][$index] ?? 0);
if($data['bro_payable_by'] == 0){
$base_premium = (float) ($data['co_premium'][$index] ?? 0);
$third_part_premium = (float) ($data['co_tp_premium'][$index] ?? 0);
$terrisom_premium = (float) ($data['co_ter_premium'][$index] ?? 0);
}else{
$base_premium = (float) ($data['base_premium'][$index] ?? 0);
$third_part_premium = (float) ($data['tp_premium'][$index] ?? 0);
$terrisom_premium = (float) ($data['ter_premium'][$index] ?? 0);
}
$sum_of_agreed_premium = $agreed_bp_per + $agreed_tp_per + $agreed_tep_per;
$expectedAmount = 0;
if ($agreed_amount > 0) {
$expectedAmount = $agreed_amount;
} elseif ($sum_of_agreed_premium > 0) {
$expectedAmount =
($base_premium * $agreed_bp_per / 100) +
($third_part_premium * $agreed_tp_per / 100) +
($terrisom_premium * $agreed_tep_per / 100);
} else {
$expectedAmount =
($base_premium * $standard_bp_per / 100) +
($third_part_premium * $standard_tp_per / 100) +
($terrisom_premium * $standard_tep_per / 100);
}
return round($expectedAmount, 2); // Optional: round to 2 decimal places
}
}