phpnuxbill/system/controllers/prepaid.php

420 lines
16 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2021-08-19 14:38:29 +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/)
2021-08-19 14:38:29 +08:00
* @copyright Copyright (C) 2014-2015 PHP Mikrotik Billing
* @license GNU General Public License version 2 or later; see LICENSE.txt
2021-08-19 14:38:29 +08:00
**/
2017-03-11 03:51:06 +08:00
_admin();
2022-10-13 15:00:54 +08:00
$ui->assign('_title', $_L['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);
2021-08-19 14:38:29 +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
}
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
require_once 'system/autoload/PEAR2/Autoload.php';
switch ($action) {
2021-08-19 14:38:29 +08:00
case 'list':
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/prepaid.js"></script>');
$username = _post('username');
if ($username != '') {
$paginator = Paginator::bootstrap('tbl_user_recharges', 'username', '%' . $username . '%');
$d = ORM::for_table('tbl_user_recharges')->where_like('username', '%' . $username . '%')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
} else {
$paginator = Paginator::bootstrap('tbl_user_recharges');
$d = ORM::for_table('tbl_user_recharges')->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
}
$ui->assign('d', $d);
2023-04-06 10:50:03 +08:00
$ui->assign('cari', $username);
2021-08-19 14:38:29 +08:00
$ui->assign('paginator', $paginator);
2022-09-18 01:00:40 +08:00
run_hook('view_list_billing'); #HOOK
2021-08-19 14:38:29 +08:00
$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);
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();
$ui->assign('in', $in);
$ui->assign('date', date("Y-m-d H:i:s"));
$ui->display('invoice.tpl');
2023-08-23 13:11:07 +08:00
_log('[' . $admin['username'] . ']: ' . 'Recharge ' . $c['username'] . ' [' . $in['plan_name'] . '][' . Lang::moneyFormat($in['price']) . ']', 'Admin', $admin['id']);
} 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
case 'print':
$id = _post('id');
$d = ORM::for_table('tbl_transactions')->where('id', $id)->find_one();
$ui->assign('d', $d);
2023-08-15 18:01:48 +08:00
$ui->assign('date', date("Y-m-d H:i:s"));
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':
$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);
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-18 01:00:40 +08:00
run_hook('view_edit_customer_plan'); #HOOK
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':
$id = $routes['2'];
$d = ORM::for_table('tbl_user_recharges')->find_one($id);
2022-09-18 01:52:39 +08:00
$mikrotik = Mikrotik::info($d['routers']);
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
2021-08-19 14:38:29 +08:00
if ($d['type'] == 'Hotspot') {
2023-08-23 13:11:07 +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-23 13:11:07 +08:00
Mikrotik::removeHotspotUser($client, $c['username']);
2021-08-19 14:38:29 +08:00
}
$d->delete();
} else {
2023-08-23 13:11:07 +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-23 13:11:07 +08:00
Mikrotik::removePpoeUser($client, $c['username']);
2021-08-19 14:38:29 +08:00
}
$d->delete();
}
2023-08-23 13:11:07 +08:00
_log('[' . $admin['username'] . ']: ' . 'Delete Plan for Customer ' . $c['username'] . ' [' . $in['plan_name'] . '][' . Lang::moneyFormat($in['price']) . ']', 'Admin', $admin['id']);
2017-03-11 03:51:06 +08:00
r2(U . 'prepaid/list', 's', $_L['Delete_Successfully']);
}
break;
case 'edit-post':
$username = _post('username');
$id_plan = _post('id_plan');
$recharged_on = _post('recharged_on');
2021-08-19 14:38:29 +08:00
$expiration = _post('expiration');
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 {
$msg .= $_L['Data_Not_Found'] . '<br>';
2017-03-11 03:51:06 +08:00
}
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;
$d->recharged_on = $recharged_on;
$d->expiration = $expiration;
$d->save();
2023-08-23 13:11:07 +08:00
Package::changeTo($username, $id_plan);
_log('[' . $admin['username'] . ']: ' . 'Edit Plan for Customer ' . $d['username'] . ' to [' . $d['plan_name'] . '][' . Lang::moneyFormat($d['price']) . ']', 'Admin', $admin['id']);
2017-03-11 03:51:06 +08:00
r2(U . 'prepaid/list', 's', $_L['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':
$ui->assign('xfooter', '<script type="text/javascript" src="ui/lib/c/voucher.js"></script>');
$code = _post('code');
if ($code != '') {
$ui->assign('code', $code);
$paginator = Paginator::bootstrap('tbl_voucher', 'code', '%' . $code . '%');
2022-09-08 15:00:46 +08:00
$d = ORM::for_table('tbl_plans')->where('enabled', '1')
2021-08-19 14:38:29 +08:00
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
2023-04-06 10:47:26 +08:00
->where_like('tbl_voucher.code', '%' . $code . '%')
2021-08-19 14:38:29 +08:00
->offset($paginator['startpoint'])
->limit($paginator['limit'])
->find_many();
} else {
$paginator = Paginator::bootstrap('tbl_voucher');
2022-09-08 15:00:46 +08:00
$d = ORM::for_table('tbl_plans')->where('enabled', '1')
2021-08-19 14:38:29 +08:00
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->offset($paginator['startpoint'])
->limit($paginator['limit'])->find_many();
}
$ui->assign('d', $d);
2023-04-06 10:47:26 +08:00
$ui->assign('_code', $code);
2021-08-19 14:38:29 +08:00
$ui->assign('paginator', $paginator);
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':
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
case 'print-voucher':
$from_id = _post('from_id') * 1;
$planid = _post('planid') * 1;
$pagebreak = _post('pagebreak') * 1;
$limit = _post('limit') * 1;
if ($pagebreak < 1) $pagebreak = 6;
if ($limit < 1) $limit = $pagebreak * 2;
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)
->limit($limit)
->find_many();
$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)
->where_gt('tbl_voucher.id', $from_id)
->count();
} 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)
->limit($limit)
->find_many();
$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)
->count();
} 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)
->limit($limit)
->find_many();
$vc = 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)
->count();
} else {
$v = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->limit($limit)
->find_many();
$vc = ORM::for_table('tbl_plans')
->join('tbl_voucher', array('tbl_plans.id', '=', 'tbl_voucher.id_plan'))
->where('tbl_voucher.status', '0')
->count();
}
2022-10-13 15:00:54 +08:00
$ui->assign('_title', $_L['Voucher_Hotspot']);
2021-08-19 14:38:29 +08:00
$ui->assign('from_id', $from_id);
$ui->assign('pagebreak', $pagebreak);
$plans = ORM::for_table('tbl_plans')->find_many();
$ui->assign('plans', $plans);
$ui->assign('limit', $limit);
$ui->assign('planid', $planid);
$ui->assign('v', $v);
$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');
$server = _post('server');
$numbervoucher = _post('numbervoucher');
$lengthcode = _post('lengthcode');
$msg = '';
if ($type == '' or $plan == '' or $server == '' or $numbervoucher == '' or $lengthcode == '') {
$msg .= $_L['All_field_is_required'] . '<br>';
}
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 == '') {
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));
2022-09-18 01:00:40 +08:00
//TODO: IMPLEMENT Voucher Generator
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;
$d->code = $code;
$d->user = '0';
$d->status = '0';
$d->save();
}
2017-03-11 03:51:06 +08:00
r2(U . 'prepaid/voucher', 's', $_L['Voucher_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
2017-03-11 03:51:06 +08:00
case 'voucher-delete':
$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();
r2(U . 'prepaid/voucher', 's', $_L['Delete_Successfully']);
}
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);
2023-08-15 18:01:48 +08:00
$ui->assign('_title', $_L['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
$user = _post('id_customer');
$code = _post('code');
$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) {
2023-08-23 13:11:07 +08:00
if (Package::rechargeUser($user, $v1['routers'], $v1['id_plan'], "Refill", "Voucher")) {
2021-08-19 14:38:29 +08:00
$v1->status = "1";
$v1->user = $c['username'];
$v1->save();
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();
$ui->assign('in', $in);
$ui->assign('date', date("Y-m-d H:i:s"));
$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 {
r2(U . 'prepaid/refill', 'e', $_L['Voucher_Not_Valid']);
}
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();
$ui->assign('in', $in);
$ui->assign('date', date("Y-m-d H:i:s"));
$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:
echo 'action not defined';
2021-08-19 14:38:29 +08:00
}