491 lines
18 KiB
PHP
491 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class TicketMasterModel extends Model
|
|
{
|
|
protected $table = 'ticket_master';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'id',
|
|
'ticket_type_id',
|
|
'claim_status_id',
|
|
'acm_id',
|
|
'insurer_id',
|
|
'tpa_id',
|
|
'client_id',
|
|
'priority',
|
|
'policy_no',
|
|
'emp_code',
|
|
'tpa_no',
|
|
'emp_name',
|
|
'insured_name',
|
|
'relationship',
|
|
'emp_mobile',
|
|
'emp_mail',
|
|
'mode_of_intimation',
|
|
'claim_type',
|
|
'hospital_name',
|
|
'doa',
|
|
'dod',
|
|
'claim_amount',
|
|
'pod_no',
|
|
'created_by',
|
|
'updated_by',
|
|
'created_at',
|
|
'updated_at',
|
|
'date_of_join',
|
|
'date_of_incep',
|
|
'dob',
|
|
'date_of_accident',
|
|
'date_of_death',
|
|
'date_of_intimat',
|
|
'si_amt',
|
|
'is_active',
|
|
'claim_number',
|
|
'emp_id',
|
|
'insured_emp_id',
|
|
];
|
|
|
|
|
|
// Callbacks
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = ["checkAndADDCreatedByValue"];
|
|
protected $afterInsert = [];
|
|
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
|
|
protected $afterUpdate = [];
|
|
protected $beforeFind = [];
|
|
protected $afterFind = [];
|
|
protected $beforeDelete = [];
|
|
protected $afterDelete = [];
|
|
|
|
protected function checkAndADDCreatedByValue(array $data)
|
|
{
|
|
// Check if 'updated_by' value is null or empty
|
|
if (empty($data['data']['created_by'])) {
|
|
// Set 'updated_by' value to the current session user ID
|
|
$data['data']['created_by'] = get_session_userid();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function checkAndUpdateUpdatedByValue(array $data)
|
|
{
|
|
// Check if 'updated_by' value is null or empty
|
|
if (empty($data['data']['updated_by'])) {
|
|
// Set 'updated_by' value to the current session user ID
|
|
$data['data']['updated_by'] = get_session_userid();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getTemplateDataByStatusID($claim_status_id)
|
|
{
|
|
|
|
$template_data = $this->table('ticket_claim_status')
|
|
->select('ticket_mail_template.*')
|
|
->join('ticket_mail_template', 'ticket_mail_template.trigger_type = ticket_claim_status.trigger_type')
|
|
->where('ticket_mail_template.is_active', 1)
|
|
->where('ticket_claim_status.is_active', 1)
|
|
->where('ticket_claim_status.id', $claim_status_id)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
return $template_data;
|
|
}
|
|
|
|
public function getTemplateDataByTicketID($ticket_id)
|
|
{
|
|
$template_data = $this
|
|
->select("ticket_mail_template.*, ticket_master.emp_mail,ticket_master.claim_status_id")
|
|
->join('ticket_claim_status', 'ticket_master.claim_status_id = ticket_claim_status.id and ticket_claim_status.is_active = 1')
|
|
->join('ticket_mail_template', '
|
|
ticket_claim_status.trigger_type = ticket_mail_template.trigger_type
|
|
and ticket_claim_status.ticket_type = ticket_mail_template.ticket_type
|
|
and ticket_mail_template.is_active = 1')
|
|
->where('ticket_master.id', $ticket_id)
|
|
->where('ticket_master.is_active', 1)
|
|
->first();
|
|
|
|
return $template_data;
|
|
}
|
|
|
|
public function getTicketDataByTicketID($ticket_id)
|
|
{
|
|
$ticket_data = $this->select("
|
|
ticket_master.*,
|
|
|
|
user_profiles.first_name as acm,
|
|
user_profiles.mobile as acm_mobile,
|
|
user_profiles.email as acm_email,
|
|
|
|
clients.common_mails,
|
|
clients.client_name,
|
|
|
|
insurers.name as insurer_name,
|
|
tpa.name as tpa_name,
|
|
ticket_notes.note as auto_query_content
|
|
")
|
|
->join('clients', 'ticket_master.client_id = clients.id', 'left')
|
|
->join('insurers', 'ticket_master.insurer_id = insurers.id', 'left')
|
|
->join('tpa', 'ticket_master.tpa_id = tpa.id', 'left')
|
|
->join('ticket_notes', 'ticket_master.id = ticket_notes.ticket_id and ticket_notes.is_active = 1 and ticket_notes.is_auto_query = 1','left')
|
|
->join('user_profiles', 'ticket_master.acm_id = user_profiles.id', 'left')
|
|
->where('ticket_master.id', $ticket_id)
|
|
->where('ticket_master.is_active', 1)
|
|
->first();
|
|
|
|
return $ticket_data;
|
|
}
|
|
|
|
//get TAT report BAND wise Data
|
|
public function getTATReport($ticket_type = null, $start_date = null, $end_date = null)
|
|
{
|
|
//set default last 3 months data date
|
|
$fromDate = date('Y-m-d', strtotime('-90 days'));
|
|
$toDate = date('Y-m-d 23:59:59');
|
|
|
|
if (!empty($start_date) && !empty($end_date)) {
|
|
$fromDate = change_date_format($start_date);
|
|
$toDate = change_date_format($end_date);
|
|
}
|
|
$ticket_type_data_1 = "";
|
|
$ticket_type_data_2 = "";
|
|
if(!empty($ticket_type)){
|
|
$ticket_type_data_1 = "AND ticket_type = $ticket_type";
|
|
$ticket_type_data_2 = "AND tm.ticket_type_id = $ticket_type";
|
|
}
|
|
|
|
// Fetch claim statuses from the `ticket_claim_status` table
|
|
$statusQuery = " SELECT
|
|
id, claim_status, ticket_type
|
|
FROM ticket_claim_status
|
|
Where is_active = 1
|
|
$ticket_type_data_1
|
|
";
|
|
$statusResult = $this->db->query($statusQuery)->getResultArray();
|
|
// dd($statusResult);
|
|
|
|
// Initialize dynamic query parts
|
|
$dynamicSelect = '';
|
|
|
|
// Loop through each claim status and generate the COALESCE and CASE statements for the SELECT
|
|
foreach ($statusResult as $status) {
|
|
|
|
$columnName = $status['claim_status']; // Default column name
|
|
|
|
if (empty($ticket_type)) {
|
|
// Append the insurance type prefix based on `ticket_type`
|
|
switch ($status['ticket_type']) {
|
|
case 1:
|
|
$columnName = "GMC - {$status['claim_status']}";
|
|
break;
|
|
case 2:
|
|
$columnName = "GPA - {$status['claim_status']}";
|
|
break;
|
|
case 3:
|
|
$columnName = "EDLI - {$status['claim_status']}";
|
|
break;
|
|
case 4:
|
|
$columnName = "GTLI - {$status['claim_status']}";
|
|
break;
|
|
}
|
|
}
|
|
|
|
$dynamicSelect .= "
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN subquery.status_id = {$status['id']} THEN 1
|
|
ELSE 0
|
|
END
|
|
),
|
|
0
|
|
) AS `$columnName`, ";
|
|
}
|
|
|
|
// Remove the trailing comma from the SELECT part
|
|
$dynamicSelect = rtrim($dynamicSelect, ', ');
|
|
// dd($dynamicSelect);
|
|
|
|
|
|
// Construct the full SQL query
|
|
$sql = "
|
|
SELECT
|
|
tc.TAT_Category,
|
|
$dynamicSelect
|
|
FROM
|
|
(
|
|
SELECT 'Above 20 Days' AS TAT_Category
|
|
UNION ALL
|
|
SELECT '13-20 Days'
|
|
UNION ALL
|
|
SELECT '7-12 Days'
|
|
UNION ALL
|
|
SELECT '0-6 Days'
|
|
) AS tc
|
|
LEFT JOIN (
|
|
SELECT
|
|
th.ticket_id,
|
|
tcs.claim_status,
|
|
tcs.id AS status_id,
|
|
CASE
|
|
WHEN DATEDIFF(
|
|
tm.updated_at,
|
|
COALESCE(tm.updated_at, th.created_at)
|
|
) BETWEEN 0 AND 6 THEN '0-6 Days'
|
|
WHEN DATEDIFF(
|
|
tm.updated_at,
|
|
COALESCE(tm.updated_at, th.created_at)
|
|
) BETWEEN 7 AND 12 THEN '7-12 Days'
|
|
WHEN DATEDIFF(
|
|
tm.updated_at,
|
|
COALESCE(tm.updated_at, th.created_at)
|
|
) BETWEEN 13 AND 20 THEN '13-20 Days'
|
|
ELSE 'Above 20 Days'
|
|
END AS TAT_Category
|
|
FROM
|
|
ticket_master tm
|
|
JOIN ticket_history th ON tm.id = th.ticket_id
|
|
JOIN ticket_claim_status tcs ON th.field_name = 'claim_status_id'
|
|
AND th.new_value = tcs.id
|
|
$ticket_type_data_2
|
|
AND tm.created_at >= '$fromDate'
|
|
AND tm.created_at <= '$toDate'
|
|
AND tm.is_active = 1
|
|
AND th.is_active = 1
|
|
AND tcs.is_active = 1
|
|
) AS subquery ON tc.TAT_Category = subquery.TAT_Category
|
|
GROUP BY
|
|
tc.TAT_Category
|
|
ORDER BY
|
|
FIELD(
|
|
tc.TAT_Category,
|
|
'Above 20 Days',
|
|
'13-20 Days',
|
|
'7-12 Days',
|
|
'0-6 Days'
|
|
);
|
|
";
|
|
|
|
// Execute the query and return the result
|
|
$data = $this->db->query($sql)->getResultArray();
|
|
// dd($this->db->getLastQuery(), $data);
|
|
|
|
// $tableData = [];
|
|
// if (!empty($data)) {
|
|
// // Extract table headers from the first row keys
|
|
// $headers = array_keys($data[0]);
|
|
// $tableData['headers'] = $headers;
|
|
// $tableData['body'] = $data;
|
|
// }
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function tpaWiseReport($policy_type = null ,$start_date = null, $end_date = null){
|
|
$ticket_type_data_1 = "";
|
|
$ticket_type_data_2 = "";
|
|
|
|
if (!empty($policy_type)) {
|
|
$ticket_type_data_1 = "AND ticket_type = $policy_type";
|
|
$ticket_type_data_2 = "AND master.ticket_type_id = $policy_type";
|
|
}
|
|
|
|
// Fetch claim statuses dynamically
|
|
$statusQuery = "SELECT id, claim_status FROM ticket_claim_status WHERE is_active = 1 $ticket_type_data_1";
|
|
$statusResult = $this->db->query($statusQuery)->getResultArray();
|
|
|
|
// Initialize dynamic query parts
|
|
$dynamicSelect = '';
|
|
$dynamicTotal = '';
|
|
$dynamicClosedTotal = '';
|
|
|
|
// Loop through each claim status and generate the CASE statements
|
|
foreach ($statusResult as $status) {
|
|
$dynamicSelect .= "
|
|
COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) AS `{$status['claim_status']}`, ";
|
|
|
|
if (in_array($status['claim_status'], ['ID NOT GENERATED', 'NON ID', 'CDA', 'INFORMATION REQUIRED','UNDER PROCESS - CLAIM NO. UPDATION',
|
|
'UNDER PROCESS - INVESTIGATION STATUS','UNDER PROCESS - QUERY DOCUMENT RECEIVED','APPROVED','PAYMENT INITIATED'])) {
|
|
$dynamicTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
}
|
|
// Include in total count
|
|
// $dynamicTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
|
|
// Include only closed-related statuses in total2 count
|
|
if (in_array($status['claim_status'], ['CLOSED', 'SETTLED', 'RETURNED', 'REJECTED'])) {
|
|
$dynamicClosedTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
}
|
|
}
|
|
|
|
// Remove the trailing commas and `+` signs
|
|
$dynamicSelect = rtrim($dynamicSelect, ', ');
|
|
$dynamicTotal = rtrim($dynamicTotal, ' +');
|
|
$dynamicClosedTotal = rtrim($dynamicClosedTotal, ' +');
|
|
// print_rr($dynamicSelect);
|
|
// Construct the final SQL query
|
|
$sql = "
|
|
SELECT
|
|
tpa.name AS TPA_NAME,
|
|
$dynamicSelect,
|
|
($dynamicTotal) AS TOTAL,
|
|
($dynamicClosedTotal) AS CLEARED_TOTAL
|
|
FROM
|
|
ticket_master master
|
|
JOIN
|
|
tpa ON master.tpa_id = tpa.id AND tpa.is_active = 1
|
|
WHERE
|
|
master.created_at BETWEEN '$start_date' AND '$end_date'
|
|
AND master.is_active = 1
|
|
$ticket_type_data_2
|
|
GROUP BY
|
|
tpa.id;
|
|
";
|
|
|
|
// Execute the query
|
|
$result = $this->db->query($sql)->getResultArray();
|
|
|
|
// print_rr($result);die();
|
|
return $result;
|
|
|
|
}
|
|
|
|
public function accountManagerWiseReport($policy_type = null ,$start_date = null, $end_date = null){
|
|
|
|
if ($policy_type == 1) {
|
|
$ticket_type_data_1 = "";
|
|
$ticket_type_data_2 = "";
|
|
|
|
if (!empty($policy_type)) {
|
|
$ticket_type_data_1 = "AND ticket_type = $policy_type";
|
|
$ticket_type_data_2 = "AND master.ticket_type_id = $policy_type";
|
|
}
|
|
|
|
// Fetch claim statuses dynamically
|
|
$statusQuery = "SELECT id, claim_status FROM ticket_claim_status WHERE is_active = 1 $ticket_type_data_1";
|
|
$statusResult = $this->db->query($statusQuery)->getResultArray();
|
|
|
|
// Initialize dynamic query parts
|
|
$dynamicSelect = '';
|
|
$dynamicTotal = '';
|
|
|
|
// Loop through each claim status and generate the CASE statements
|
|
foreach ($statusResult as $status) {
|
|
$dynamicSelect .= "
|
|
COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) AS `{$status['claim_status']}`, ";
|
|
|
|
|
|
// Include in total count
|
|
$dynamicTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
|
|
// Include only closed-related statuses in total2 count
|
|
|
|
}
|
|
|
|
// Remove the trailing commas and `+` signs
|
|
$dynamicSelect = rtrim($dynamicSelect, ', ');
|
|
$dynamicTotal = rtrim($dynamicTotal, ' +');
|
|
// print_rr($dynamicSelect);
|
|
// Construct the final SQL query
|
|
$sql = "
|
|
SELECT
|
|
user_profiles.first_name AS ACM_NAME,
|
|
$dynamicSelect,
|
|
($dynamicTotal) AS TOTAL
|
|
FROM
|
|
ticket_master master
|
|
JOIN
|
|
user_profiles ON master.acm_id = user_profiles.id AND user_profiles.is_active = 1
|
|
WHERE
|
|
master.created_at BETWEEN '$start_date' AND '$end_date'
|
|
AND master.is_active = 1
|
|
$ticket_type_data_2
|
|
GROUP BY
|
|
user_profiles.id;
|
|
";
|
|
|
|
$result = $this->db->query($sql)->getResultArray();
|
|
// dd($result);
|
|
|
|
return $result;
|
|
|
|
|
|
}else{
|
|
$ticket_type_data_1 = "";
|
|
$ticket_type_data_2 = "";
|
|
|
|
if (!empty($policy_type)) {
|
|
$ticket_type_data_1 = "AND ticket_type = $policy_type";
|
|
$ticket_type_data_2 = "AND master.ticket_type_id = $policy_type";
|
|
}
|
|
|
|
// Fetch claim statuses dynamically
|
|
$statusQuery = "SELECT id, claim_status FROM ticket_claim_status WHERE is_active = 1 $ticket_type_data_1";
|
|
$statusResult = $this->db->query($statusQuery)->getResultArray();
|
|
|
|
// Initialize dynamic query parts
|
|
$dynamicSelect = '';
|
|
$dynamicTotal = '';
|
|
$dynamicClosedTotal = '';
|
|
|
|
// Loop through each claim status and generate the CASE statements
|
|
foreach ($statusResult as $status) {
|
|
$dynamicSelect .= "
|
|
COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) AS `{$status['claim_status']}`, ";
|
|
|
|
if (in_array($status['claim_status'], ['CLAIM INTIMATION', 'INTIMATION TO INSURER', 'CLIENT PENDING', 'INSURER PENDING','INVESTIGATION',
|
|
'APPROVED','ON HOLD'])) {
|
|
$dynamicTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
}
|
|
// Include in total count
|
|
// $dynamicTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
|
|
// Include only closed-related statuses in total2 count
|
|
if (in_array($status['claim_status'], ['CLOSED', 'SETTLED', 'NOT COVERED'])) {
|
|
$dynamicClosedTotal .= "COUNT(CASE WHEN master.claim_status_id = {$status['id']} THEN master.id END) + ";
|
|
}
|
|
}
|
|
|
|
// Remove the trailing commas and `+` signs
|
|
$dynamicSelect = rtrim($dynamicSelect, ', ');
|
|
$dynamicTotal = rtrim($dynamicTotal, ' +');
|
|
$dynamicClosedTotal = rtrim($dynamicClosedTotal, ' +');
|
|
// print_rr($dynamicSelect);
|
|
// Construct the final SQL query
|
|
$sql = "
|
|
SELECT
|
|
user_profiles.first_name AS ACM_NAME,
|
|
$dynamicSelect,
|
|
($dynamicTotal) AS TOTAL,
|
|
($dynamicClosedTotal) AS CLEARED_TOTAL
|
|
FROM
|
|
ticket_master master
|
|
JOIN
|
|
user_profiles ON master.acm_id = user_profiles.id AND user_profiles.is_active = 1
|
|
WHERE
|
|
master.created_at BETWEEN '$start_date' AND '$end_date'
|
|
AND master.is_active = 1
|
|
$ticket_type_data_2
|
|
GROUP BY
|
|
user_profiles.id;
|
|
";
|
|
|
|
// Execute the query
|
|
$result = $this->db->query($sql)->getResultArray();
|
|
|
|
// print_rr($result);die();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
}
|