donation/app/Models/NotificationModel.php
2024-06-18 06:37:52 +00:00

281 lines
13 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class NotificationModel extends Model
{
// protected $table;
protected $table = 'templates';
protected $primaryKey = 'template_id';
// protected $allowedFields = ['invoice_id','invoice_number','donor_id','invoice_date','event_id','business_id','created_on','business_id'];
protected $allowedFields = ['template_name', 'subject', 'message','mode','crteated_on', 'created_by', 'updated_on', 'updated_by', 'isactive','donor_id'];
public function setCustomTable($tableName)
{
$this->table = $tableName;
}
// public function getTasksDueLastWeek()
// {
// $now = date('Y-m-d');
// $oneWeekAgo = date('Y-m-d', strtotime('-7 days', strtotime($now)));
// return $this->where('due_date >=', $oneWeekAgo)
// ->where('due_date <=', $now)
// ->findAll();
// }
public function getSubscriptionDueDetail($date){
// $now = date('Y-m-d');
// $lastWeek = date('Y-m-d', strtotime('+7 days'));
// $date_format = date('Y-m-d', $date);
return $this->db->table('subscription as S' )
->join('customers as C', 'C.customer_id = S.customer_id', 'left')
->join('category as CM', 'CM.id = S.scheme_id', 'left')
->join('business as B', 'B.business_id = S.business_id', 'left')
->join('book_categories as BC', 'BC.category_id = S.scheme_id', 'left')
->join('books as BK', 'BC.book_id = BK.book_id', 'left')
->select('concat("Your subscription going to expried in ",DATEDIFF(S.to_subscription, CURDATE())," days") as statement,DATEDIFF(S.to_subscription, CURDATE()) as countdays,CM.name,concat(C.first_name, " ", C.last_name) as customer_name,C.email as customer_email,C.mobile_no as customer_mobile,S.sub_id,S.scheme_id,S.customer_id,S.from_subscription,S.to_subscription,S.business_id,B.title as business_name,B.address as business_address,B.city as business_city,B.state as business_state,B.postal_code as business_postal_code,B.email as business_email,B.mobile_no as business_mobile_no,S.to_subscription,S.from_subscription,BK.title,BK.book_id')
// ->where('S.to_subscription >=', $now)->where('S.to_subscription <=',$lastWeek)
->where('S.to_subscription =',date('Y-m-d', strtotime($date)))
->groupBy('S.sub_id')
->get()->getResultArray();
}
public function getTemplateDetails($session_bid){
return $this->db->table('templates as T' )
->join('users as U1', 'U1.user_id = T.created_by', 'left')
->join('users as U2', 'U2.user_id = T.updated_by', 'left')
->select('T.template_id,T.template_name,T.mode,T.created_on,T.created_by,T.updated_on,T.updated_by,DATE_FORMAT(T.created_on, "%d/%m/%Y %h:%i %p") AS formatted_created_on,concat(U1.first_name," ",U1.last_name) as created_by_name,concat(U2.first_name," ",U2.last_name) as updated_by_name,T.isactive')
->whereNotIn('T.template_name',array('EXPIRAY_NOTIFY_TEMPLATE_EMAIL','EXPIRAY_NOTIFY_TEMPLATE_WHATSAPP'))
->where('U1.business_id =',$session_bid)
->get()->getResultArray();
}
public function saveDetails($data,$pk_name,$t_name){
$id = $data[$pk_name];
$statement_string = ucfirst(str_replace('_', ' ', $t_name));
if($id != ''){
unset($data[$pk_name]); // Remove the id from the data to avoid updating it
unset($data['created_by']); // bcoz here data Updating here.
if ($this->db->table($t_name)->where($pk_name, $id)->update($data)) {
$affectedRows = $this->db->affectedRows();
$statement['success'] = $statement_string.' has been updated successfully.';
$statement['error'] = "";
$statement['log'] = $statement_string.": has been updated successfully. Updated ".$pk_name." = " .$id." Affected Rows = ".$affectedRows;
} else {
$statement['success'] = "";
$statement['error'] = $statement_string.' update failed. Please try again.';
$statement['log'] = $statement_string.": Err Failed to update ID = " .$id ;
}
}else{
unset($data['updated_by']);
if ($this->db->table($t_name)->insert($data)) {
$insertID = $this->db->insertID();
$statement['success'] = $statement_string.' has been Inserted successfully.';
$statement['error'] = "";
$statement['log'] = $statement_string.": has been added successfully. Inserted ID = " .$insertID ;
} else {
$statement['success'] = "";
$statement['error'] = $statement_string." could not be added. Please try again..";
$statement['log'] = $statement_string.": Err could not be added. Please try again.";
}
}
return $statement;
}
public function getCampaignDetails($session_bid){
$result = $this->db->table('notification_campaign as NC' )
->join('users as U1', 'U1.user_id = NC.created_by', 'left')
->join('users as U2', 'U2.user_id = NC.updated_by', 'left')
->join('templates as T', 'T.template_id = NC.template_id', 'left')
->select('NC.campaign_id,NC.campaign_name,T.template_id,T.template_name,NC.mode,NC.created_on,NC.created_by,NC.updated_on,NC.updated_by,DATE_FORMAT(NC.created_on, "%d/%m/%Y %h:%i %p") AS formatted_created_on,concat(U1.first_name," ",U1.last_name) as created_by_name,concat(U2.first_name," ",U2.last_name) as updated_by_name,NC.isactive,NC.group_id,NC.scheduled_date,if(NC.scheduled_time IS NULL ,"",NC.scheduled_time) as scheduled_time')
->whereNotIn('T.template_name',array('EXPIRAY_NOTIFY_TEMPLATE_EMAIL','EXPIRAY_NOTIFY_TEMPLATE_WHATSAPP'))
->where('U1.business_id =',$session_bid)
->get()->getResultArray();
if (!empty($result)) {
foreach ($result as $i => $item) {
$group_id = unserialize($item['group_id']);
$group_names = [];
$group_query = [];
foreach ($group_id as $j) {
$cg_data = $this->db->table('donor_groups as CG')
->select('CG.group_id, CG.group_name,CG.group_query, CG.isactive')
->where(['CG.isactive' => 1, 'CG.group_id' => $j])
->get()
->getRowArray();
if ($cg_data) {
$group_names[] = $cg_data['group_name'];
$group_query[] = $cg_data['group_query'];
// array_push($group_sql, $cg_data['group_query']);
}
}
$result[$i]['group_name'] = !empty($group_names) ? implode(", ", $group_names) : '';
$result[$i]['customer_group'] = !empty($group_query) ? $this->customerGroupQueryExecution($group_query) : '';
$result[$i]['customer_group_count'] = !empty($result[$i]['customer_group']) ? count($this->customerGroupQueryExecution($group_query)) : 0;
}
}
return $result;
}
public function customerGroupQueryExecution($queries){
$results = [];
// Execute the queries
foreach ($queries as $query) {
if(!empty($query)){
$queryResult = $this->db->query($query)->getResult();
$results[] = $queryResult;}
}
// echo "<pre>";
// print_r($results);
// echo "</pre>";
// Create a new array to store the unique results based on customer_id
$uniqueCustomers = array();
foreach ($results as $innerArray) {
foreach ($innerArray as $customer) {
// echo json_encode($customer);die;
$customerId = $customer->donor_id;
// Check if the customer_id already exists in $uniqueCustomers
if (!isset($uniqueCustomers[$customerId])) {
$uniqueCustomers[$customerId] = $customer;
}
}
}
// Convert the $uniqueCustomers array back to a numerically indexed array
$uniqueCustomers = array_values($uniqueCustomers);
return $uniqueCustomers;
// echo "<pre>";
// print_r($uniqueCustomers);
// echo "</pre>";
// die;
}
// $query = "INSERT IGNORE INTO notification_history_children (fkid,customer_id,receiving_entry,message,result,sent_time)
// VALUES (1, 1, 'somes@gmail2.com','mail msg',NULL,'2023-12-22 14:20:50'),
// (1, 1, '9638527411','whatapp msg',NULL,'2023-12-22 14:20:50')";
// $result = $db->query($query)->getResultArray();
public function getExpDate()
{
$db = \Config\Database::connect();
$query = "SELECT * FROM donor_groups WHERE group_name = 'EXPIRYDATE'";
$result = $db->query($query)->getResultArray();
if (isset($result[0]["group_query"])) {
return $result[0]["group_query"];
} else {
error_log('group_query is not available in getExpDate()');
return ;
}
}
public function getTempName($constant)
{
$db = \Config\Database::connect();
$query = "SELECT * FROM templates WHERE template_name = '".$constant."'";
// print_r($query);die;
$result = $db->query($query)->getResultArray();
if (isset($result)) {
return $result;
} else {
error_log('group_query is not available in getExpDate()');
return $query;
}
}
public function update_notification_campaign_status($data,$id){
if($id != ''){
$where = ['isactive' => 1, 'status' => 0 ,'campaign_id'=>(int)$id];
if ($this->db->table('notification_campaign')->where($where)->update($data)) {
$affectedRows = $this->db->affectedRows();
$statement = $affectedRows > 0 ? 'Notification Campaign Status has been updated successfully. Aff Row'.$affectedRows." NCID = " .$id : 'Notification Campaign Status Update failed. Bcoz Already Affected Count = '.$affectedRows.". NCID = " .$id ;
} else {
$statement = "Notification Campaign Status Update failed. NCID = " .$id;
}
}
else{
$statement = "Notification Campaign Status Update failed. Bcoz Not Found NCID";
}
return $statement;
}
public function save_notification_history_parents($data){
$this->db->table('notification_history_parents')->insert($data);
$insertID = $this->db->insertID();
return $insertID;
}
public function update_notification_history_parent_status($data,$id,$campaign_id){
if($id != ''){
$where = ['id' => (int)$id, 'status' => 0 ,'campaign_id'=>(int)$campaign_id];
if ($this->db->table('notification_history_parents')->where($where)->update($data)) {
$affectedRows = $this->db->affectedRows();
$statement = $affectedRows > 0 ? 'Notification history parent Status has been updated successfully. Aff Row'.$affectedRows." NHPID = " .$id : 'Notification history parent Status Update failed. Bcoz Already Affected Count = '.$affectedRows.". NHPID = " .$id ;
} else {
$statement = "Notification history parent Status Update failed. NHPID = " .$id;
}
}
else{
$statement = "Notification history parent Status Update failed. Bcoz Not Found NHPID";
}
return $statement;
}
public function save_notification_history_child($data){
// $this->db->ignore(true); // Enable "INSERT IGNORE" behavior
// $this->db->table('notification_history_children')->insert($data);
// $insertID = $this->db->insertID(); // Get the last inserted ID
// return $insertID;
// $insert_query = $this->db->insert_string('notification_history_children', $data);
// $insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
// $this->db->query($insert_query);
// $insertID = $this->db->insertID(); // Get the last inserted ID
// return $insertID;
$table = 'notification_history_children';
$insert_query = "INSERT IGNORE INTO $table (" . implode(', ', array_keys($data)) . ") VALUES (" . rtrim(str_repeat('?, ', count($data)), ', ') . ")";
$this->db->query($insert_query, array_values($data));
// print_r($this->db->getLastQuery());die;
$insertID = $this->db->insertID();
return $insertID;
}
public function update_notification_history_child($data, $where){
$id = isset($where['id']) ? $where['id'] : "";
if ($id != '') {
$updateResult = $this->db->table('notification_history_children')->where($where)->update($data);
// print_r($this->db->getLastQuery());die;
if ($updateResult) {
$affectedRows = $this->db->affectedRows();
$statement = 'Notification history children has been updated successfully. Aff Row ' . $affectedRows . ' NHCID = ' . $id;
} else {
$statement = 'Notification history children Update failed. NHCID = ' . $id;
}
} else {
$statement = 'Notification history children Update failed. Bcoz Not Found NHCID';
}
return $statement;
}
}