nhance/app/Controllers/BDSReportController.php
2025-01-24 09:52:14 +05:30

170 lines
7.5 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 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;
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();
}
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 stmt.co_share_id = pt_co.id and ptp.policy_category = $insurerCategory and pt_co.insurer_id = ins.id THEN stmt.actual_bp_amt + stmt.actual_tp_amt + stmt.actual_tep_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
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
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 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(co.actual_bp_amt + co.actual_tep_amt + co.actual_tp_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
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 ptco ON pt.id = ptco.pt_id AND ptco.is_active = 1
LEFT JOIN co_share_stmt_details AS co ON ptco.id = co.co_share_id AND co.is_active = 1
WHERE pot.is_active = 1
AND pot.policy_category = $insurerCategory
GROUP BY pot.bap;
";
$data['bap_wise_data'] = $this->policyTypeModel->query($sql)->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'
];
return $this->loadLayout('irba_report', $data);
} else {
$fromDate = $this->request->getPost('fromDate');
$toDate = $this->request->getPost('toDate');
$category = $this->request->getPost('category');
$report_type = $this->request->getPost('report_type');
// 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 = [];
}
$html = view('bds_report_Insurer_wise_Data', $viewData);
return $this->respond(['status' => true, 'html' => $html], 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 = [];
}
$html = view('bds_report_bap_wise_data', $viewData);
return $this->respond(['status' => true, 'html' => $html], 200);
}
}
}
// -----------------------------------------------------------------------------------------
}