phpnuxbill/system/controllers/prepaid.php

674 lines
29 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2023-10-18 18:24:00 +08:00
2017-03-11 03:51:06 +08:00
/**
2023-10-12 16:55:42 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2021-08-19 14:38:29 +08:00
**/
2023-10-12 16:55:42 +08:00
2017-03-11 03:51:06 +08:00
_admin();
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Recharge Account'));
2017-03-11 03:51:06 +08:00
$ui->assign('_system_menu', 'prepaid');
$action = $routes['1'];
$admin = Admin::_info();
$ui->assign('_admin', $admin);
2023-08-23 13:11:07 +08:00
$select2_customer = <<<EOT
<script>
document.addEventListener("DOMContentLoaded", function(event) {
$('#personSelect').select2({
theme: "bootstrap",
ajax: {
url: function(params) {
if(params.term != undefined){
return './index.php?_route=autoload/customer_select2&s='+params.term;
}else{
return './index.php?_route=autoload/customer_select2';
}
}
}
});
});
</script>
EOT;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
switch ($action) {
2023-09-15 15:15:43 +08:00
case 'sync':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2023-09-15 15:15:43 +08:00
set_time_limit(-1);
$plans = ORM::for_table('tbl_user_recharges')->where('status', 'on')->find_many();
$log = '';
$router = '';
foreach ($plans as $plan) {
2023-10-12 16:47:45 +08:00
if ($router != $plan['routers'] && $plan['routers'] != 'radius') {
2023-09-15 15:15:43 +08:00
$mikrotik = Mikrotik::info($plan['routers']);
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
$router = $plan['routers'];
}
$p = ORM::for_table('tbl_plans')->findOne($plan['plan_id']);
$c = ORM::for_table('tbl_customers')->findOne($plan['customer_id']);
2023-10-18 18:24:00 +08:00
if ($plan['routers'] == 'radius') {
Radius::customerAddPlan($c, $p, $plan['expiration'] . ' ' . $plan['time']);
} else {
2023-10-12 16:47:45 +08:00
if ($plan['type'] == 'Hotspot') {
Mikrotik::addHotspotUser($client, $p, $c);
} else if ($plan['type'] == 'PPPOE') {
Mikrotik::addPpoeUser($client, $p, $c);
}
2023-09-15 15:15:43 +08:00
}
$log .= "DONE : $plan[username], $plan[namebp], $plan[type], $plan[routers]<br>";
}
2024-02-19 17:28:55 +08:00
if ($isApi) {
showResult(true, $log);
}
2023-09-15 15:15:43 +08:00
r2(U . 'prepaid/list', 's', $log);
2021-08-19 14:38:29 +08:00
case 'list':
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/prepaid.js"></script>');
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Customer'));
2024-02-19 17:28:55 +08:00
$search = _post('search');
if ($search != '') {
$paginator = Paginator::build(ORM::for_table('tbl_user_recharges'), ['username' => '%' . $search . '%'], $search);
$d = ORM::for_table('tbl_user_recharges')->where_like('username', '%' . $search . '%')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
2021-08-19 14:38:29 +08:00
} else {
2023-10-24 13:27:30 +08:00
$paginator = Paginator::build(ORM::for_table('tbl_user_recharges'));
2024-02-19 17:28:55 +08:00
$d = ORM::for_table('tbl_user_recharges')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_array();
}
run_hook('view_list_billing'); #HOOK
if ($isApi) {
showResult(true, $action, $d, ['search' => $search]);
2021-08-19 14:38:29 +08:00
}
$ui->assign('d', $d);
2024-02-19 17:28:55 +08:00
$ui->assign('search', $search);
2021-08-19 14:38:29 +08:00
$ui->assign('paginator', $paginator);
$ui->display('prepaid.tpl');
2017-03-11 03:51:06 +08:00
break;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
case 'recharge':
2023-08-23 13:11:07 +08:00
$ui->assign('xfooter', $select2_customer);
2022-09-08 15:00:46 +08:00
$p = ORM::for_table('tbl_plans')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('p', $p);
2022-09-08 15:00:46 +08:00
$r = ORM::for_table('tbl_routers')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('r', $r);
2023-09-15 15:15:43 +08:00
if (isset($routes['2']) && !empty($routes['2'])) {
2023-09-15 12:33:46 +08:00
$ui->assign('cust', ORM::for_table('tbl_customers')->find_one($routes['2']));
}
2022-09-18 01:00:40 +08:00
run_hook('view_recharge'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('recharge.tpl');
break;
case 'recharge-user':
2021-08-19 14:38:29 +08:00
$id = $routes['2'];
$ui->assign('id', $id);
2017-03-11 03:51:06 +08:00
2021-08-19 14:38:29 +08:00
$c = ORM::for_table('tbl_customers')->find_many();
$ui->assign('c', $c);
2022-09-08 15:00:46 +08:00
$p = ORM::for_table('tbl_plans')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('p', $p);
2022-09-08 15:00:46 +08:00
$r = ORM::for_table('tbl_routers')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('r', $r);
2022-09-18 01:00:40 +08:00
run_hook('view_recharge_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('recharge-user.tpl');
2021-08-19 14:38:29 +08:00
break;
2017-03-11 03:51:06 +08:00
case 'recharge-post':
$id_customer = _post('id_customer');
2021-08-19 14:38:29 +08:00
$type = _post('type');
2017-03-11 03:51:06 +08:00
$server = _post('server');
2021-08-19 14:38:29 +08:00
$plan = _post('plan');
$date_only = date("Y-m-d");
$time = date("H:i:s");
$msg = '';
if ($id_customer == '' or $type == '' or $server == '' or $plan == '') {
$msg .= 'All field is required' . '<br>';
}
if ($msg == '') {
2023-08-23 13:11:07 +08:00
if (Package::rechargeUser($id_customer, $server, $plan, "Recharge", $admin['fullname'])) {
2023-08-15 18:01:48 +08:00
$c = ORM::for_table('tbl_customers')->where('id', $id_customer)->find_one();
$in = ORM::for_table('tbl_transactions')->where('username', $c['username'])->order_by_desc('id')->find_one();
2024-02-23 15:40:47 +08:00
Package::createInvoice($in);
2023-08-15 18:01:48 +08:00
$ui->display('invoice.tpl');
_log('[' . $admin['username'] . ']: ' . 'Recharge ' . $c['username'] . ' [' . $in['plan_name'] . '][' . Lang::moneyFormat($in['price']) . ']', $admin['user_type'], $admin['id']);
2023-08-23 13:11:07 +08:00
} else {
2023-08-15 18:01:48 +08:00
r2(U . 'prepaid/recharge', 'e', "Failed to recharge account");
2022-09-05 16:11:22 +08:00
}
2021-08-19 14:38:29 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'prepaid/recharge', 'e', $msg);
}
break;
2021-08-19 14:38:29 +08:00
2023-10-18 18:49:54 +08:00
case 'view':
$id = $routes['2'];
2024-02-23 12:39:25 +08:00
$in = ORM::for_table('tbl_transactions')->where('id', $id)->find_one();
$ui->assign('in', $in);
2023-10-24 13:27:30 +08:00
if (!empty($routes['3']) && $routes['3'] == 'send') {
2023-10-20 14:57:12 +08:00
$c = ORM::for_table('tbl_customers')->where('username', $d['username'])->find_one();
2023-10-24 13:27:30 +08:00
if ($c) {
2023-10-20 14:57:12 +08:00
Message::sendInvoice($c, $d);
2023-10-24 13:27:30 +08:00
r2(U . 'prepaid/view/' . $id, 's', "Success send to customer");
2023-10-20 14:57:12 +08:00
}
2023-10-24 13:27:30 +08:00
r2(U . 'prepaid/view/' . $id, 'd', "Customer not found");
2023-10-18 18:49:54 +08:00
}
2024-02-23 15:40:47 +08:00
Package::createInvoice($in);
2023-10-20 14:57:12 +08:00
$ui->assign('_title', 'View Invoice');
2023-10-18 18:49:54 +08:00
$ui->display('invoice.tpl');
break;
2021-08-19 14:38:29 +08:00
case 'print':
$content = $_POST['content'];
2024-02-19 17:28:55 +08:00
if (!empty($content)) {
2024-02-23 12:39:25 +08:00
if ($_POST['nux'] == 'print') {
2024-02-22 19:27:18 +08:00
//header("Location: nux://print?text=".urlencode($content));
2024-02-23 12:39:25 +08:00
$ui->assign('nuxprint', "nux://print?text=" . urlencode($content));
2024-02-22 19:27:18 +08:00
}
$ui->assign('content', $content);
2024-02-19 17:28:55 +08:00
} else {
$id = _post('id');
$d = ORM::for_table('tbl_transactions')->where('id', $id)->find_one();
$ui->assign('in', $d);
$ui->assign('date', Lang::dateAndTimeFormat($d['recharged_on'], $d['recharged_time']));
}
2021-08-19 14:38:29 +08:00
2022-09-18 01:00:40 +08:00
run_hook('print_invoice'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('invoice-print.tpl');
break;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
case 'edit':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2017-03-11 03:51:06 +08:00
$id = $routes['2'];
$d = ORM::for_table('tbl_user_recharges')->find_one($id);
2021-08-19 14:38:29 +08:00
if ($d) {
$ui->assign('d', $d);
$p = ORM::for_table('tbl_plans')->where('enabled', '1')->where_not_equal('type', 'Balance')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('p', $p);
2022-09-18 01:00:40 +08:00
run_hook('view_edit_customer_plan'); #HOOK
2024-02-02 14:38:22 +08:00
$ui->assign('_title', 'Edit Plan');
2017-03-11 03:51:06 +08:00
$ui->display('prepaid-edit.tpl');
2021-08-19 14:38:29 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'services/list', 'e', $_L['Account_Not_Found']);
}
break;
case 'delete':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2017-03-11 03:51:06 +08:00
$id = $routes['2'];
$d = ORM::for_table('tbl_user_recharges')->find_one($id);
2021-08-19 14:38:29 +08:00
if ($d) {
2022-09-18 01:00:40 +08:00
run_hook('delete_customer_active_plan'); #HOOK
2023-10-04 17:11:55 +08:00
$p = ORM::for_table('tbl_plans')->find_one($d['plan_id']);
if ($p['is_radius']) {
Radius::customerDeactivate($d['username']);
} else {
$mikrotik = Mikrotik::info($d['routers']);
if ($d['type'] == 'Hotspot') {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-09-07 10:20:31 +08:00
Mikrotik::removeHotspotUser($client, $d['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removeHotspotActiveUser($client, $d['username']);
2023-10-04 17:11:55 +08:00
} else {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-09-07 10:20:31 +08:00
Mikrotik::removePpoeUser($client, $d['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removePpoeActive($client, $d['username']);
2021-08-19 14:38:29 +08:00
}
}
2023-10-04 17:11:55 +08:00
$d->delete();
_log('[' . $admin['username'] . ']: ' . 'Delete Plan for Customer ' . $c['username'] . ' [' . $in['plan_name'] . '][' . Lang::moneyFormat($in['price']) . ']', $admin['user_type'], $admin['id']);
2024-02-13 14:54:01 +08:00
r2(U . 'prepaid/list', 's', Lang::T('Data Deleted Successfully'));
2017-03-11 03:51:06 +08:00
}
break;
case 'edit-post':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2017-03-11 03:51:06 +08:00
$username = _post('username');
$id_plan = _post('id_plan');
$recharged_on = _post('recharged_on');
2021-08-19 14:38:29 +08:00
$expiration = _post('expiration');
2023-08-28 09:57:33 +08:00
$time = _post('time');
2017-03-11 03:51:06 +08:00
$id = _post('id');
$d = ORM::for_table('tbl_user_recharges')->find_one($id);
2021-08-19 14:38:29 +08:00
if ($d) {
} else {
2024-02-13 14:54:01 +08:00
$msg .= Lang::T('Data Not Found') . '<br>';
2017-03-11 03:51:06 +08:00
}
2023-11-21 14:47:10 +08:00
$p = ORM::for_table('tbl_plans')->where('id', $id_plan)->where('enabled', '1')->find_one();
2023-11-15 12:08:18 +08:00
if ($d) {
} else {
$msg .= ' Plan Not Found<br>';
}
2021-08-19 14:38:29 +08:00
if ($msg == '') {
2022-09-18 01:00:40 +08:00
run_hook('edit_customer_plan'); #HOOK
2017-03-11 03:51:06 +08:00
$d->username = $username;
$d->plan_id = $id_plan;
2024-02-02 14:38:22 +08:00
$d->namebp = $p['name_plan'];
2023-08-28 10:44:57 +08:00
//$d->recharged_on = $recharged_on;
2017-03-11 03:51:06 +08:00
$d->expiration = $expiration;
2023-08-28 09:57:33 +08:00
$d->time = $time;
2024-02-19 17:28:55 +08:00
if ($d['status'] == 'off') {
if (strtotime($expiration . ' ' . $time) > time()) {
2024-02-02 14:38:22 +08:00
$d->status = 'on';
}
2024-01-29 12:34:07 +08:00
}
2024-02-19 17:28:55 +08:00
if ($p['is_radius']) {
2023-11-20 10:02:33 +08:00
$d->routers = 'radius';
2024-02-19 17:28:55 +08:00
} else {
2023-11-20 10:02:33 +08:00
$d->routers = $p['routers'];
}
2017-03-11 03:51:06 +08:00
$d->save();
2024-02-19 17:28:55 +08:00
if ($d['status'] == 'on') {
2024-02-02 14:39:03 +08:00
Package::changeTo($username, $id_plan, $id);
}
_log('[' . $admin['username'] . ']: ' . 'Edit Plan for Customer ' . $d['username'] . ' to [' . $d['namebp'] . '][' . Lang::moneyFormat($p['price']) . ']', $admin['user_type'], $admin['id']);
2024-02-13 14:54:01 +08:00
r2(U . 'prepaid/list', 's', Lang::T('Data Updated Successfully'));
2021-08-19 14:38:29 +08:00
} else {
r2(U . 'prepaid/edit/' . $id, 'e', $msg);
2017-03-11 03:51:06 +08:00
}
break;
2021-08-19 14:38:29 +08:00
case 'voucher':
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Prepaid Vouchers'));
2024-02-20 17:49:03 +08:00
$limit = 10;
$page = _get('p', 0);
$pageNow = $page * $limit;
$search = _req('search');
if ($search != '') {
if (in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
$d = ORM::for_table('tbl_plans')->where('enabled', '1')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where_like('tbl_voucher.code', '%' . $search . '%')
->offset($pageNow)
->limit($limit)
->findArray();
} else if ($admin['user_type'] == 'Agent') {
$sales = [];
$sls = ORM::for_table('tbl_users')->select('id')->where('root', $admin['id'])->findArray();
foreach ($sls as $s) {
$sales[] = $s['id'];
}
$sales[] = $admin['id'];
$d = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where_in('generated_by', $sales)
->where_like('tbl_voucher.code', '%' . $search . '%')
->offset($pageNow)
->limit($limit)
->findArray();
}
2021-08-19 14:38:29 +08:00
} else {
2024-02-20 17:49:03 +08:00
if (in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
$d = ORM::for_table('tbl_plans')->where('enabled', '1')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->offset($pageNow)
->limit($limit)
->findArray();
} else if ($admin['user_type'] == 'Agent') {
$sales = [];
$sls = ORM::for_table('tbl_users')->select('id')->where('root', $admin['id'])->findArray();
foreach ($sls as $s) {
$sales[] = $s['id'];
}
$sales[] = $admin['id'];
$d = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where_in('generated_by', $sales)
->offset($pageNow)
->limit($limit)
->findArray();
}
2021-08-19 14:38:29 +08:00
}
// extract admin
$admins = [];
foreach ($d as $k) {
2024-02-19 17:28:55 +08:00
if (!empty($k['generated_by'])) {
$admins[] = $k['generated_by'];
}
}
2024-02-19 17:28:55 +08:00
if (count($admins) > 0) {
$adms = ORM::for_table('tbl_users')->where_in('id', $admins)->find_many();
unset($admins);
2024-02-19 17:28:55 +08:00
foreach ($adms as $adm) {
$tipe = $adm['user_type'];
2024-02-19 17:28:55 +08:00
if ($tipe == 'Sales') {
$tipe = ' [S]';
2024-02-19 17:28:55 +08:00
} else if ($tipe == 'Agent') {
$tipe = ' [A]';
2024-02-19 17:28:55 +08:00
} else {
$tipe == '';
}
2024-02-19 17:28:55 +08:00
$admins[$adm['id']] = $adm['fullname'] . $tipe;
}
}
$ui->assign('admins', $admins);
2021-08-19 14:38:29 +08:00
$ui->assign('d', $d);
2024-02-20 17:49:03 +08:00
$ui->assign('search', $search);
$ui->assign('page', $page);
2022-09-18 01:00:40 +08:00
run_hook('view_list_voucher'); #HOOK
2021-08-19 14:38:29 +08:00
$ui->display('voucher.tpl');
2017-03-11 03:51:06 +08:00
break;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
case 'add-voucher':
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Add Vouchers'));
2021-08-19 14:38:29 +08:00
$c = ORM::for_table('tbl_customers')->find_many();
$ui->assign('c', $c);
2022-09-08 15:00:46 +08:00
$p = ORM::for_table('tbl_plans')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('p', $p);
2022-09-08 15:00:46 +08:00
$r = ORM::for_table('tbl_routers')->where('enabled', '1')->find_many();
2021-08-19 14:38:29 +08:00
$ui->assign('r', $r);
2022-09-18 01:00:40 +08:00
run_hook('view_add_voucher'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('voucher-add.tpl');
break;
2021-08-19 14:38:29 +08:00
2023-12-19 12:45:18 +08:00
case 'remove-voucher':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2023-12-19 12:45:18 +08:00
$d = ORM::for_table('tbl_voucher')->where_equal('status', '1')->findMany();
if ($d) {
$jml = 0;
foreach ($d as $v) {
2024-02-19 17:28:55 +08:00
if (!ORM::for_table('tbl_user_recharges')->where_equal("method", 'Voucher - ' . $v['code'])->findOne()) {
$v->delete();
$jml++;
}
}
2024-02-19 17:28:55 +08:00
r2(U . 'prepaid/voucher', 's', "$jml " . Lang::T('Data Deleted Successfully'));
2023-12-19 12:45:18 +08:00
}
2021-08-19 14:38:29 +08:00
case 'print-voucher':
2023-09-26 14:50:02 +08:00
$from_id = _post('from_id');
$planid = _post('planid');
$pagebreak = _post('pagebreak');
$limit = _post('limit');
2023-10-18 18:24:00 +08:00
$vpl = _post('vpl');
2023-10-24 13:27:30 +08:00
if (empty($vpl)) {
2023-10-18 18:24:00 +08:00
$vpl = 3;
}
if ($pagebreak < 1) $pagebreak = 12;
2021-08-19 14:38:29 +08:00
if ($limit < 1) $limit = $pagebreak * 2;
2023-10-24 13:27:30 +08:00
if (empty($from_id)) {
2023-10-18 18:24:00 +08:00
$from_id = 0;
}
2021-08-19 14:38:29 +08:00
if ($from_id > 0 && $planid > 0) {
$v = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->where('tbl_plans.id', $planid)
->where_gt('tbl_voucher.id', $from_id)
2024-02-20 17:49:03 +08:00
->limit($limit);
2021-08-19 14:38:29 +08:00
$vc = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->where('tbl_plans.id', $planid)
2024-02-20 17:49:03 +08:00
->where_gt('tbl_voucher.id', $from_id);
2021-08-19 14:38:29 +08:00
} else if ($from_id == 0 && $planid > 0) {
$v = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->where('tbl_plans.id', $planid)
2024-02-20 17:49:03 +08:00
->limit($limit);
2021-08-19 14:38:29 +08:00
$vc = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
2024-02-20 17:49:03 +08:00
->where('tbl_plans.id', $planid);
2021-08-19 14:38:29 +08:00
} else if ($from_id > 0 && $planid == 0) {
$v = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->where_gt('tbl_voucher.id', $from_id)
2024-02-20 17:49:03 +08:00
->limit($limit);
2021-08-19 14:38:29 +08:00
$vc = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
2024-02-20 17:49:03 +08:00
->where_gt('tbl_voucher.id', $from_id);
2021-08-19 14:38:29 +08:00
} else {
$v = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
2024-02-20 17:49:03 +08:00
->limit($limit);
2021-08-19 14:38:29 +08:00
$vc = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
2024-02-20 17:49:03 +08:00
->where('tbl_voucher.status', '0');
}
if (in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
$v = $v->find_many();
$vc = $vc->count();
} else {
$sales = [];
$sls = ORM::for_table('tbl_users')->select('id')->where('root', $admin['id'])->findArray();
foreach ($sls as $s) {
$sales[] = $s['id'];
}
$sales[] = $admin['id'];
$v = $v->where_in('generated_by', $sales)->find_many();
$vc = $vc->where_in('generated_by', $sales)->count();
2021-08-19 14:38:29 +08:00
}
2023-10-18 18:24:00 +08:00
$template = file_get_contents("pages/Voucher.html");
$template = str_replace('[[company_name]]', $config['CompanyName'], $template);
2021-08-19 14:38:29 +08:00
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Hotspot Voucher'));
2021-08-19 14:38:29 +08:00
$ui->assign('from_id', $from_id);
2023-10-18 18:24:00 +08:00
$ui->assign('vpl', $vpl);
2021-08-19 14:38:29 +08:00
$ui->assign('pagebreak', $pagebreak);
$plans = ORM::for_table('tbl_plans')->find_many();
$ui->assign('plans', $plans);
$ui->assign('limit', $limit);
$ui->assign('planid', $planid);
2023-10-18 18:24:00 +08:00
$voucher = [];
2023-10-20 15:31:56 +08:00
$n = 1;
2023-10-18 18:24:00 +08:00
foreach ($v as $vs) {
$temp = $template;
$temp = str_replace('[[qrcode]]', '<img src="qrcode/?data=' . $vs['code'] . '">', $temp);
$temp = str_replace('[[price]]', Lang::moneyFormat($vs['price']), $temp);
$temp = str_replace('[[voucher_code]]', $vs['code'], $temp);
$temp = str_replace('[[plan]]', $vs['name_plan'], $temp);
2023-10-20 15:31:56 +08:00
$temp = str_replace('[[counter]]', $n, $temp);
2023-10-18 18:24:00 +08:00
$voucher[] = $temp;
2023-10-20 15:31:56 +08:00
$n++;
2023-10-18 18:24:00 +08:00
}
2023-10-24 13:27:30 +08:00
$ui->assign('voucher', $voucher);
2021-08-19 14:38:29 +08:00
$ui->assign('vc', $vc);
//for counting pagebreak
$ui->assign('jml', 0);
2022-09-18 01:00:40 +08:00
run_hook('view_print_voucher'); #HOOK
2021-08-19 14:38:29 +08:00
$ui->display('print-voucher.tpl');
break;
2017-03-11 03:51:06 +08:00
case 'voucher-post':
$type = _post('type');
2021-08-19 14:38:29 +08:00
$plan = _post('plan');
2024-01-08 16:39:04 +08:00
$voucher_format = _post('voucher_format');
$prefix = _post('prefix');
2021-08-19 14:38:29 +08:00
$server = _post('server');
$numbervoucher = _post('numbervoucher');
$lengthcode = _post('lengthcode');
$msg = '';
if ($type == '' or $plan == '' or $server == '' or $numbervoucher == '' or $lengthcode == '') {
2024-02-13 14:54:01 +08:00
$msg .= Lang::T('All field is required') . '<br>';
2021-08-19 14:38:29 +08:00
}
if (Validator::UnsignedNumber($numbervoucher) == false) {
$msg .= 'The Number of Vouchers must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2021-08-19 14:38:29 +08:00
if (Validator::UnsignedNumber($lengthcode) == false) {
$msg .= 'The Length Code must be a number' . '<br>';
2017-03-11 03:51:06 +08:00
}
2021-08-19 14:38:29 +08:00
if ($msg == '') {
2024-02-19 17:28:55 +08:00
if (!empty($prefix)) {
$d = ORM::for_table('tbl_appconfig')->where('setting', 'voucher_prefix')->find_one();
if ($d) {
$d->value = $prefix;
$d->save();
} else {
$d = ORM::for_table('tbl_appconfig')->create();
$d->setting = 'voucher_prefix';
$d->value = $prefix;
$d->save();
}
}
2022-09-18 01:00:40 +08:00
run_hook('create_voucher'); #HOOK
2021-08-19 14:38:29 +08:00
for ($i = 0; $i < $numbervoucher; $i++) {
$code = strtoupper(substr(md5(time() . rand(10000, 99999)), 0, $lengthcode));
2024-01-08 16:39:04 +08:00
if ($voucher_format == 'low') {
2023-10-18 18:24:00 +08:00
$code = strtolower($code);
2024-01-08 16:39:04 +08:00
} else if ($voucher_format == 'rand') {
2023-10-18 18:24:00 +08:00
$code = Lang::randomUpLowCase($code);
}
2021-08-19 14:38:29 +08:00
$d = ORM::for_table('tbl_voucher')->create();
$d->type = $type;
$d->routers = $server;
$d->id_plan = $plan;
2024-02-19 17:28:55 +08:00
$d->code = $prefix . $code;
2021-08-19 14:38:29 +08:00
$d->user = '0';
$d->status = '0';
2024-02-07 11:11:30 +08:00
$d->generated_by = $admin['id'];
2021-08-19 14:38:29 +08:00
$d->save();
}
2024-02-19 17:28:55 +08:00
if ($numbervoucher == 1) {
r2(U . 'prepaid/voucher-view/' . $d->id(), 's', Lang::T('Create Vouchers Successfully'));
}
2021-08-19 14:38:29 +08:00
2024-02-13 14:54:01 +08:00
r2(U . 'prepaid/voucher', 's', Lang::T('Create Vouchers Successfully'));
2021-08-19 14:38:29 +08:00
} else {
r2(U . 'prepaid/add-voucher/' . $id, 'e', $msg);
2017-03-11 03:51:06 +08:00
}
break;
2021-08-19 14:38:29 +08:00
case 'voucher-view':
2024-02-20 17:49:03 +08:00
$id = $routes[2];
if (in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
$voucher = ORM::for_table('tbl_voucher')->find_one($id);
2024-02-19 17:28:55 +08:00
} else {
2024-02-20 17:49:03 +08:00
$sales = [];
$sls = ORM::for_table('tbl_users')->select('id')->where('root', $admin['id'])->findArray();
foreach ($sls as $s) {
$sales[] = $s['id'];
}
$sales[] = $admin['id'];
$voucher = ORM::for_table('tbl_voucher')
->find_one($id);
if (!in_array($voucher['generated_by'], $sales)) {
r2(U . 'prepaid/voucher/', 'e', Lang::T('Voucher Not Found'));
}
}
if (!$voucher) {
r2(U . 'prepaid/voucher/', 'e', Lang::T('Voucher Not Found'));
}
$plan = ORM::for_table('tbl_plans')->find_one($d['id_plan']);
if ($voucher && $plan) {
2024-02-19 17:28:55 +08:00
$content = Lang::pad($config['CompanyName'], ' ', 2) . "\n";
$content .= Lang::pad($config['address'], ' ', 2) . "\n";
$content .= Lang::pad($config['phone'], ' ', 2) . "\n";
$content .= Lang::pad("", '=') . "\n";
$content .= Lang::pads('ID', $voucher['id'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Code'), $voucher['code'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Plan Name'), $plan['name_plan'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Type'), $voucher['type'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Plan Price'), Lang::moneyFormat($plan['price']), ' ') . "\n";
$content .= Lang::pads(Lang::T('Sales'), $admin['fullname'] . ' #' . $admin['id'], ' ') . "\n";
$content .= Lang::pad("", '=') . "\n";
$content .= Lang::pad($config['note'], ' ', 2) . "\n";
$ui->assign('print', $content);
$config['printer_cols'] = 30;
2024-02-19 17:28:55 +08:00
$content = Lang::pad($config['CompanyName'], ' ', 2) . "\n";
$content .= Lang::pad($config['address'], ' ', 2) . "\n";
$content .= Lang::pad($config['phone'], ' ', 2) . "\n";
$content .= Lang::pad("", '=') . "\n";
$content .= Lang::pads('ID', $voucher['id'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Code'), $voucher['code'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Plan Name'), $plan['name_plan'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Type'), $voucher['type'], ' ') . "\n";
$content .= Lang::pads(Lang::T('Plan Price'), Lang::moneyFormat($plan['price']), ' ') . "\n";
$content .= Lang::pads(Lang::T('Sales'), $admin['fullname'] . ' #' . $admin['id'], ' ') . "\n";
$content .= Lang::pad("", '=') . "\n";
$content .= Lang::pad($config['note'], ' ', 2) . "\n";
$ui->assign('_title', Lang::T('View'));
2024-02-23 12:39:25 +08:00
$ui->assign('whatsapp', urlencode("```$content```"));
$ui->display('voucher-view.tpl');
2024-02-19 17:28:55 +08:00
} else {
r2(U . 'prepaid/voucher/', 'e', Lang::T('Voucher Not Found'));
}
break;
2017-03-11 03:51:06 +08:00
case 'voucher-delete':
2024-02-16 15:52:49 +08:00
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
r2(U . "dashboard", 'e', Lang::T('You do not have permission to access this page'));
}
2017-03-11 03:51:06 +08:00
$id = $routes['2'];
2022-09-18 01:00:40 +08:00
run_hook('delete_voucher'); #HOOK
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_voucher')->find_one($id);
2021-08-19 14:38:29 +08:00
if ($d) {
2017-03-11 03:51:06 +08:00
$d->delete();
2024-02-13 14:54:01 +08:00
r2(U . 'prepaid/voucher', 's', Lang::T('Data Deleted Successfully'));
2017-03-11 03:51:06 +08:00
}
break;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
case 'refill':
2023-08-23 13:11:07 +08:00
$ui->assign('xfooter', $select2_customer);
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Refill Account'));
2022-09-18 01:00:40 +08:00
run_hook('view_refill'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('refill.tpl');
break;
2021-08-19 14:38:29 +08:00
2017-03-11 03:51:06 +08:00
case 'refill-post':
2021-08-19 14:38:29 +08:00
$code = _post('code');
2023-08-30 10:01:37 +08:00
$user = ORM::for_table('tbl_customers')->where('id', _post('id_customer'))->find_one();
2021-08-19 14:38:29 +08:00
$v1 = ORM::for_table('tbl_voucher')->where('code', $code)->where('status', 0)->find_one();
2022-09-18 01:00:40 +08:00
run_hook('refill_customer'); #HOOK
2021-08-19 14:38:29 +08:00
if ($v1) {
if (Package::rechargeUser($user['id'], $v1['routers'], $v1['id_plan'], "Voucher", $code)) {
2021-08-19 14:38:29 +08:00
$v1->status = "1";
2023-08-30 10:01:37 +08:00
$v1->user = $user['username'];
2021-08-19 14:38:29 +08:00
$v1->save();
2023-08-30 10:01:37 +08:00
$in = ORM::for_table('tbl_transactions')->where('username', $user['username'])->order_by_desc('id')->find_one();
2024-02-23 15:40:47 +08:00
Package::createInvoice($in);
2023-08-15 18:01:48 +08:00
$ui->display('invoice.tpl');
2023-08-23 13:11:07 +08:00
} else {
2023-08-15 18:01:48 +08:00
r2(U . 'prepaid/refill', 'e', "Failed to refill account");
2021-08-19 14:38:29 +08:00
}
} else {
2024-02-13 14:54:01 +08:00
r2(U . 'prepaid/refill', 'e', Lang::T('Voucher Not Valid'));
2021-08-19 14:38:29 +08:00
}
2017-03-11 03:51:06 +08:00
break;
2023-08-15 18:01:48 +08:00
case 'deposit':
$ui->assign('_title', Lang::T('Refill Balance'));
2023-08-23 13:11:07 +08:00
$ui->assign('xfooter', $select2_customer);
2023-08-15 18:01:48 +08:00
$ui->assign('p', ORM::for_table('tbl_plans')->where('enabled', '1')->where('type', 'Balance')->find_many());
run_hook('view_deposit'); #HOOK
$ui->display('deposit.tpl');
break;
case 'deposit-post':
$user = _post('id_customer');
$plan = _post('id_plan');
run_hook('deposit_customer'); #HOOK
if (!empty($user) && !empty($plan)) {
2023-08-23 13:11:07 +08:00
if (Package::rechargeUser($user, 'balance', $plan, "Deposit", $admin['fullname'])) {
2023-08-15 18:01:48 +08:00
$c = ORM::for_table('tbl_customers')->where('id', $user)->find_one();
$in = ORM::for_table('tbl_transactions')->where('username', $c['username'])->order_by_desc('id')->find_one();
2024-02-23 15:40:47 +08:00
Package::createInvoice($in);
2023-08-15 18:01:48 +08:00
$ui->display('invoice.tpl');
2023-08-23 13:11:07 +08:00
} else {
2023-08-15 18:01:48 +08:00
r2(U . 'prepaid/refill', 'e', "Failed to refill account");
}
} else {
r2(U . 'prepaid/refill', 'e', "All field is required");
}
break;
2017-03-11 03:51:06 +08:00
default:
2023-09-27 16:01:48 +08:00
$ui->display('a404.tpl');
2021-08-19 14:38:29 +08:00
}