nhance/app/Controllers/BDSReportController.php
2026-02-12 12:34:24 +05:30

1193 lines
68 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use Kint\Kint;
use App\Models\ClientModel;
use App\Models\ClientPolicyModel;
use App\Models\InsurerModel;
use App\Models\InsurerBranchModel;
use App\Models\PolicyTransactionModel;
use App\Models\PTCOShareDetailsModel;
use App\Models\COShareStmtDetailsModel;
use App\Models\PolicyTypeModel;
use App\Models\NhanceBranchModel;
use CodeIgniter\API\ResponseTrait;
class BDSReportController extends AdminController
{
use ResponseTrait;
protected $myLogger;
protected $clientModel;
protected $clientPolicyModel;
protected $insurerModel;
protected $insurerBranchModel;
protected $policyTransactionModel;
protected $PTCOShareDetailsModel;
protected $coShareStmtDetailsModel;
protected $policyTypeModel;
protected $policyTatReportType;
protected $nhanceBranchModel;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->clientModel = new ClientModel();
$this->clientPolicyModel = new ClientPolicyModel();
$this->insurerModel = new InsurerModel();
$this->insurerBranchModel = new InsurerBranchModel();
$this->policyTransactionModel = new PolicyTransactionModel();
$this->PTCOShareDetailsModel = new PTCOShareDetailsModel();
$this->coShareStmtDetailsModel = new COShareStmtDetailsModel();
$this->policyTypeModel = new PolicyTypeModel();
$this->nhanceBranchModel = new NhanceBranchModel();
$this->policyTatReportType = [
'1' => "TAT Band Wise report",
'2' => "Account Manger Status Wise Report",
'3' => "Account Manger TAT Wise Report"
];
}
// public function Insurer_wise_Data($insurerCategory, $fromDate, $toDate)
// {
// $fromDate = \DateTime::createFromFormat('d-m-Y', $fromDate)->format('Y-m-d');
// $toDate = \DateTime::createFromFormat('d-m-Y', $toDate)->format('Y-m-d');
// try {
// $sql = "
// select ins.id,ins.name as insurers,
// COALESCE(SUM(CASE WHEN pt.action_type = 'inception' and pt.insurer_id = ins.id and ptp.policy_category = $insurerCategory THEN 1 ELSE 0 END), 0) AS Policies,
// COALESCE(SUM(CASE WHEN ptp.policy_category = $insurerCategory and pt_co.insurer_id = ins.id THEN (pt_co.bp_amt + pt_co.tp_amt + pt_co.tep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS Premium,
// COALESCE(SUM(CASE WHEN stmt.co_share_id = pt_co.id and ptp.policy_category = $insurerCategory and pt_co.insurer_id = ins.id THEN stmt.reward + stmt.actual_bp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.actual_tp_brokerage_amt ELSE 0 END), 0) AS Revenue,
// COALESCE(SUM(CASE WHEN ptp.policy_category = $insurerCategory and pt_co.insurer_id = ins.id THEN pt_co.exp_amt ELSE 0 END), 0) AS Expected_amt
// from insurers ins
// left join pt_co_share_details pt_co on ins.id = pt_co.insurer_id and pt_co.is_active = 1
// left join co_share_stmt_details stmt on pt_co.id = stmt.co_share_id and stmt.is_active = 1
// left join policy_transaction pt on pt_co.pt_id = pt.id and pt.is_active = 1 and pt.policy_issue_date >= '$fromDate' and pt.policy_issue_date <= '$toDate'
// left join policy_type ptp on pt.policy_type_id = ptp.id and ptp.policy_category = $insurerCategory and ptp.is_active = 1
// where ins.is_active = 1 and (ptp.policy_category = $insurerCategory or ptp.policy_category is null)
// group by ins.id
// ";
// $data['insurer_wise_data'] = $this->insurerModel->query($sql)->getResultArray();
// // log_message('error',$this->insurerModel->getLastQuery());
// // log_message('error',json_encode($data));
// } catch (\Exception $e) {
// $data['message'] = 'No data Found';
// $this->myLogger->logme('error', $e->getMessage());
// return $data;
// }
// return ($data);
// }
public function Insurer_wise_Data($insurerCategory, $fromDate, $toDate)
{
$fromDate = \DateTime::createFromFormat('d-m-Y', $fromDate)->format('Y-m-d');
$toDate = \DateTime::createFromFormat('d-m-Y', $toDate)->format('Y-m-d');
try {
$sql = "
SELECT
ins.id,
ins.name AS insurers,
COALESCE(SUM(CASE WHEN pt.action_type = 'inception' AND pt.insurer_id = ins.id AND ptp.policy_category = :insurerCategory: THEN 1 ELSE 0 END), 0) AS Policies,
COALESCE(SUM(CASE WHEN ptp.policy_category = :insurerCategory: AND pt_co.insurer_id = ins.id THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS Premium,
COALESCE(SUM(CASE WHEN stmt.co_share_id = pt_co.id AND ptp.policy_category = :insurerCategory: AND pt_co.insurer_id = ins.id THEN stmt.reward + stmt.actual_bp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.actual_tp_brokerage_amt ELSE 0 END), 0) AS Revenue,
COALESCE(SUM(CASE WHEN ptp.policy_category = :insurerCategory: AND pt_co.insurer_id = ins.id THEN pt_co.exp_amt ELSE 0 END), 0) AS Expected_amt
FROM
insurers ins
LEFT JOIN
pt_co_share_details pt_co ON ins.id = pt_co.insurer_id AND pt_co.is_active = 1
LEFT JOIN
co_share_stmt_details stmt ON pt_co.id = stmt.co_share_id AND stmt.is_active = 1
LEFT JOIN
policy_transaction pt ON pt_co.pt_id = pt.id AND pt.is_active = 1 AND pt.policy_issue_date >= :fromDate: AND pt.policy_issue_date <= :toDate:
LEFT JOIN
policy_type ptp ON pt.policy_type_id = ptp.id AND ptp.policy_category = :insurerCategory: AND ptp.is_active = 1
WHERE
ins.is_active = 1 AND (ptp.policy_category = :insurerCategory: OR ptp.policy_category IS NULL)
GROUP BY
ins.id;
";
$binds = ['insurerCategory'=>$insurerCategory,'fromDate'=>$fromDate,'toDate'=>$toDate];
$data['insurer_wise_data'] = $this->insurerModel->query($sql,$binds)->getResultArray();
// log_message('error',$this->insurerModel->getLastQuery());
// log_message('error',json_encode($data));
} catch (\Exception $e) {
$data['message'] = 'No data Found';
$this->myLogger->logme('error', $e->getMessage());
return $data;
}
return ($data);
}
public function Bap_Wise_Data($insurerCategory, $fromDate, $toDate)
{
$fromDate = \DateTime::createFromFormat('d-m-Y', $fromDate)->format('Y-m-d');
$toDate = \DateTime::createFromFormat('d-m-Y', $toDate)->format('Y-m-d');
try {
$sql = "
SELECT
pot.bap AS bap,
COALESCE(COUNT(DISTINCT pt.id), 0) AS Policies,
COALESCE(SUM((pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt)), 0) AS Premium,
COALESCE(SUM(co.reward + co.actual_bp_brokerage_amt + co.actual_tep_brokerage_amt + co.actual_tp_brokerage_amt), 0) AS Revenue,
COALESCE(SUM(pt_co.exp_amt), 0) AS Expected_amt
FROM policy_type AS pot
LEFT JOIN policy_transaction AS pt ON pot.id = pt.policy_type_id AND pt.is_active = 1 AND pt.action_type = 'inception' and pt.policy_issue_date >= :fromDate: and pt.policy_issue_date <= :toDate:
LEFT JOIN pt_co_share_details AS pt_co ON pt.id = pt_co.pt_id AND pt_co.is_active = 1
LEFT JOIN co_share_stmt_details AS co ON pt_co.id = co.co_share_id AND co.is_active = 1
WHERE pot.is_active = 1
AND pot.policy_category = :insurerCategory:
GROUP BY pot.bap;
";
$binds = ['insurerCategory'=>$insurerCategory,'fromDate'=>$fromDate,'toDate'=>$toDate];
$data['bap_wise_data'] = $this->policyTypeModel->query($sql,$binds)->getResultArray();
} catch (\Exception $e) {
$data['message'] = 'No Data Found';
$this->myLogger->logme('error', $e->getMessage());
return $data;
}
return ($data);
}
public function irba_report()
{
if ($this->request->is('get')) {
$default_start = new \DateTime();
$data['default_end'] = $default_start->format('d-m-Y');
$data['default_start'] = $default_start->modify('-90 days')->format('d-m-Y');
$data['categories'] = [
'Life' => 'life',
'Non Life' => 'nonLife'
];
$data['report_type'] = [
'Insurer' => 'insurer',
'BAP' => 'bap',
'BAP-Client' => 'bapClient',
'BAP-Insurer' => 'bapInsurer',
'Client' => 'client'
];
$data['tab_name'] = "IRDA Reports";
$data['page_name'] = "IRDA Report";
return $this->loadLayout('irba_report', $data);
} else {
$request_post_data = $this->request->getPost();
$sanitized_post_data = sanitizeInputArrayAdvanced($request_post_data);
$fromDate = $sanitized_post_data['fromDate'] ?? null;
$toDate = $sanitized_post_data['toDate'] ?? null;
$category = $sanitized_post_data['category'] ?? null;
$report_type = $sanitized_post_data['report_type'] ?? null;
// log_message('error',json_encode($_POST));die();
if ($report_type == 'insurer') {
$life = $category == 'life' ? 1 : 0;
$viewData = $this->Insurer_wise_Data($life, $fromDate, $toDate);
if (isset($data['message'])) {
return $this->respond(['status' => false, 'message' => $data['message']], 200);
}
// Ensure $viewData is an array
if (!is_array($viewData)) {
$viewData = [];
}
$view_name = "bds_report_Insurer_wise_Data" ;
$html = view('bds_report_Insurer_wise_Data', $viewData);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
} else if ($report_type == 'bap') {
$life = $category == 'life' ? 1 : 0;
$viewData = $this->Bap_wise_Data($life, $fromDate, $toDate);
if (isset($data['message'])) {
return $this->respond(['status' => false, 'message' => $data['message']], 200);
}
// Ensure $viewData is an array
if (!is_array($viewData)) {
$viewData = [];
}
$view_name = "bds_report_bap_wise_data";
$html = view('bds_report_bap_wise_data', $viewData);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
} else if ($report_type == 'bapInsurer') {
//Condition for BAP Insurer wise date
$category = $category == 'life' ? 1 : 0;
$viewData['data'] = $this->getBAPInsurerData($category, $fromDate, $toDate);
$view_name = "irda_bap_insurer_report_list";
$html = view('irda_bap_insurer_report_list', $viewData);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
} else if ($report_type == 'bapClient') {
//Condition for BAP Client wise date
$category = $category == 'life' ? 1 : 0;
$viewData['data'] = $this->getBAPClientData($category, $fromDate, $toDate);
$view_name = "irda_bap_client_report_list";
$html = view('irda_bap_client_report_list', $viewData);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
} else if ($report_type == 'client') {
//Condition for Client wise date
$category = $category == 'life' ? 1 : 0;
$viewData['data'] = $this->getClientData($category, $fromDate, $toDate);
$view_name = "irda_client_report_list";
$html = view('irda_client_report_list', $viewData);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
}
}
}
// ------------------------------------------------------------------------------------------------
//Function for get BAP INSURE wise data for premium , policy count , revenue only
public function getBAPInsurerData($category = 0, $start_date = null, $end_date = null)
{
$category = (int)$category;
try {
$this->myLogger->logme('error', 'getBAPInsurerData function called');
$this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
$this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
//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);
}
$this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
$db = db_connect();
$builder = $db->query("
select ins.id, ins.name as insurer_name,
-- Health Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS health_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS health_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS health_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN pt_co.exp_amt ELSE 0 END), 0) AS health_exp_amt,
-- Misc Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS misc_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS misc_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS misc_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN pt_co.exp_amt ELSE 0 END), 0) AS misc_exp_amt,
-- Motor Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS motor_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS motor_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS motor_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN pt_co.exp_amt ELSE 0 END), 0) AS motor_exp_amt,
-- Engineering Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS engineering_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS engineering_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS engineering_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN pt_co.exp_amt ELSE 0 END), 0) AS engineering_exp_amt,
-- Fire Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS fire_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS fire_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS fire_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN pt_co.exp_amt ELSE 0 END), 0) AS fire_exp_amt,
-- Liability Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS liability_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS liability_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS liability_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN pt_co.exp_amt ELSE 0 END), 0) AS liability_exp_amt,
-- Life Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS life_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS life_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS life_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN pt_co.exp_amt ELSE 0 END), 0) AS life_exp_amt,
-- Marine Cargo Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_cargo_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS marine_cargo_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS marine_cargoe_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN pt_co.exp_amt ELSE 0 END), 0) AS marine_cargo_exp_amt,
-- Marine Hull Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_hull_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS marine_hull_premium,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS marine_hull_revenue,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN pt_co.exp_amt ELSE 0 END), 0) AS marine_hull_exp_amt,
-- Grand Totals
COALESCE(
SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END)+
SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END),
0
) AS total_policy_count,
COALESCE(
SUM(CASE WHEN ptp.bap = 'Health' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Life' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Motor' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Fire' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Engineering' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Liability' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END)+
SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END)+
SUM(CASE WHEN ptp.bap = 'Misc' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END),
0
) AS total_premium,
COALESCE(
SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END)+
SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END)+
SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END),
0
) AS total_revenue
from insurers ins
left join pt_co_share_details pt_co on ins.id = pt_co.insurer_id and pt_co.is_active = 1
left join co_share_stmt_details stmt on pt_co.id = stmt.co_share_id and stmt.is_active = 1
left join policy_transaction pt on pt_co.pt_id = pt.id and pt.is_active = 1 AND pt.policy_issue_date >= '$fromDate' AND pt.policy_issue_date <= '$toDate'
left join policy_type ptp on pt.policy_type_id = ptp.id and ptp.policy_category = $category
where ins.is_active = 1
group by ins.id
ORDER BY
health_premium DESC,
misc_premium DESC,
motor_premium DESC,
engineering_premium DESC,
fire_premium DESC,
liability_premium DESC,
life_premium DESC,
marine_cargo_premium DESC,
marine_hull_premium DESC;
");
// Get the query
$result = $builder->getResultArray();
$this->myLogger->logme('error', 'Query executed successfully.');
// dd(db_connect()->getLastQuery(),$result);
return $result;
} catch (\Exception $e) {
// Log the exception details for debugging
$this->myLogger->logme('error', 'Error occurred in getBAPInsurerData: {message}', ['message' => $e->getMessage()]);
$this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
return [];
}
}
//Function for get BAP CLIENT wise data for premium , policy count , revenue only
public function getBAPClientData($category = 0, $start_date = null, $end_date = null)
{
$category = (int)$category;
try {
$this->myLogger->logme('error', 'getBAPClientData function called');
$this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
$this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
//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);
}
$this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
$db = db_connect();
$builder = $db->query("
SELECT
c.id AS client_id,
c.client_name,
-- Health Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS health_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS health_premium,
-- Misc Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS misc_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS misc_premium,
-- Motor Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS motor_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS motor_premium,
-- Engineering Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS engineering_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS engineering_premium,
-- Fire Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS fire_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS fire_premium,
-- Liability Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS liability_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS liability_premium,
-- Life Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS life_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS life_premium,
-- Marine Cargo Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_cargo_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS marine_cargo_premium,
-- Marine Hull Policies
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_hull_policy_count,
COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN (pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) ELSE 0 END), 0) AS marine_hull_premium,
-- Grand Totals
COALESCE(SUM(CASE WHEN pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS total_policy_count,
COALESCE(SUM(pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt), 0) AS total_premium
FROM clients c
LEFT JOIN policy_transaction pt ON pt.client_id = c.id AND pt.is_active = 1 AND pt.policy_issue_date >= '$fromDate' AND pt.policy_issue_date <= '$toDate'
LEFT JOIN policy_type ptp ON pt.policy_type_id = ptp.id
LEFT JOIN pt_co_share_details pt_co ON pt_co.pt_id = pt.id AND pt_co.is_active = 1
LEFT JOIN co_share_stmt_details stmt ON stmt.co_share_id = pt_co.id AND stmt.is_active = 1
WHERE c.is_active = 1
AND ptp.policy_category = $category
GROUP BY c.id
ORDER BY
health_premium DESC,
misc_premium DESC,
motor_premium DESC,
engineering_premium DESC,
fire_premium DESC,
liability_premium DESC,
life_premium DESC,
marine_cargo_premium DESC,
marine_hull_premium DESC
");
// Get the query
$result = $builder->getResultArray();
// dd(db_connect()->getLastQuery(),$result);
return $result;
} catch (\Exception $e) {
// Log the exception details for debugging
$this->myLogger->logme('error', 'Error occurred in getBAPClientData: {message}', ['message' => $e->getMessage()]);
$this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
return [];
}
}
//Function for get CLIENT wise data for premium and policy count only
public function getClientData($category = 0, $start_date = null, $end_date = null)
{
$category = (int)$category;
try {
$this->myLogger->logme('error', 'getClientData function called');
$this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
$this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
//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);
}
$this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
$db = db_connect();
$builder = $db->query("
select
c.id as client_id,
c.client_name,
(
select
count(policy_transaction.id)
from policy_transaction
left join policy_type on policy_transaction.policy_type_id = policy_type.id
where policy_transaction.client_id = c.id
and policy_transaction.action_type = 'inception'
and policy_transaction.is_active = 1
) as policy_count,
COALESCE((
select
sum(pt_co.cop_amt + pt_co.cotp_amt + pt_co.cotep_amt + pt_co.non_comm_per_amt) as premium
from co_share_stmt_details stmt
left join pt_co_share_details pt_co on stmt.co_share_id = pt_co.id
left join policy_transaction pt on pt_co.pt_id = pt.id
where pt.client_id = c.id
and stmt.is_active = 1
and pt_co.is_active = 1
and pt.is_active = 1
), 0) as premium
from clients c
join policy_transaction on c.id = policy_transaction.client_id AND policy_transaction.policy_issue_date >= '$fromDate' AND policy_transaction.policy_issue_date <= '$toDate'
join policy_type on policy_transaction.policy_type_id = policy_type.id and policy_type.policy_category = $category
where c.is_active = 1
group by client_name
order by premium desc
");
// Get the query
$result = $builder->getResultArray();
// dd(db_connect()->getLastQuery(),$result);
return $result;
} catch (\Exception $e) {
// Log the exception details for debugging
$this->myLogger->logme('error', 'Error occurred in getClientData: {message}', ['message' => $e->getMessage()]);
$this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
return [];
}
}
// //Function for get BAP INSURE wise data for premium , policy count , revenue only
// public function getBAPInsurerData($category = 0, $start_date = null, $end_date = null)
// {
// try {
// $this->myLogger->logme('error', 'getBAPInsurerData function called');
// $this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
// $this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
// //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);
// }
// $this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
// $db = db_connect();
// $builder = $db->query("
// select ins.id, ins.name as insurer_name,
// -- Health Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS health_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS health_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS health_revenue,
// -- Misc Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS misc_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS misc_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS misc_revenue,
// -- Motor Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS motor_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS motor_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS motor_revenue,
// -- Engineering Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS engineering_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS engineering_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS engineering_revenue,
// -- Fire Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS fire_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS fire_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS fire_revenue,
// -- Liability Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS liability_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS liability_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS liability_revenue,
// -- Life Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS life_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS life_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS life_revenue,
// -- Marine Cargo Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_cargo_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS marine_cargo_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS marine_cargoe_revenue,
// -- Marine Hull Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_hull_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS marine_hull_premium,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END), 0) AS marine_hull_revenue,
// -- Grand Totals
// COALESCE(
// SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END)+
// SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END),
// 0
// ) AS total_policy_count,
// COALESCE(
// SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END)+
// SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END)+
// SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END),
// 0
// ) AS total_premium,
// COALESCE(
// SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END) +
// SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END)+
// SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END)+
// SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_brokerage_amt + stmt.actual_tp_brokerage_amt + stmt.actual_tep_brokerage_amt + stmt.reward ELSE 0 END),
// 0
// ) AS total_revenue
// from insurers ins
// left join pt_co_share_details pt_co on ins.id = pt_co.insurer_id and pt_co.is_active = 1
// left join co_share_stmt_details stmt on pt_co.id = stmt.co_share_id and stmt.is_active = 1
// left join policy_transaction pt on pt_co.pt_id = pt.id and pt.is_active = 1 AND pt.policy_issue_date >= '$fromDate' AND pt.policy_issue_date <= '$toDate'
// left join policy_type ptp on pt.policy_type_id = ptp.id and ptp.policy_category = $category
// where ins.is_active = 1
// group by ins.id
// ORDER BY
// health_premium DESC,
// misc_premium DESC,
// motor_premium DESC,
// engineering_premium DESC,
// fire_premium DESC,
// liability_premium DESC,
// life_premium DESC,
// marine_cargo_premium DESC,
// marine_hull_premium DESC;
// ");
// // Get the query
// $result = $builder->getResultArray();
// $this->myLogger->logme('error', 'Query executed successfully.');
// // dd(db_connect()->getLastQuery(),$result);
// return $result;
// } catch (\Exception $e) {
// // Log the exception details for debugging
// $this->myLogger->logme('error', 'Error occurred in getBAPInsurerData: {message}', ['message' => $e->getMessage()]);
// $this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
// return [];
// }
// }
// //Function for get BAP CLIENT wise data for premium , policy count , revenue only
// public function getBAPClientData($category = 0, $start_date = null, $end_date = null)
// {
// try {
// $this->myLogger->logme('error', 'getBAPClientData function called');
// $this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
// $this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
// //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);
// }
// $this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
// $db = db_connect();
// $builder = $db->query("
// SELECT
// c.id AS client_id,
// c.client_name,
// -- Health Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Health' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS health_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Health' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS health_premium,
// -- Misc Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS misc_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Misc' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS misc_premium,
// -- Motor Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS motor_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Motor' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS motor_premium,
// -- Engineering Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS engineering_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Engineering' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS engineering_premium,
// -- Fire Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS fire_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Fire' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS fire_premium,
// -- Liability Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS liability_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Liability' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS liability_premium,
// -- Life Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Life' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS life_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Life' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS life_premium,
// -- Marine Cargo Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_cargo_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Cargo' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS marine_cargo_premium,
// -- Marine Hull Policies
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' AND pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS marine_hull_policy_count,
// COALESCE(SUM(CASE WHEN ptp.bap = 'Marine Hull' THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt ELSE 0 END), 0) AS marine_hull_premium,
// -- Grand Totals
// COALESCE(SUM(CASE WHEN pt.action_type = 'inception' THEN 1 ELSE 0 END), 0) AS total_policy_count,
// COALESCE(SUM(stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt), 0) AS total_premium
// FROM clients c
// LEFT JOIN policy_transaction pt ON pt.client_id = c.id AND pt.is_active = 1 AND pt.policy_issue_date >= '$fromDate' AND pt.policy_issue_date <= '$toDate'
// LEFT JOIN policy_type ptp ON pt.policy_type_id = ptp.id
// LEFT JOIN pt_co_share_details ptco ON ptco.pt_id = pt.id AND ptco.is_active = 1
// LEFT JOIN co_share_stmt_details stmt ON stmt.co_share_id = ptco.id AND stmt.is_active = 1
// WHERE c.is_active = 1
// AND ptp.policy_category = $category
// GROUP BY c.id
// ORDER BY
// health_premium DESC,
// misc_premium DESC,
// motor_premium DESC,
// engineering_premium DESC,
// fire_premium DESC,
// liability_premium DESC,
// life_premium DESC,
// marine_cargo_premium DESC,
// marine_hull_premium DESC
// ");
// // Get the query
// $result = $builder->getResultArray();
// // dd(db_connect()->getLastQuery(),$result);
// return $result;
// } catch (\Exception $e) {
// // Log the exception details for debugging
// $this->myLogger->logme('error', 'Error occurred in getBAPClientData: {message}', ['message' => $e->getMessage()]);
// $this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
// return [];
// }
// }
// //Function for get CLIENT wise data for premium and policy count only
// public function getClientData($category = 0, $start_date = null, $end_date = null)
// {
// try {
// $this->myLogger->logme('error', 'getClientData function called');
// $this->myLogger->logme('error', 'category : {data}', ['data' => $category]);
// $this->myLogger->logme('error', 'start date : {start_date} - end date {end_date} ', ['start_date' => $start_date, 'end_date' => $end_date]);
// //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);
// }
// $this->myLogger->logme('error', 'From date : {fromDate} - To date {toDate} ', ['fromDate' => $fromDate, 'toDate' => $toDate]);
// $db = db_connect();
// $builder = $db->query("
// select
// c.id as client_id,
// c.client_name,
// (
// select
// count(policy_transaction.id)
// from policy_transaction
// left join policy_type on policy_transaction.policy_type_id = policy_type.id
// where policy_transaction.client_id = c.id
// and policy_transaction.action_type = 'inception'
// and policy_transaction.is_active = 1
// ) as policy_count,
// COALESCE((
// select
// sum(stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_amt) as premium
// from co_share_stmt_details stmt
// left join pt_co_share_details ptco on stmt.co_share_id = ptco.id
// left join policy_transaction pt on ptco.pt_id = pt.id
// where pt.client_id = c.id
// and stmt.is_active = 1
// and ptco.is_active = 1
// and pt.is_active = 1
// ), 0) as premium
// from clients c
// join policy_transaction on c.id = policy_transaction.client_id AND policy_transaction.policy_issue_date >= '$fromDate' AND policy_transaction.policy_issue_date <= '$toDate'
// join policy_type on policy_transaction.policy_type_id = policy_type.id and policy_type.policy_category = $category
// where c.is_active = 1
// group by client_name
// order by premium desc
// ");
// // Get the query
// $result = $builder->getResultArray();
// // dd(db_connect()->getLastQuery(),$result);
// return $result;
// } catch (\Exception $e) {
// // Log the exception details for debugging
// $this->myLogger->logme('error', 'Error occurred in getClientData: {message}', ['message' => $e->getMessage()]);
// $this->myLogger->logme('error', 'Stack trace: {trace}', ['trace' => $e->getTraceAsString()]);
// return [];
// }
// }
// ---------------RENEWAL REPORT-----------------------------------------------------------------------------------
public function renewalReport()
{
if ($this->request->is('get')) {
$default_start = new \DateTime();
$data['default_end'] = $default_start->format('d-m-Y');
$data['default_start'] = $default_start->modify('-60 days')->format('d-m-Y');
$data['issuer_branch'] = $this->nhanceBranchModel->where('is_active', 1)->findAll();
$data['client_list'] = $this->clientModel->where('is_active', 1)->findAll();
$data['tab_name'] = "Renewal Reports";
$data['page_name'] = "Renewal Report";
return $this->loadLayout('renewal_search', $data);
} else {
$request_post_data = $this->request->getPost();
$sanitized_post_data = sanitizeInputArrayAdvanced($request_post_data);
$fromDate = $sanitized_post_data['fromDate'] ?? null;
$toDate = $sanitized_post_data['toDate'] ?? null;
$client_id = $sanitized_post_data['client_id'] ?? null;
$client_type = $sanitized_post_data['client_type'] ?? null;
$issuer_branch = $sanitized_post_data['issuer_branch'] ?? null;
$data['issuer_branch'] = $this->nhanceBranchModel->where('is_active', 1)->findAll();
$data['client_type'] = [1 => 'Group', 2 => 'Individual'];
$data['renewal_data'] = $this->policyTransactionModel->getRenewalReportData($fromDate, $toDate, $client_type, $client_id, $issuer_branch);
// print_r($data); die;
$view_name = "bds_renewal_report_list";
$html = view('bds_renewal_report_list', $data);
return $this->respond(['status' => true, 'html' => $html , 'view_name' => $view_name], 200);
}
}
/** New arugument Introduced Named as "$isInternalCalled"
* Functional Based called means $isInternalCalled is True.
* Output returning as Array
* Route Based called means $isInternalCalled is False. becoz this is External Call
* Output returning as Page
*/
public function bdsTATReport($isInternalCalled = false)
{
$sql = "
SELECT
tc.TAT_Category,
COALESCE(SUM(CASE WHEN subquery.status = 'under_process' THEN 1 ELSE 0 END), 0) AS `Under Process`,
COALESCE(SUM(CASE WHEN subquery.status = 'client_pending' THEN 1 ELSE 0 END), 0) AS `Client Pending`,
COALESCE(SUM(CASE WHEN subquery.status = 'insurer_pending' THEN 1 ELSE 0 END), 0) AS `Insurer Pending`,
COALESCE(SUM(CASE WHEN subquery.status = 'instalment_pending' THEN 1 ELSE 0 END), 0) AS `Instalment Pending`,
COALESCE(SUM(CASE WHEN subquery.status = 'co_insurer_pending' THEN 1 ELSE 0 END), 0) AS `Co-Insurer Pending`,
COALESCE(SUM(CASE WHEN subquery.status = 'tpa_pending' THEN 1 ELSE 0 END), 0) AS `TPA Pending`,
COALESCE(SUM(CASE WHEN subquery.status = 'validated' THEN 1 ELSE 0 END), 0) AS `Validated`,
COALESCE(SUM(CASE WHEN subquery.status = 'cancelled' THEN 1 ELSE 0 END), 0) AS `Cancelled`,
COALESCE(SUM(CASE WHEN subquery.status = 'completed' THEN 1 ELSE 0 END), 0) AS `Completed`,
COALESCE(SUM(CASE WHEN subquery.status = 'lost' THEN 1 ELSE 0 END), 0) AS `Lost` FROM
(
SELECT 'Above 12 Days' AS TAT_Category
UNION ALL
SELECT '9-12 Days'
UNION ALL
SELECT '5-8 Days'
UNION ALL
SELECT '0-4 Days'
) AS tc
LEFT JOIN (
SELECT
pt.id AS policy_id,
ls.status,
CASE
WHEN DATEDIFF(CURRENT_DATE, pt.created_at) BETWEEN 0 AND 4 THEN '0-4 Days'
WHEN DATEDIFF(CURRENT_DATE, pt.created_at) BETWEEN 5 AND 8 THEN '5-8 Days'
WHEN DATEDIFF(CURRENT_DATE, pt.created_at) BETWEEN 9 AND 12 THEN '9-12 Days'
ELSE 'Above 12 Days'
END AS TAT_Category
FROM
policy_transaction pt
LEFT JOIN (
SELECT
pts1.*
FROM
policy_transaction_status pts1
INNER JOIN (
SELECT
policy_tran_id,
MAX(created_at) AS max_created_at
FROM
policy_transaction_status
WHERE is_active = 1
GROUP BY policy_tran_id
) pts2
ON pts1.policy_tran_id = pts2.policy_tran_id
AND pts1.created_at = pts2.max_created_at
WHERE pts1.is_active = 1
) ls
ON pt.id = ls.policy_tran_id
WHERE pt.is_active = 1
) AS subquery
ON tc.TAT_Category = subquery.TAT_Category
GROUP BY
tc.TAT_Category
ORDER BY
FIELD(tc.TAT_Category, '0-4 Days', '5-8 Days' , '9-12 Days' , 'Above 12 Days' )
";
// Run the query
$db = \Config\Database::connect();
$query = $db->query($sql);
// Get the result
$result = $query->getResultArray();
// dd(db_connect()->getLastQuery(),$result);
$data['data'] = $result;
// dd($data, get_role_id(), ENROLLMENT_TEAM_ID, user_team());
if ($isInternalCalled) { return $result; }
$data['tab_name'] = "BDS TAT Band Wise Report";
$data['page_name'] = "BDS TAT Band Wise Report";
return $this->loadLayout('bds_tat_wise_report', $data);
}
public function accountMangerStatusBDSReport()
{
$data['tab_name'] = "Account Manager BDS Report";
$data['page_name'] = "Account Manager BDS Report";
$sql = "
SELECT
COALESCE(ACM_Name, 'Total') AS ACM_Name,
COALESCE(SUM(CASE WHEN status = 'instalment_pending' THEN 1 ELSE 0 END), 0) AS `Instalment Pending`,
COALESCE(SUM(CASE WHEN status = 'under_process' THEN 1 ELSE 0 END), 0) AS `Under Process`,
COALESCE(SUM(CASE WHEN status = 'client_pending' THEN 1 ELSE 0 END), 0) AS `Client Pending`,
COALESCE(SUM(CASE WHEN status = 'co_insurer_pending' THEN 1 ELSE 0 END), 0) AS `Co Insurer Pending`,
COALESCE(SUM(CASE WHEN status = 'tpa_pending' THEN 1 ELSE 0 END), 0) AS `TPA Pending`,
COALESCE(SUM(CASE WHEN status = 'validated' THEN 1 ELSE 0 END), 0) AS `Validated`,
COALESCE(SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END), 0) AS `Cancelled`,
COALESCE(SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END), 0) AS `Completed`,
COALESCE(SUM(CASE WHEN status = 'lost' THEN 1 ELSE 0 END), 0) AS `Lost`,
COALESCE(SUM(CASE WHEN status = 'insurer_pending' THEN 1 ELSE 0 END), 0) AS `Insurer Pending`
FROM (
SELECT
up.first_name AS ACM_Name,
ls.status
FROM policy_transaction pt
JOIN clients c ON pt.client_id = c.id AND c.is_active = 1
JOIN client_rm crm ON crm.client_id = c.id AND crm.is_active = 1 AND crm.level = 3
JOIN user_profiles up ON up.id = crm.user_id AND up.is_active = 1
LEFT JOIN (
SELECT
pts1.policy_tran_id,
pts1.status,
pts1.created_at
FROM policy_transaction_status pts1
INNER JOIN (
SELECT
policy_tran_id,
MAX(created_at) AS max_created_at
FROM policy_transaction_status
WHERE is_active = 1
GROUP BY policy_tran_id
) pts2
ON pts1.policy_tran_id = pts2.policy_tran_id
AND pts1.created_at = pts2.max_created_at
WHERE pts1.is_active = 1
) ls ON pt.id = ls.policy_tran_id
WHERE pt.is_active = 1
AND DATEDIFF(CURDATE(), ls.created_at) >= 4
) AS sub
GROUP BY ACM_Name;
";
$db = \Config\Database::connect();
$query = $db->query($sql);
// Get the result
$result = $query->getResultArray();
return $result;
}
public function accountManagerTatBDSReport(){
$sql = "
SELECT
COALESCE(ACM_Name, 'Total') AS ACM_Name,
COALESCE(SUM(CASE WHEN tat_bucket = '0-4 Days' THEN 1 ELSE 0 END), 0) AS `0-4 Days`,
COALESCE(SUM(CASE WHEN tat_bucket = '5-8 Days' THEN 1 ELSE 0 END), 0) AS `5-8 Days`,
COALESCE(SUM(CASE WHEN tat_bucket = '9-12 Days' THEN 1 ELSE 0 END), 0) AS `9-12 Days`,
COALESCE(SUM(CASE WHEN tat_bucket = 'Above 12 Days' THEN 1 ELSE 0 END), 0) AS `Above 12 Days`
FROM (
SELECT
up.first_name AS ACM_Name,
CASE
WHEN DATEDIFF(CURDATE(), ls.created_at) BETWEEN 0 AND 4 THEN '0-4 Days'
WHEN DATEDIFF(CURDATE(), ls.created_at) BETWEEN 5 AND 8 THEN '5-8 Days'
WHEN DATEDIFF(CURDATE(), ls.created_at) BETWEEN 9 AND 12 THEN '9-12 Days'
WHEN DATEDIFF(CURDATE(), ls.created_at) > 12 THEN 'Above 12 Days'
ELSE 'Unknown'
END AS tat_bucket
FROM policy_transaction pt
JOIN clients c ON pt.client_id = c.id AND c.is_active = 1
JOIN client_rm crm ON crm.client_id = c.id AND crm.is_active = 1 AND crm.level = 3
JOIN user_profiles up ON up.id = crm.user_id AND up.is_active = 1
LEFT JOIN (
SELECT
pts1.policy_tran_id,
pts1.status,
pts1.created_at
FROM policy_transaction_status pts1
INNER JOIN (
SELECT
policy_tran_id,
MAX(created_at) AS max_created_at
FROM policy_transaction_status
WHERE is_active = 1
GROUP BY policy_tran_id
) pts2
ON pts1.policy_tran_id = pts2.policy_tran_id
AND pts1.created_at = pts2.max_created_at
WHERE pts1.is_active = 1
) ls ON pt.id = ls.policy_tran_id
WHERE pt.is_active = 1
AND ls.created_at IS NOT NULL
) AS sub
GROUP BY ACM_Name;
";
$db = \Config\Database::connect();
$query = $db->query($sql);
// Get the result
$result = $query->getResultArray();
return $result;
}
public function getMultiReport($report_type){
$data['tab_name'] = "BDS Reports";
if ($report_type == 1){
$data['page_name'] = "BDS TAT Report";
$data['page_title'] = "";
$data['data'] = $this->bdsTATReport(true); // Functional Based called means True
}else if ($report_type == 2){
$data['page_name'] = "Account Manager Status Report";
$data['page_title'] = "";
$data['data'] = $this->accountMangerStatusBDSReport();
}else{
$data['page_name'] = "Account Manager TAT Report";
$data['page_title'] = "";
$data['data'] = $this->accountManagerTatBDSReport();
}
return $this->loadLayout('bds_multi_report', $data);
}
}