From bdce350f2b17f7934dc421b107f51409b51f3505 Mon Sep 17 00:00:00 2001 From: Venkatesh Date: Tue, 4 Aug 2026 15:44:03 +0530 Subject: [PATCH] FEAT_REJECTED_NOTIFICATION --- app/Controllers/ClientController.php | 15 ++ app/Controllers/EmployeeRestController.php | 130 ++++++++++++++++- app/Controllers/NotificationController.php | 1 + app/Views/notification.php | 162 ++++++++++++++++++++- 4 files changed, 300 insertions(+), 8 deletions(-) diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index f47dbc68..e5573f82 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -10049,6 +10049,7 @@ class ClientController extends AdminController $member_common_mail_template = $this->notificationModel->where('client_id', NULL)->where('template_name', 'member_common_mail')->get()->getResultArray()[0] ?? []; $hr_cd_insufficient_balance_mail_template = $this->notificationModel->where('client_id', NULL)->where('template_name', 'hr_cd_insufficient_balance_mail')->get()->getResultArray()[0] ?? []; + $dependent_rejection_mail_template = $this->notificationModel->where('client_id', NULL)->where('template_name', 'dependent_rejection_mail')->get()->getResultArray()[0] ?? []; $default_member_welcome_mail_template = [ 'client_id' => $client_id, @@ -10134,6 +10135,19 @@ class ClientController extends AdminController 'mail_content_copy' => null ]; + $default_dependent_rejection_mail_template = [ + 'client_id' => $client_id, + 'template_name' => 'dependent_rejection_mail', + 'subject' => $dependent_rejection_mail_template['subject'] ?? 'Dependent Rejected - {{client_name}}', + 'mail_content' => $dependent_rejection_mail_template['mail_content'] ?? '

Dear {{employee_name}},

The dependent below has been rejected.

Client{{client_name}}
Branch{{branch_name}}
Policy{{policy_no}}
Employee{{employee_name}}
Employee Code{{employee_code}}
Dependent Name{{dependent_name}}
Relationship{{relationship}}
Date of Birth{{dob}}
Effective Date{{effective_date}}
Reject Reason{{reject_reason}}
Processed By{{processed_by}}

Best regards,
Nhance Team

This is an auto-generated email. Please do not reply.

', + 'mail_content_json' => $dependent_rejection_mail_template['mail_content_json'] ?? '', + 'created_by' => get_session_userid(), + 'created_at' => date('Y-m-d H:i:s'), + 'updated_by' => null, + 'updated_at' => date('Y-m-d H:i:s'), + 'mail_content_copy' => null + ]; + $this->myLogger->logme('error', 'Default Mail Template Data ' . json_encode([ $default_member_welcome_mail_template, $default_member_remainder_mail_template, @@ -10149,6 +10163,7 @@ class ClientController extends AdminController $default_member_ecard_mail_template, $default_member_common_mail_template, $hr_cd_insufficient_balance_mail_template, + $default_dependent_rejection_mail_template, ]); diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 71b8bac3..01c6ceea 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -354,12 +354,21 @@ class EmployeeRestController extends AdminController if ($checkDataExist) { + $data = [ + 'basic_cover_si' => $basic_cover_si, + 'updated_by' => $self_data['id'] ?? null, + ]; + + if (!empty($dependent_effective_date)) { + $data['date_coverage'] = $this->convertDateFormatYMD($dependent_effective_date); + } + $this->employeePolicyModel ->where('client_policy_id', $client_policy_id) ->where('employee_id', $employee_id) ->where('is_active', 1) - ->set(['basic_cover_si' => $basic_cover_si, 'updated_by' => $self_data['id'] ?? null]) - ->update(); + ->set($data)->update(); + } else { $data['employee_id'] = $employee_id; @@ -6311,7 +6320,7 @@ class EmployeeRestController extends AdminController $downloadUrl = base_url('claim-form-download/' . md5((string) $clientPolicy['insurer_id'])); - log_message('info', '[getInsurerClaimFormDownloadUrl] Success | ' . json_encode([ + log_message('error', '[getInsurerClaimFormDownloadUrl] Success | ' . json_encode([ 'client_policy_id' => (int) $clientPolicyId, 'insurer_id' => (int) $clientPolicy['insurer_id'], 'file_source' => $claimFormAvailability['source'], @@ -6422,7 +6431,7 @@ class EmployeeRestController extends AdminController if ($Count > 0) { if (isset($data[0]->id)) { - $this->createEmployeePolicyData($data[0]->id, $data[0]->emp_code, $data[0]->client_id, $data[0]->client_policy_id, $data[0]->basic_cover_si); + $this->createEmployeePolicyData($data[0]->id, $data[0]->emp_code, $data[0]->client_id, $data[0]->client_policy_id, $data[0]->basic_cover_si, null, ['dependent_effective_date' => $data[0]->dependent_effective_date, 'hr_id' => $data[0]->hr_id ?? null]); } else { $payable_employee = $this->getEmployeePayableValue($data[0]->client_policy_id, $data[0]->relationship); $this->createEmployeePolicyData($employee, $data[0]->emp_code, $data[0]->client_id, $data[0]->client_policy_id, $data[0]->basic_cover_si, $payable_employee, ['dependent_effective_date' => $data[0]->dependent_effective_date, 'hr_id' => $data[0]->hr_id ?? null]); @@ -6523,7 +6532,7 @@ class EmployeeRestController extends AdminController }))); if (empty($recipientEmails)) { - log_message('info', '[DependentAddMail] No recipient emails found for client_id=' . $clientId . ', branch_id=' . $branchId); + log_message('error', '[DependentAddMail] No recipient emails found for client_id=' . $clientId . ', branch_id=' . $branchId); return; } @@ -6570,14 +6579,112 @@ class EmployeeRestController extends AdminController ]; $mailResult = MailHelper::send_email($mailPayload); - $this->myLogger->logme('info', '[DependentAddMail] mail_result=' . json_encode($mailResult)); - + $this->myLogger->logme('error', '[DependentAddMail] mail_result=' . json_encode($mailResult)); + } catch (\Throwable $th) { log_message('error', '[DependentAddMail] ' . $th->getMessage() . ' in ' . $th->getFile() . ':' . $th->getLine()); $this->myLogger->logme('error', '[DependentAddMail] ' . $th->getMessage()); } } + /** + * Send dependent rejection notification mail using client notification template + * only when the template is enabled for that client. + */ + private function sendDependentRejectedNotificationMail(array $employee, array $employeePolicy, string $rejectReason, string $processedByRole): void + { + try { + $clientId = (int) ($employee['client_id'] ?? 0); + $branchId = (int) ($employee['client_branch_id'] ?? 0); + $clientPolicyId = (int) ($employeePolicy['client_policy_id'] ?? 0); + $empCode = (string) ($employee['emp_code'] ?? ''); + $dependentName = (string) ($employee['name'] ?? ''); + + if ($clientId <= 0 || $branchId <= 0 || $clientPolicyId <= 0) { + log_message('error', '[DependentRejectMail] Skipped - invalid IDs | client_id=' . $clientId . ', branch_id=' . $branchId . ', client_policy_id=' . $clientPolicyId . ', emp_code=' . $empCode); + return; + } + + $notificationData = $this->notificationModel + ->where('client_id', $clientId) + ->where('template_name', 'dependent_rejection_mail') + ->where('enabled', 1) + ->first(); + + if (empty($notificationData) || empty($notificationData['subject']) || empty($notificationData['mail_content'])) { + log_message('error', '[DependentRejectMail] Skipped - template disabled or missing | client_id=' . $clientId . ', emp_code=' . $empCode . ', dependent=' . $dependentName); + return; + } + + $clientData = $this->clientModel + ->select('client_name, common_mails') + ->where('id', $clientId) + ->first(); + + $branchData = $this->clientBranchModel + ->select('branch_name') + ->where('id', $branchId) + ->first(); + + $policyData = $this->clientPolicyModel + ->select('policy_no') + ->where('id', $clientPolicyId) + ->first(); + + $selfEmployee = $this->employeeModel + ->select('name, email_corporate, email_personal') + ->where('client_id', $clientId) + ->where('emp_code', (string) ($employee['emp_code'] ?? '')) + ->where('relationship', 'Self') + ->where('is_active', 1) + ->first(); + + $selfEmployeeEmail = trim((string) ($selfEmployee['email_corporate'] ?? $selfEmployee['email_personal'] ?? '')); + if (! filter_var($selfEmployeeEmail, FILTER_VALIDATE_EMAIL)) { + log_message('error', '[DependentRejectMail] Skipped - invalid self employee email | client_id=' . $clientId . ', emp_code=' . $empCode . ', dependent=' . $dependentName); + return; + } + + $clientCommonMails = trim((string) ($clientData['common_mails'] ?? '')); + + $placeholders = [ + '{{client_name}}' => (string) ($clientData['client_name'] ?? ''), + '{{branch_name}}' => (string) ($branchData['branch_name'] ?? ''), + '{{policy_no}}' => (string) ($policyData['policy_no'] ?? ''), + '{{employee_name}}' => (string) ($selfEmployee['name'] ?? ''), + '{{employee_code}}' => (string) ($employee['emp_code'] ?? ''), + '{{dependent_name}}'=> (string) ($employee['name'] ?? ''), + '{{relationship}}' => (string) ($employee['relationship'] ?? ''), + '{{dob}}' => ! empty($employee['dob']) ? date('d/m/Y', strtotime((string) $employee['dob'])) : '-', + '{{effective_date}}'=> ! empty($employeePolicy['date_coverage']) ? date('d/m/Y', strtotime((string) $employeePolicy['date_coverage'])) : '-', + '{{reject_reason}}' => $rejectReason !== '' ? $rejectReason : '-', + '{{processed_by}}' => $processedByRole, + ]; + + $subject = (string) $notificationData['subject']; + $mailContent = (string) $notificationData['mail_content']; + foreach ($placeholders as $key => $value) { + $subject = str_ireplace($key, $value, $subject); + $mailContent = str_ireplace($key, $value, $mailContent); + } + + log_message('error', '[DependentRejectMail] Sending mail | client_id=' . $clientId . ', emp_code=' . $empCode . ', dependent=' . $dependentName . ', to=' . $selfEmployeeEmail . ', bcc=' . ($clientCommonMails !== '' ? $clientCommonMails : '-')); + + $mailResult = MailHelper::send_email([ + 'mail' => $selfEmployeeEmail, + 'subject' => $subject, + 'message' => $mailContent, + 'bcc' => $clientCommonMails, + ]); + + log_message('error', '[DependentRejectMail] mail_result=' . json_encode($mailResult)); + $this->myLogger->logme('error', '[DependentRejectMail] mail_result=' . json_encode($mailResult)); + } catch (\Throwable $th) { + log_message('error', '[DependentRejectMail] ' . $th->getMessage() . ' in ' . $th->getFile() . ':' . $th->getLine()); + $this->myLogger->logme('error', '[DependentRejectMail] ' . $th->getMessage()); + } + } + public function deleteDependencev2() { try { @@ -7002,6 +7109,15 @@ class EmployeeRestController extends AdminController return $this->respond(['status' => 'failed', 'code' => 500, 'data' => 'Failed to update employee policy status'], 200); } + if ($status === 'rejected') { + $this->sendDependentRejectedNotificationMail( + employee: $employee, + employeePolicy: $employee_policy, + rejectReason: $reject_reason, + processedByRole: $processed_by_role + ); + } + return $this->respond([ 'status' => 'success', 'code' => 200, diff --git a/app/Controllers/NotificationController.php b/app/Controllers/NotificationController.php index 0df383ef..8f532640 100755 --- a/app/Controllers/NotificationController.php +++ b/app/Controllers/NotificationController.php @@ -96,6 +96,7 @@ class NotificationController extends AdminController 'account_maneger_summary_mail_btn', 'client_hr_summary_mail_btn', 'hr_cd_insufficient_balance_mail_btn', + 'dependent_rejection_mail_btn', ]; $data = []; $emptyTemplate = []; diff --git a/app/Views/notification.php b/app/Views/notification.php index 8dd20c09..7ff82b3b 100755 --- a/app/Views/notification.php +++ b/app/Views/notification.php @@ -132,6 +132,7 @@ $account_maneger_summary_mail = 0; $client_hr_summary_mail = 0; $hr_cd_insufficient_balance_mail = 0; + $dependent_rejection_mail = 0; if (isset($notification)) { foreach ($notification as $value) { @@ -167,6 +168,9 @@ if ($value['template_name'] == 'hr_cd_insufficient_balance_mail') { $hr_cd_insufficient_balance_mail = $value['enabled']; } + if ($value['template_name'] == 'dependent_rejection_mail') { + $dependent_rejection_mail = $value['enabled']; + } } } ?>

@@ -349,6 +353,23 @@ + +
+
+
+ + Dependent Rejection Mail +
+
+ + + +
+
+
@@ -1016,6 +1037,92 @@
+ + +