From c82b6b6acf0375559ec25aea7c17cb6c0b3eb8de Mon Sep 17 00:00:00 2001 From: Ibnu Maksum Date: Fri, 15 Mar 2024 10:38:05 +0700 Subject: [PATCH] Change Attribute to Bill Only --- system/autoload/Package.php | 30 ++++++---------- system/autoload/User.php | 58 ++++++++++++++++++++++++++++-- system/controllers/customers.php | 60 +++++++++++++++----------------- system/controllers/order.php | 45 +++++------------------- system/controllers/prepaid.php | 20 ++--------- system/cron.php | 20 +++++------ system/cron_reminder.php | 5 ++- ui/ui/customers-view.tpl | 23 ++++++------ ui/ui/recharge-confirm.tpl | 7 +--- 9 files changed, 131 insertions(+), 137 deletions(-) diff --git a/system/autoload/Package.php b/system/autoload/Package.php index 018f9198..1d03738a 100644 --- a/system/autoload/Package.php +++ b/system/autoload/Package.php @@ -32,29 +32,21 @@ class Package $c = ORM::for_table('tbl_customers')->where('id', $id_customer)->find_one(); $p = ORM::for_table('tbl_plans')->where('id', $plan_id)->find_one(); - // Additional cost $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - // if empty then it doesnt have cycle, if zero then it finish - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } - if ($add_cost > 0 && $router_name != 'balance') { - $bills = User::getAttributes("Bill", $id_customer); - foreach ($bills as $k => $v) { - $note .= $k . " : " . Lang::moneyFormat($v) . "\n"; - } - } - // Zero cost recharge if (isset($zero) && $zero == 1) { $p['price'] = 0; - $add_cost = 0; + }else{ + // Additional cost + list($bills, $add_cost) = User::getBills($id_customer); + if ($add_cost > 0 && $router_name != 'balance') { + foreach ($bills as $k => $v) { + $note .= $k . " : " . Lang::moneyFormat($v) . "\n"; + } + } } + if (!$p['enabled']) { if (!isset($admin) || !isset($admin['id']) || empty($admin['id'])) { r2(U . 'home', 'e', Lang::T('Plan Not found')); @@ -576,8 +568,8 @@ class Package "\nPrice: " . Lang::moneyFormat($p['price'])); } } - if ($add_rem > 0) { - User::setAttribute('Additional Remaining', ($add_rem - 1), $id_customer); + if (count($bills) > 0) { + User::billsPaid($bills, $id_customer); } run_hook("recharge_user_finish"); Message::sendInvoice($c, $t); diff --git a/system/autoload/User.php b/system/autoload/User.php index 265dd474..574b034e 100644 --- a/system/autoload/User.php +++ b/system/autoload/User.php @@ -26,6 +26,60 @@ class User return 0; } + public static function getBills($id = 0) + { + if (!$id) { + $id = User::getID(); + if (!$id) { + return []; + } + } + $addcost = 0; + $bills = []; + $attrs = User::getAttributes('Bill', $id); + foreach ($attrs as $k => $v) { + // if has : then its an installment + if (strpos($v, ":") === false) { + // Not installment + $bills[$k] = $v; + $addcost += $v; + } else { + // installment + list($cost, $rem) = explode(":", $v); + // :0 installment is done + if ($rem != 0) { + $bills[$k] = $cost; + $addcost += $cost; + } + } + } + return [$bills, $addcost]; + } + + public static function billsPaid($bills, $id = 0) + { + if (!$id) { + $id = User::getID(); + if (!$id) { + return []; + } + } + foreach ($bills as $k => $v) { + // if has : then its an installment + $v = User::getAttribute($k, $id); + if (strpos($v, ":") === false) { + // Not installment, no need decrement + } else { + // installment + list($cost, $rem) = explode(":", $v); + // :0 installment is done + if ($rem != 0) { + User::setAttribute($k, "$cost:".($rem - 1), $id); + } + } + } + } + public static function setAttribute($name, $value, $id = 0) { if (!$id) { @@ -35,7 +89,7 @@ class User } } $f = ORM::for_table('tbl_customers_fields')->where('field_name', $name)->where('customer_id', $id)->find_one(); - if(!$f){ + if (!$f) { $f = ORM::for_table('tbl_customers_fields')->create(); $f->customer_id = $id; $f->field_name = $name; @@ -45,7 +99,7 @@ class User if ($result) { return $result; } - }else{ + } else { $f->field_value = $value; $f->save(); return $f['id']; diff --git a/system/controllers/customers.php b/system/controllers/customers.php index 309650cf..3424eb2b 100644 --- a/system/controllers/customers.php +++ b/system/controllers/customers.php @@ -97,20 +97,14 @@ switch ($action) { _alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard"); } $id_customer = $routes['2']; - $b = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->find_one(); + $plan_id = $routes['3']; + $b = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->where('plan_id', $plan_id)->find_one(); if ($b) { $gateway = 'Recharge'; $channel = $admin['fullname']; $cust = User::_info($id_customer); $plan = ORM::for_table('tbl_plans')->find_one($b['plan_id']); - $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if($add_rem != 0){ - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } + list($bills, $add_cost) = User::getBills($id_customer); if ($using == 'balance' && $config['enable_balance'] == 'yes') { if (!$cust) { r2(U . 'prepaid/recharge', 'e', Lang::T('Customer not found')); @@ -127,10 +121,8 @@ switch ($action) { $zero = 1; $gateway = 'Recharge Zero'; } - $bills = User::getAttributes("Bill", $id_customer); $ui->assign('bills', $bills); $ui->assign('add_cost', $add_cost); - $ui->assign('add_rem', $add_rem); $ui->assign('cust', $cust); $ui->assign('gateway', $gateway); $ui->assign('channel', $channel); @@ -138,16 +130,19 @@ switch ($action) { $ui->assign('using', 'cash'); $ui->assign('plan', $plan); $ui->display('recharge-confirm.tpl'); + }else{ + r2(U . 'customers/view/' . $id_customer, 'e', 'Cannot find active plan'); } - r2(U . 'customers/view/' . $id_customer, 'e', 'Cannot find active plan'); + break; case 'deactivate': if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { _alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard"); } $id_customer = $routes['2']; - $b = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->find_one(); + $plan_id = $routes['3']; + $b = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->where('plan_id', $plan_id)->find_one(); if ($b) { - $p = ORM::for_table('tbl_plans')->where('id', $b['plan_id'])->where('enabled', '1')->find_one(); + $p = ORM::for_table('tbl_plans')->where('id', $b['plan_id'])->find_one(); if ($p) { if ($p['is_radius']) { Radius::customerDeactivate($b['username']); @@ -175,27 +170,28 @@ switch ($action) { break; case 'sync': $id_customer = $routes['2']; - $b = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->where('status', 'on')->find_one(); - if ($b) { - $c = ORM::for_table('tbl_customers')->find_one($id_customer); - $p = ORM::for_table('tbl_plans')->where('id', $b['plan_id'])->where('enabled', '1')->find_one(); - if ($p) { - if ($p['is_radius']) { - Radius::customerAddPlan($c, $p, $p['expiration'] . ' ' . $p['time']); - r2(U . 'customers/view/' . $id_customer, 's', 'Success sync customer to Radius'); - } else { - $mikrotik = Mikrotik::info($b['routers']); - $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); - if ($b['type'] == 'Hotspot') { - Mikrotik::addHotspotUser($client, $p, $c); - } else if ($b['type'] == 'PPPOE') { - Mikrotik::addPpoeUser($client, $p, $c); + $bs = ORM::for_table('tbl_user_recharges')->where('customer_id', $id_customer)->where('status', 'on')->findMany(); + if ($bs) { + $routers = []; + foreach ($bs as $b) { + $c = ORM::for_table('tbl_customers')->find_one($id_customer); + $p = ORM::for_table('tbl_plans')->where('id', $b['plan_id'])->where('enabled', '1')->find_one(); + if ($p) { + $routers[] = $b['routers']; + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, $p['expiration'] . ' ' . $p['time']); + } else { + $mikrotik = Mikrotik::info($b['routers']); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + if ($b['type'] == 'Hotspot') { + Mikrotik::addHotspotUser($client, $p, $c); + } else if ($b['type'] == 'PPPOE') { + Mikrotik::addPpoeUser($client, $p, $c); + } } - r2(U . 'customers/view/' . $id_customer, 's', 'Success sync customer to Mikrotik'); } - } else { - r2(U . 'customers/view/' . $id_customer, 'e', 'Customer plan is inactive'); } + r2(U . 'customers/view/' . $id_customer, 's', 'Sync success to '.implode(", ",$routers)); } r2(U . 'customers/view/' . $id_customer, 'e', 'Cannot find active plan'); break; diff --git a/system/controllers/order.php b/system/controllers/order.php index 877fb80b..9df265f2 100644 --- a/system/controllers/order.php +++ b/system/controllers/order.php @@ -131,18 +131,8 @@ switch ($action) { $router = Mikrotik::info($trx['routers']); $plan = ORM::for_table('tbl_plans')->find_one($trx['plan_id']); $bandw = ORM::for_table('tbl_bandwidth')->find_one($plan['id_bw']); - - $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - }else{ - $bills = User::getAttributes("Bill", $id_customer); - $ui->assign('bills', $bills); - } - } + list($bills, $add_cost) = User::getBills($id_customer); + $ui->assign('bills', $bills); $ui->assign('add_cost', $add_cost); $ui->assign('trx', $trx); $ui->assign('router', $router); @@ -167,14 +157,7 @@ switch ($action) { } else { $router_name = $plan['routers']; } - $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } + list($bills, $add_cost) = User::getBills($id_customer); if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price']) { if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) { // if success, then get the balance @@ -210,15 +193,11 @@ switch ($action) { } if (isset($_POST['send']) && $_POST['send'] == 'plan') { $target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one(); - $add_rem = User::getAttribute("Additional Remaining", $target['id']); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $target['id']); - if (!empty($add_cost)) { - $bills = User::getAttributes("Bill", $target['id']); - $ui->assign('bills', $bills); - $ui->assign('add_cost', $add_cost); - $plan['price'] += $add_cost; - } + list($bills, $add_cost) = User::getBills($target['id']); + if (!empty($add_cost)) { + $ui->assign('bills', $bills); + $ui->assign('add_cost', $add_cost); + $plan['price'] += $add_cost; } if (!$target) { r2(U . 'home', 'd', Lang::T('Username not found')); @@ -363,13 +342,7 @@ switch ($action) { } $add_cost = 0; if ($router['name'] != 'balance') { - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } + list($bills, $add_cost) = User::getBills($id_customer); } if (empty($id)) { $d = ORM::for_table('tbl_payment_gateway')->create(); diff --git a/system/controllers/prepaid.php b/system/controllers/prepaid.php index b9f11034..7e2ce627 100644 --- a/system/controllers/prepaid.php +++ b/system/controllers/prepaid.php @@ -115,14 +115,7 @@ switch ($action) { $channel = $admin['fullname']; $cust = User::_info($id_customer); $plan = ORM::for_table('tbl_plans')->find_one($planId); - $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if($add_rem != 0){ - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } + list($bills, $add_cost) = User::getBills($id_customer); if ($using == 'balance' && $config['enable_balance'] == 'yes') { if (!$cust) { r2(U . 'prepaid/recharge', 'e', Lang::T('Customer not found')); @@ -139,10 +132,8 @@ switch ($action) { $zero = 1; $gateway = 'Recharge Zero'; } - $bills = User::getAttributes("Bill", $id_customer); $ui->assign('bills', $bills); $ui->assign('add_cost', $add_cost); - $ui->assign('add_rem', $add_rem); $ui->assign('cust', $cust); $ui->assign('gateway', $gateway); $ui->assign('channel', $channel); @@ -173,14 +164,7 @@ switch ($action) { $gateway = 'Recharge'; $channel = $admin['fullname']; $cust = User::_info($id_customer); - $add_cost = 0; - $add_rem = User::getAttribute("Additional Remaining", $id_customer); - if($add_rem != 0){ - $add_cost = User::getAttribute("Additional Cost", $id_customer); - if (empty($add_cost)) { - $add_cost = 0; - } - } + list($bills, $add_cost) = User::getBills($id_customer); if ($using == 'balance' && $config['enable_balance'] == 'yes') { $plan = ORM::for_table('tbl_plans')->find_one($planId); if (!$cust) { diff --git a/system/cron.php b/system/cron.php index e722a9d8..398c3146 100644 --- a/system/cron.php +++ b/system/cron.php @@ -46,24 +46,23 @@ foreach ($d as $ds) { $client = Mikrotik::getClient($m['ip_address'], $m['username'], $m['password']); if (!empty($p['pool_expired'])) { Mikrotik::setHotspotUserPackage($client, $c['username'], 'EXPIRED NUXBILL ' . $p['pool_expired']); - // }if (!empty($p['list_expired'])) { - // $ip = Mikrotik::getIpHotspotUser($client, $ds['username']); - // Mikrotik::addIpToAddressList($client, $ip, $p['list_expired'], $c['username']); + // }if (!empty($p['list_expired'])) { + // $ip = Mikrotik::getIpHotspotUser($client, $ds['username']); + // Mikrotik::addIpToAddressList($client, $ip, $p['list_expired'], $c['username']); } else { Mikrotik::removeHotspotUser($client, $c['username']); } Mikrotik::removeHotspotActiveUser($client, $c['username']); } - echo Message::sendPackageNotification($c, $u['namebp'], $price, $textExpired, $config['user_notification_expired'])."\n"; + echo Message::sendPackageNotification($c, $u['namebp'], $price, $textExpired, $config['user_notification_expired']) . "\n"; //update database user dengan status off $u->status = 'off'; $u->save(); // autorenewal from deposit if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) { - $add_rem = User::getAttribute("Additional Remaining", $ds['customer_id']); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $ds['customer_id']); + list($bills, $add_cost) = User::getBills($ds['customer_id']); + if ($add_cost > 0) { if (!empty($add_cost)) { $p['price'] += $add_cost; } @@ -116,16 +115,15 @@ foreach ($d as $ds) { } Mikrotik::removePpoeActive($client, $c['username']); } - echo Message::sendPackageNotification($c, $u['namebp'], $price, $textExpired, $config['user_notification_expired'])."\n"; + echo Message::sendPackageNotification($c, $u['namebp'], $price, $textExpired, $config['user_notification_expired']) . "\n"; $u->status = 'off'; $u->save(); // autorenewal from deposit if ($config['enable_balance'] == 'yes' && $c['auto_renewal']) { - $add_rem = User::getAttribute("Additional Remaining", $ds['customer_id']); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $ds['customer_id']); + list($bills, $add_cost) = User::getBills($ds['customer_id']); + if ($add_cost > 0) { if (!empty($add_cost)) { $p['price'] += $add_cost; } diff --git a/system/cron_reminder.php b/system/cron_reminder.php index 82d2b611..f69cf906 100644 --- a/system/cron_reminder.php +++ b/system/cron_reminder.php @@ -38,9 +38,8 @@ foreach ($d as $ds) { $u = ORM::for_table('tbl_user_recharges')->where('id', $ds['id'])->find_one(); $p = ORM::for_table('tbl_plans')->where('id', $u['plan_id'])->find_one(); $c = ORM::for_table('tbl_customers')->where('id', $ds['customer_id'])->find_one(); - $add_rem = User::getAttribute("Additional Remaining", $ds['customer_id']); - if ($add_rem != 0) { - $add_cost = User::getAttribute("Additional Cost", $ds['customer_id']); + list($bills, $add_cost) = User::getBills($ds['customer_id']); + if ($add_cost > 0) { if (!empty($add_cost)) { $p['price'] += $add_cost; } diff --git a/ui/ui/customers-view.tpl b/ui/ui/customers-view.tpl index 8e38dcad..0a550a07 100644 --- a/ui/ui/customers-view.tpl +++ b/ui/ui/customers-view.tpl @@ -107,25 +107,28 @@
- -
{/foreach} - {Lang::T('Back')}
+
+ + +
{Lang::T('Total')}