fix bulk broadcasting message

This commit is contained in:
iBNu Maksum 2025-02-04 15:13:42 +07:00
parent 3386b17b1b
commit 8d9919afa7
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5
5 changed files with 136 additions and 67 deletions

View File

@ -95,49 +95,65 @@ EOT;
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
$group = $_POST['group'];
$message = $_POST['message'];
$via = $_POST['via'];
$test = isset($_POST['test']) && $_POST['test'] === 'on' ? 'yes' : 'no';
$batch = $_POST['batch'];
$delay = $_POST['delay'];
set_time_limit(0);
// Initialize counters
$totalSMSSent = 0;
$totalSMSFailed = 0;
$totalWhatsappSent = 0;
$totalWhatsappFailed = 0;
$batchStatus = [];
$totalCustomers = 0;
$batchStatus = $_SESSION['batchStatus'];
$page = _req('page', -1);
if (_req('send') == 'now') {
// 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++;
// Check if fields are empty
if ($group == '' || $message == '' || $via == '') {
r2(getUrl('message/send_bulk'), 'e', Lang::T('All fields are required'));
} else {
// Get customer details from the database based on the selected group
if ($group == 'all') {
$customers = ORM::for_table('tbl_customers')->find_many()->as_array();
$customers = ORM::for_table('tbl_customers')
->offset($startpoint)
->limit($batch)->find_array();
} elseif ($group == 'new') {
// Get customers created just a month ago
$customers = ORM::for_table('tbl_customers')->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)")->find_many()->as_array();
$customers = ORM::for_table('tbl_customers')->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)")
->offset($startpoint)->limit($batch)
->find_array();
} elseif ($group == 'expired') {
// Get expired user recharges where status is 'off'
$expired = ORM::for_table('tbl_user_recharges')->where('status', 'off')->find_many();
$customer_ids = [];
foreach ($expired as $recharge) {
$customer_ids[] = $recharge->customer_id;
}
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_many()->as_array();
$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();
} elseif ($group == 'active') {
// Get active user recharges where status is 'on'
$active = ORM::for_table('tbl_user_recharges')->where('status', 'on')->find_many();
$customer_ids = [];
foreach ($active as $recharge) {
$customer_ids[] = $recharge->customer_id;
}
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_many()->as_array();
$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();
}
// Set the batch size
@ -225,6 +241,9 @@ EOT;
}
}
}
$ui->assign('page', $page);
$ui->assign('totalCustomers', $totalCustomers);
$_SESSION['batchStatus'] = $batchStatus;
$ui->assign('batchStatus', $batchStatus);
$ui->assign('totalSMSSent', $totalSMSSent);
$ui->assign('totalSMSFailed', $totalSMSFailed);

View File

@ -967,5 +967,15 @@
"Create_Bandwidth_Package_for_expired_Internet_Package": "Create Bandwidth Package for expired Internet Package",
"Maps": "Maps",
"Enable": "Enable",
"Continue_the_process_of_changing_Routers_": "Continue the process of changing Routers?"
"Continue_the_process_of_changing_Routers_": "Continue the process of changing Routers?",
"Customer_cannot_buy_disabled_Package__but_admin_can_recharge_it__use_it_if_you_want_only_admin_recharge_it": "Customer cannot buy disabled Package, but admin can recharge it, use it if you want only admin recharge it",
"Postpaid_will_have_fix_expired_date": "Postpaid will have fix expired date",
"Personal_Package_will_only_show_to_personal_Customer__Business_package_will_only_show_to_Business_Customer": "Personal Package will only show to personal Customer, Business package will only show to Business Customer",
"If_you_enable_Radius__choose_device_to_radius__except_if_you_have_custom_device_": "If you enable Radius, choose device to radius, except if you have custom device.",
"This_Device_are_the_logic_how_PHPNuxBill_Communicate_with_Mikrotik_or_other_Devices": "This Device are the logic how PHPNuxBill Communicate with Mikrotik or other Devices",
"Expired_will_be_this_date_every_month": "Expired will be this date every month",
"Continue_the_process_of_adding_the_PPPoE_Package_": "Continue the process of adding the PPPoE Package?",
"Personal_Package_will_only_show_to_personal_Customer__Business_Package_will_only_show_to_Business_Customer": "Personal Package will only show to personal Customer, Business Package will only show to Business Customer",
"Continue_the_PPPoE_Package_change_process_": "Continue the PPPoE Package change process?",
"Sending_message_in_progress__Don_t_close_this_page_": "Sending message in progress. Don't close this page."
}

View File

