diff --git a/app/Config/Routes.php b/app/Config/Routes.php index cfeef129..ebfe2a84 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -98,6 +98,7 @@ $routes->get("subscribers_list/", "Subscription::index"); $routes->get("new_subscription_invoice/(:any)", "Invoice::new_subscription_invoice/$1"); $routes->post("add_subscription/", "Subscription::add_subscription/"); $routes->post('subscription/download_details', 'Subscription::download_details'); +$routes->get("notify_the_expiry_subscribers/", "Subscription::notify_the_expiry_subscribers"); diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index ccc981cf..95c21b1c 100755 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -558,20 +558,23 @@ public function create_or_update_subscription($invoice_id,$invoice_status, $msg_ 'updated_by' => get_logged_user_id(), 'is_renew' => $renewal, 'membership_id'=>$membership_id, - 'status' =>1 + 'status' =>1, + 'isactive' =>$invoice_status === 'Approved' ? 1 : 0 ]; - + $check_existing_customer_id = $subModel->where('invoice_id',$invoice_id)->findAll(); //dd($check_existing_customer_id); - if($check_existing_customer_id){ - log_message('info',json_encode($subscription_data)); - $where = ['invoice_id'=>$invoice_id]; - $subModel->updateData($subscription_data,$where);//update the data - } - else{ - //insert the data - $model->insertSubscriptionData($subscription_data,$invoice_status); - } + // if($check_existing_customer_id){ + // unset($data['created_by']); + // log_message('info',json_encode($subscription_data)); + // $where = ['invoice_id'=>$invoice_id]; + // $subModel->updateData($subscription_data,$where);//update the data + // } + // else{ + // //insert the data + // unset($data['updated_by']); + $model->insertupdateSubscriptionData($subscription_data,$invoice_status); + // } } } diff --git a/app/Controllers/Notifications.php b/app/Controllers/Notifications.php index b4c6a15e..2a0d5184 100755 --- a/app/Controllers/Notifications.php +++ b/app/Controllers/Notifications.php @@ -571,9 +571,8 @@ class Notifications extends BaseController ################################################################ public function getExpCustomerDetail($window_time = null) { - // Email flow only Whatsapp flow Deleted. $this->update_expired_members(); - echo "Hi, "; + $response = "
Hi, ";
# Step 1: add the days to retrive the data//
$providedDate = date('Y-m-d');
$newDate = date('Y-m-d', strtotime($providedDate . ' + ' . $window_time . ' days'));
@@ -602,12 +601,17 @@ class Notifications extends BaseController
$notification = new NotificationHelper();
if (empty($queryResult)) {
$this->logger->error("There is no customer expiring on date:{$newDate}");
- echo " There is no customer expiring. till dated :{$newDate}";
+ $response .= " There is no customer expiring. till dated :{$newDate}";
return;
}
$emailTemplateName = $NotificationModel->getTempName('EXPIRY NOTIFY TEMPLATE EMAIL');
- echo "Started.
Customer expiring. till dated = ".date('d-m-Y', strtotime($newDate))."
There are ".count($queryResult)." persons
";
+ $response .= "Started.
Customer expiring. till dated = ".date('d/m/Y', strtotime($newDate))."
There are ".count($queryResult)." persons
";
+ if(!empty($queryResult)){
+ usort($queryResult, function ($a, $b) {
+ return $b['to_subscription'] <=> $a['to_subscription']; // Descending order
+ });
+ }
foreach ($queryResult as $inx => $row) {
$status = $row['status'];
$userFirstName = $row['first_name'];
@@ -623,128 +627,254 @@ class Notifications extends BaseController
$this->logger->error('There is no subcription Name');
continue;
}
- echo ($inx+1).") #".$customerId." - Person Name : ".$fullName." ----- ( Expiry on ". date('d-m-Y', strtotime($expirydate)).")
";
+ $response .= ($inx+1).") #".$customerId." - Person Name : ".$fullName." ----- ( Expiry on ". date('d/m/Y', strtotime($expirydate)).")
";
if ($status === 'Approved') {
- # EXPIRY NOTIFY TEMPLATE EMAIL
- $emailTemplateName = $NotificationModel->getTempName('EXPIRY NOTIFY TEMPLATE EMAIL');
- if(empty($row['email'])){
- echo ($inx+1).") Email Not Available
";
- continue;
- }
+ # EXPIRY NOTIFY TEMPLATE EMAIL
+ $emailTemplateName = $NotificationModel->getTempName('EXPIRY NOTIFY TEMPLATE EMAIL');
+ if(empty($row['email'])){
+ $response .= ($inx+1).") Email Not Available
";
+ continue;
+ }
- $existingcheck = $NotificationModel->checkEmailAlreadyExisting($row['email'],$customerId);
- $check_existing = 0;
- $check_date = "";
- $check_diff_days = "";
+ $existingcheck = $NotificationModel->checkEmailAlreadyExisting($row['email'],$customerId);
+ $check_existing = 0;
+ $check_date = "";
+ $check_diff_days = "";
- if(!empty($existingcheck)){
+ if(!empty($existingcheck)){
- foreach ($existingcheck as $i => $r) {
- if($r['receiving_status'] == 1){
- $check_existing = 1;
- $check_date = $r['sent_time'];
- $sentTimestamp = strtotime($r['sent_time']);
- $provedTimestamp = strtotime($providedDate);
+ foreach ($existingcheck as $i => $r) {
+ if($r['receiving_status'] == 1){
+ $check_existing = 1;
+ $check_date = $r['sent_time'];
+ $sentTimestamp = strtotime($r['sent_time']);
+ $provedTimestamp = strtotime($providedDate);
-
- $diffInSeconds = abs($provedTimestamp - $sentTimestamp);
- $diffInDays = $diffInSeconds / (60 * 60 * 24);
+
+ $diffInSeconds = abs($provedTimestamp - $sentTimestamp);
+ $diffInDays = $diffInSeconds / (60 * 60 * 24);
- $check_diff_days = $diffInDays;
+ $check_diff_days = $diffInDays;
+ }
}
}
- }
- if($check_existing){
- echo ($inx+1).") Email already send on - ".date('d-m-Y h:i A', strtotime($check_date))."
";
- continue;
- }
-
- if (count($emailTemplateName) && !empty($emailTemplateName)) {
- $campaign_id = 1;
- # Insertion on History Email Means Campaign ID 1 Hardcoded
- $history_parents_data = ['campaign_id' => $campaign_id,'status' => 0];
- $history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data);
-
- # Step 4: Prepare email content based on the template
- $emailContent = $emailTemplateName[0]['message'];
- if (isset($fullName)) {
- $emailContent = str_replace("{USER_NAME}", $fullName, $emailContent);
+ if($check_existing){
+ $response .= ($inx+1).") Email already send on - ".date('d/m/Y h:i A', strtotime($check_date))."
";
+ continue;
}
- if (isset($schemename)) {
- $emailContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent);
- }
- if (isset($expirydate)) {
- $expiryon = $expirydate ? date('d-m-Y', strtotime($expirydate)) : '';
- $emailContent = str_replace("{EXPIRY_DATE}", $expiryon, $emailContent);
- $replaceContent = (int)$countdays == 0 ? 'expires today, ' : 'expiring on ';
- $emailContent = str_replace("expiring on", $replaceContent, $emailContent);
- }
- if (isset($membership_id)) {
- $emailContent = str_replace("{RENEWAL_LINK}", base_url() . 'subscription_renewal/' . $membership_id, $emailContent);
- }
-
-
- $emailData = [
- 'template_name' => 'EXPIRY NOTIFY TEMPLATE EMAIL',
- 'recipient_email' => $row['email'],
- 'subject' => 'Your Subscription Expiry Notification',
- 'description' => $emailContent,
- ];
-
+ if (count($emailTemplateName) && !empty($emailTemplateName)) {
+ $campaign_id = 1;
+ # Insertion on History Email Means Campaign ID 1 Hardcoded
+ $history_parents_data = ['campaign_id' => $campaign_id,'status' => 0];
+ $history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data);
- $history_child_data = [ 'fkid' => $history_parent_id,
- 'customer_id' => $customerId,
- 'receiving_entity' => $row['email'],
- 'receiving_status' => 0,
- 'message' => $emailContent];
- # Step 5: Insertion on History Child
- $history_child_id = $NotificationModel->save_notification_history_child($history_child_data);
+ # Step 4: Prepare email content based on the template
+ $emailContent = $emailTemplateName[0]['message'];
+ if (isset($fullName)) {
+ $emailContent = str_replace("{USER_NAME}", $fullName, $emailContent);
+ }
+ if (isset($schemename)) {
+ $emailContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent);
+ }
+ if (isset($expirydate)) {
+ $expiryon = $expirydate ? date('d-m-Y', strtotime($expirydate)) : '';
+ $emailContent = str_replace("{EXPIRY_DATE}", $expiryon, $emailContent);
+ $replaceContent = (int)$countdays == 0 ? 'expires today, ' : 'expiring on ';
+ $emailContent = str_replace("expiring on", $replaceContent, $emailContent);
+ }
+ if (isset($membership_id)) {
+ $link = base_url() . 'subscription_renewal/' . $membership_id;
+ $emailContent = str_replace("{RENEWAL_LINK}", ''.$link.'', $emailContent);
+ }
+
- # Step 6:Send email using NotificationHelper
- $emailResult = $notification->sendEmail($emailData);
- $this->logger->error("EXPIRAY NOTIFY TEMPLATE EMAIL Email CID = ".$customerId." ".json_encode($emailResult));
-
- if (isset($emailResult['success'])) {
- $history_msg = 'Email sent successfully';
- $history_child_updatedata = ['receiving_status'=>1,'result'=>$history_msg];
- $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
- $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
- $this->logger->error($history_child_statement);
+ $emailData = [
+ 'template_name' => 'EXPIRY NOTIFY TEMPLATE EMAIL',
+ 'recipient_email' => $row['email'],
+ 'subject' => 'Your Subscription Expiry Notification',
+ 'description' => $emailContent,
+ ];
+
- $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
- $this->logger->error($history_parent_statement);
- echo ($inx+1).") ".$history_msg."
";
+ $history_child_data = [ 'fkid' => $history_parent_id,
+ 'customer_id' => $customerId,
+ 'receiving_entity' => $row['email'],
+ 'receiving_status' => 0,
+ 'message' => $emailContent];
+ # Step 5: Insertion on History Child
+ $history_child_id = $NotificationModel->save_notification_history_child($history_child_data);
+
+ # Step 6:Send email using NotificationHelper
+ $emailResult = $notification->sendEmail($emailData);
+
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE EMAIL Email CID = ".$customerId." ".json_encode($emailResult));
+
+ if (isset($emailResult['success'])) {
+ $history_msg = 'Email sent successfully';
+ $history_child_updatedata = ['receiving_status'=>1,'result'=>$history_msg];
+ $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
+ $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
+ $this->logger->error($history_child_statement);
+
+ $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
+ $this->logger->error($history_parent_statement);
+ $response .= ($inx+1).") ".$history_msg."
";
+ } else {
+ $history_msg = (strtolower(gettype($emailResult)) == "string" ? $emailResult : json_encode($emailResult)) ;
+ $history_child_updatedata = ['receiving_status'=>0,'result'=>$history_msg];
+ $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
+ $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
+ $this->logger->error($history_child_statement);
+
+ $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
+ $this->logger->error($history_parent_statement);
+ $response .= ($inx+1).") Failed to send email
";
+ }
} else {
- $history_msg = (strtolower(gettype($emailResult)) == "string" ? $emailResult : json_encode($emailResult)) ;
- $history_child_updatedata = ['receiving_status'=>0,'result'=>$history_msg];
- $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
- $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
- $this->logger->error($history_child_statement);
-
- $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
- $this->logger->error($history_parent_statement);
- echo ($inx+1).") Failed to send email
";
+ $history_msg = 'There is no email template found by the name of EXPIRY NOTIFY TEMPLATE EMAIL';
+ // $receiving_status = 0 ;
+ $this->logger->error($history_msg);
+ $response .= $history_msg."
";
+ continue;
}
- } else {
- $history_msg = 'There is no email template found by the name of EXPIRY NOTIFY TEMPLATE EMAIL';
- // $receiving_status = 0 ;
- $this->logger->error($history_msg);
- echo $history_msg."
";
- continue;
- }
-
}
else{
- echo ($inx+1).") Staus = ".$status."
";
+ $response .= ($inx+1).") Staus = ".$status."
";
$this->logger->error("status is Draft");
}
# EXPIRY NOTIFY TEMPLATE EMAIL Closed
+ if ($status === 'Approved') {
+ $existingcheck2 = $NotificationModel->checkEmailAlreadyExisting($userMobileNumber,$customerId);
+ $check_existing2 = 0;
+ $check_date2 = "";
+ $check_diff_days2 = "";
+ if(!empty($existingcheck2)){
+
+ foreach ($existingcheck2 as $i2 => $r2) {
+ if($r2['receiving_status'] == 1){
+ $check_existing2 = 1;
+ $check_date2 = $r2['sent_time'];
+ $sentTimestamp2 = strtotime($r2['sent_time']);
+ $provedTimestamp2 = strtotime($providedDate);
+
+
+ $diffInSeconds2 = abs($provedTimestamp2 - $sentTimestamp2);
+ $diffInDays2 = $diffInSeconds2 / (60 * 60 * 24);
+
+ $check_diff_days2 = $diffInDays2;
+ }
+ }
+ }
+ if($check_existing2){
+ $response .= ($inx+1).") whatsapp already send ".$check_date2."
";
+ continue;
+ }
+ # EXPIRY NOTIFY TEMPLATE WHATSAPP
+ $whatsappTemplateName = $NotificationModel->getTempName('EXPIRY NOTIFY TEMPLATE WHATSAPP');
+ // print_r($whatsappTemplateName);die;
+ if ((count($whatsappTemplateName) > 0) && !empty($whatsappTemplateName)) {
+ $campaign_id = 2; // hardcoded because static Template.
+ $history_parents_data = ['campaign_id' => 2,'status' => 0];
+ $history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data);
+ if (empty($userMobileNumber)) {
+ $history_msg = 'There is no subscriber mobile number';
+ $history_child_updatedata = ['receiving_status'=>0,'result'=>$history_msg];
+ $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
+ $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
+ $this->logger->error($history_child_statement);
+
+ $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
+ $this->logger->error($history_parent_statement);
+ $this->logger->error($history_msg);
+ $response .= ($inx+1).") ".$history_msg."
";
+ continue;
+ }
+ # Insertion on History Whatsapp Means Campaign ID 2 Hardcoded
+ $history_parents_data = ['campaign_id' => 2,'status' => 0];
+ $history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data);
+
+ # Step 7: Prepare whatsapp content based on the template
+ $whatsappContent = $whatsappTemplateName[0]['message'];
+ if (isset($fullName)) {
+ $whatsappContent = str_replace("{USER_NAME}", $fullName, $whatsappContent);
+ }
+ if (isset($schemename)) {
+ $whatsappContent = str_replace("{SCHEME_NAME}", $schemename, $whatsappContent);
+ }
+ if (isset($expirydate)) {
+ $expiryon = $expirydate ? date('d-m-Y', strtotime($expirydate)) : '';
+ $whatsappContent = str_replace("{EXPIRY_DATE}", $expiryon, $whatsappContent);
+ }
+ if (isset($customerId)) {
+ $whatsappContent = str_replace("{RENEWAL_LINK}", base_url() . 'subscription_renewal/' . $customerId, $whatsappContent);
+ }
+ # Step 8: Insertion on History Child for whatsapp
+ $history_child_data = [ 'fkid' => $history_parent_id,
+ 'customer_id' => $customerId,
+ 'receiving_entity' => $userMobileNumber,
+ 'receiving_status' => 0,
+ 'message' => strip_tags(ltrim($whatsappContent))];
+ $history_child_id = $NotificationModel->save_notification_history_child($history_child_data);
+
+
+ # Step 8: Send Whatsapp using NotificationHelper
+ $params = (object) Null;
+ $url = SEND_WAAI_URL;
+ $params->number = (int)'91' . $userMobileNumber;
+ $params->type = "text";
+ $params->message = strip_tags(ltrim($whatsappContent));
+ $params->instance_id = $_ENV['WAAI_INSTANCE'];
+ $params->access_token = $_ENV['WAAI_TOKEN'];
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request = " . json_encode($params));
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request type b4 = " . gettype($params));
+ $whatsapp_result = $notification->sendWhatsAppMessage($url, "POST", $params);
+ $receiving_status = $whatsapp_result['receiving_status'];
+ $history_msg = strtolower(gettype($whatsapp_result)) == "string" ? $whatsapp_result : json_encode($whatsapp_result['reponse']);
+ $history_child_updatedata = ['receiving_status'=>$receiving_status,'result'=>$history_msg];
+ $history_child_where = ['fkid' => $history_parent_id,'id' => (int)$history_child_id];
+ $history_child_statement = $NotificationModel->update_notification_history_child($history_child_updatedata,$history_child_where);
+ $this->logger->error($history_child_statement);
+
+ $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,$campaign_id);
+ $this->logger->error($history_parent_statement);
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE WHATSAPP Reponse = " . $history_msg);
+
+ if(strtolower(gettype($whatsapp_result)) == "string"){
+ $response .= ($inx+1).") ".$whatsapp_result."
";
+ }
+ else{
+ $result = json_encode($whatsapp_result['reponse']);
+ $decodedResult = json_decode($result, true);
+ if ($decodedResult && isset($decodedResult['message']['key']['remoteJid'])) {
+ $remoteJid = $decodedResult['message']['key']['remoteJid'];
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE WHATSAPP Remote Jid = " . $remoteJid);
+ $response .= ($inx+1).") Whatsapp sent successfully
";
+ } else {
+ // echo "Failed to extract remoteJid";
+ $this->logger->error("EXPIRAY NOTIFY TEMPLATE WHATSAPP Failed to extract remoteJid ");
+ $response .= ($inx+1).") Failed to send Whatsapp
";
+ }
+ }
+ } else {
+ // $receiving_status = 0;
+ $history_msg = 'There is no Whatsapp template found by the name of EXPIRAY NOTIFY TEMPLATE WHATSAPP ';
+ $this->logger->error($history_msg);
+ $response .= "There is no Whatsapp template
";
+ continue;
+ }
+ # EXPIRY NOTIFY TEMPLATE WHATSAPP Closed
+ }
+ else{
+ $this->logger->error("status is draft");
+ }
# Loop Closed
- echo "Process Completed
";
}
+ $response .= "Process Completed
";
+ return $this->response->setBody($response);
+
}
diff --git a/app/Controllers/Subscription.php b/app/Controllers/Subscription.php
index b6d8fb5e..5325bb1e 100755
--- a/app/Controllers/Subscription.php
+++ b/app/Controllers/Subscription.php
@@ -281,9 +281,10 @@ public function download_details()
}
}
-
-
-
+public function notify_the_expiry_subscribers(){
+ $data['page_name'] = 'Notify the Expiry Subscribers';
+ $this->render_page('notify_the_expiry_subscribers',$data);
+}
}
// public function generate_pdf()
diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php
index cbc6884e..70e3deb3 100755
--- a/app/Models/InvoiceModel.php
+++ b/app/Models/InvoiceModel.php
@@ -350,14 +350,14 @@ public function getJoinedData($where, $orderby = [])
return $data;
}
-public function insertSubscriptionData($data,$invoice_status)
+public function insertupdateSubscriptionData($data,$invoice_status)
{
if (!empty($data)) {
$invoice_id = $data['invoice_id'];
$customer_id = $data['customer_id'];
$scheme_id = $data['scheme_id'];
$data['isactive'] = $invoice_status === 'Approved' ? 1 : 0 ;
- $where = ['invoice_id' => $invoice_id,'customer_id' => $customer_id,'scheme_id' => $scheme_id];
+ $where = ['invoice_id' => $invoice_id]; // invoice id refer with srinivasan // initally invoice id, scheme id and customer id
$query = $this->db->table('subscription')->select('sub_id')->where($where)->get()->getRow();
if (!empty($query)) {
diff --git a/app/Views/book_form.php b/app/Views/book_form.php
index 39f8f9a5..2e6b67c3 100755
--- a/app/Views/book_form.php
+++ b/app/Views/book_form.php
@@ -25,18 +25,18 @@
getFlashdata('success')) : ?>