From 2021bea47850b1f0dc4fe689adb181dfba395fb4 Mon Sep 17 00:00:00 2001 From: Venkatesh Date: Tue, 7 Jul 2026 10:19:41 +0530 Subject: [PATCH] FIX_INSTALLMENT_REMINDER_MAIL --- .../PolicyTransactionController.php | 59 ++-- app/Models/BdsPlacementModel.php | 80 ++++-- ...ds_installment_reminder_email_template.php | 260 ++++++++++++++++++ 3 files changed, 358 insertions(+), 41 deletions(-) create mode 100644 app/Views/bds_installment_reminder_email_template.php diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 8c2436d7..7d144862 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -5431,7 +5431,7 @@ class PolicyTransactionController extends BaseController } $this->myLogger->logme("error", "Data for BDS Installment " . json_encode($bdsInstallmentData)); - // print_rr($bdsInstallmentData);die(); + // dd($bdsInstallmentData);die(); if (empty($bdsInstallmentData)) { $this->myLogger->logme("error", "No Client Installment is Due in the 5th Day"); @@ -5474,44 +5474,63 @@ class PolicyTransactionController extends BaseController try { - $installment_amount = $data['installment_amount']; - $payment_date = date('d-m-Y', strtotime($data['payment_date'])); - $client = $data['client_name']; - $branch = $data['branch_name']; - $sales_person_mail = $data['sales_person']; - $heads = $data['heads']; - $admins = $data['admins']; - $buisness_team = $data['buisness_team']; - $policy_no = $data['policy_no']; - $client_short_name = isset($data['short_name']) && $data['short_name'] != null ? $data['short_name'] : $data['client_name']; + helper('excel_util_helper'); - $subject = "Installment Amount Due For Client - {$client_short_name} - Policy NO({$policy_no}) is Due On - {$payment_date}"; + $installment_amount = (float) ($data['installment_amount'] ?? 0); + $payment_date = date('d-m-Y', strtotime($data['payment_date'])); + $client = $data['client_name']; + $branch = $data['branch_name']; + $sales_person_mail = $data['sales_person']; + $policy_no = trim((string) ($data['policy_no'] ?? '')); + $client_short_name = isset($data['short_name']) && $data['short_name'] != null ? $data['short_name'] : $data['client_name']; + $formatted_amount = '₹ ' . formatIndianCurrency(number_format($installment_amount, 2, '.', '')); + $is_overdue = strtotime($data['payment_date']) < strtotime(date('Y-m-d')); + $policy_label = $policy_no !== '' ? "Policy #{$policy_no}" : 'Policy Pending'; + $policy_period = ''; - $message = "Policy Installment for Client - {$client}, Branch - {$branch} is Due on {$payment_date} - with amount of {$installment_amount}.
- Policy No : {$policy_no}"; + if (!empty($data['policy_start_date']) && !empty($data['policy_end_date'])) { + $policy_period = date('d-m-Y', strtotime($data['policy_start_date'])) + . ' to ' + . date('d-m-Y', strtotime($data['policy_end_date'])); + } + $subject = $is_overdue + ? "Overdue Installment Reminder - {$client_short_name} | {$policy_label} | Due {$payment_date}" + : "Installment Payment Reminder - {$client_short_name} | {$policy_label} | Due {$payment_date}"; + + $message = view('bds_installment_reminder_email_template', [ + 'client_name' => $client, + 'branch_name' => $branch, + 'policy_no' => $policy_no !== '' ? $policy_no : 'Not Assigned', + 'policy_type' => $data['policy_type'] ?? '', + 'insurer_name' => $data['insurer_name'] ?? '', + 'policy_period' => $policy_period, + 'payment_date' => $payment_date, + 'installment_amount' => $formatted_amount, + 'is_overdue' => $is_overdue, + 'generated_on' => date('d-m-Y H:i'), + ]); $contactPersonEmail = trim((string) ($data['contact_person_email'] ?? '')); $to_mail = array_merge( - array_column($heads, 'email'), - array_column($admins, 'email'), - array_column($buisness_team, 'email'), + [$data['acm_email']], [$sales_person_mail], $contactPersonEmail !== '' ? [$contactPersonEmail] : [] ); - $to_mail = array_values(array_unique(array_filter($to_mail))); + $to_mail = array_values(array_unique(array_filter($to_mail, static function ($email) { + return is_string($email) && filter_var(trim($email), FILTER_VALIDATE_EMAIL); + }))); $this->myLogger->logme("error", "Selected To Address : " . json_encode($to_mail)); - // dd($to_mail); return [ 'to_mail' => $to_mail, 'message' => $message, 'subject' => $subject ]; + } catch (Exception $e) { $this->myLogger->logme('error', 'Exception: ' . $e->getMessage() . 'Page: ' . $e->getFile() . ' Line: ' . $e->getLine()); diff --git a/app/Models/BdsPlacementModel.php b/app/Models/BdsPlacementModel.php index 341e16a2..ff1fbc61 100644 --- a/app/Models/BdsPlacementModel.php +++ b/app/Models/BdsPlacementModel.php @@ -135,37 +135,56 @@ class BdsPlacementModel extends Model { /** @var BdsConfig $config */ $config = config(BdsConfig::class); - // print_r($config);die(); if (!$config->shouldFetchInstallmentRemindersToday()) { return []; } - $heads = $this->db->table('user_profiles') - ->select('email')->where(['role' => 5, 'is_active' => 1]) - ->get()->getResultArray(); + // $heads = $this->db->table('user_profiles') + // ->select('email')->where(['role' => 5, 'is_active' => 1]) + // ->get()->getResultArray(); - $admins = $this->db->table('user_profiles') - ->select('email')->where(['role' => 1, 'is_active' => 1]) - ->get()->getResultArray(); + // $admins = $this->db->table('user_profiles') + // ->select('email')->where(['role' => 1, 'is_active' => 1]) + // ->get()->getResultArray(); - $businessTeam = $this->db->table("user_teams ut") - ->select("up.email") - ->join('user_profiles up', 'up.id = ut.user_id AND up.is_active = 1') - ->where(["ut.team_id" => 7, "ut.is_active" => 1]) - ->get()->getResultArray(); + // $businessTeam = $this->db->table("user_teams ut") + // ->select("up.email") + // ->join('user_profiles up', 'up.id = ut.user_id AND up.is_active = 1') + // ->where(["ut.team_id" => 7, "ut.is_active" => 1]) + // ->get()->getResultArray(); $builder = $this->db->table("lead_installment_payment_details lipd") - ->select("lipd.*, COALESCE(ct.client_name, leads.client_name) as client_name, COALESCE(ct.short_name, leads.client_short_name) as short_name, COALESCE(cb.branch_name, leads.branch_name) as branch_name, leads.salse_person_id, leads.contact_person_email, cp.policy_no") + ->select(" + lipd.*, + COALESCE(ct.client_name, leads.client_name) as client_name, + COALESCE(ct.short_name, leads.client_short_name) as short_name, + COALESCE(cb.branch_name, leads.branch_name) as branch_name, + leads.salse_person_id, + leads.contact_person_email, + leads.contact_person_name, + leads.contact_person_mobile, + COALESCE(cp.policy_no, cp_src.policy_no) as policy_no, + COALESCE(cp.policy_start_date, cp_src.policy_start_date) as policy_start_date, + COALESCE(cp.policy_end_date, cp_src.policy_end_date) as policy_end_date, + COALESCE(ins.name, ins_src.name) as insurer_name, + COALESCE(pt.policy_type, pt_src.policy_type) as policy_type, + leads.acm_id, + leads.is_policy_created + ") ->join("leads", "leads.id = lipd.lead_id") ->join("clients ct", "ct.id = leads.client_id", "left") ->join("client_branch cb", "cb.id = leads.client_branch_id", "left") - ->join("client_policy cp", "cp.id = leads.source_policy_id", "left") + ->join("client_policy cp", "cp.id = leads.is_policy_created", "left") + ->join("client_policy cp_src", "cp_src.id = leads.source_policy_id", "left") + ->join("insurers ins", "ins.id = cp.insurer_id", "left") + ->join("insurers ins_src", "ins_src.id = cp_src.insurer_id", "left") + ->join("policy_type pt", "pt.id = cp.policy_type_id", "left") + ->join("policy_type pt_src", "pt_src.id = cp_src.policy_type_id", "left") ->where("lipd.is_active", 1) ->where("lipd.utr_no IS NULL OR lipd.utr_no = ''"); $targetPaymentDate = $this->addBusinessDays($config->installmentReminderBusinessDays); - // print_r($targetPaymentDate);die(); if ($config->installmentReminderFetchOverduePendingUtr) { $builder->groupStart() @@ -178,27 +197,46 @@ class BdsPlacementModel extends Model $data = $builder->get()->getResultArray(); - // print_r($this->db->getLastQuery()->getQuery());die(); - foreach ($data as &$row) { $sales_person_ids = json_decode($row['salse_person_id'], true); $sales_person_id = $sales_person_ids[0] ?? null; + $acm_id = $row['acm_id'] ?? null; + + if ($acm_id) { + $acm_user = $this->db->table('user_profiles') + ->select('email, first_name, last_name') + ->where('id', (int) $acm_id) + ->get() + ->getRowArray(); + + $row['acm_email'] = $acm_user['email'] ?? 'N/A'; + $row['acm_name'] = trim( + ($acm_user['first_name'] ?? '') . ' ' . ($acm_user['last_name'] ?? '') + ) ?: 'Not Assigned'; + } else { + $row['acm_email'] = 'N/A'; + $row['acm_name'] = 'Not Assigned'; + } if ($sales_person_id) { $user = $this->db->table('user_profiles') - ->select('email') + ->select('email, first_name, last_name') ->where('id', (int) $sales_person_id) ->get() ->getRowArray(); $row['sales_person'] = $user['email'] ?? 'N/A'; + $row['sales_person_name'] = trim( + ($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '') + ) ?: 'Not Assigned'; } else { $row['sales_person'] = 'Not Assigned'; + $row['sales_person_name'] = 'Not Assigned'; } - $row['heads'] = $heads; - $row['admins'] = $admins; - $row['buisness_team'] = $businessTeam; + // $row['heads'] = $heads; + // $row['admins'] = $admins; + // $row['buisness_team'] = $businessTeam; } return $data; diff --git a/app/Views/bds_installment_reminder_email_template.php b/app/Views/bds_installment_reminder_email_template.php new file mode 100644 index 00000000..edf9a406 --- /dev/null +++ b/app/Views/bds_installment_reminder_email_template.php @@ -0,0 +1,260 @@ + + + + + + + + +
+
+

Policy Installment Payment Reminder

+
Nhance India Insurance
+
+ +
+ +
+ Payment Overdue: The installment for was due on . Kindly arrange the payment at the earliest to keep the policy active. +
+ +
+ Payment Reminder: An installment payment for is due on . Please review the details below. +
+ + +

+ This is a reminder that a policy installment payment is for + ( branch). + Kindly process the payment on or before the due date to ensure uninterrupted policy coverage. +

+ +

+ Payment Details +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Organization
Branch
Policy Number
Policy Type
Insurance Company
Policy Period
Due Date
Amount Payable
Status + + + +
+ +
+

Important Instructions

+
    +
  • Please arrange the installment payment of on or before .
  • +
  • After making the payment, kindly share the payment reference / UTR details for confirmation and record update.
  • +
  • If the payment has already been made, please share the transaction details at the earliest.
  • +
  • For any clarification regarding the amount or due date, please coordinate with the concerned team.
  • +
+
+ +

+ Timely payment helps ensure continuous insurance coverage. Thank you for your cooperation. +

+
+ + +
+ +