@ -113,15 +113,16 @@
});
function ask(field, text){
var txt = field.innerHTML;
if (confirm(text)) {
setTimeout(() => {
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, '');
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, txt);
field.removeAttribute("disabled");
}, 5000);
return true;
} else {
setTimeout(() => {
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, '');
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, txt);
field.removeAttribute("disabled");
}, 500);
return false;

View File

@ -4,18 +4,26 @@
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="panel panel-primary panel-hovered panel-stacked mb30">
{if $page>0 && $totalCustomers>0}
<div class="alert alert-info" role="alert">{Lang::T("Sending message in progress. Don't close this page.")}</div>
{/if}
<div class="panel panel-primary panel-hovered panel-stacked mb30 {if $page>0 && $totalCustomers >0}hidden{/if}">
<div class="panel-heading">{Lang::T('Send Bulk Message')}</div>
<div class="panel-body">
<form class="form-horizontal" method="post" role="form" id="bulkMessageForm" action="">
<form class="form-horizontal" method="get" role="form" id="bulkMessageForm" action="">
<input type="hidden" name="page" value="{if $page>0 && $totalCustomers==0}-1{else}{$page}{/if}">
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Group')}</label>
<div class="col-md-6">
<select class="form-control" name="group" id="group">
<option value="all" selected>{Lang::T('All Customers')}</option>
<option value="new">{Lang::T('New Customers')}</option>
<option value="expired">{Lang::T('Expired Customers')}</option>
<option value="active">{Lang::T('Active Customers')}</option>
<option value="all" {if $group == 'all'}selected{/if}>{Lang::T('All Customers')}
</option>
<option value="new" {if $group == 'new'}selected{/if}>{Lang::T('New Customers')}
</option>
<option value="expired" {if $group == 'expired'}selected{/if}>
{Lang::T('Expired Customers')}</option>
<option value="active" {if $group == 'active'}selected{/if}>
{Lang::T('Active Customers')}</option>
</select>
</div>
</div>
@ -23,9 +31,10 @@
<label class="col-md-2 control-label">{Lang::T('Send Via')}</label>
<div class="col-md-6">
<select class="form-control" name="via" id="via">
<option value="sms" selected>{Lang::T('SMS')}</option>
<option value="wa">{Lang::T('WhatsApp')}</option>
<option value="both">{Lang::T('SMS and WhatsApp')}</option>
<option value="sms" {if $via == 'sms'}selected{/if}>{Lang::T('SMS')}</option>
<option value="wa" {if $via == 'wa'}selected{/if}>{Lang::T('WhatsApp')}</option>
<option value="both" {if $via == 'both'}selected{/if}>{Lang::T('SMS and WhatsApp')}
</option>
</select>
</div>
</div>
@ -33,14 +42,14 @@
<label class="col-md-2 control-label">{Lang::T('Message per time')}</label>
<div class="col-md-6">
<select class="form-control" name="batch" id="batch">
<option value="5">{Lang::T('5 Messages')}</option>
<option value="10" selected>{Lang::T('10 Messages')}</option>
<option value="15">{Lang::T('15 Messages')}</option>
<option value="20">{Lang::T('20 Messages')}</option>
<option value="20">{Lang::T('30 Messages')}</option>
<option value="20">{Lang::T('40 Messages')}</option>
<option value="20">{Lang::T('50 Messages')}</option>
<option value="20">{Lang::T('60 Messages')}</option>
<option value="5" {if $batch == '5'}selected{/if}>{Lang::T('5 Messages')}</option>
<option value="10" {if $batch == '10'}selected{/if}>{Lang::T('10 Messages')}</option>
<option value="15" {if $batch == '15'}selected{/if}>{Lang::T('15 Messages')}</option>
<option value="20" {if $batch == '20'}selected{/if}>{Lang::T('20 Messages')}</option>
<option value="30" {if $batch == '30'}selected{/if}>{Lang::T('30 Messages')}</option>
<option value="40" {if $batch == '40'}selected{/if}>{Lang::T('40 Messages')}</option>
<option value="50" {if $batch == '50'}selected{/if}>{Lang::T('50 Messages')}</option>
<option value="60" {if $batch == '60'}selected{/if}>{Lang::T('60 Messages')}</option>
</select>{Lang::T('Use 20 and above if you are sending to all customers to avoid server time out')}
</div>
</div>
@ -48,20 +57,21 @@
<label class="col-md-2 control-label">{Lang::T('Delay')}</label>
<div class="col-md-6">
<select class="form-control" name="delay" id="delay">
<option value="0" selected>{Lang::T('No Delay')}</option>
<option value="5">{Lang::T('5 Seconds')}</option>
<option value="10">{Lang::T('10 Seconds')}</option>
<option value="15">{Lang::T('15 Seconds')}</option>
<option value="20">{Lang::T('20 Seconds')}</option>
<option value="0" {if $delay == '0'}selected{/if}>{Lang::T('No Delay')}</option>
<option value="5" {if $delay == '5'}selected{/if}>{Lang::T('5 Seconds')}</option>
<option value="10" {if $delay == '10'}selected{/if}>{Lang::T('10 Seconds')}</option>
<option value="15" {if $delay == '15'}selected{/if}>{Lang::T('15 Seconds')}</option>
<option value="20" {if $delay == '20'}selected{/if}>{Lang::T('20 Seconds')}</option>
</select>{Lang::T('Use at least 5 secs if you are sending to all customers to avoid being banned by your message provider')}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Message')}</label>
<div class="col-md-6">
<textarea class="form-control" id="message" name="message"
placeholder="{Lang::T('Compose your message...')}" rows="5"></textarea>
<input name="test" type="checkbox"> {Lang::T('Testing [if checked no real message is sent]')}
<textarea class="form-control" id="message" name="message" required
placeholder="{Lang::T('Compose your message...')}" rows="5">{$message}</textarea>
<input name="test" type="checkbox">
{Lang::T('Testing [if checked no real message is sent]')}
</div>
<p class="help-block col-md-4">
{Lang::T('Use placeholders:')}
@ -77,8 +87,15 @@
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" onclick="return ask(this, 'Continue the process of sending mass messages?')" type="submit" name=send value=now>
{Lang::T('Send Message')}</button>
{if $page >= 0}
<button class="btn btn-success" id="submit" type="submit" name=send value=now>
{Lang::T('Send Message')}</button>
{else}
<button class="btn btn-success"
onclick="return ask(this, 'Continue the process of sending mass messages?')"
type="submit" name=send value=now>
{Lang::T('Send Message')}</button>
{/if}
<a href="{$_url}dashboard" class="btn btn-default">{Lang::T('Cancel')}</a>
</div>
</div>
@ -90,10 +107,11 @@
</div>
{if $batchStatus}
<p><span class="label label-success">{Lang::T('Total SMS Sent')}: {$totalSMSSent}</span> <span class="label label-danger">{Lang::T('Total SMS
<p><span class="label label-success">{Lang::T('Total SMS Sent')}: {$totalSMSSent}</span> <span
class="label label-danger">{Lang::T('Total SMS
Failed')}: {$totalSMSFailed}</span> <span class="label label-success">{Lang::T('Total WhatsApp Sent')}:
{$totalWhatsappSent}</span> <span class="label label-danger">{Lang::T('Total WhatsApp Failed')}:
{$totalWhatsappFailed}</span></p>
{$totalWhatsappSent}</span> <span class="label label-danger">{Lang::T('Total WhatsApp Failed')}:
{$totalWhatsappFailed}</span></p>
{/if}
<div class="box">
<div class="box-header">
@ -112,12 +130,12 @@
</thead>
<tbody>
{foreach $batchStatus as $customer}
<tr>
<td>{$customer.name}</td>
<td>{$customer.phone}</td>
<td>{$customer.message}</td>
<td>{$customer.status}</td>
</tr>
<tr>
<td>{$customer.name}</td>
<td>{$customer.phone}</td>
<td>{$customer.message}</td>
<td>{$customer.status}</td>
</tr>
{/foreach}
</tbody>
</table>
@ -131,12 +149,32 @@
<script>
var $j = jQuery.noConflict();
$j(document).ready(function () {
$j(document).ready(function() {
$j('#messageResultsTable').DataTable();
});
{if $page>0 && $totalCustomers >0}
setTimeout(() => {
document.getElementById('submit').click();
}, 2000);
{/if}
{if $page>0 && $totalCustomers==0}
Swal.fire({
icon: 'success',
title: 'Bulk Send Done',
position: 'top-end',
showConfirmButton: false,
timer: 5000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
});
{/if}
</script>
{include file="sections/footer.tpl"}
{include file="sections/footer.tpl"}

View File

@ -160,15 +160,16 @@
});
function ask(field, text){
var txt = field.innerHTML;
if (confirm(text)) {
setTimeout(() => {
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, '');
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, txt);
field.removeAttribute("disabled");
}, 5000);
return true;
} else {
setTimeout(() => {
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, '');
field.innerHTML = field.innerHTML.replace(`<span class="loading"></span>`, txt);
field.removeAttribute("disabled");
}, 500);
return false;