2024-03-18 01:35:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
|
|
|
|
* by https://t.me/ibnux
|
|
|
|
**/
|
|
|
|
|
|
|
|
_admin();
|
|
|
|
$ui->assign('_title', Lang::T('Send Message'));
|
|
|
|
$ui->assign('_system_menu', 'message');
|
|
|
|
|
|
|
|
$action = $routes['1'];
|
|
|
|
$ui->assign('_admin', $admin);
|
|
|
|
|
|
|
|
if (empty($action)) {
|
|
|
|
$action = 'send';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'send':
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
|
|
|
|
|
|
|
$select2_customer = <<<EOT
|
|
|
|
<script>
|
|
|
|
document.addEventListener("DOMContentLoaded", function(event) {
|
|
|
|
$('#personSelect').select2({
|
|
|
|
theme: "bootstrap",
|
|
|
|
ajax: {
|
|
|
|
url: function(params) {
|
|
|
|
if(params.term != undefined){
|
2024-11-04 13:57:28 +07:00
|
|
|
return './?_route=autoload/customer_select2&s='+params.term;
|
2024-03-18 01:35:48 +01:00
|
|
|
}else{
|
2024-11-04 13:57:28 +07:00
|
|
|
return './?_route=autoload/customer_select2';
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
EOT;
|
2024-03-18 12:28:54 +01:00
|
|
|
if (isset($routes['2']) && !empty($routes['2'])) {
|
|
|
|
$ui->assign('cust', ORM::for_table('tbl_customers')->find_one($routes['2']));
|
|
|
|
}
|
|
|
|
$id = $routes['2'];
|
|
|
|
$ui->assign('id', $id);
|
2024-03-18 01:35:48 +01:00
|
|
|
$ui->assign('xfooter', $select2_customer);
|
2025-02-04 10:56:02 +07:00
|
|
|
$ui->display('admin/message/single.tpl');
|
2024-03-18 01:35:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send-post':
|
|
|
|
// Check user permissions
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get form data
|
|
|
|
$id_customer = $_POST['id_customer'];
|
|
|
|
$message = $_POST['message'];
|
|
|
|
$via = $_POST['via'];
|
|
|
|
|
|
|
|
// Check if fields are empty
|
|
|
|
if ($id_customer == '' or $message == '' or $via == '') {
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send'), 'e', Lang::T('All field is required'));
|
2024-03-18 01:35:48 +01:00
|
|
|
} else {
|
|
|
|
// Get customer details from the database
|
|
|
|
$c = ORM::for_table('tbl_customers')->find_one($id_customer);
|
|
|
|
|
|
|
|
// Replace placeholders in the message with actual values
|
|
|
|
$message = str_replace('[[name]]', $c['fullname'], $message);
|
|
|
|
$message = str_replace('[[user_name]]', $c['username'], $message);
|
|
|
|
$message = str_replace('[[phone]]', $c['phonenumber'], $message);
|
|
|
|
$message = str_replace('[[company_name]]', $config['CompanyName'], $message);
|
|
|
|
|
|
|
|
|
|
|
|
//Send the message
|
|
|
|
if ($via == 'sms' || $via == 'both') {
|
|
|
|
$smsSent = Message::sendSMS($c['phonenumber'], $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($via == 'wa' || $via == 'both') {
|
|
|
|
$waSent = Message::sendWhatsapp($c['phonenumber'], $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($smsSent) || isset($waSent)) {
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send'), 's', Lang::T('Message Sent Successfully'));
|
2024-03-18 01:35:48 +01:00
|
|
|
} else {
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send'), 'e', Lang::T('Failed to send message'));
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send_bulk':
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
2025-02-04 15:13:42 +07:00
|
|
|
set_time_limit(0);
|
2024-03-18 01:35:48 +01:00
|
|
|
// Initialize counters
|
2024-03-21 16:52:52 +01:00
|
|
|
$totalSMSSent = 0;
|
|
|
|
$totalSMSFailed = 0;
|
|
|
|
$totalWhatsappSent = 0;
|
|
|
|
$totalWhatsappFailed = 0;
|
2025-02-04 15:13:42 +07:00
|
|
|
$totalCustomers = 0;
|
|
|
|
$batchStatus = $_SESSION['batchStatus'];
|
|
|
|
$page = _req('page', -1);
|
2024-03-21 16:52:52 +01:00
|
|
|
|
|
|
|
if (_req('send') == 'now') {
|
2025-02-04 15:13:42 +07:00
|
|
|
// Get form data
|
|
|
|
$group = $_REQUEST['group'];
|
|
|
|
$message = $_REQUEST['message'];
|
|
|
|
$via = $_REQUEST['via'];
|
|
|
|
$test = isset($_REQUEST['test']) && $_REQUEST['test'] === 'on' ? 'yes' : 'no';
|
|
|
|
$batch = $_REQUEST['batch'];
|
|
|
|
$delay = $_REQUEST['delay'];
|
|
|
|
|
|
|
|
$ui->assign('group', $group);
|
|
|
|
$ui->assign('message', $message);
|
|
|
|
$ui->assign('via', $via);
|
|
|
|
$ui->assign('test', $test);
|
|
|
|
$ui->assign('batch', $batch);
|
|
|
|
$ui->assign('delay', $delay);
|
|
|
|
if($page<0){
|
|
|
|
$batchStatus = [];
|
|
|
|
$page = 0;
|
|
|
|
}
|
|
|
|
$startpoint = $page * $batch;
|
|
|
|
$page++;
|
2024-03-21 16:52:52 +01:00
|
|
|
// Check if fields are empty
|
|
|
|
if ($group == '' || $message == '' || $via == '') {
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send_bulk'), 'e', Lang::T('All fields are required'));
|
2024-03-21 16:52:52 +01:00
|
|
|
} else {
|
|
|
|
// Get customer details from the database based on the selected group
|
|
|
|
if ($group == 'all') {
|
2025-02-04 15:13:42 +07:00
|
|
|
$customers = ORM::for_table('tbl_customers')
|
|
|
|
->offset($startpoint)
|
|
|
|
->limit($batch)->find_array();
|
2024-03-21 16:52:52 +01:00
|
|
|
} elseif ($group == 'new') {
|
|
|
|
// Get customers created just a month ago
|
2025-02-04 15:13:42 +07:00
|
|
|
$customers = ORM::for_table('tbl_customers')->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)")
|
|
|
|
->offset($startpoint)->limit($batch)
|
|
|
|
->find_array();
|
2024-03-21 16:52:52 +01:00
|
|
|
} elseif ($group == 'expired') {
|
|
|
|
// Get expired user recharges where status is 'off'
|
2025-02-04 15:13:42 +07:00
|
|
|
$expired = ORM::for_table('tbl_user_recharges')->select('customer_id')->where('status', 'off')
|
|
|
|
->offset($startpoint)->limit($batch)
|
|
|
|
->find_array();
|
|
|
|
$customer_ids = array_column($expired, 'customer_id');
|
|
|
|
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_array();
|
2024-03-21 16:52:52 +01:00
|
|
|
} elseif ($group == 'active') {
|
|
|
|
// Get active user recharges where status is 'on'
|
2025-02-04 15:13:42 +07:00
|
|
|
$active = ORM::for_table('tbl_user_recharges')->select('customer_id')->where('status', 'on')
|
|
|
|
->offset($startpoint)->limit($batch)
|
|
|
|
->find_array();
|
|
|
|
$customer_ids = array_column($active, 'customer_id');
|
|
|
|
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_array();
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 16:52:52 +01:00
|
|
|
// Set the batch size
|
|
|
|
$batchSize = $batch;
|
|
|
|
|
|
|
|
// Calculate the number of batches
|
|
|
|
$totalCustomers = count($customers);
|
|
|
|
$totalBatches = ceil($totalCustomers / $batchSize);
|
|
|
|
|
2025-02-04 15:29:29 +07:00
|
|
|
// Loop through customers in the current batch and send messages
|
|
|
|
foreach ($customers as $customer) {
|
|
|
|
// Create a copy of the original message for each customer and save it as currentMessage
|
|
|
|
$currentMessage = $message;
|
|
|
|
$currentMessage = str_replace('[[name]]', $customer['fullname'], $currentMessage);
|
|
|
|
$currentMessage = str_replace('[[user_name]]', $customer['username'], $currentMessage);
|
|
|
|
$currentMessage = str_replace('[[phone]]', $customer['phonenumber'], $currentMessage);
|
|
|
|
$currentMessage = str_replace('[[company_name]]', $config['CompanyName'], $currentMessage);
|
|
|
|
|
|
|
|
if(empty($customer['phonenumber'])){
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'No Phone Number'
|
|
|
|
];
|
|
|
|
}else
|
|
|
|
// Send the message based on the selected method
|
|
|
|
if ($test === 'yes') {
|
|
|
|
// Only for testing, do not send messages to customers
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'Test Mode - Message not sent'
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Send the actual messages
|
|
|
|
if ($via == 'sms' || $via == 'both') {
|
|
|
|
$smsSent = Message::sendSMS($customer['phonenumber'], $currentMessage);
|
|
|
|
if ($smsSent) {
|
|
|
|
$totalSMSSent++;
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'SMS Message Sent'
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$totalSMSFailed++;
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'SMS Message Failed'
|
|
|
|
];
|
2024-03-21 16:52:52 +01:00
|
|
|
}
|
2025-02-04 15:29:29 +07:00
|
|
|
}
|
2024-03-21 16:52:52 +01:00
|
|
|
|
2025-02-04 15:29:29 +07:00
|
|
|
if ($via == 'wa' || $via == 'both') {
|
|
|
|
$waSent = Message::sendWhatsapp($customer['phonenumber'], $currentMessage);
|
|
|
|
if ($waSent) {
|
|
|
|
$totalWhatsappSent++;
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'WhatsApp Message Sent'
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$totalWhatsappFailed++;
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => $customer['phonenumber'],
|
|
|
|
'message' => $currentMessage,
|
|
|
|
'status' => 'WhatsApp Message Failed'
|
|
|
|
];
|
2024-03-21 16:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-04 15:13:42 +07:00
|
|
|
$ui->assign('page', $page);
|
|
|
|
$ui->assign('totalCustomers', $totalCustomers);
|
|
|
|
$_SESSION['batchStatus'] = $batchStatus;
|
2024-03-21 16:52:52 +01:00
|
|
|
$ui->assign('batchStatus', $batchStatus);
|
|
|
|
$ui->assign('totalSMSSent', $totalSMSSent);
|
|
|
|
$ui->assign('totalSMSFailed', $totalSMSFailed);
|
|
|
|
$ui->assign('totalWhatsappSent', $totalWhatsappSent);
|
|
|
|
$ui->assign('totalWhatsappFailed', $totalWhatsappFailed);
|
2025-02-04 10:56:02 +07:00
|
|
|
$ui->display('admin/message/bulk.tpl');
|
2024-03-18 01:35:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send_sms'), 'e', 'action not defined');
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|