donation/app/Models/NotificationModel.php

187 lines
7.9 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class NotificationModel extends Model
{
protected $table;
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(){
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')
->get()->getResultArray();
}
public function saveDetails($data,$pk_name,$t_name){
$id = $data[$pk_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'] = ucfirst($t_name).' has been updated successfully.';
$statement['error'] = "";
$statement['log'] = ucfirst($t_name).": has been updated successfully. Updated ".$pk_name." = " .$id." Affected Rows = ".$affectedRows;
} else {
$statement['success'] = "";
$statement['error'] = ucfirst($t_name).' update failed. Please try again.';
$statement['log'] = ucfirst($t_name).": 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'] = ucfirst($t_name).' has been Inserted successfully.';
$statement['error'] = "";
$statement['log'] = ucfirst($t_name).": has been added successfully. Inserted ID = " .$insertID ;
} else {
$statement['success'] = "";
$statement['error'] = ucfirst($t_name)." could not be added. Please try again..";
$statement['log'] = ucfirst($t_name).": Err could not be added. Please try again.";
}
}
return $statement;
}
public function getCampaignDetails(){
$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,NC.scheduled_time')
->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('customer_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) {
$customerId = $customer->customer_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;
}
public function getExpDate()
{
$db = \Config\Database::connect();
$query = "SELECT * FROM customer_groups WHERE group_name = 'EXPIRYDAT'";
$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()
{
$db = \Config\Database::connect();
$query = "SELECT * FROM templates WHERE template_name = '".EXPIRAY_NOTIFY_TEMPLATE_EMAIL."'";
// 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;
}
}
}