From 9b049889d0ad319f397ddd2a8a6e25333b61e7e0 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Sat, 30 Aug 2025 16:56:52 +0530 Subject: [PATCH] FEAT_INSURER_REMAINDER_MAIL_CRONE_JOB_FUNCTION : RV --- app/Config/Routes.php | 3 + app/Controllers/LeadsController.php | 230 +++++++++++++++++++++++++++- 2 files changed, 226 insertions(+), 7 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0e4c1f27..7aaefbc2 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -467,6 +467,8 @@ $routes->cli('cli/new_gdrive_token', 'GoogleDriveController::generateNewGoogleDr $routes->cli('cli/enrollOpendAndClose', 'DashboardController::updatePolicyEnrollmentStatus'); $routes->cli("cli/sendCroneRemainderMail", "DashboardController::sendCroneRemainderMail"); $routes->cli('cli/update-emp-policy-status', 'ClientController::updateEmpAndPolicyStatus'); +$routes->cli('cli/update-emp-policy-status', 'ClientController::updateEmpAndPolicyStatus'); +$routes->cli('cli/insurerRFQRemainder', 'LeadsController::remainderForQcr'); //Employee login api's $routes->post("/employeeRest/verifyEmployeeNumber", "RestAuthenticationController::verifyEmployeeWithMobileNumber"); @@ -669,4 +671,5 @@ $routes->get("ticketHistoryList", "ThzController::ticketHistoryList"); //for testing $routes->group('test', function($routes) { $routes->post('api/saveForm', 'TestingController::saveForm'); + $routes->get('insurerRFQRemainder', 'LeadsController::remainderForQcr'); }); diff --git a/app/Controllers/LeadsController.php b/app/Controllers/LeadsController.php index 2b8bc53a..8c5efb1c 100644 --- a/app/Controllers/LeadsController.php +++ b/app/Controllers/LeadsController.php @@ -876,21 +876,40 @@ class LeadsController extends BaseController $qcr_created = true; } - if (($data['submit_type'] ?? "") == 'QCR') { + $quote_received_insurer = []; + if (($data['submit_type'] ?? "") === 'QCR') { + $inputJson = json_decode($data['json'], true); - $proposal_data = array_pop($inputJson); - if (isset($proposal_data['proposal_data']['over_all_column_data'])) { - $proposel_count = $proposal_data['proposal_data']['over_all_column_data']; + $proposal_data = $inputJson['proposal_data'] ?? array_pop($inputJson); + + if (isset($proposal_data['over_all_column_data'])) { + $proposel_count = $proposal_data['over_all_column_data']; + + $this->myLogger->logme("error", "insurer id : " . json_encode($proposel_count)); + foreach ($proposel_count as $key => $value) { if (!empty($value['insurers'])) { $qcr_created = true; + + foreach ($value['insurers'] as $insurer => $id) { + if (!empty($id['id'])) { + $quote_received_insurer[] = $id['id']; + } + } } } } } + $this->myLogger->logme("error", "quote_received_insurer : " . json_encode($quote_received_insurer)); $data['type'] = 1; + //update the quote received insurer data to the lead + if(count($quote_received_insurer) != 0){ + $this->leadsModel->update($lead_id, ['quote_received_insurer' => $quote_received_insurer]); + $this->myLogger->logme("error", "quote_received_insurer data updated in the lead: " . json_encode($quote_received_insurer)); + } + // Deactivate existing RFQs for this lead and type if (!isset($data['rfq_primaryKey']) && empty($data['rfq_primaryKey'])) { $this->RFQModel @@ -2573,16 +2592,20 @@ class LeadsController extends BaseController } $type = ""; + $insurer_ids = null; if (in_array($recipient_type, ['placement'])) { $type = "placement"; } else if (in_array($recipient_type, ['insurer'])) { $type = "insurer"; + $insurer_ids = json_decode($params['insurer_ids'] ?? "", true) ?? []; }else if (in_array($recipient_type, ['internal'])) { $type = "internal"; }else if (in_array($recipient_type, ['client'])) { $type = "client"; } + // print_r($insurer_ids); die; + if ($recipient_type === 'placement') { $lead_data = $this->leadsModel->find($lead_id); @@ -2777,10 +2800,14 @@ class LeadsController extends BaseController } // print_r($subject); die; - $common = ['module' => "leads", 'pk' => $lead_id, 'mail_type' => $type]; + $common = ['module' => "leads", 'pk' => $lead_id, 'mail_type' => $type, 'common' => null]; if ($recipient_data) { - foreach ($recipient_data as $recipient) { + foreach ($recipient_data as $position => $recipient) { + if ($recipient_type == 'insurer'){ + $common['common'] = $insurer_ids[$position] ?? null; + } + $message = $original_message; $message = str_replace("{{RECIPIENT_NAME}}", $recipient['name'] != "" ? $recipient['name'] : " ", $message); $message = str_replace("{{CLIENT_NAME}}", $lead_data['client_name'] != "" ? $lead_data['client_name'] : "", $message); @@ -4919,7 +4946,7 @@ class LeadsController extends BaseController // Start table with clean styling $html .= "

{$category}

"; - $html .= ""; + $html .= "
"; // Table header with light background $html .= ""; @@ -4946,4 +4973,193 @@ class LeadsController extends BaseController return $html; } + + // function for insurer remainder mail cron job + public function remainderForQcr() + { + $lead_data = $this->leadsModel + ->select( + 'leads.*, + user_profiles.email as created_person_email, + policy_type.policy_type, + policy_type.long_name + ') + ->join('policy_type', 'leads.policy_type_id = policy_type.id') + ->join('user_profiles', 'leads.created_by = user_profiles.id', 'left') + ->where('leads.is_active', 1) + ->where('user_profiles.is_active', 1) + ->where('leads.status', 'rfq_sent') + ->where('leads.next_reminder_date', date('Y-m-d')) + ->findAll(); + + // dd($lead_data); + + if (count($lead_data) == 0) { + $this->myLogger->logme("info", "No leads found for reminder mail at " . date('Y-m-d H:i:s')); + print_r(["status" => true, "message" => "There is no data to send reminder mail"]); + return ["status" => true, "message" => "There is no data to send reminder mail"]; + + } + + $from_mail = "im@nhanceindia.in"; + $subject = "Reminder for RFQ"; + $content = ` +

Dear Sir,

+

Greetings from Nhance India!

+

This is a gentle reminder regarding our earlier mail about the RFQ for the {{POLICY_TYPE}} policy pertaining to {{CLIENT_NAME}}.

+

We kindly request you to share the competitive quotes at the earliest possible convenience.

+

In case of any clarification, please feel free to contact us.

+

Thank you for your prompt attention to this matter.


+

Best regards,

+

Nhance India

+ + `; + + $reminder_sended_lead = []; + + foreach ($lead_data as $lead) { + + $leadId = $lead['id']; + $reply_to = $lead['created_person_email'] ?? ""; + + if (isset($lead['policy_end_date']) && $lead['policy_end_date'] != null) { + $subject = "{{CLIENT_NAME}} _ {{POLICY_TYPE}} _ {{RFQ_OR_QCR}} _ {{POLICY_YEAR}} {{POLICY_END_DATE}}"; + } else { + $subject = "{{CLIENT_NAME}} _ {{POLICY_TYPE}} _ {{RFQ_OR_QCR}} _ {{POLICY_YEAR}}"; + } + + $mail_content = $this->transformMailContent($lead, $content, "RFQ"); + $mail_subject = $this->transformMailContent($lead, $subject, "RFQ"); + + + $this->myLogger->logme("info", "Processing lead_id={$leadId} for reminder mails"); + + // get sales person + created_by emails + $sales_person_emails = $this->userModel + ->select('email') + ->where('is_active', 1) + ->whereIn('id', json_decode($lead['salse_person_id'] ?? "[]", true)) + ->findAll(); + + $email_list = array_column($sales_person_emails, 'email'); + if (!empty($reply_to)) { + $email_list[] = $reply_to; + } + $email_list = array_filter(array_unique($email_list)); + $email_string = implode(',', $email_list); + + // Get latest sent mail timestamp + $createdAtRow = $this->gmailSentHistoryModel + ->select("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as created_at", false) + ->where('isactive', 1) + ->where('mail_type', 'insurer') + ->where('pk', $leadId) + ->orderBy('id', 'desc') + ->first(); + + if (!$createdAtRow) { + $this->myLogger->logme("warning", "No sent history found for lead_id={$leadId}, skipping..."); + continue; + } + + $createdAt = $createdAtRow['created_at']; + + // Fetch mail data for that timestamp + $mail_datas = $this->gmailSentHistoryModel + ->where('isactive', 1) + ->where('mail_type', "insurer") + ->where('pk', $leadId) + ->where("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')", $createdAt) + ->findAll(); + + if (empty($mail_datas)) { + $this->myLogger->logme("warning", "No mail data found for lead_id={$leadId} at created_at={$createdAt}"); + continue; + } + + // Filter only pending insurers + $quote_received_insurer = json_decode($lead['quote_received_insurer'] ?? "[]", true); + $reminder_mail_data = []; + + if (empty($quote_received_insurer)) { + $reminder_mail_data = $mail_datas; + } else { + foreach ($mail_datas as $mail_value) { + $insurer_id = $mail_value['common'] ?? ""; + if (!in_array($insurer_id, $quote_received_insurer)) { + $reminder_mail_data[] = $mail_value; + } + } + } + + if (empty($reminder_mail_data)) { + $this->myLogger->logme("info", "All insurers already responded for lead_id={$leadId}, no reminder needed"); + continue; + } + + $this->myLogger->logme("info", "Sending " . count($reminder_mail_data) . " reminder mail(s) for lead_id={$leadId}"); + + $common = ['module' => "leads", 'pk' => $leadId, 'mail_type' => "remainder", 'common' => null]; + + foreach ($reminder_mail_data as $content) { + $insurerId = $content['common'] ?? null; + $common['common'] = $insurerId; + + try { + + if ($lead['is_insurer_auto_mail'] == 1) { + // send reminder to insurer + $res = MailHelper::send_email([ + 'from_mail' => $from_mail, + 'mail' => $content['mail'] ?? "", + 'cc' => json_decode($content['cc'] ?? "", true) ?? "", + 'subject' => $mail_subject ?? "", + 'message' => $mail_content ?? "", + 'reply_to' => $reply_to, + 'bcc' => json_decode($content["bcc"] ?? "", true) ?? "", + 'common' => $common + ]); + $this->myLogger->logme("info", "Reminder mail sent to the insurer | lead_id={$leadId}, insurer_id={$insurerId}, response=" . json_encode($res)); + } + + $reminder_sended_lead[] = [ + 'lead_id' => $leadId, + 'insurer_id' => $insurerId, + 'response' => $res + ]; + + } catch (\Throwable $e) { + $errorDetails = [ + 'error_message' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack_trace' => $e->getTraceAsString(), + ]; + $this->myLogger->logme("error", "Exception while sending reminder | lead_id={$leadId}, insurer_id={$insurerId}, error=" . json_encode($errorDetails)); + $reminder_sended_lead[] = [ + 'lead_id' => $leadId, + 'insurer_id' => $insurerId, + 'response' => ['status' => false, 'error' => $errorDetails] + ]; + } + + // send reminder to sales person / creator only + $internal_mail_response = MailHelper::send_email([ + 'mail' => $from_mail, + 'cc' => $email_string, + 'subject' => $mail_subject ?? "", + 'message' => $mail_content ?? "", + 'common' => $common + ]); + + $this->myLogger->logme("info", "Reminder mail sent to sales persons | lead_id={$leadId}, recipients={$email_string}, response=" . json_encode($internal_mail_response)); + + + } + } + + return ['status' => true, "data" => $reminder_sended_lead]; + } + + }