From 03d800583f15ac890c78a9677a6944d84821d703 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Tue, 9 Sep 2025 17:01:26 +0530 Subject: [PATCH] GWM : Email functions for all creation and rejection --- app/Controllers/AgentAuthController.php | 7 +- app/Controllers/EnquiryController.php | 6 +- app/Controllers/PolicyController.php | 2 + app/Controllers/QuotationController.php | 11 +- app/Controllers/StaffAuthController.php | 7 +- app/Controllers/StaffController.php | 6 +- app/Helpers/mail_helper.php | 192 ++++++++++++++++++------ 7 files changed, 172 insertions(+), 59 deletions(-) diff --git a/app/Controllers/AgentAuthController.php b/app/Controllers/AgentAuthController.php index 6eedcd1..4b63215 100644 --- a/app/Controllers/AgentAuthController.php +++ b/app/Controllers/AgentAuthController.php @@ -61,11 +61,8 @@ class AgentAuthController extends ResourceController $update = $this->AgentModel->where('email', $email)->where('is_active', 1)->set(['email_otp' => $otp])->update(); if ($update) { - - $subject = 'Nhance user verification - OTP'; - $mail_content = $otp . ' is your verification code for Nhance.'; - - $EmailResult = send_login_otp( $email , $subject , $mail_content); + + $EmailResult = trigger_email('login_otp',['email'=>$email, 'name'=>$agentData['name'], 'otp'=>$otp, ]); if ($EmailResult['status'] == 'success') { $result = ['agent_verification' => true, 'message' => "Verified Successfully"]; diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index a878fc0..7824c81 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -159,7 +159,9 @@ class EnquiryController extends ResourceController 'created_by' => $data['created_by'] ]; - $this->enquiryModel->insert($insertData); + $enquiryId = $this->enquiryModel->insert($insertData , true); + + trigger_email('enquiry_created', ['enquiry_id' => $enquiryId]); return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry created successfully'], 200); @@ -264,6 +266,8 @@ class EnquiryController extends ResourceController $this->enquiryModel->update($id, $updateData); + trigger_email('enquiry_assigned', ['enquiry_id' => $id]); + return $this->respond(['status' => 'success', 'code' => 200, 'message' => 'Enquiry updated successfully'], 200); diff --git a/app/Controllers/PolicyController.php b/app/Controllers/PolicyController.php index 3e2d4ba..911a755 100644 --- a/app/Controllers/PolicyController.php +++ b/app/Controllers/PolicyController.php @@ -133,6 +133,8 @@ class PolicyController extends ResourceController } } + trigger_email('policy_created', ['policy_id' => $policyId]); + return $this->respond(['status' => 'success', 'code' => 200, 'data' => []], 200); } catch (\Exception $e) { diff --git a/app/Controllers/QuotationController.php b/app/Controllers/QuotationController.php index 13437aa..3b2bbba 100644 --- a/app/Controllers/QuotationController.php +++ b/app/Controllers/QuotationController.php @@ -79,7 +79,9 @@ class QuotationController extends ResourceController 'manager_id' => $data['manager_id'] ]; - $this->QuotationModel->insert($insertData); + $id = $this->QuotationModel->insert($insertData, true); + + trigger_email('quotation_created', ['quotation_id' => $id]); //update enquiry status $enquiryData = $this->EnquiryModel->where('id',$data['enquiry_id'])->where('status','Awaiting Quotation')->first(); @@ -171,6 +173,13 @@ class QuotationController extends ResourceController ->update(); } } + + if( $newStatus == 'Accepted') + { + trigger_email('quotation_accepted', ['quotation_id' => $id]); + }else{ + trigger_email('quotation_rejected', ['quotation_id' => $id]); + } diff --git a/app/Controllers/StaffAuthController.php b/app/Controllers/StaffAuthController.php index 3d2b73e..c95b2e7 100644 --- a/app/Controllers/StaffAuthController.php +++ b/app/Controllers/StaffAuthController.php @@ -61,11 +61,8 @@ class StaffAuthController extends ResourceController $update = $this->StaffModel->where('email', $email)->where('is_active', 1)->set(['email_otp' => $otp])->update(); if ($update) { - - $subject = 'Nhance user verification - OTP'; - $mail_content = $otp . ' is your verification code for Nhance.'; - - $EmailResult = send_login_otp( $email , $subject , $mail_content); + + $EmailResult = trigger_email('login_otp',['email'=>$email, 'name'=>$StaffData['name'], 'otp'=>$otp, ]); if ($EmailResult['status'] == 'success') { $result = ['Staff_verification' => true, 'message' => "Verified Successfully"]; diff --git a/app/Controllers/StaffController.php b/app/Controllers/StaffController.php index 6f7ae4f..06193c9 100644 --- a/app/Controllers/StaffController.php +++ b/app/Controllers/StaffController.php @@ -178,11 +178,11 @@ class StaffController extends ResourceController public function downloadManagerIncentiveFile() { try { - $file_name = $this->request->getGet('file_name'); + $id = $this->request->getGet('id'); // External API URL - $url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($file_name); + $url = "https://venbait.in/nhance/dev/user/download-incentive-file/" . urlencode($id); // Use CURL request to fetch the file $client = \Config\Services::curlrequest(); @@ -192,7 +192,7 @@ class StaffController extends ResourceController // Pass the file contents back as download return $this->response ->setHeader('Content-Type', $response->getHeaderLine('Content-Type')) - ->setHeader('Content-Disposition', 'attachment; filename="' . basename($file_name) . '"') + ->setHeader('Content-Disposition', 'attachment; id="' . basename($id) . '"') ->setBody($response->getBody()); } else { return $this->respond([ diff --git a/app/Helpers/mail_helper.php b/app/Helpers/mail_helper.php index c4a9c60..4914897 100644 --- a/app/Helpers/mail_helper.php +++ b/app/Helpers/mail_helper.php @@ -1,63 +1,167 @@ [ - "address" => $sender, - "name" => "Nhance-Partner" - ], - "to" => [ - ["email_address" => ["address" => $to_email]] - ], - "subject" => $subject, - "htmlbody" => $message, - ]; + $enquiryModel = new EnquiryModel(); + $quotationModel = new QuotationModel(); + $policyModel = new PolicyModel(); + $agentModel = new AgentModel(); + $staffModel = new StaffModel(); - + $to_email = ''; + $subject = ''; + $message = ''; - $headers = [ - "Content-Type: application/json", - "Authorization: Zoho-enczapikey " . $apiKey - ]; + switch ($action) { + case 'login_otp': + $to_email = $data['email']; + $subject = "Your Login OTP"; + $message = "

Hello {$data['name']},

Your OTP is {$data['otp']}

"; + break; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $endpoint); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + case 'enquiry_created': + $enquiryData = $enquiryModel->where(['id' => $data['enquiry_id'] ])->first(); + $agentData = $agentModel->where(['id' => $enquiryData['agent_id'] ])->first(); + $staffData = $staffModel->where(['id' => $enquiryData['manager_id'] , 'role_id' => 1 ])->first(); - $response = curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $to_email = $staffData['email']; + $subject = "New Enquiry Created"; + $message = "

A new enquiry has been created by {$agentData['name']}.

"; + break; - if (curl_errno($ch)) { - $error_msg = curl_error($ch); - log_message('error', "[EMAIL ERROR] Curl failed: {$error_msg}"); - return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $error_msg]; - } + case 'enquiry_assigned': + $enquiryData = $enquiryModel->where(['id' => $data['enquiry_id'] ])->first(); + $staffData = $staffModel->where(['id' => $enquiryData['assigned_to'] , 'role_id' => 2 ])->first(); - curl_close($ch); + $to_email = $staffData['email']; + $subject = "Enquiry Assigned"; + $message = "

Enquiry #{$data['enquiry_id']} assigned to {$staffData['name']}.

"; + break; - $resp = json_decode($response, true); + case 'quotation_created': + $quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first(); + $agentData = $agentModel->where(['id' => $quotationData['agent_id'] ])->first(); - if (isset($resp['data'][0]['message']) && $resp['data'][0]['message'] == 'Email request received') { - log_message('debug', 'OTP Send Success to agent , email = ' . $to_email); - return ['status' => 'success', 'code' => 200, 'message' => 'Send Success']; - } else { - log_message('error', "[OTP Send FAILED] , email = {$to_email}, HTTP: {$httpCode}, Response: {$response}"); - return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $response]; - } + $to_email = $agentData['email']; + $subject = "Quotation Created"; + $message = "

A new quotation #{$data['quotation_id']} has been created for you.

"; + break; + + case 'quotation_accepted': + $quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first(); + $staffData = $staffModel->where(['id' => $quotationData['assigned_to'] , 'role_id' => 2 ])->first(); + + $to_email = $staffData['email']; // assigned staff + $subject = "Quotation Accepted"; + $message = "

Quotation #{$data['quotation_id']} was accepted.

"; + break; + + case 'quotation_rejected': + $quotationData = $quotationModel->where(['id' => $data['quotation_id'] ])->first(); + $staffData = $staffModel->where(['id' => $quotationData['assigned_to'] , 'role_id' => 2 ])->first(); + + $to_email = $staffData['email']; // assigned staff + $subject = "Quotation Rejected"; + $message = "

Quotation #{$data['quotation_id']} was rejected.

"; + break; + + case 'policy_created': + $policyData = $policyModel->where(['id' => $data['policy_id'] ])->first(); + $agentData = $agentModel->where(['id' => $policyData['agent_id'] ])->first(); + + $to_email = $agentData['email']; + $subject = "Policy Created"; + $message = "

Policy #{$policyData['policy_number']} has been created successfully.

"; + break; + + default: + log_message('error', "[EMAIL ERROR] Unknown action '{$action}'"); + return ['status' => 'failed', 'code' => 400, 'message' => 'Unknown email action']; + } + + return send_email($to_email, $subject, $message); + + } catch (\Exception $e) { + log_message('error', "[EMAIL ERROR] Exception: " . $e->getMessage()); + return ['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()]; + } + } +} + + +if (!function_exists('send_email')) { + function send_email( $to_email , $subject , $message) + { + try { + // Set up ZeptoMail API request + $endpoint = "https://api.zeptomail.com/v1.1/email"; + $apiKey = env('ZOHO_API_KEY'); + $sender = env('ZOHO_SENDER_MAIL'); + + $payload = [ + "from" => [ + "address" => $sender, + "name" => "Nhance-Partner" + ], + "to" => [ + ["email_address" => ["address" => $to_email]] + ], + "subject" => $subject, + "htmlbody" => $message, + ]; + + + + $headers = [ + "Content-Type: application/json", + "Authorization: Zoho-enczapikey " . $apiKey + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $endpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_errno($ch)) { + $error_msg = curl_error($ch); + log_message('error', "[EMAIL ERROR] Curl failed: {$error_msg}"); + return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $error_msg]; + } + + curl_close($ch); + + $resp = json_decode($response, true); + + if (isset($resp['data'][0]['message']) && $resp['data'][0]['message'] == 'Email request received') { + log_message('debug', 'OTP Send Success to agent , email = ' . $to_email); + return ['status' => 'success', 'code' => 200, 'message' => 'Send Success']; + } else { + log_message('error', "[OTP Send FAILED] , email = {$to_email}, HTTP: {$httpCode}, Response: {$response}"); + return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $response]; + } } catch (\Exception $e) {