phpnuxbill/system/controllers/customers.php

284 lines
11 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2022-09-01 16:35:54 +08:00
2017-03-11 03:51:06 +08:00
/**
2022-10-16 15:50:24 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
2022-09-01 16:35:54 +08:00
**/
2017-03-11 03:51:06 +08:00
_admin();
2022-10-13 15:00:54 +08:00
$ui->assign('_title', $_L['Customers']);
2017-03-11 03:51:06 +08:00
$ui->assign('_system_menu', 'customers');
$action = $routes['1'];
$admin = Admin::_info();
$ui->assign('_admin', $admin);
2022-09-01 16:35:54 +08:00
if ($admin['user_type'] != 'Admin' and $admin['user_type'] != 'Sales') {
r2(U . "dashboard", 'e', $_L['Do_Not_Access']);
2017-03-11 03:51:06 +08:00
}
switch ($action) {
case 'list':
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/customers.js"></script>');
2023-06-15 17:46:36 +08:00
$search = _post('search');
2022-09-18 01:00:40 +08:00
run_hook('list_customers'); #HOOK
2023-06-15 17:46:36 +08:00
if ($search != '') {
2023-08-23 16:00:34 +08:00
$paginator = Paginator::bootstrapRaw('tbl_customers', "(`username` LIKE '%$search%' OR `fullname` LIKE '%$search%' OR `phonenumber` LIKE '%$search%' OR `email` LIKE '%$search%')", [$search, $search, $search, $search]);
2023-06-15 17:46:36 +08:00
$d = ORM::for_table('tbl_customers')
2023-08-23 16:00:34 +08:00
->where_raw("(`username` LIKE '%$search%' OR `fullname` LIKE '%$search%' OR `phonenumber` LIKE '%$search%' OR `email` LIKE '%$search%')", [$search, $search, $search, $search])
2023-08-21 18:09:44 +08:00
->offset($paginator['startpoint'])
->limit($paginator['limit'])
->order_by_desc('id')->find_many();
2022-09-01 16:35:54 +08:00
} else {
$paginator = Paginator::bootstrap('tbl_customers');
2023-09-07 11:54:20 +08:00
$d = ORM::for_table('tbl_customers')
->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
2022-09-01 16:35:54 +08:00
}
2023-06-15 17:46:36 +08:00
$ui->assign('search', htmlspecialchars($search));
2022-09-01 16:35:54 +08:00
$ui->assign('d', $d);
$ui->assign('paginator', $paginator);
2017-03-11 03:51:06 +08:00
$ui->display('customers.tpl');
break;
case 'add':
2022-09-18 01:00:40 +08:00
run_hook('view_add_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('customers-add.tpl');
break;
2023-08-23 16:00:34 +08:00
case 'viewu':
$customer = ORM::for_table('tbl_customers')->where('username', $routes['2'])->find_one();
2023-08-21 18:09:44 +08:00
case 'view':
$id = $routes['2'];
run_hook('view_customer'); #HOOK
2023-08-23 16:00:34 +08:00
if(!$customer){
$customer = ORM::for_table('tbl_customers')->find_one($id);
}
2023-08-21 18:09:44 +08:00
if ($customer) {
$v = $routes['3'];
if (empty($v) || $v == 'order') {
$v = 'order';
// $paginator = Paginator::bootstrap('tbl_payment_gateway', 'username', $customer['username']);
// print_r($paginator);
$order = ORM::for_table('tbl_payment_gateway')
->where('username', $customer['username'])
->offset(0)
->limit(30)
->order_by_desc('id')
->find_many();
// $ui->assign('paginator', $paginator);
$ui->assign('order', $order);
2023-08-23 16:00:34 +08:00
} else if ($v == 'activation') {
2023-08-21 18:09:44 +08:00
// $paginator = Paginator::bootstrap('tbl_transactions', 'username', $customer['username']);
$activation = ORM::for_table('tbl_transactions')
->where('username', $customer['username'])
->offset(0)
->limit(30)
->order_by_desc('id')
->find_many();
// $ui->assign('paginator', $paginator);
$ui->assign('activation', $activation);
}
2023-08-23 16:00:34 +08:00
$package = ORM::for_table('tbl_user_recharges')->where('username',$customer['username'])->find_one();
$ui->assign('package', $package);
2023-08-21 18:09:44 +08:00
$ui->assign('v', $v);
$ui->assign('d', $customer);
$ui->display('customers-view.tpl');
} else {
r2(U . 'customers/list', 'e', $_L['Account_Not_Found']);
}
break;
2017-03-11 03:51:06 +08:00
case 'edit':
$id = $routes['2'];
2022-09-18 01:00:40 +08:00
run_hook('edit_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_customers')->find_one($id);
2022-09-01 16:35:54 +08:00
if ($d) {
$ui->assign('d', $d);
2017-03-11 03:51:06 +08:00
$ui->display('customers-edit.tpl');
2022-09-01 16:35:54 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'customers/list', 'e', $_L['Account_Not_Found']);
}
break;
case 'delete':
$id = $routes['2'];
2022-09-18 01:00:40 +08:00
run_hook('delete_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_customers')->find_one($id);
2022-09-01 16:35:54 +08:00
if ($d) {
$c = ORM::for_table('tbl_user_recharges')->where('username', $d['username'])->find_one();
if ($c) {
2022-09-18 01:52:39 +08:00
$mikrotik = Mikrotik::info($c['routers']);
2022-09-01 16:35:54 +08:00
if ($c['type'] == 'Hotspot') {
2023-08-21 18:09:44 +08:00
if (!$config['radius_mode']) {
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']);
2022-09-01 16:35:54 +08:00
}
} else {
2023-08-21 18:09:44 +08:00
if (!$config['radius_mode']) {
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']);
2022-09-01 16:35:54 +08:00
}
}
try {
$d->delete();
} catch (Exception $e) {
2023-08-21 18:09:44 +08:00
} catch (Throwable $e) {
2022-09-01 16:35:54 +08:00
}
try {
$c->delete();
} catch (Exception $e) {
}
} else {
try {
$d->delete();
} catch (Exception $e) {
2023-08-21 18:09:44 +08:00
} catch (Throwable $e) {
2022-09-01 16:35:54 +08:00
}
try {
$c->delete();
} catch (Exception $e) {
2023-08-21 18:09:44 +08:00
} catch (Throwable $e) {
2022-09-01 16:35:54 +08:00
}
}
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
r2(U . 'customers/list', 's', $_L['User_Delete_Ok']);
}
break;
case 'add-post':
$username = _post('username');
$fullname = _post('fullname');
$password = _post('password');
2023-08-14 14:21:41 +08:00
$pppoe_password = _post('pppoe_password');
2023-08-09 15:54:38 +08:00
$email = _post('email');
2017-03-11 03:51:06 +08:00
$address = _post('address');
2022-09-01 16:35:54 +08:00
$phonenumber = _post('phonenumber');
2022-09-18 01:00:40 +08:00
run_hook('add_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$msg = '';
2022-09-01 16:35:54 +08:00
if (Validator::Length($username, 35, 2) == false) {
$msg .= 'Username should be between 3 to 55 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
if (Validator::Length($fullname, 36, 2) == false) {
$msg .= 'Full Name should be between 3 to 25 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
if (!Validator::Length($password, 35, 2)) {
$msg .= 'Password should be between 3 to 35 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
$d = ORM::for_table('tbl_customers')->where('username', $username)->find_one();
if ($d) {
$msg .= $_L['account_already_exist'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
if ($msg == '') {
2017-03-11 03:51:06 +08:00
$d = ORM::for_table('tbl_customers')->create();
2023-08-09 15:54:38 +08:00
$d->username = Lang::phoneFormat($username);
2017-03-11 03:51:06 +08:00
$d->password = $password;
2023-08-14 14:21:41 +08:00
$d->pppoe_password = $pppoe_password;
2023-08-09 15:54:38 +08:00
$d->email = $email;
2017-03-11 03:51:06 +08:00
$d->fullname = $fullname;
$d->address = $address;
2023-08-09 15:54:38 +08:00
$d->phonenumber = Lang::phoneFormat($phonenumber);
2017-03-11 03:51:06 +08:00
$d->save();
r2(U . 'customers/list', 's', $_L['account_created_successfully']);
2022-09-01 16:35:54 +08:00
} else {
2017-03-11 03:51:06 +08:00
r2(U . 'customers/add', 'e', $msg);
}
break;
case 'edit-post':
2023-08-09 15:54:38 +08:00
$username = Lang::phoneFormat(_post('username'));
2017-03-11 03:51:06 +08:00
$fullname = _post('fullname');
$password = _post('password');
2023-08-14 14:21:41 +08:00
$pppoe_password = _post('pppoe_password');
2023-08-09 15:54:38 +08:00
$email = _post('email');
2017-03-11 03:51:06 +08:00
$address = _post('address');
2023-08-09 15:54:38 +08:00
$phonenumber = Lang::phoneFormat(_post('phonenumber'));
2022-09-18 01:00:40 +08:00
run_hook('edit_customer'); #HOOK
2017-03-11 03:51:06 +08:00
$msg = '';
2022-09-01 16:35:54 +08:00
if (Validator::Length($username, 16, 2) == false) {
$msg .= 'Username should be between 3 to 15 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
2023-08-09 15:54:38 +08:00
if (Validator::Length($fullname, 26, 1) == false) {
$msg .= 'Full Name should be between 2 to 25 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
if ($password != '') {
if (!Validator::Length($password, 15, 2)) {
$msg .= 'Password should be between 3 to 15 characters' . '<br>';
2017-03-11 03:51:06 +08:00
}
}
$id = _post('id');
$d = ORM::for_table('tbl_customers')->find_one($id);
2022-09-01 16:35:54 +08:00
if (!$d) {
$msg .= $_L['Data_Not_Found'] . '<br>';
2017-03-11 03:51:06 +08:00
}
2022-09-01 16:35:54 +08:00
if ($d['username'] != $username) {
$c = ORM::for_table('tbl_customers')->where('username', $username)->find_one();
if ($c) {
$msg .= $_L['account_already_exist'] . '<br>';
2017-03-11 03:51:06 +08:00
}
}
2022-09-01 16:35:54 +08:00
if ($msg == '') {
$c = ORM::for_table('tbl_user_recharges')->where('username', $username)->find_one();
if ($c) {
2022-09-18 01:52:39 +08:00
$mikrotik = Mikrotik::info($c['routers']);
2022-09-01 16:35:54 +08:00
if ($c['type'] == 'Hotspot') {
2023-08-21 18:09:44 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-21 18:09:44 +08:00
Mikrotik::setHotspotUser($client, $c['username'], $password);
Mikrotik::removeHotspotActiveUser($client, $user['username']);
2022-09-01 16:35:54 +08:00
}
$d->password = $password;
$d->save();
} else {
2023-08-21 18:09:44 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-21 18:09:44 +08:00
if (!empty($d['pppoe_password'])) {
2023-08-14 14:21:41 +08:00
Mikrotik::setPpoeUser($client, $c['username'], $d['pppoe_password']);
2023-08-21 18:09:44 +08:00
} else {
2023-08-14 14:21:41 +08:00
Mikrotik::setPpoeUser($client, $c['username'], $password);
}
2023-08-21 18:09:44 +08:00
Mikrotik::removePpoeActive($client, $user['username']);
2022-09-01 16:35:54 +08:00
}
$d->password = $password;
$d->save();
}
$d->username = $username;
if ($password != '') {
$d->password = $password;
}
2023-08-14 14:21:41 +08:00
$d->pppoe_password = $pppoe_password;
2022-09-01 16:35:54 +08:00
$d->fullname = $fullname;
2023-08-09 15:54:38 +08:00
$d->email = $email;
2022-09-01 16:35:54 +08:00
$d->address = $address;
$d->phonenumber = $phonenumber;
$d->save();
} else {
$d->username = $username;
if ($password != '') {
$d->password = $password;
}
$d->fullname = $fullname;
2023-08-14 14:21:41 +08:00
$d->pppoe_password = $pppoe_password;
2023-08-09 15:54:38 +08:00
$d->email = $email;
2022-09-01 16:35:54 +08:00
$d->address = $address;
$d->phonenumber = $phonenumber;
$d->save();
}
2017-03-11 03:51:06 +08:00
r2(U . 'customers/list', 's', 'User Updated Successfully');
2022-09-01 16:35:54 +08:00
} else {
r2(U . 'customers/edit/' . $id, 'e', $msg);
2017-03-11 03:51:06 +08:00
}
break;
default:
2023-08-21 18:09:44 +08:00
r2(U . 'customers/list', 'e', 'action not defined');
2022-09-01 16:35:54 +08:00
}