diff --git a/.env b/.env index fb60f3b2..e5bfe125 100644 --- a/.env +++ b/.env @@ -152,7 +152,7 @@ WAAI_INSTANCE = '656876690A9FD' #-------------------------------------------------------------------- # Email Host Name,User,Passcode And Port #-------------------------------------------------------------------- -SMTP_Host = 'smtp.sendgrid.net' -SMTP_User = 'apikey' -SMTP_Pass = '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' -SMTP_Port = 587 +SMTP_HOST = 'smtp.sendgrid.net' +SMTP_USER = 'apikey' +SMTP_PASS = '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' +SMTP_PORT = '587' diff --git a/app/Config/Constants.php b/app/Config/Constants.php index f26c5517..153861d9 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -130,11 +130,12 @@ define('order_child_column', ["invoice_id","wp_api_line_items_id","customer_id"] define('WAAI_TOKEN', getenv('WAAI_TOKEN')); define('WAAI_INSTANCE', getenv('WAAI_INSTANCE')); -define('SMTP_Host', getenv('SMTP_Host')); -define('SMTP_User', getenv('SMTP_User')); -define('SMTP_Pass', getenv('SMTP_Pass')); -define('SMTP_Port', getenv('SMTP_Port')); +define('SMTP_HOST', getenv('SMTP_HOST')); +define('SMTP_USER', getenv('SMTP_USER')); +define('SMTP_PASS', getenv('SMTP_PASS')); +define('SMTP_PORT', getenv('SMTP_PORT')); ## GET Constant Based on Env File Ends... +define('DUMMY_VAR', $_ENV['WAAI_TOKEN']); define('SEND_WAAI_URL', 'https://waai.in/api/send'); define('SEND_WAAI_GROUP_URL', 'https://waai.in/api/send_group'); diff --git a/app/Config/Email.php b/app/Config/Email.php index ad655dff..5cb4e15a 100644 --- a/app/Config/Email.php +++ b/app/Config/Email.php @@ -28,22 +28,22 @@ class Email extends BaseConfig /** * SMTP Server Address */ - public string $SMTPHost = SMTP_Host; + public string $SMTPHost = 'smtp.sendgrid.net'; /** * SMTP Username */ - public string $SMTPUser = SMTP_User; + public string $SMTPUser = 'apikey'; /** * SMTP Password */ - public string $SMTPPass = SMTP_Pass; + public string $SMTPPass = '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'; /** * SMTP Port */ - public int $SMTPPort = SMTP_Port; + public int $SMTPPort = 587; /** * SMTP Timeout (in seconds) diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index 3fef3672..ef9e8abf 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -65,7 +65,6 @@ class Customer extends BaseController // Retrieve subscriber data based on the customer ID $subscriberData = $customerModel->getSubscriberDataByCustomerId($id); $data['subscriptionData'] = $subscriberData; - // Fetch invoice data for the customer $CustomerModel = new CustomerModel(); $invoices = $CustomerModel->getInvoicesByCustomerId($id); @@ -483,7 +482,7 @@ class Customer extends BaseController // Now you can use $whereCondition in your SQL query $db = \Config\Database::connect(); // $sql = "SELECT customer_id,CONCAT(first_name,'',last_name) as customer_name,email,mobile_no FROM customers WHERE ".$whereCondition; - $sql = "SELECT CA.city,CA.state,I.invoice_date,C.customer_id,CONCAT(C.first_name,' ',C.last_name) as customer_name,C.email,C.mobile_no ,S.to_subscription as due_date, C.isactive ,count(I.invoice_id) as invoice_count_raised_by_customer, S.scheme_id , CM.name as scheme_name ,CM.name, + $sql = "SELECT CA.city,CA.state,I.invoice_date,C.customer_id,CONCAT(C.first_name,' ',C.last_name) as customer_name,C.email,C.mobile_no ,S.to_subscription as due_date, C.isactive ,count(I.invoice_id) as invoice_count_raised_by_customer, S.scheme_id , BK.title as scheme_name ,CM.name as scheme_type_name, DATE_FORMAT(S.to_subscription, '%d/%m/%Y') AS formatted_due_date, DATE_FORMAT(I.invoice_date, '%d/%m/%Y') AS formatted_invoice_date FROM customers as C @@ -491,6 +490,7 @@ class Customer extends BaseController LEFT JOIN customer_addresses CA on CA.customer_id = C.customer_id and CA.address_type = 1 and CA.isactive = 1 LEFT JOIN subscription S on S.customer_id = C.customer_id LEFT JOIN book_categories BC on BC.book_id = S.scheme_id + LEFT JOIN books BK on BK.book_id = S.scheme_id LEFT JOIN category CM on CM.id = BC.category_id WHERE C.isactive = 1 AND ".$whereCondition." GROUP BY C.customer_id"; $this->logger->info("Customer Group: SQL = ".$sql); diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index f815ceda..1890f137 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -21,11 +21,12 @@ class Home extends BaseController } else { $where = []; } - $model = new HomeModel(); + $model = new HomeModel(); $data['customer'] = $model->customers($where); $data['customer']['label'] = "Customer"; $data['invoice'] = $model->invoice(); - $data['active_category'] = $model->schemes(); + // $data['active_category'] = $model->schemes(); + $data['active_schemes'] = $model->active_schemes(); $data['expiring_membership'] = $model->expiring_membership_list(); // $data['active_category'] = array_filter($model->schemes(), function ($element) { // return $element['count'] !== '0' ; diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 78fd1ccd..beb96774 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -536,8 +536,8 @@ class Invoice extends BaseController $params->number = (int)'91' . $recipient_mobile; $params->type = "text"; $params->message = $template; - $params->instance_id = WAAI_INSTANCE; - $params->access_token = WAAI_TOKEN; + $params->instance_id = $_ENV['WAAI_INSTANCE']; + $params->access_token = $_ENV['WAAI_TOKEN']; $this->logger->info("Invoice: approve notification Email Request data = ".json_encode($records)); $email_result = $notification->sendEmail($records); $this->logger->info("Invoice: approve notification Email Response = " . json_encode($email_result)); diff --git a/app/Controllers/Notifications.php b/app/Controllers/Notifications.php index a09a7921..ef4fca89 100755 --- a/app/Controllers/Notifications.php +++ b/app/Controllers/Notifications.php @@ -372,104 +372,134 @@ class Notifications extends BaseController return redirect()->route('campaign_creation'); } - public function getExpCustomerDetail($window_time = null) - { - // Step 1: add the days to retrive th e data// + public function getExpCustomerDetail($window_time = null) + { + // Step 1: add the days to retrive the data// $providedDate = date('Y-m-d'); $newDate = date('Y-m-d', strtotime($providedDate . ' + ' . $window_time . ' days')); - - // Step 2 :Get the custoemr detail// - $NotificationModel = new NotificationModel(); - $ExpCustomerDetail = $NotificationModel->getExpDate(); // Retrieve the stored query - if (empty($ExpCustomerDetail)) { - - $this->logger->info('No customer group found by the name of EXPIRYDATE'); + + // Step 2 :Get the customer detail// + $NotificationModel = new NotificationModel(); + $ExpCustomerDetail = $NotificationModel->getExpDate(); // Retrieve the stored query + if (empty($ExpCustomerDetail)) { + $this->logger->info('No customer group found by the name of EXPIRYDATE'); return; } - //Step 3: Replace the placeholder WINDOW_TIME with the user-provided value - $modifiedQuery = str_replace('WINDOW_TIME', $window_time, $ExpCustomerDetail); - // print_r($modifiedQuery);die; - if (empty($modifiedQuery)) { - - $this->logger->info('There is no sub query'); + + //Step 3: Replace the placeholder WINDOW_TIME with the user-provided value + $modifiedQuery = str_replace('WINDOW_TIME', $window_time, $ExpCustomerDetail); + + if (empty($modifiedQuery)) { + $this->logger->info('There is no sub query'); return; } - $db = \Config\Database::connect(); - $queryResult = $db->query($modifiedQuery)->getResultArray(); - $notification = new NotificationHelper(); - if (empty($queryResult)) { - - $this->logger->info("There is no customer expiring on date:{$newDate}"); - return; + $db = \Config\Database::connect(); + $queryResult = $db->query($modifiedQuery)->getResultArray(); + $notification = new NotificationHelper(); + if (empty($queryResult)) { + $this->logger->info("There is no customer expiring on date:{$newDate}"); + return; } - foreach ($queryResult as $row) { - $templateName = $NotificationModel->getTempName(); - if (empty($templateName)) { - - $this->logger->info('There is no template found by the name of EXPIRAY_NOTIFY_TEMPLATE'); - return; + foreach ($queryResult as $row) { + $userFirstName = $row['first_name']; + $userLastName = $row['last_name']; + $fullName = $userFirstName . ' ' . $userLastName; + $userMobileNumber = $row['mobile_no']; + $schemename = $row['title']; + $expirydate = $row['to_subscription']; + if (empty($fullName)) { + $this->logger->info('There is no subcription Name'); + continue; } - $userFirstName = $row['first_name']; - $userLastName = $row['last_name']; - $fullName = $userFirstName . ' ' . $userLastName; - - $schemename = $row['title']; - $expirydate = $row['to_subscription']; - if (empty($fullName)) { - - $this->logger->info('There is no subcription Name'); - return; - } - //Step 4: Prepare email content based on the template - $emailContent = $templateName[0]['message']; - $emailContent = str_replace("{USER_NAME}", $fullName, $emailContent); - $emailContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent); - $emailContent = str_replace("{EXPIRY_DATE}", $expirydate, $emailContent); - - - $emailData = [ - 'template_name'=>'', - 'recipient_email' => $row['email'], - 'subject' => 'Your Subscription Expiry Notification', - - 'description' =>$emailContent, - ]; + $emailTemplateName = $NotificationModel->getTempName(EXPIRAY_NOTIFY_TEMPLATE_EMAIL); + if(!empty($emailTemplateName)){ + //Step 4: Prepare email content based on the template + $emailContent = $emailTemplateName[0]['message']; + $emailContent = str_replace("{USER_NAME}", $fullName, $emailContent); + $emailContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent); + $emailContent = str_replace("{EXPIRY_DATE}", $expirydate, $emailContent); - // Step 5:Send email using NotificationHelper - $emailResult = $notification->sendEmail($emailData); - - - if (isset($emailResult['success'])) { - echo 'Email sent successfully'; - } else { - echo 'Failed to send email'; - - } - $templateName = $NotificationModel->getTempName(); - } - } + $emailData = [ + 'template_name' => '', + 'recipient_email' => $row['email'], + 'subject' => 'Your Subscription Expiry Notification', + 'description' => $emailContent, + ]; + + // Step 5:Send email using NotificationHelper + $emailResult = $notification->sendEmail($emailData); + + if (isset($emailResult['success'])) { + echo 'Email sent successfully'; + } else { + echo 'Failed to send email'; + } + }else{ + $this->logger->info('There is no email template found by the name of EXPIRAY_NOTIFY_TEMPLATE_EMAIL'); + continue; + }// $emailTemplateName + + $whatsappTemplateName = $NotificationModel->getTempName(EXPIRAY_NOTIFY_TEMPLATE_WHATSAPP); + if(!empty($whatsappTemplateName)){ + if (empty($userMobileNumber)) { + $this->logger->info('There is no subscriber mobile number'); + continue; + } + //Step 6: Prepare whatsapp content based on the template + $whatsappContent = $whatsappTemplateName[0]['message']; + $whatsappContent = str_replace("{USER_NAME}", $fullName, $emailContent); + $whatsappContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent); + $whatsappContent = str_replace("{EXPIRY_DATE}", $expirydate, $emailContent); + + // Step 7: Send email using NotificationHelper + $params = (object) Null; + $url = SEND_WAAI_URL; + $params->number = (int)'91' . $userMobileNumber; + $params->type = "text"; + $params->message = $whatsappContent; + $params->instance_id = $_ENV['WAAI_INSTANCE']; + $params->access_token = $_ENV['WAAI_TOKEN']; + $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request = " . json_encode($params)); + $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request type b4 = " . gettype($params)); + $whatsapp_result = $notification->sendWhatsAppMessage($url, "POST", $params); + $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Reponse = " .$whatsapp_result); + }else{ + $this->logger->info('There is no Whatsapp template found by the name of EXPIRAY NOTIFY TEMPLATE WHATSAPP '); + continue; + }// $Whatsapp Template Name + }// for loop + } public function auto_scheduled_notification() { $this->logger->info("Auto Scheduled Notification Started"); + $nc_id = ""; $model = new NotificationModel(); $model->setTable('notification_campaign'); $where = ['status'=>0]; $notification_campaign_result = $model->where($where)->findAll(); $today = date('Y-m-d'); $customer_details = array(); + $customer_details_template = array(); if (!empty($notification_campaign_result)) { foreach ($notification_campaign_result as $n => $ncr) { if($today == $ncr['scheduled_date']){ + // echo "
";print_r($ncr);echo "";die; //working fine. + $nc_id = $ncr['campaign_id']; $this->logger->info("Step 1 : (Campaign) Scheduled Date = " .$ncr['scheduled_date']."/".$ncr['scheduled_time'].", Today = ".$today ); - $customer_details = $this->auto_scheduled_template_details($ncr); + $customer_details_template = $this->auto_scheduled_template_details($ncr); }else{ $this->logger->info("Step 1 : (Campaign) Scheduled Date = " .$ncr['scheduled_date'].", Today = ".$today ); } } - if(!empty($customer_details)){ - $customer_details = $this->auto_scheduled_send_message($customer_details); + if(!empty($customer_details_template)){ + // echo "
";print_r($customer_details_template);echo "";die; + $customer_details = $this->auto_scheduled_send_message($customer_details_template); + if(!empty($customer_details)){ + $this->auto_scheduled_notification_campaign_status($nc_id); + $nc_id = ""; + } } }else{ $this->logger->info("Step 1 : Campaign details not found "); @@ -486,12 +516,15 @@ class Notifications extends BaseController $model->setTable('templates'); $where = ['template_id'=>$template_id]; $template_result = $model->where($where)->findAll(); + // echo "
";print_r($template_result);echo "";die; $flattened_array = array(); // its have customer Details... if (!empty($template_result)) { $group_details = array(); foreach ($template_result as $t => $tr) { $this->logger->info("Step 2 : (Template) id = ".$tr['template_id']."/".$tr['template_name']." , Template Mode = ".$tr['mode']." NotifyCampian Mode = ".$nc_mode); $details = $this->auto_scheduled_customer_group_details($tr,$ncr); + // echo "
";print_r($details);echo "";die; + if(!empty($details)){ foreach ($details as $d => $dr) { $group_details[] = $dr['group_query_results']; @@ -504,7 +537,7 @@ class Notifications extends BaseController } } } - + // echo "
";print_r($flattened_array);echo "";die; // Don't Delete It Duplication Check 07-12-2023 // // Extract customer IDs using array_column // $customer_ids = array_column($flattened_array, 'customer_id'); @@ -522,6 +555,7 @@ class Notifications extends BaseController // } // // Now $uniqueData contains entries with unique customer IDs // print_r($unique_data); + // echo "
";print_r($flattened_array);echo "";die; } }else{ $this->logger->info("Step 2 : Template Details details not found"); @@ -531,6 +565,7 @@ class Notifications extends BaseController } public function auto_scheduled_customer_group_details($tr_object,$ncr_object){ + $this->logger->info("To Retrieved Customer Group Details for Auto Scheduled"); $group_id = $ncr_object['group_id']; $nc_mode = $ncr_object['mode']; @@ -545,6 +580,7 @@ class Notifications extends BaseController if (!empty($group_arr_result)) { foreach ($group_arr_result as $g => $gar) { + $group_details[$g]['group_query'] = $gar['group_query']; $group_details[$g]['group_id'] = $gar['group_id']; $db = \Config\Database::connect(); @@ -557,6 +593,7 @@ class Notifications extends BaseController $row['nc_mode'] = $nc_mode; // mode from notification_campaign Table Cross Check purpose $row['t_mode'] = $t_mode; // mode from template Table Cross Check purpose $row['template_message'] = $tr_object['message']; + $row['template_subject'] = $tr_object['subject']; $row['campaign_id'] = $ncr_object['campaign_id']; } $group_details[$g]['group_query_results'] = $results; @@ -578,49 +615,134 @@ class Notifications extends BaseController public function auto_scheduled_send_message($customer_details){ $this->logger->info("To Send Message on Customer for Auto Scheduled"); + $return_statement = array(); if (!empty($customer_details)) { foreach($customer_details as $c => $cd) { + switch($cd['nc_mode']) { case "Whatsapp": - $this->auto_scheduled_whatsapp($cd); + $whatsapp = $this->auto_scheduled_whatsapp($cd); + array_push($return_statement, $whatsapp); break; case "Email": - $this->auto_scheduled_email($cd); + $email = $this->auto_scheduled_email($cd); + array_push($return_statement, $email); break; default: - $this->logger->error("Can't able to identity the mode for Auto Scheduled"); + $this->logger->error($cd["customer_name"]." Can't able to identity the mode for Auto Scheduled"); + array_push($return_statement, $cd["customer_name"]." Can't able to identity the mode for Auto Scheduled"); } } } + return $return_statement; } - public function auto_scheduled_whatsapp($cd){ - // $cd["city"] - // $cd["state"] - // $cd["invoice_date"] - // $cd["customer_id"] - echo $cd["customer_name"]; - // $cd["email"] - // $cd["mobile_no"] - // $cd["due_date"] - // $cd["isactive"] - // $cd["invoice_count_raised_by_customer"] - // $cd["scheme_id"] - // $cd["name"] - // $cd["group_id"] - // $cd["nc_mode"] - // $cd["t_mode"] - // $cd["template_message"] - // $cd["campaign_id"] - echo "
"; - print_r($cd); - echo ""; - } public function auto_scheduled_email($cd){ - echo $cd["customer_name"]; - echo "
"; - print_r($cd); - echo ""; + // // $cd["city"] + // // $cd["state"] + // // $cd["invoice_date"] + // // $cd["customer_id"] + // echo $cd["customer_name"]; + // // $cd["email"] + // // $cd["mobile_no"] + // // $cd["due_date"] + // // $cd["isactive"] + // // $cd["invoice_count_raised_by_customer"] + // // $cd["scheme_id"] + // // $cd["name"] + // // $cd["group_id"] + // // $cd["nc_mode"] + // // $cd["t_mode"] + // // $cd["template_message"] + // // $cd["campaign_id"] + $this->logger->info("Auto Notification : Email Customer Details = " . json_encode($cd)); + + if(!empty($cd["template_message"] )){ + + $emailContent = $cd["template_message"] ; + $emailContent = str_replace("{USER_NAME}", $cd["customer_name"], $emailContent); + $emailContent = str_replace("{SCHEME_NAME}", $cd["name"], $emailContent); + $emailContent = str_replace("{EXPIRY_DATE}", $cd["formatted_due_date"], $emailContent); + + $emailData = [ + 'template_name' => '', + 'recipient_email' => $cd["email"], + 'subject' => $cd["template_subject"] != "" ? $cd["template_subject"] : ucfirst(str_replace('_', ' ', (string)$cd["name"]))." Notification" , + 'description' => $emailContent, + ]; + + + $notification = new NotificationHelper(); + // $emailResult = $notification->sendEmail($emailData); + $emailResult = array(); + + $this->logger->info("Auto Notification : Email Content = " . $emailContent); + $this->logger->info("Auto Notification : Email Response = " . json_encode($emailResult)); + if (isset($emailResult['success'])) { + $this->logger->info("Auto Notification : ".$cd["customer_name"]."`s mail sended Mail ID = ".$cd["email"]); + return $cd["customer_name"]." mail sended"; + } else { + $this->logger->info("Auto Notification : ".$cd["customer_name"]."`s mail not sended Mail ID = ".$cd["email"]); + return $cd["customer_name"]." mail not sended"; + } + }else{ + $this->logger->info('There is no email template found'); + return $cd["customer_name"]." mail template not found"; + } + } + public function auto_scheduled_whatsapp($cd){ + + if(!empty($cd["template_message"])){ + if (empty($cd["mobile_no"])) { + $this->logger->info('There is no mobile number Founded'); + return $cd["customer_name"]." mobile number not found"; + } + echo "
";print_r($cd);echo "";die; + $this->logger->info('testing purpose ==> '.$cd["customer_name"]." -- ".$cd["name"]." -- ".$cd["due_date"]); + + $whatsappContent = $cd["template_message"]; + $whatsappContent = str_replace("{USER_NAME}", $cd["customer_name"], $whatsappContent); + $whatsappContent = str_replace("{SCHEME_NAME}", $cd["name"], $whatsappContent); + $whatsappContent = str_replace("{EXPIRY_DATE}", $cd["formatted_due_date"], $whatsappContent); + + + $params = (object) Null; + $url = SEND_WAAI_URL; + $params->number = (int)'91' . $cd["mobile_no"]; + $params->type = "text"; + $params->message = $whatsappContent; + $params->instance_id = $_ENV['WAAI_INSTANCE']; + $params->access_token = $_ENV['WAAI_TOKEN']; + $this->logger->info("Auto Notification : Whatsapp Content = " . $whatsappContent); + $this->logger->info($cd["name"]." Whatsapp Request = " . json_encode($params)); + $this->logger->info($cd["name"]." Whatsapp Request type b4 = " . gettype($params)); + $notification = new NotificationHelper(); + // $whatsapp_result = $notification->sendWhatsAppMessage($url, "POST", $params); + $whatsapp_result = array(); + // $this->logger->info($cd["name"]." Whatsapp Reponse = " .$whatsapp_result); + + return $cd["customer_name"]."/".$cd["name"]." whatsapp template found not found"; + }else{ + $this->logger->info('There is no Whatsapp template found by the name of '.$cd["name"]); + return $cd["customer_name"]."/".$cd["name"]." whatsapp template found not found"; + }// $Whatsapp Template Name + } + + public function auto_scheduled_notification_campaign_status($campaign_id){ + // echo "
diff --git a/app/Views/print_addresses_form.php b/app/Views/print_addresses_form.php index 7fd8b3fd..338afcfa 100644 --- a/app/Views/print_addresses_form.php +++ b/app/Views/print_addresses_form.php @@ -11,7 +11,7 @@ } .invoice { width: 48%; - height: 150px; + height: 225px; margin-left: 2px; float: left; page-break-inside: avoid;