Merge pull request #358 from gerandonk/Development

add sync function
This commit is contained in:
iBNu Maksum 2024-12-03 13:35:03 +07:00 committed by GitHub
commit f9e7a8dbb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 86 additions and 14 deletions

View File

@ -284,7 +284,7 @@ switch ($action) {
if ($_app_stage != 'demo') { if ($_app_stage != 'demo') {
if (file_exists($dvc)) { if (file_exists($dvc)) {
require_once $dvc; require_once $dvc;
(new $p['device'])->add_customer($c, $p); (new $p['device'])->sync_customer($c, $p);
} else { } else {
new Exception(Lang::T("Devices Not Found")); new Exception(Lang::T("Devices Not Found"));
} }

View File

@ -110,7 +110,7 @@ if (isset($_GET['sync']) && !empty($_GET['sync'])) {
if ($_app_stage != 'demo') { if ($_app_stage != 'demo') {
if (file_exists($dvc)) { if (file_exists($dvc)) {
require_once $dvc; require_once $dvc;
(new $p['device'])->add_customer($c, $p); (new $p['device'])->sync_customer($c, $p);
} else { } else {
new Exception(Lang::T("Devices Not Found")); new Exception(Lang::T("Devices Not Found"));
} }

View File

@ -49,7 +49,7 @@ switch ($action) {
if ($_app_stage != 'demo') { if ($_app_stage != 'demo') {
if (file_exists($dvc)) { if (file_exists($dvc)) {
require_once $dvc; require_once $dvc;
(new $p['device'])->add_customer($c, $p); (new $p['device'])->sync_customer($c, $p);
} else { } else {
new Exception(Lang::T("Devices Not Found")); new Exception(Lang::T("Devices Not Found"));
} }

View File

@ -41,6 +41,31 @@ class MikrotikHotspot
$this->addHotspotUser($client, $plan, $customer); $this->addHotspotUser($client, $plan, $customer);
} }
function sync_customer($customer, $plan)
{
$mikrotik = $this->info($plan['routers']);
$client = $this->getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
$t = ORM::for_table('tbl_user_recharges')->where('username', $customer['username'])->where('status', 'on')->find_one();
if ($t) {
$printRequest = new RouterOS\Request('/ip/hotspot/user/print');
$printRequest->setArgument('.proplist', '.id,limit-uptime,limit-bytes-total');
$printRequest->setQuery(RouterOS\Query::where('name', $customer['username']));
$userInfo = $client->sendSync($printRequest);
$id = $userInfo->getProperty('.id');
$uptime = $userInfo->getProperty('limit-uptime');
$data = $userInfo->getProperty('limit-bytes-total');
if (!empty($id) && (!empty($uptime) || !empty($data))) {
$setRequest = new RouterOS\Request('/ip/hotspot/user/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('profile', $t['namebp']);
$client->sendSync($setRequest);
} else {
$this->add_customer($customer, $plan);
}
}
}
function remove_customer($customer, $plan) function remove_customer($customer, $plan)
{ {
$mikrotik = $this->info($plan['routers']); $mikrotik = $this->info($plan['routers']);

View File

@ -78,6 +78,11 @@ class MikrotikPppoe
} }
} }
function sync_customer($customer, $plan)
{
$this->add_customer($customer, $plan);
}
function remove_customer($customer, $plan) function remove_customer($customer, $plan)
{ {
$mikrotik = $this->info($plan['routers']); $mikrotik = $this->info($plan['routers']);

View File

@ -58,6 +58,11 @@ class MikrotikVpn
} }
} }
function sync_customer($customer, $plan)
{
$this->add_customer($customer, $plan);
}
function remove_customer($customer, $plan) function remove_customer($customer, $plan)
{ {
$mikrotik = $this->info($plan['routers']); $mikrotik = $this->info($plan['routers']);

View File

@ -50,18 +50,42 @@ class Radius
if ($p['validity_unit'] == 'Months') { if ($p['validity_unit'] == 'Months') {
$date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month')); $date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month'));
$time = date("H:i:s");
} else if ($p['validity_unit'] == 'Period') { } else if ($p['validity_unit'] == 'Period') {
$date_tmp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month')); $current_date = new DateTime($date_only);
$dt1 = new DateTime("$date_only"); $exp_date = clone $current_date;
$dt2 = new DateTime("$date_tmp"); $exp_date->modify('first day of next month');
$diff = $dt2->diff($dt1); $exp_date->setDate($exp_date->format('Y'), $exp_date->format('m'), $day_exp);
$sum = $diff->format("%a"); // => 453
if ($sum >= 35 * $p['validity']) { $min_days = 7 * $p['validity'];
$date_exp = date("Y-m-$day_exp", strtotime('+0 month')); $max_days = 35 * $p['validity'];
} else {
$date_exp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month')); $days_until_exp = $exp_date->diff($current_date)->days;
};
$time = date("23:59:00"); // If less than min_days away, move to the next period
while ($days_until_exp < $min_days) {
$exp_date->modify('+1 month');
$days_until_exp = $exp_date->diff($current_date)->days;
}
// If more than max_days away, move to the previous period
while ($days_until_exp > $max_days) {
$exp_date->modify('-1 month');
$days_until_exp = $exp_date->diff($current_date)->days;
}
// Final check to ensure we're not less than min_days or in the past
if ($days_until_exp < $min_days || $exp_date <= $current_date) {
$exp_date->modify('+1 month');
}
// Adjust for multiple periods
if ($p['validity'] > 1) {
$exp_date->modify('+' . ($p['validity'] - 1) . ' months');
}
$date_exp = $exp_date->format('Y-m-d');
$time = "23:59:59";
} else if ($p['validity_unit'] == 'Days') { } else if ($p['validity_unit'] == 'Days') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' day'))); $datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' day')));
$date_exp = $datetime[0]; $date_exp = $datetime[0];
@ -81,6 +105,14 @@ class Radius
} }
} }
function sync_customer($customer, $plan)
{
$t = ORM::for_table('tbl_user_recharges')->where('username', $customer['username'])->where('status', 'on')->findOne();
$date_exp = $t['expiration'];
$time = $t['time'];
$this->customerAddPlan($customer, $plan, $date_exp . ' ' . $time);
}
function remove_customer($customer, $plan) function remove_customer($customer, $plan)
{ {
if (!empty($plan['plan_expired'])) { if (!empty($plan['plan_expired'])) {

View File

@ -22,6 +22,11 @@ class RadiusRest {
{ {
} }
function sync_customer($customer, $plan)
{
$this->add_customer($customer, $plan);
}
// Remove Customer to Mikrotik/Device // Remove Customer to Mikrotik/Device
function remove_customer($customer, $plan) function remove_customer($customer, $plan)
{ {