phpnuxbill/system/autoload/Package.php

405 lines
18 KiB
PHP
Raw Normal View History

2022-09-10 17:01:51 +08:00
<?php
/**
2023-10-12 16:55:42 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2022-09-10 17:01:51 +08:00
**/
2023-10-12 16:55:42 +08:00
2022-09-10 17:01:51 +08:00
class Package
{
2022-09-16 12:19:53 +08:00
/**
* @param int $id_customer String user identifier
* @param string $router_name router name for this package
* @param int $plan_id plan id for this package
* @param string $gateway payment gateway name
* @param string $channel channel payment gateway
* @return boolean
*/
2022-09-10 17:01:51 +08:00
public static function rechargeUser($id_customer, $router_name, $plan_id, $gateway, $channel)
{
2023-09-01 14:40:26 +08:00
global $_c;
2022-09-10 17:01:51 +08:00
$date_now = date("Y-m-d H:i:s");
$date_only = date("Y-m-d");
2023-08-28 10:44:57 +08:00
$time_only = date("H:i:s");
2022-09-10 17:01:51 +08:00
$time = date("H:i:s");
if ($id_customer == '' or $router_name == '' or $plan_id == '') {
return false;
}
$c = ORM::for_table('tbl_customers')->where('id', $id_customer)->find_one();
$p = ORM::for_table('tbl_plans')->where('id', $plan_id)->where('enabled', '1')->find_one();
2023-08-14 16:59:53 +08:00
if ($router_name == 'balance') {
// insert table transactions
2023-08-18 10:47:03 +08:00
$inv = "INV-" . Package::_raid(5);
2023-08-14 16:59:53 +08:00
$t = ORM::for_table('tbl_transactions')->create();
$t->invoice = $inv;
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$t->recharged_time = date("H:i:s");
2023-08-14 16:59:53 +08:00
$t->expiration = $date_only;
$t->time = $time;
$t->method = "$gateway - $channel";
$t->routers = $router_name;
$t->type = "Balance";
$t->save();
2023-09-13 17:23:51 +08:00
$balance_before = $c['balance'];
2023-08-14 16:59:53 +08:00
Balance::plus($id_customer, $p['price']);
2023-09-13 17:23:51 +08:00
$balance = $c['balance'] + $p['price'];
2023-08-14 16:59:53 +08:00
2023-08-24 12:35:23 +08:00
$textInvoice = Lang::getNotifText('invoice_balance');
2023-08-14 16:59:53 +08:00
$textInvoice = str_replace('[[company_name]]', $_c['CompanyName'], $textInvoice);
$textInvoice = str_replace('[[address]]', $_c['address'], $textInvoice);
$textInvoice = str_replace('[[phone]]', $_c['phone'], $textInvoice);
$textInvoice = str_replace('[[invoice]]', $inv, $textInvoice);
2023-09-11 17:26:41 +08:00
$textInvoice = str_replace('[[date]]', Lang::dateTimeFormat($date_now), $textInvoice);
2024-02-07 11:11:30 +08:00
$textInvoice = str_replace('[[payment_gateway]]', $gateway, $textInvoice);
$textInvoice = str_replace('[[payment_channel]]', $channel, $textInvoice);
2023-08-14 16:59:53 +08:00
$textInvoice = str_replace('[[type]]', 'Balance', $textInvoice);
$textInvoice = str_replace('[[plan_name]]', $p['name_plan'], $textInvoice);
2023-09-11 17:26:41 +08:00
$textInvoice = str_replace('[[plan_price]]', Lang::moneyFormat($p['price']), $textInvoice);
$textInvoice = str_replace('[[name]]', $c['fullname'], $textInvoice);
2023-08-14 16:59:53 +08:00
$textInvoice = str_replace('[[user_name]]', $c['username'], $textInvoice);
$textInvoice = str_replace('[[user_password]]', $c['password'], $textInvoice);
2023-08-16 09:30:05 +08:00
$textInvoice = str_replace('[[footer]]', $_c['note'], $textInvoice);
2023-09-13 17:23:51 +08:00
$textInvoice = str_replace('[[balance_before]]', Lang::moneyFormat($balance_before), $textInvoice);
$textInvoice = str_replace('[[balance]]', Lang::moneyFormat($balance), $textInvoice);
2023-08-14 16:59:53 +08:00
if ($_c['user_notification_payment'] == 'sms') {
Message::sendSMS($c['phonenumber'], $textInvoice);
} else if ($_c['user_notification_payment'] == 'wa') {
Message::sendWhatsapp($c['phonenumber'], $textInvoice);
}
return true;
}
/**
* 1 Customer only can have 1 PPPOE and 1 Hotspot Plan
*/
$b = ORM::for_table('tbl_user_recharges')
->where('customer_id', $id_customer)
->where('routers', $router_name)
->where('Type', $p['type'])
->find_one();
2022-09-10 17:01:51 +08:00
2022-09-18 01:52:39 +08:00
$mikrotik = Mikrotik::info($router_name);
2022-09-10 17:01:51 +08:00
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')));
$date_exp = $datetime[0];
$time = $datetime[1];
} else if ($p['validity_unit'] == 'Mins') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' minute')));
$date_exp = $datetime[0];
$time = $datetime[1];
}
if ($p['type'] == 'Hotspot') {
if ($b) {
2023-09-01 10:02:39 +08:00
if ($b['namebp'] == $p['name_plan'] && $b['status'] == 'on') {
// if it same internet plan, expired will extend
if ($p['validity_unit'] == 'Months') {
$date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months'));
2023-09-06 11:48:52 +08:00
$time = $b['time'];
} else if ($p['validity_unit'] == 'Days') {
$date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' days'));
2023-09-06 11:48:52 +08:00
$time = $b['time'];
} else if ($p['validity_unit'] == 'Hrs') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' hours')));
$date_exp = $datetime[0];
$time = $datetime[1];
} else if ($p['validity_unit'] == 'Mins') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' minutes')));
$date_exp = $datetime[0];
$time = $datetime[1];
}
}
2024-02-20 11:58:50 +08:00
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p, "$date_exp $time");
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removeHotspotUser($client, $c['username']);
Mikrotik::removeHotspotActiveUser($client, $c['username']);
Mikrotik::addHotspotUser($client, $p, $c);
}
2022-09-10 17:01:51 +08:00
$b->customer_id = $id_customer;
$b->username = $c['username'];
$b->plan_id = $plan_id;
$b->namebp = $p['name_plan'];
$b->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$b->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$b->expiration = $date_exp;
$b->time = $time;
$b->status = "on";
$b->method = "$gateway - $channel";
$b->routers = $router_name;
$b->type = "Hotspot";
$b->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
2023-08-18 10:47:03 +08:00
$t->invoice = "INV-" . Package::_raid(5);
2022-09-10 17:01:51 +08:00
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$t->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "$gateway - $channel";
$t->routers = $router_name;
$t->type = "Hotspot";
$t->save();
} else {
2023-10-04 15:00:04 +08:00
if ($p['is_radius']) {
2023-10-12 14:42:52 +08:00
Radius::customerAddPlan($c, $p, "$date_exp $time");
2023-10-04 15:00:04 +08:00
}else{
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-09-06 11:48:52 +08:00
Mikrotik::removeHotspotUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removeHotspotActiveUser($client, $c['username']);
2023-03-06 15:50:55 +08:00
Mikrotik::addHotspotUser($client, $p, $c);
2022-09-10 17:01:51 +08:00
}
$d = ORM::for_table('tbl_user_recharges')->create();
$d->customer_id = $id_customer;
$d->username = $c['username'];
$d->plan_id = $plan_id;
$d->namebp = $p['name_plan'];
$d->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$d->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$d->expiration = $date_exp;
$d->time = $time;
$d->status = "on";
$d->method = "$gateway - $channel";
$d->routers = $router_name;
$d->type = "Hotspot";
$d->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
2023-08-18 10:47:03 +08:00
$t->invoice = "INV-" . Package::_raid(5);
2022-09-10 17:01:51 +08:00
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$t->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "$gateway - $channel";
$t->routers = $router_name;
$t->type = "Hotspot";
$t->save();
}
2023-03-06 15:50:55 +08:00
Message::sendTelegram("#u$c[username] #buy #Hotspot \n" . $p['name_plan'] .
2022-09-10 17:01:51 +08:00
"\nRouter: " . $router_name .
"\nGateway: " . $gateway .
"\nChannel: " . $channel .
2023-08-24 12:35:23 +08:00
"\nPrice: " . Lang::moneyFormat($p['price']));
2022-09-10 17:01:51 +08:00
} else {
if ($b) {
2023-09-01 10:02:39 +08:00
if ($b['namebp'] == $p['name_plan'] && $b['status'] == 'on') {
// if it same internet plan, expired will extend
if ($p['validity_unit'] == 'Months') {
$date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months'));
$time = $b['time'];
} else if ($p['validity_unit'] == 'Days') {
$date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' days'));
$time = $b['time'];
} else if ($p['validity_unit'] == 'Hrs') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' hours')));
$date_exp = $datetime[0];
$time = $datetime[1];
} else if ($p['validity_unit'] == 'Mins') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' minutes')));
$date_exp = $datetime[0];
$time = $datetime[1];
}
}
2024-02-20 11:58:50 +08:00
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p, "$date_exp $time");
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removePpoeUser($client, $c['username']);
Mikrotik::removePpoeActive($client, $c['username']);
Mikrotik::addPpoeUser($client, $p, $c);
}
2022-09-10 17:01:51 +08:00
$b->customer_id = $id_customer;
$b->username = $c['username'];
$b->plan_id = $plan_id;
$b->namebp = $p['name_plan'];
$b->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$b->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$b->expiration = $date_exp;
$b->time = $time;
$b->status = "on";
$b->method = "$gateway - $channel";
$b->routers = $router_name;
$b->type = "PPPOE";
$b->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
2023-08-18 10:47:03 +08:00
$t->invoice = "INV-" . Package::_raid(5);
2022-09-10 17:01:51 +08:00
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$t->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "$gateway - $channel";
$t->routers = $router_name;
$t->type = "PPPOE";
$t->save();
} else {
2023-10-04 15:00:04 +08:00
if ($p['is_radius']) {
2023-10-12 14:42:52 +08:00
Radius::customerAddPlan($c, $p, "$date_exp $time");
2023-10-04 15:00:04 +08:00
}else{
2022-09-18 01:52:39 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-28 11:38:59 +08:00
Mikrotik::removePpoeUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removePpoeActive($client, $c['username']);
2023-03-06 15:50:55 +08:00
Mikrotik::addPpoeUser($client, $p, $c);
2022-09-10 17:01:51 +08:00
}
$d = ORM::for_table('tbl_user_recharges')->create();
$d->customer_id = $id_customer;
$d->username = $c['username'];
$d->plan_id = $plan_id;
$d->namebp = $p['name_plan'];
$d->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$d->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$d->expiration = $date_exp;
$d->time = $time;
$d->status = "on";
$d->method = "$gateway - $channel";
$d->routers = $router_name;
$d->type = "PPPOE";
$d->save();
// insert table transactions
$t = ORM::for_table('tbl_transactions')->create();
2023-08-18 10:47:03 +08:00
$t->invoice = "INV-" . Package::_raid(5);
2022-09-10 17:01:51 +08:00
$t->username = $c['username'];
$t->plan_name = $p['name_plan'];
$t->price = $p['price'];
$t->recharged_on = $date_only;
2023-08-28 11:12:55 +08:00
$t->recharged_time = $time_only;
2022-09-10 17:01:51 +08:00
$t->expiration = $date_exp;
$t->time = $time;
$t->method = "$gateway - $channel";
$t->routers = $router_name;
$t->type = "PPPOE";
$t->save();
}
2023-03-06 15:50:55 +08:00
Message::sendTelegram("#u$c[username] #buy #PPPOE \n" . $p['name_plan'] .
2022-09-10 17:01:51 +08:00
"\nRouter: " . $router_name .
"\nGateway: " . $gateway .
"\nChannel: " . $channel .
2023-08-24 12:35:23 +08:00
"\nPrice: " . Lang::moneyFormat($p['price']));
2022-09-10 17:01:51 +08:00
}
2024-02-07 11:11:30 +08:00
Message::sendInvoice($c, $t);
2022-09-10 17:01:51 +08:00
return true;
}
2023-08-16 10:48:07 +08:00
2023-11-15 12:08:18 +08:00
public static function changeTo($username, $plan_id, $from_id)
2023-08-16 10:48:07 +08:00
{
$c = ORM::for_table('tbl_customers')->where('username', $username)->find_one();
$p = ORM::for_table('tbl_plans')->where('id', $plan_id)->where('enabled', '1')->find_one();
2023-11-15 12:08:18 +08:00
$b = ORM::for_table('tbl_user_recharges')->find_one($from_id);
2023-11-21 15:41:21 +08:00
if($p['routers'] == $b['routers'] && $b['routers'] != 'radius'){
2023-11-15 12:08:18 +08:00
$mikrotik = Mikrotik::info($p['routers']);
}else{
$mikrotik = Mikrotik::info($b['routers']);
}
// delete first
2023-08-16 10:48:07 +08:00
if ($p['type'] == 'Hotspot') {
if ($b) {
2023-11-15 12:08:18 +08:00
if (!$p['is_radius']) {
2023-08-16 10:48:07 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removeHotspotUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removeHotspotActiveUser($client, $c['username']);
2023-08-16 10:48:07 +08:00
}
} else {
2023-11-15 12:08:18 +08:00
if (!$p['is_radius']) {
2023-08-16 10:48:07 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-28 11:38:59 +08:00
Mikrotik::removeHotspotUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removeHotspotActiveUser($client, $c['username']);
2023-08-16 10:48:07 +08:00
}
}
} else {
if ($b) {
2023-11-15 12:08:18 +08:00
if (!$p['is_radius']) {
2023-08-16 10:48:07 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::removePpoeUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removePpoeActive($client, $c['username']);
2023-08-16 10:48:07 +08:00
}
} else {
2023-11-15 12:08:18 +08:00
if (!$p['is_radius']) {
2023-08-16 10:48:07 +08:00
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-28 11:38:59 +08:00
Mikrotik::removePpoeUser($client, $c['username']);
2023-09-13 10:37:05 +08:00
Mikrotik::removePpoeActive($client, $c['username']);
2023-11-15 12:08:18 +08:00
}
}
}
// call the next mikrotik
2023-11-21 15:41:21 +08:00
if($p['routers'] != $b['routers'] && $p['routers'] != 'radius'){
2023-11-15 12:08:18 +08:00
$mikrotik = Mikrotik::info($p['routers']);
}
if ($p['type'] == 'Hotspot') {
if ($b) {
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p, $b['expiration'].''.$b['time']);
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::addHotspotUser($client, $p, $c);
}
} else {
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p, $b['expiration'].''.$b['time']);
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::addHotspotUser($client, $p, $c);
}
}
} else {
if ($b) {
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p);
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::addPpoeUser($client, $p, $c);
}
} else {
if ($p['is_radius']) {
Radius::customerAddPlan($c, $p);
}else{
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
2023-08-16 10:48:07 +08:00
Mikrotik::addPpoeUser($client, $p, $c);
}
}
}
}
2023-08-18 10:47:03 +08:00
public static function _raid($l)
{
return substr(str_shuffle(str_repeat('0123456789', $l)), 0, $l);
}
2022-09-10 17:01:51 +08:00
}