Change Attribute to Bill Only

This commit is contained in:
Ibnu Maksum 2024-03-15 10:38:05 +07:00
parent 532fbf7337
commit c82b6b6acf
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5
9 changed files with 131 additions and 137 deletions

View File

@ -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);

View File

@ -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'];

View File

@ -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;

View File

@ -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();

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -107,25 +107,28 @@
</ul>
<div class="row">
<div class="col-xs-4">
<a href="{$_url}customers/deactivate/{$d['id']}" id="{$d['id']}"
<a href="{$_url}customers/deactivate/{$d['id']}/{$package['plan_id']}" id="{$d['id']}"
class="btn btn-danger btn-block btn-sm"
onclick="return confirm('This will deactivate Customer Plan, and make it expired')">{Lang::T('Deactivate')}</a>
</div>
<div class="col-xs-4">
<a href="{$_url}customers/recharge/{$d['id']}"
onclick="return confirm('This will extend Customer plan, same as recharge')"
<div class="col-xs-8">
<a href="{$_url}customers/recharge/{$d['id']}/{$package['plan_id']}"
class="btn btn-success btn-sm btn-block">{Lang::T('Recharge')}</a>
</div>
<div class="col-xs-4">
<a href="{$_url}customers/sync/{$d['id']}"
onclick="return confirm('This will sync Customer to Mikrotik?')"
class="btn btn-primary btn-sm btn-block">{Lang::T('Sync')}</a>
</div>
</div>
</div>
</div>
{/foreach}
<a href="{$_url}customers/list" class="btn btn-primary btn-sm btn-block mt-1">{Lang::T('Back')}</a><br>
<div class="row">
<div class="col-xs-4">
<a href="{$_url}customers/list" class="btn btn-primary btn-sm btn-block">{Lang::T('Back')}</a>
</div>
<div class="col-xs-8">
<a href="{$_url}customers/sync/{$d['id']}"
onclick="return confirm('This will sync Customer to Mikrotik?')"
class="btn btn-info btn-sm btn-block">{Lang::T('Sync')}</a>
</div>
</div>
</div>
<div class="col-sm-8 col-md-8">
<ul class="nav nav-tabs">

View File

@ -55,7 +55,7 @@
</ul>
<center><b>{Lang::T('Total')}</b></center>
<ul class="list-group list-group-unbordered">
{if $add_rem != 0 and $using neq 'zero' and $add_cost>0}
{if $using neq 'zero' and $add_cost>0}
{foreach $bills as $k => $v}
<li class="list-group-item">
<b>{$k}</b> <span class="pull-right">{Lang::moneyFormat($v)}</span>
@ -65,11 +65,6 @@
<b>{Lang::T('Additional Cost')}</b> <span
class="pull-right">{Lang::moneyFormat($add_cost)}</span>
</li>
{if $add_rem != ''}
<li class="list-group-item">
<b>{Lang::T('Remaining')}</b> <span class="pull-right">{$add_rem}</span>
</li>
{/if}
<li class="list-group-item">
<b>{Lang::T('Total')}</b> <small>({Lang::T('Plan Price')} +{Lang::T('Additional Cost')})</small><span class="pull-right"
style="font-size: large; font-weight:bolder; font-family: 'Courier New', Courier, monospace; ">{Lang::moneyFormat($plan['price']+$add_cost)}</span>