From ec9a06f468dd3aa0ddeeffac70b50727c895c8cc Mon Sep 17 00:00:00 2001 From: Focuslinkstech <45756999+Focuslinkstech@users.noreply.github.com> Date: Thu, 6 Mar 2025 18:44:20 +0100 Subject: [PATCH] feat: add service type selection and display in message bulk management --- system/controllers/message.php | 157 +++++++++++++++++++++------------ ui/ui/admin/message/bulk.tpl | 23 ++++- 2 files changed, 122 insertions(+), 58 deletions(-) diff --git a/system/controllers/message.php b/system/controllers/message.php index ec06ea1b..b2df636d 100644 --- a/system/controllers/message.php +++ b/system/controllers/message.php @@ -134,30 +134,55 @@ EOT; $page = $_REQUEST['page'] ?? 0; $router = $_REQUEST['router'] ?? null; $test = isset($_REQUEST['test']) && $_REQUEST['test'] === 'on' ? true : false; + $service = $_REQUEST['service'] ?? ''; - if (empty($group) || empty($message) || empty($via)) { + if (empty($group) || empty($message) || empty($via) || empty($service)) { die(json_encode(['status' => 'error', 'message' => 'All fields are required'])); } // Get batch of customers based on group $startpoint = $page * $batch; $customers = []; + $totalCustomers = 0; if (isset($router) && !empty($router)) { - $router = ORM::for_table('tbl_routers')->find_one($router); - if (!$router) { - die(json_encode(['status' => 'error', 'message' => 'Invalid router'])); + switch ($router) { + case 'radius': + $routerName = 'Radius'; + break; + default: + $router = ORM::for_table('tbl_routers')->find_one($router); + if (!$router) { + die(json_encode(['status' => 'error', 'message' => 'Invalid router'])); + } + $routerName = $router->name; + break; } + } + if (isset($router) && !empty($router)) { $query = ORM::for_table('tbl_user_recharges') ->left_outer_join('tbl_customers', 'tbl_user_recharges.customer_id = tbl_customers.id') - ->where('tbl_user_recharges.routers', $router->name) - ->offset($startpoint) + ->where('tbl_user_recharges.routers', $routerName); + + switch ($service) { + case 'all': + break; + default: + $validServices = ['PPPoE', 'Hotspot', 'VPN']; + if (in_array($service, $validServices)) { + $query->where('type', $service); + } + break; + } + + $totalCustomers = $query->count(); + + $query->offset($startpoint) ->limit($batch); switch ($group) { case 'all': - // No additional conditions needed break; case 'new': $query->where_raw("DATE(recharged_on) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)"); @@ -170,6 +195,7 @@ EOT; break; } + // Fetch the customers $query->selects([ ['tbl_customers.phonenumber', 'phonenumber'], ['tbl_user_recharges.customer_id', 'customer_id'], @@ -179,20 +205,74 @@ EOT; } else { switch ($group) { case 'all': - $customers = ORM::for_table('tbl_customers')->offset($startpoint)->limit($batch)->find_array(); + $totalCustomersQuery = ORM::for_table('tbl_customers'); + + switch ($service) { + case 'all': + break; + default: + $validServices = ['PPPoE', 'Hotspot', 'VPN']; + if (in_array($service, $validServices)) { + $totalCustomersQuery->where('service_type', $service); + } + break; + } + $totalCustomers = $totalCustomersQuery->count(); + $customers = $totalCustomersQuery->offset($startpoint)->limit($batch)->find_array(); break; + case 'new': - $customers = ORM::for_table('tbl_customers') - ->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)") - ->offset($startpoint)->limit($batch)->find_array(); + $totalCustomersQuery = ORM::for_table('tbl_customers') + ->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)"); + + switch ($service) { + case 'all': + break; + default: + $validServices = ['PPPoE', 'Hotspot', 'VPN']; + if (in_array($service, $validServices)) { + $totalCustomersQuery->where('service_type', $service); + } + break; + } + $totalCustomers = $totalCustomersQuery->count(); + $customers = $totalCustomersQuery->offset($startpoint)->limit($batch)->find_array(); break; + case 'expired': - $customers = ORM::for_table('tbl_user_recharges')->where('status', 'off') - ->select('customer_id')->offset($startpoint)->limit($batch)->find_array(); + $totalCustomersQuery = ORM::for_table('tbl_user_recharges') + ->where('status', 'off'); + + switch ($service) { + case 'all': + break; + default: + $validServices = ['PPPoE', 'Hotspot', 'VPN']; + if (in_array($service, $validServices)) { + $totalCustomersQuery->where('type', $service); + } + break; + } + $totalCustomers = $totalCustomersQuery->count(); + $customers = $totalCustomersQuery->select('customer_id')->offset($startpoint)->limit($batch)->find_array(); break; + case 'active': - $customers = ORM::for_table('tbl_user_recharges')->where('status', 'on') - ->select('customer_id')->offset($startpoint)->limit($batch)->find_array(); + $totalCustomersQuery = ORM::for_table('tbl_user_recharges') + ->where('status', 'on'); + + switch ($service) { + case 'all': + break; + default: + $validServices = ['PPPoE', 'Hotspot', 'VPN']; + if (in_array($service, $validServices)) { + $totalCustomersQuery->where('type', $service); + } + break; + } + $totalCustomers = $totalCustomersQuery->count(); + $customers = $totalCustomersQuery->select('customer_id')->offset($startpoint)->limit($batch)->find_array(); // Get customer data break; } } @@ -202,45 +282,6 @@ EOT; $customers = []; } - // Calculate total customers for the group - $totalCustomers = 0; - if ($router) { - switch ($group) { - case 'all': - $totalCustomers = ORM::for_table('tbl_user_recharges')->where('routers', $router->routers)->count(); - break; - case 'new': - $totalCustomers = ORM::for_table('tbl_user_recharges') - ->where_raw("DATE(recharged_on) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)") - ->where('routers', $router->routers) - ->count(); - break; - case 'expired': - $totalCustomers = ORM::for_table('tbl_user_recharges')->where('status', 'off')->where('routers', $router->routers)->count(); - break; - case 'active': - $totalCustomers = ORM::for_table('tbl_user_recharges')->where('status', 'on')->where('routers', $router->routers)->count(); - break; - } - } else { - switch ($group) { - case 'all': - $totalCustomers = ORM::for_table('tbl_customers')->count(); - break; - case 'new': - $totalCustomers = ORM::for_table('tbl_customers') - ->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)") - ->count(); - break; - case 'expired': - $totalCustomers = ORM::for_table('tbl_user_recharges')->where('status', 'off')->count(); - break; - case 'active': - $totalCustomers = ORM::for_table('tbl_user_recharges')->where('status', 'on')->count(); - break; - } - } - // Send messages $totalSMSSent = 0; $totalSMSFailed = 0; @@ -271,7 +312,9 @@ EOT; 'name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'Test Mode', - 'message' => $currentMessage + 'message' => $currentMessage, + 'service' => $service, + 'router' => $routerName, ]; } else { if ($via == 'sms' || $via == 'both') { @@ -307,7 +350,9 @@ EOT; 'message' => $currentMessage, 'totalSent' => $totalSMSSent + $totalWhatsappSent, 'totalFailed' => $totalSMSFailed + $totalWhatsappFailed, - 'hasMore' => $hasMore + 'hasMore' => $hasMore, + 'service' => $service, + 'router' => $routerName, ]); break; diff --git a/ui/ui/admin/message/bulk.tpl b/ui/ui/admin/message/bulk.tpl index fec406bc..a8a57dba 100644 --- a/ui/ui/admin/message/bulk.tpl +++ b/ui/ui/admin/message/bulk.tpl @@ -15,12 +15,26 @@
+
+ +
+ +
+
@@ -101,6 +115,8 @@ {Lang::T('Phone')} {Lang::T('Status')} {Lang::T('Message')} + {Lang::T('Router')} + {Lang::T('Service Type')} @@ -140,7 +156,8 @@ batch: $('#batch').val(), router: $('#router').val() || '', page: page, - test: $('#test').is(':checked') ? 'on' : 'off' + test: $('#test').is(':checked') ? 'on' : 'off', + service: $('#service').val(), }, dataType: 'json', beforeSend: function () { @@ -171,7 +188,9 @@ msg.name, msg.phone, `${msg.status}`, - msg.message || 'No message' + msg.message || 'No message', + msg.router ? msg.router : 'All Router', + msg.service == 'all' ? 'All Service' : (msg.service || 'No Service') ]).draw(false); // Add row without redrawing the table });