diff --git a/.env b/.env index 2958ca22..d0fe98e8 100755 --- a/.env +++ b/.env @@ -183,3 +183,8 @@ SMTP_HOST = 'smtp.sendgrid.net' SMTP_USER = 'apikey' SMTP_PASS = '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' SMTP_PORT = '587' + +#---------------------------------------------------------------------- +# New Whatsapp Access Token +#---------------------------------------------------------------------- +# whatsAppToken = 'EAARGyzEtu2kBO1mK3ILZAvZCd8D3QNpHo2uU5SWZBXbZC3eNL9hFpZA5WebolazilZA0RfU0zdu6gNZAW1yGCYiXFgZBC1YQfZBErv0QKAYQAT9wMOTy2f0C3m4ZAK8ZCwt3mGlURfa6m2uUCyNK4qEUy2Evx7scd8aanXLFr8MJUbVNJP8BPPSpUJW4A0dZAaci56lrPGnw0hV7jV9ISGmDLNYZD'; \ No newline at end of file diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 43a43045..ffd70b48 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -175,6 +175,7 @@ $routes->post("campaign_creation_insert", "Notifications::campaign_creation_inse # Auto run process routes $routes->get('process_scheduled_notifications/', 'Notifications::processScheduledNotifications'); +$routes->get('process_customergroup_notifications/(:any)/(:any)', 'Notifications::customer_group_notification/$1/$2'); $routes->get('acknowlegdement', 'Notifications::noifications_acknowlegdement'); $routes->get('subscription_inactive', 'Invoice::subscription_inactive'); diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index accc455c..a92bbaac 100755 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -300,16 +300,20 @@ class Customer extends BaseController } public function view_customer_group($id){ helper('session'); + $model = new CustomerModel(); $session_bid = get_business_id(); - + $data['type_values'] = $model->getType(); + $data['mode_values'] = $model->getMode(); + $data['category_values'] = $model->getCategory(); + $data['field'] = [ ['value'=>'','fieldflag'=>1,'text'=> 'Choose the Field','disable' => false], - ['value'=>'C.type','fieldflag'=>1,'text'=> 'Type','disable' => false], + ['value'=>'C.type','fieldflag'=>3,'text'=> 'Type','disable' => false], ['value'=>'CA.city','fieldflag'=>1,'text'=> 'City','disable' => false], // ['value'=>'CA.state','fieldflag'=>1,'text'=> 'State','disable' => false], ['value'=>'CA.postal_code','fieldflag'=>1,'text'=> 'Postal Code','disable' => false], ['value'=>'C.mode','fieldflag'=>3,'text'=> 'Mode','disable' => false], - ['value'=>'CM.name','fieldflag'=>1,'text'=> 'Category','disable' => false], + ['value'=>'CM.name','fieldflag'=>3,'text'=> 'Category','disable' => false], ['value'=>'I.invoice_date','fieldflag'=>2,'text'=> 'Invoice Date','disable' => false], ['value'=>'S.to_subscription','fieldflag'=>2,'text'=> 'Expiry Date','disable' => false]]; diff --git a/app/Controllers/Notifications.php b/app/Controllers/Notifications.php index 724ae0e3..b8d7a138 100755 --- a/app/Controllers/Notifications.php +++ b/app/Controllers/Notifications.php @@ -782,7 +782,7 @@ class Notifications extends BaseController $this->logger->error("Auto Scheduled Notification Started"); $model = new NotificationModel(); #step 1 get scheduled notification details - $notification_campaign_result = $this->getScheduledNotificationDetails(); + $notification_campaign_result = $this->getScheduledNotificationDetails(0,0); $this->logger->error("Auto Scheduled Campaign Details Count = ".count($notification_campaign_result)); if(!count($notification_campaign_result)) { @@ -902,12 +902,18 @@ class Notifications extends BaseController } #step 1 get scheduled notification details - function getScheduledNotificationDetails() + function getScheduledNotificationDetails($tid,$cid) { $model = new NotificationModel(); - $templateIds = [1, 2]; - $model->setTable('notification_campaign'); - $notification_campaign_result = $model->whereIn('template_id', $templateIds)->where('isactive', 1)->findAll(); + if($tid == 0&&$cid == 0){ + $templateIds = [1, 2]; + $model->setTable('notification_campaign'); + $notification_campaign_result = $model->whereIn('template_id', $templateIds)->where('isactive', 1)->findAll(); + }else{ + $templateIds = [$tid]; + $model->setTable('notification_campaign'); + $notification_campaign_result = $model->whereIn('template_id', $templateIds)->where(['isactive'=>1,'campaign_id'=>$cid])->findAll(); + } // $today = date('Y-m-d'); // $currentTime = date('H:i:s'); @@ -923,7 +929,6 @@ class Notifications extends BaseController return $notification_campaign_result; } - #step 2 get template details function getTemplateDetails($template_id) { @@ -972,6 +977,7 @@ class Notifications extends BaseController $notificationContent = str_replace("{USER_NAME}", $dataPointsArray["customer_name"], $notificationContent); $notificationContent = str_replace("{SCHEME_NAME}", $dataPointsArray["scheme_name"], $notificationContent); $notificationContent = str_replace("{EXPIRY_DATE}", $dataPointsArray["formatted_due_date"], $notificationContent); + $notificationContent = str_replace("{RENEWAL_LINK}", base_url() . 'subscription_renewal/' . $dataPointsArray["customer_id"], $notificationContent); return $notificationContent; } ############## Auto Scheduled Notification Ends ################ @@ -987,6 +993,119 @@ class Notifications extends BaseController $this->render_page('noifications_acknowlegdement', $data); } ############## Notification Acknowlegdement Ends ################ + public function customer_group_notification($tid,$cid){ + $this->logger->error("Customer Group Notification Started"); + + $this->logger->error("Customer Group Notification Ended"); + $model = new NotificationModel(); + #step 1 get scheduled notification details + $notification_campaign_result = $this->getScheduledNotificationDetails($tid,$cid); + // print_r($notification_campaign_result); + //looping multiple notification campaign + if(count($notification_campaign_result)) + { + foreach($notification_campaign_result as $index => $single_campaign) + { + echo "Notification Campaign Name = ".$single_campaign['campaign_name']."
"; + #step 2 get template details + $template = $this->getTemplateDetails($single_campaign['template_id']); + $customer_details = $this->getCustomerDetails($single_campaign['group_id']); + if(count($template) && count($customer_details)) + { + #step 3 execute the customer group query and return customer details + $sent_customer_details = []; + if(count($customer_details)) + { + #step 2.1 + $history_parents_data = ['campaign_id' => $single_campaign['campaign_id'],'status' => 0]; + $history_parent_id = $model->save_notification_history_parents($history_parents_data); + $single_campaign['history_parent_id'] = $history_parent_id; + + foreach($customer_details as $customer) + { + $customer['customer_name'] = isset($customer['customer_name']) + ? $customer['customer_name'] + : (isset($customer['first_name']) && isset($customer['last_name']) + ? $customer['first_name'] . ' ' . $customer['last_name'] + : ''); + $customer['scheme_name'] = isset($customer['scheme_name']) ? $customer['scheme_name'] : (isset($customer['title']) ? $customer['title'] : ""); + $formatted_due_date = ""; + if(isset($customer['to_subscription'])){ + $dateObj = date_create($customer['to_subscription']); + $formatted_due_date = date_format($dateObj, 'd-m-Y'); + }else{ + $formatted_due_date = $customer['formatted_due_date']; + } + $customer['formatted_due_date'] = $formatted_due_date; + + + #step 4 replace templates palceholders with data points + $notificationContent = $this->replaceTemplateWithDataPoints($template['message'],$customer); + $history_child_data = [ + 'fkid' => $single_campaign['history_parent_id'], + 'customer_id' => $customer['customer_id'], + 'receiving_entity' => $single_campaign['mode'] == 'Email' ? $customer['email'] : $customer['mobile_no'], + 'receiving_status' => 0, + 'message' => $notificationContent]; + $history_child_id = $model->save_notification_history_child($history_child_data); + if(!in_array($customer['customer_id'],$sent_customer_details)) + { + $this->logger->error("Auto Scheduled Customer CID = ".$customer['customer_id'].", CN = ".$customer['customer_name']); + #step 5 send notification + $notification = new NotificationHelper(); + if($single_campaign['mode'] == 'Email') + { + $email_response = $notification->sendEmail(array('recipient_email' => $customer['email'],'subject' => $template['subject'],'template_name' => $template['template_name'],'description' => $notificationContent)); + $mail_log = isset($email_response['success']) ? 'Email sent successfully' : 'Failed to send email'; + $receiving_status = isset($email_response['success']) ? 1 : 0; + $this->logger->error("Auto Scheduled Email CID = ".$customer['customer_id']." ".$mail_log); + + $history_child_where = ['fkid' => $single_campaign['history_parent_id'],'id' => (int)$history_child_id]; + $history_child_updatedata = ['receiving_status'=>$receiving_status, 'result'=>isset($email_response['success']) ? $mail_log : (strtolower(gettype($email_response)) == "string" ? $email_response : json_encode($email_response)) ]; + $history_child_statement = $model->update_notification_history_child($history_child_updatedata,$history_child_where); + $this->logger->error($history_child_statement); + + } + if($single_campaign['mode'] == 'Whatsapp') + { + $params = (object) Null; + $url = SEND_WAAI_URL; + $params->type = "text"; + $params->instance_id = $_ENV['WAAI_INSTANCE']; + $params->access_token = $_ENV['WAAI_TOKEN']; + $params->message = strip_tags(ltrim($notificationContent)); + $params->number = (int)'91' . $customer['mobile_no']; + $whatsapp_reponse = $notification->sendWhatsAppMessage($url, "POST", $params); + $whatsapp_reponse_msg = strtolower(gettype($whatsapp_reponse)) == "string" ? $whatsapp_reponse : json_encode($whatsapp_reponse['reponse']); + // echo "Whatsapp Response = ".$whatsapp_reponse_msg."
"; + $this->logger->error("Auto Scheduled Whatsapp CID = ".$customer['customer_id']." ".$whatsapp_reponse_msg); + $history_child_where = ['fkid' => $single_campaign['history_parent_id'],'id' => (int)$history_child_id]; + + $history_child_updatedata = ['receiving_status'=>$whatsapp_reponse['receiving_status'],'result'=>$whatsapp_reponse_msg]; + $history_child_statement = $model->update_notification_history_child($history_child_updatedata,$history_child_where); + $this->logger->error($history_child_statement); + + } + + array_push($sent_customer_details,$customer['customer_id']); + } + + } + + } + #step 6 update campaign status to 1 + $statement = $model->update_notification_campaign_status(['status' => 0],$single_campaign['campaign_id']); + echo "Final Campaign Status = ".$statement."
"; + $this->logger->error($statement); + + #step 7 update history parent status to 1 + $history_parent_statement = $model->update_notification_history_parent_status(['status' => 1],$history_parent_id,$single_campaign['campaign_id']); + echo "Final Acknowlegdement = ".$history_parent_statement."
"; + $this->logger->error($history_parent_statement); + } + } + } + } } diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index a38050ea..10677611 100755 --- a/app/Models/CustomerModel.php +++ b/app/Models/CustomerModel.php @@ -258,6 +258,30 @@ public function storeCustomerAddresses($billingAddress, $shippingAddress) } return ["billing_addr_id"=>$billing_addr_id,"shipping_addr_id"=>$shipping_addr_id]; // Return true indicating successful insertion } +public function getType(){ + return $this->db->table('customers as C' ) + ->select('C.type as value') + ->groupBy('C.type') + ->get() + ->getResultArray(); +} +public function getMode(){ + return $this->db->table('customers as C' ) + ->select('C.mode as value') + ->where(['C.mode != ' => '']) + ->groupBy('C.mode') + ->get() + ->getResultArray(); +} +public function getCategory(){ + return $this->db->table('category as C' ) + ->select('C.name as value') + ->where(['C.name != ' => '','C.isactive' => 1]) + ->groupBy('C.name') + ->get() + ->getResultArray(); +} + public function get_customer_details_by_customer_id($customer_id) { diff --git a/app/Views/campaign_creation.php b/app/Views/campaign_creation.php index 241ad267..ed2f1937 100755 --- a/app/Views/campaign_creation.php +++ b/app/Views/campaign_creation.php @@ -64,7 +64,10 @@ " class="edit-button" title="Click to Edit Campaign" > - " class="delete-button" title="Click to Delete Campaign" > + + " class="delete-button" title="Click to Delete Campaign" > + " class="send-button" title="Send Now" > + diff --git a/app/Views/customer_group_form.php b/app/Views/customer_group_form.php index 5ad71d40..3c9a6ff7 100755 --- a/app/Views/customer_group_form.php +++ b/app/Views/customer_group_form.php @@ -30,7 +30,7 @@ - name="column[]" onchange="resetWhenChange(this,)">