333 lines
18 KiB
PHP
333 lines
18 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;
|
|
|
|
|
|
class BDSReportController extends BaseController
|
|
{
|
|
protected $myLogger;
|
|
protected $clientModel;
|
|
protected $clientPolicyModel;
|
|
protected $insurerModel;
|
|
protected $insurerBranchModel;
|
|
protected $policyTransactionModel;
|
|
protected $PTCOShareDetailsModel;
|
|
protected $coShareStmtDetailsModel;
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
//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 = $start_date;
|
|
$toDate = $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
|
|
|
|
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 = $start_date;
|
|
$toDate = $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
|
|
|
|
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 = $start_date;
|
|
$toDate = $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
|
|
join policy_type on policy_transaction.policy_type_id = policy_type.id
|
|
where c.is_active = 1
|
|
and policy_type.policy_category = $category
|
|
AND policy_transaction.policy_issue_date >= '$fromDate'
|
|
AND policy_transaction.policy_issue_date <= '$toDate'
|
|
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 [];
|
|
}
|
|
}
|
|
}
|