diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 96c36a35..a0d62ffb 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -427,7 +427,7 @@ $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) { }); $routes->group("report", ["filter" => "authMVC"], function ($routes) { - $routes->match(['get', 'post'],"list", "PolicyTransactionController::reportBDS"); + $routes->match(['get', 'post'],"list", "PolicyTransactionController::reportBDSNew"); $routes->match(['get', 'post'],"listNew", "PolicyTransactionController::reportBDSNew"); $routes->get("report-varience-list", "PolicyTransactionController::reportVarience"); $routes->get("report-business-list", "PolicyTransactionController::reportBusinessList"); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 1b7310b6..69296a0c 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -53,6 +53,7 @@ use Illuminate\Http\Request; use App\Controllers\EmployeeServiceController; use App\Models\ClaimFilesModel; +use App\Models\TicketMailTemplateModel; use Kreait\Firebase\Factory; use Kreait\Firebase\Messaging\CloudMessage; use Kreait\Firebase\Messaging\Notification; @@ -90,6 +91,7 @@ class EmployeeRestController extends AdminController protected $hrAccessControlModel; protected $insurerModel; protected $hrFileUploadModel; + protected $ticketMailTemplateModel; public function __construct() @@ -122,6 +124,7 @@ class EmployeeRestController extends AdminController $this->hrAccessControlModel = new HRAccessControlModel(); $this->insurerModel = new InsurerModel(); $this->hrFileUploadModel = new HrFileUploadModel(); + $this->ticketMailTemplateModel = new TicketMailTemplateModel(); } @@ -4174,7 +4177,11 @@ class EmployeeRestController extends AdminController ]; // Save into DB - $this->hrFileUploadModel->insert($data); + $result = $this->hrFileUploadModel->insert($data); + + if($result && $data['file_action'] == "addition"){ + $this->giveNotificationToClientsAccountManager($data); + } return $this->respondCreated([ 'status' => true, @@ -4188,6 +4195,51 @@ class EmployeeRestController extends AdminController } + private function giveNotificationToClientsAccountManager($data){ + + // $data = [ + // 'client_id' => $this->request->getPost('client_id'), + // 'client_branch_id' => $this->request->getPost('client_branch_id'), + // 'policy_id' => $this->request->getPost('policy_id'), + // 'policy_no' => $this->request->getPost('policy_no'), + // 'file_name' => $newFileName, + // 'file_action' => $this->request->getPost('file_action'), + // 'status' => 'Yet to start', + // 'created_by' => $this->request->getPost('created_by'), + // 'updated_by' => $this->request->getPost('created_by'), + // ]; + + $client_name = $this->clientModel->where('id',$data['client_id'])->findAll()[0]['client_name'] ?? [] ; + + $account_manager_email = $this->clientRMModel->findAccountManagerEmail($data['client_id']); + + $templateName = "inception_addition"; + + $mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? []; + + if(empty($mailTemplate)){ + //empty mail template please update the template in database or create one..!! + log_message("error","Empty mail template for inception_addition please update the template in database or create one..!!"); + return ; + } + + $mailTemplate['mail_content'] = str_replace('%client_name%', $client_name ?? '', $mailTemplate['mail_content']); + $mailTemplate['mail_content'] = str_replace('%policy_no%', $data['policy_no'] ?? '', $mailTemplate['mail_content']); + + $from_mail = ""; + $to_mail = $account_manager_email; + $subject = $mailTemplate['subject']; + $message = $mailTemplate['content']; + $cc_string = ""; + $attachments = ""; + $reply_to = ""; + $bcc_string = ""; + + + MailHelper::send_email(['from_mail' => $from_mail, 'mail' => $to_mail, 'cc' => $cc_string, 'subject' => $subject, 'message' => $message, 'attachments' => $attachments, 'reply_to' => $reply_to, 'bcc' => $bcc_string]); + + } + public function updateHrFileUploadData() { try { diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 9b39f41f..bf508a48 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -2409,8 +2409,8 @@ class PolicyTransactionController extends BaseController // $client_name = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[3]); //clientname from excel - $endorsement_no = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[2]); //policy_end_date from excel - $endorsement_no = preg_replace( '/[\x{200B}\x{200C}\x{200D}\x{FEFF}\x{00A0}\x{200E}\x{200F}\x{202A}-\x{202E}]/u', '', $excel_row[2]); //policy_end_date from excel + $endorsement_no = preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $excel_row[2]??''); //policy_end_date from excel + $endorsement_no = preg_replace( '/[\x{200B}\x{200C}\x{200D}\x{FEFF}\x{00A0}\x{200E}\x{200F}\x{202A}-\x{202E}]/u', '', $excel_row[2]??''); //policy_end_date from excel // Kint::dump($excel_key,$policy_no,$endorsement_no,$policy_start_date,$policy_end_date);//die(); foreach ($source_data as $source_key => $source_row) { diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index a9629d73..7b5b4874 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -1829,24 +1829,35 @@ class PolicyTransactionModel extends Model // ============================== if ($date_type == 'statement_month' && $start_date != 0 && $end_date != 0) { $date_condition = " - AND insurer_statements.month >= '{$start_date}' - AND insurer_statements.month <= '{$end_date}' + AND insurer_statements_oq.month >= '{$start_date}' + AND insurer_statements_oq.month <= '{$end_date}' "; } // ============================== // BASE QUERY // ============================== - $builder = $this->db->table('policy_transaction') + + + // ============================== + // Without Statement Upload + // ============================== + + + $builder2 = $this->db->table('policy_transaction') ->select(" - policy_transaction.*, + policy_transaction.id AS id, + policy_transaction.endorsement_no AS endorsement_no, + policy_transaction.ref AS ref, DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, - DATE_FORMAT( - IF(policy_transaction.month IS NULL, - policy_transaction.policy_issue_date, - policy_transaction.month - ), '%b %Y' - ) AS policy_issue_month, + + DATE_FORMAT( policy_transaction.policy_issue_date,, '%b %Y') AS policy_issue_month, + + CASE + WHEN 1 = 1 THEN 'no statement uploaded' + ELSE 'statement uploaded' + END AS statement_uploaded , + CASE WHEN clients.client_type = 1 THEN 'Group' @@ -1898,89 +1909,15 @@ class PolicyTransactionModel extends Model -- ============================== -- SUBQUERY: Total IRDA Amount + -- Alias Agreed Amount ...!! -- ============================== - ROUND(( - SELECT ( - SUM(co_share_stmt_details.actual_bp_brokerage_amt) + - SUM(co_share_stmt_details.actual_tp_brokerage_amt) + - SUM(co_share_stmt_details.actual_tep_brokerage_amt) + - SUM(co_share_stmt_details.reward) - ) - FROM co_share_stmt_details - JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id - WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id - AND co_share_stmt_details.is_active = 1 - AND insurer_statements.is_active = 1 - {$date_condition} - ), 2) AS total_irda_amt, + ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt, -- Reward Only - ROUND(( - SELECT SUM(co_share_stmt_details.reward) - FROM co_share_stmt_details - JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id - WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id - AND co_share_stmt_details.is_active = 1 - AND insurer_statements.is_active = 1 - {$date_condition} - ), 2) AS reward, + ROUND(0, 2) AS reward, -- Billed Amount - ROUND(( - SELECT SUM( - COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.reward, 0) - ) - FROM co_share_stmt_details - JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id - JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id - WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id - AND co_share_stmt_details.is_active = 1 - AND pt_table.is_active = 1 - AND insurer_statements.is_active = 1 - AND insurer_statements.invoice_status IS NOT NULL - {$date_condition} - ), 2) AS billed_amt, - - -- Unbilled Amount - ROUND( - ( - ( - SELECT - SUM( - COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.reward, 0) - ) - FROM co_share_stmt_details - JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id - WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id - AND co_share_stmt_details.is_active = 1 - {$date_condition} - ) - - - ( - SELECT - SUM( - COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + - COALESCE(co_share_stmt_details.reward, 0) - ) - FROM co_share_stmt_details - JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id - JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id - WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id - AND co_share_stmt_details.is_active = 1 - AND pt_table.is_active = 1 - AND insurer_statements.is_active = 1 - AND insurer_statements.invoice_status IS NULL - {$date_condition} - ) - ), 2) AS unbilled_amt, + ROUND(0, 2) AS billed_amt, created_user.first_name AS user_name, @@ -2012,9 +1949,255 @@ class PolicyTransactionModel extends Model ->join('tpa_branch', 'policy_transaction.tpa_branch_id = tpa_branch.id', 'left') ->join('user_profiles AS sales_user', 'policy_transaction.sales_generated_by = sales_user.id', 'left') ->join('user_profiles AS service_user', 'policy_transaction.serviced_by = service_user.id', 'left') - ->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left') + ->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left') + ->where('policy_transaction.is_active', 1) - ->where('pt_co_share_details.is_active', 1); + ->where('pt_co_share_details.is_active', 1) + + // ======================================================== + // GROUP BY Insurer Statement Months + // ======================================================= + ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id '); + + // ============================== + // ROLE-BASED FILTERS + // ============================== + if ( + (!in_array(get_role_id(), [1, 5])) && + !( + in_array(MANAGEMENT_TEAM_ID, user_team()) || + in_array(FINANCE_TEAM_ID, user_team()) || + in_array(BUSINESS_TEAM_ID, user_team()) + ) + ) { + if (get_role_id() == 4 && in_array(POS_TEAM_ID, user_team())) { + $builder2->where('policy_transaction.created_by', get_session_userid()); + } + } + + // ============================== + // ADDITIONAL FILTERS + // ============================== + if (!empty($where)) { + log_message('info', 'Where condition: ' . json_encode($where)); + $builder2->where($where); + } + + if ($start_date != 0 && $end_date != 0 && $date_type != 0 && $date_type != 'statement_month') { + $startDate = date('Y-m-d 00:00:00', strtotime($start_date)); + $endDate = date('Y-m-d 23:59:59', strtotime($end_date)); + + $builder2->where("policy_transaction.{$date_type} >=", $startDate) + ->where("policy_transaction.{$date_type} <=", $endDate); + } + + + // ============================== + // FILTER BY IDs + // ============================== + if ($client_id != 0) $builder2->where('policy_transaction.client_id', $client_id); + if ($insurer_id != 0) $builder2->where('policy_transaction.insurer_id', $insurer_id); + if ($client_branch_id != 0) $builder2->where('policy_transaction.client_branch_id', $client_branch_id); + if ($insurer_branch_id != 0) $builder2->where('policy_transaction.insurer_branch_id', $insurer_branch_id); + if ($client_policy_id != 0) $builder2->where('policy_transaction.client_policy_id', $client_policy_id); + if ($user_id != 0) $builder2->where('policy_transaction.created_by', $user_id); + if ($policy_type_id != 0) $builder2->where('client_policy.policy_type_id', $policy_type_id); + if ($issuer != 0) $builder2->where('policy_transaction.issuer', $issuer); + + // ============================== + // DEFAULT 90-DAY FILTER + // ============================== + if ( + $client_id == 0 && + $insurer_id == 0 && + $policy_type_id == 0 && + $date_type == 0 && + $issuer == 0 + ) { + $fromDate = date('Y-m-d', strtotime('-90 days')); + $toDate = date('Y-m-d 23:59:59'); + + if (empty($where)) { + $builder2->where('policy_transaction.created_at >=', $fromDate) + ->where('policy_transaction.created_at <=', $toDate); + } + } + + + + + + + + + + + + + + + + + + + + + + // ============================== + // With Statement Upload + // ============================== + + $builder = $this->db->table('policy_transaction') + ->select(" + policy_transaction.id AS id, + policy_transaction.endorsement_no AS endorsement_no, + policy_transaction.ref AS ref, + DATE_FORMAT(policy_transaction.policy_issue_date, '%d %b %Y') AS policy_issue_date, + DATE_FORMAT( + IF(insurer_statements_oq.month IS NULL, + policy_transaction.policy_issue_date, + insurer_statements_oq.month + ), '%b %Y' + ) AS policy_issue_month, + + CASE + WHEN insurer_statements_oq.month IS NULL THEN 'no statement uploaded' + ELSE 'statement uploaded' + END AS statement_uploaded , + + + CASE + WHEN clients.client_type = 1 THEN 'Group' + WHEN clients.client_type = 2 THEN 'Retail' + ELSE '-' + END AS client_type, + + CASE + WHEN policy_transaction.revenue_type = 'NA' THEN 'Fresh' + ELSE 'Renewal' + END AS revenue_type, + + CASE + WHEN policy_transaction.action_type = 'inception' THEN 'Policy' + ELSE 'Endorsement' + END AS action_type, + + clients.client_name AS client_name, + clients.short_name AS client_short_name, + client_branch.branch_name AS client_branch_name, + client_branch.address1 AS client_address, + policy_type.policy_type, + policy_type.bap, + insurers.name AS insurer_name, + insurers.short_name AS insurer_short_name, + insurer_branch.branch_name AS insurer_branch_name, + insurer_branch.branch_code AS insurer_branch_code, + user_profiles.first_name AS user_name, + vehicle.vehicle_no, + tpa.name AS tpa_name, + pt_co_share_details.remark AS remarks, + pt_co_share_details.cop_amt AS bp_amt, + pt_co_share_details.exp_amt, + pt_co_share_details.id AS pt_id, + sales_user.first_name AS salse_person_name, + service_user.first_name AS service_person_name, + + ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS premium_wo_gst, + ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2) AS gst_amount, + ROUND( + ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) + + ROUND((ROUND(pt_co_share_details.cop_amt + pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) * 18 / 100), 2), + 2) AS total_premium, + + ROUND(pt_co_share_details.cotp_amt + pt_co_share_details.cotep_amt, 2) AS tp_or_ter, + DATEDIFF(policy_transaction.policy_end_date, CURDATE()) AS days, + (pt_co_share_details.agreed_tp_per + pt_co_share_details.agreed_tep_per) AS agreed_tp_or_ter_per, + pt_co_share_details.agreed_bp_per, + + -- ============================== + -- SUBQUERY: Total IRDA Amount + -- Alias Agreed Amount ...!! + -- ============================== + ROUND(pt_co_share_details.exp_amt, 2) AS total_irda_amt, + -- Reward Only + ROUND(( + SELECT SUM(co_share_stmt_details.reward) + FROM co_share_stmt_details + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND insurer_statements.is_active = 1 + AND insurer_statements.month = insurer_statements_oq.month + {$date_condition} + GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') + ), 2) AS reward, + + -- Billed Amount + ROUND(( + SELECT SUM( + COALESCE(co_share_stmt_details.actual_bp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tp_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.actual_tep_brokerage_amt, 0) + + COALESCE(co_share_stmt_details.reward, 0) + ) + FROM co_share_stmt_details + JOIN pt_co_share_details AS pt_table ON co_share_stmt_details.co_share_id = pt_table.id + JOIN insurer_statements ON co_share_stmt_details.statement_id = insurer_statements.id + WHERE co_share_stmt_details.co_share_id = pt_co_share_details.id + AND co_share_stmt_details.is_active = 1 + AND pt_table.is_active = 1 + AND insurer_statements.is_active = 1 + AND insurer_statements.invoice_status IS NOT NULL + AND insurer_statements.month = insurer_statements_oq.month + {$date_condition} + GROUP BY('policy_transaction.policy_no , insurer_statements.month , pt_co_share_details.insurer_id') + ), 2) AS billed_amt, + + created_user.first_name AS user_name, + + CASE + WHEN pt_co_share_details.co_share_type IN (0, 1) THEN policy_transaction.policy_no + WHEN pt_co_share_details.co_share_type > 1 THEN + CASE + WHEN pt_co_share_details.follower_policy_no IS NULL OR pt_co_share_details.follower_policy_no = '' + THEN policy_transaction.policy_no + ELSE pt_co_share_details.follower_policy_no + END + ELSE policy_transaction.policy_no + END AS policy_no + ") + + // ============================== + // JOINS + // ============================== + ->join('pt_co_share_details', 'policy_transaction.id = pt_co_share_details.pt_id', 'left') + ->join('clients', 'clients.id = policy_transaction.client_id') + ->join('client_branch', 'policy_transaction.client_branch_id = client_branch.id', 'left') + ->join('client_policy', 'policy_transaction.client_policy_id = client_policy.id', 'left') + ->join('user_profiles', 'policy_transaction.created_by = user_profiles.id', 'left') + ->join('vehicle', 'policy_transaction.vehicle_id = vehicle.id', 'left') + ->join('policy_type', 'policy_transaction.policy_type_id = policy_type.id', 'left') + ->join('insurers', 'pt_co_share_details.insurer_id = insurers.id', 'left') + ->join('insurer_branch', 'pt_co_share_details.insurer_branch_id = insurer_branch.id', 'left') + ->join('tpa', 'policy_transaction.tpa_id = tpa.id', 'left') + ->join('tpa_branch', 'policy_transaction.tpa_branch_id = tpa_branch.id', 'left') + ->join('user_profiles AS sales_user', 'policy_transaction.sales_generated_by = sales_user.id', 'left') + ->join('user_profiles AS service_user', 'policy_transaction.serviced_by = service_user.id', 'left') + ->join('user_profiles AS created_user', 'policy_transaction.created_by = created_user.id', 'left') + + + ->join('co_share_stmt_details', 'pt_co_share_details.id = co_share_stmt_details.co_share_id','left') + ->join('insurer_statements insurer_statements_oq','insurer_statements_oq.id = co_share_stmt_details.statement_id','left') + + ->where('policy_transaction.is_active', 1) + ->where('pt_co_share_details.is_active', 1) + ->where('insurer_statements_oq.id IS NOT NULL') + + + // ======================================================== + // GROUP BY Insurer Statement Months + // ======================================================= + ->groupBy('policy_transaction.policy_no , pt_co_share_details.insurer_id ,insurer_statements_oq.month'); // ============================== // ROLE-BASED FILTERS @@ -2092,17 +2275,22 @@ class PolicyTransactionModel extends Model } } - // ============================== - // ORDER & EXECUTION - // ============================== - $builder->orderBy('policy_transaction.id', 'desc'); - $result = $builder->get()->getResultArray(); - // Uncomment if you need to debug SQL - // dd($this->db->getLastQuery()); + + + $sql1 = $builder2->getCompiledSelect(); + $sql2 = $builder->getCompiledSelect(); + + $finalSql = "($sql1) UNION ALL ($sql2) + ORDER BY policy_no DESC, insurer_branch_name ASC, statement_uploaded ASC, + STR_TO_DATE(policy_issue_month, '%b %Y') ASC"; + + + $result = $this->db->query($finalSql)->getResultArray(); return $result; + } diff --git a/app/Views/report_bds.php b/app/Views/report_bds.php index 446f2ada..ff3eba14 100644 --- a/app/Views/report_bds.php +++ b/app/Views/report_bds.php @@ -113,8 +113,91 @@ table.dataTable tbody td {
- - $row){ ?> + + + + + $row){ + + $policy_no = $row['policy_no']; + + $policy_updated_month = $row['policy_issue_month']; + + $policy_type = $row['policy_type']; + + $statement_status = $row['statement_uploaded']; + + $client_name = $row['client_name']; + + $insurer_branch_name = $row['insurer_branch_name']; + + + + $uniqueRecord = "$policy_no-$policy_updated_month-$policy_type-$client_name-$insurer_branch_name" ; + + // irrespective of months or multiple months + $uniqueAgreedAmountPerInsurer = "$policy_no-$policy_type-$client_name-$insurer_branch_name" ; + + + + if(isset($uniquePoliciesReport[$uniqueRecord]) && $statement_status == "no statement uploaded" ){ + continue ; + } + else if(isset($uniquePoliciesReport[$uniqueRecord]) && $statement_status == "statement uploaded" ){ + unset($uniquePoliciesReport[$uniqueRecord]); + }else{ + + if(isset($uniqueAgreedAmountList[$uniqueAgreedAmountPerInsurer]) ){ + $uniqueAgreedAmountList[$uniqueAgreedAmountPerInsurer] -= $row['billed_amt']; + $row['total_irda_amt'] = 0; + $row['outstanding_amt'] = $uniqueAgreedAmountList[$uniqueAgreedAmountPerInsurer]; + $row['multiple_months'] = true; + } + else{ + $row['outstanding_amt'] = $row['total_irda_amt'] - $row['billed_amt']; + $uniqueAgreedAmountList[$uniqueAgreedAmountPerInsurer] = $row['total_irda_amt'] ; + $row['multiple_months'] = false; + } + + + + $uniquePoliciesReport[$uniqueRecord] = $row; + } + + + + + + + } + + + + + + + $report_list = array_values($uniquePoliciesReport); + + ?> + + + + $row){ + + ?>