phpnuxbill/system/controllers/voucher.php

229 lines
9.6 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
/**
2022-08-23 17:33:21 +08:00
* PHP Mikrotik Billing (https://ibnux.github.io/phpmixbill/)
2022-08-23 17:33:21 +08:00
**/
2017-03-11 03:51:06 +08:00
_auth();
2022-10-13 15:00:54 +08:00
$ui->assign('_title', $_L['Voucher']);
2017-03-11 03:51:06 +08:00
$ui->assign('_system_menu', 'voucher');
$action = $routes['1'];
$user = User::_info();
$ui->assign('_user', $user);
use PEAR2\Net\RouterOS;
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
require_once 'system/autoload/PEAR2/Autoload.php';
switch ($action) {
2022-08-23 17:33:21 +08:00
2017-03-11 03:51:06 +08:00
case 'activation':
2022-09-18 01:00:40 +08:00
run_hook('view_activate_voucher'); #HOOK
2017-03-11 03:51:06 +08:00
$ui->display('user-activation.tpl');
break;
case 'activation-post':
$code = _post('code');
2022-08-23 17:33:21 +08:00
$v1 = ORM::for_table('tbl_voucher')->where('code', $code)->where('status', 0)->find_one();
$c = ORM::for_table('tbl_customers')->find_one($user['id']);
$p = ORM::for_table('tbl_plans')->find_one($v1['id_plan']);
$b = ORM::for_table('tbl_user_recharges')->where('customer_id', $user['id'])->find_one();
$date_now = date("Y-m-d H:i:s");
$date_only = date("Y-m-d");
$time = date("H:i:s");
2022-09-18 01:52:39 +08:00
$mikrotik = Mikrotik::info($v1['routers']);
if ($p['validity_unit'] == 'Months') {
$date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month'));
} else if ($p['validity_unit'] == 'Days') {
$date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' day'));
} else if ($p['validity_unit'] == 'Hrs') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' hour')));
2022-09-05 16:11:22 +08:00
$date_exp = $datetime[0];
$time = $datetime[1];
2022-09-18 01:52:39 +08:00
} else if ($p['validity_unit'] == 'Mins') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' minute')));
2022-09-05 16:11:22 +08:00
$date_exp = $datetime[0];
$time = $datetime[1];
}
2022-09-18 01:00:40 +08:00
run_hook('customer_activate_voucher'); #HOOK
2022-08-23 17:33:21 +08:00
if ($v1) {
if ($v1['type'] == 'Hotspot') {
if ($b) {
2022-09-21 15:15:00 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removeHotspotUser($client, $c['username']);
Mikrotik::addHotspotUser($client, $p, $c);
2022-08-23 17:33:21 +08:00
}
$b->customer_id = $user['id'];
$b->username = $c['username'];
$b->plan_id = $v1['id_plan'];
$b->namebp = $p['name_plan'];
$b->recharged_on = $date_only;
$b->expiration = $date_exp;
$b->time = $time;
$b->status = "on";
$b->method = "voucher";
$b->routers = $v1['routers'];
$b->type = "Hotspot";
$b->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
$t->invoice = "INV-" . _raid(5);
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "voucher";
$t->routers = $v1['routers'];
$t->type = "Hotspot";
$t->save();
} else {
2022-09-21 15:15:00 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::addHotspotUser($client, $p, $c);
2022-08-23 17:33:21 +08:00
}
$d = ORM::for_table('tbl_user_recharges')->create();
$d->customer_id = $user['id'];
$d->username = $c['username'];
$d->plan_id = $v1['id_plan'];
$d->namebp = $p['name_plan'];
$d->recharged_on = $date_only;
$d->expiration = $date_exp;
$d->time = $time;
$d->status = "on";
$d->method = "voucher";
$d->routers = $v1['routers'];
$d->type = "Hotspot";
$d->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
$t->invoice = "INV-" . _raid(5);
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "voucher";
$t->routers = $v1['routers'];
$t->type = "Hotspot";
$t->save();
}
$v1->status = "1";
$v1->user = $c['username'];
$v1->save();
2022-09-05 16:11:22 +08:00
// Telegram to Admin
2022-09-18 01:52:39 +08:00
sendTelegram('#u' . $c['username'] . " Activate #Voucher #Hotspot\n" . $p['name_plan'] .
"\nCode: " . $code .
"\nRouter: " . $v1['routers'] .
"\nPrice: " . $p['price']);
2022-08-23 17:33:21 +08:00
} else {
if ($b) {
2022-09-21 15:15:00 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removePpoeUser($client, $c['username']);
Mikrotik::addPpoeUser($client, $p, $c);
2022-09-07 17:11:35 +08:00
}
2022-08-23 17:33:21 +08:00
$b->customer_id = $user['id'];
$b->username = $c['username'];
$b->plan_id = $v1['id_plan'];
$b->namebp = $p['name_plan'];
$b->recharged_on = $date_only;
$b->expiration = $date_exp;
$b->time = $time;
$b->status = "on";
$b->method = "voucher";
$b->routers = $v1['routers'];
$b->type = "PPPOE";
$b->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
$t->invoice = "INV-" . _raid(5);
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "voucher";
$t->routers = $v1['routers'];
$t->type = "PPPOE";
$t->save();
} else {
2022-09-21 15:15:00 +08:00
if (!$config['radius_mode']) {
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::addPpoeUser($client, $p, $c);
2022-08-23 17:33:21 +08:00
}
$d = ORM::for_table('tbl_user_recharges')->create();
$d->customer_id = $user['id'];
$d->username = $c['username'];
$d->plan_id = $v1['id_plan'];
$d->namebp = $p['name_plan'];
$d->recharged_on = $date_only;
$d->expiration = $date_exp;
$d->time = $time;
$d->status = "on";
$d->method = "voucher";
$d->routers = $v1['routers'];
$d->type = "PPPOE";
$d->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
$t->invoice = "INV-" . _raid(5);
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "voucher";
$t->routers = $v1['routers'];
$t->type = "PPPOE";
$t->save();
}
$v1->status = "1";
$v1->user = $c['username'];
$v1->save();
2022-09-05 16:11:22 +08:00
// Telegram to Admin
2022-09-18 01:52:39 +08:00
sendTelegram('#u' . $c['username'] . " Activate #Voucher #PPPOE\n" . $p['name_plan'] .
"\nCode: " . $code .
"\nRouter: " . $v1['routers'] .
"\nPrice: " . $p['price']);
2022-08-23 17:33:21 +08:00
}
r2(U . "voucher/list-activated", 's', $_L['Activation_Vouchers_Successfully']);
} else {
r2(U . 'voucher/activation', 'e', $_L['Voucher_Not_Valid']);
}
2017-03-11 03:51:06 +08:00
break;
case 'list-activated':
2022-08-23 17:33:21 +08:00
$paginator = Paginator::bootstrap('tbl_transactions', 'username', $user['username']);
$d = ORM::for_table('tbl_transactions')->where('username', $user['username'])->offset($paginator['startpoint'])->limit($paginator['limit'])->order_by_desc('id')->find_many();
$ui->assign('d', $d);
$ui->assign('paginator', $paginator);
2022-09-18 01:00:40 +08:00
run_hook('customer_view_activation_list'); #HOOK
2022-08-23 17:33:21 +08:00
$ui->display('user-activation-list.tpl');
2017-03-11 03:51:06 +08:00
break;
default:
$ui->display('404.tpl');
2022-08-23 17:33:21 +08:00
}