2017-03-11 02:51:06 +07:00
< ? php
2023-10-18 17:24:00 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing ( https :// github . com / hotspotbilling / phpnuxbill / )
* by https :// t . me / ibnux
2021-08-19 13:38:29 +07:00
**/
2023-10-12 15:55:42 +07:00
2017-03-11 02:51:06 +07:00
_admin ();
2024-02-13 13:54:01 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Recharge Account' ));
2017-03-11 02:51:06 +07:00
$ui -> assign ( '_system_menu' , 'prepaid' );
$action = $routes [ '1' ];
$ui -> assign ( '_admin' , $admin );
2023-08-23 12:11:07 +07:00
$select2_customer = <<< EOT
< script >
document . addEventListener ( " DOMContentLoaded " , function ( event ) {
$ ( '#personSelect' ) . select2 ({
theme : " bootstrap " ,
ajax : {
url : function ( params ) {
if ( params . term != undefined ){
return './index.php?_route=autoload/customer_select2&s=' + params . term ;
} else {
return './index.php?_route=autoload/customer_select2' ;
}
}
}
});
});
</ script >
EOT ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
switch ( $action ) {
2023-09-15 14:15:43 +07:00
case 'sync' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2023-09-15 14:15:43 +07:00
set_time_limit ( - 1 );
$plans = ORM :: for_table ( 'tbl_user_recharges' ) -> where ( 'status' , 'on' ) -> find_many ();
$log = '' ;
$router = '' ;
foreach ( $plans as $plan ) {
2023-10-12 15:47:45 +07:00
if ( $router != $plan [ 'routers' ] && $plan [ 'routers' ] != 'radius' ) {
2023-09-15 14:15:43 +07:00
$mikrotik = Mikrotik :: info ( $plan [ 'routers' ]);
$client = Mikrotik :: getClient ( $mikrotik [ 'ip_address' ], $mikrotik [ 'username' ], $mikrotik [ 'password' ]);
$router = $plan [ 'routers' ];
}
$p = ORM :: for_table ( 'tbl_plans' ) -> findOne ( $plan [ 'plan_id' ]);
$c = ORM :: for_table ( 'tbl_customers' ) -> findOne ( $plan [ 'customer_id' ]);
2023-10-18 17:24:00 +07:00
if ( $plan [ 'routers' ] == 'radius' ) {
Radius :: customerAddPlan ( $c , $p , $plan [ 'expiration' ] . ' ' . $plan [ 'time' ]);
} else {
2023-10-12 15:47:45 +07:00
if ( $plan [ 'type' ] == 'Hotspot' ) {
Mikrotik :: addHotspotUser ( $client , $p , $c );
} else if ( $plan [ 'type' ] == 'PPPOE' ) {
Mikrotik :: addPpoeUser ( $client , $p , $c );
}
2023-09-15 14:15:43 +07:00
}
$log .= " DONE : $plan[username] , $plan[namebp] , $plan[type] , $plan[routers] <br> " ;
}
2024-02-19 16:28:55 +07:00
if ( $isApi ) {
showResult ( true , $log );
}
2023-09-15 14:15:43 +07:00
r2 ( U . 'prepaid/list' , 's' , $log );
2021-08-19 13:38:29 +07:00
case 'list' :
$ui -> assign ( 'xfooter' , '<script type="text/javascript" src="ui/lib/c/prepaid.js"></script>' );
2024-02-13 13:54:01 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Customer' ));
2024-02-19 16:28:55 +07:00
$search = _post ( 'search' );
if ( $search != '' ) {
$paginator = Paginator :: build ( ORM :: for_table ( 'tbl_user_recharges' ), [ 'username' => '%' . $search . '%' ], $search );
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> where_like ( 'username' , '%' . $search . '%' ) -> offset ( $paginator [ 'startpoint' ]) -> limit ( $paginator [ 'limit' ]) -> order_by_desc ( 'id' ) -> find_many ();
2021-08-19 13:38:29 +07:00
} else {
2023-10-24 12:27:30 +07:00
$paginator = Paginator :: build ( ORM :: for_table ( 'tbl_user_recharges' ));
2024-02-19 16:28:55 +07:00
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> offset ( $paginator [ 'startpoint' ]) -> limit ( $paginator [ 'limit' ]) -> order_by_desc ( 'id' ) -> find_array ();
}
run_hook ( 'view_list_billing' ); #HOOK
if ( $isApi ) {
showResult ( true , $action , $d , [ 'search' => $search ]);
2021-08-19 13:38:29 +07:00
}
$ui -> assign ( 'd' , $d );
2024-02-19 16:28:55 +07:00
$ui -> assign ( 'search' , $search );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'paginator' , $paginator );
$ui -> display ( 'prepaid.tpl' );
2017-03-11 02:51:06 +07:00
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'recharge' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
2023-09-15 14:15:43 +07:00
if ( isset ( $routes [ '2' ]) && ! empty ( $routes [ '2' ])) {
2023-09-15 11:33:46 +07:00
$ui -> assign ( 'cust' , ORM :: for_table ( 'tbl_customers' ) -> find_one ( $routes [ '2' ]));
}
2022-09-18 00:00:40 +07:00
run_hook ( 'view_recharge' ); #HOOK
2017-03-11 02:51:06 +07:00
$ui -> display ( 'recharge.tpl' );
break ;
2024-03-14 12:14:11 +07:00
case 'recharge-confirm' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2024-03-14 12:14:11 +07:00
$id_customer = _post ( 'id_customer' );
$server = _post ( 'server' );
$planId = _post ( 'plan' );
$using = _post ( 'using' );
2017-03-11 02:51:06 +07:00
2024-03-14 12:14:11 +07:00
$msg = '' ;
if ( $id_customer == '' or $server == '' or $planId == '' or $using == '' ) {
$msg .= Lang :: T ( 'All field is required' ) . '<br>' ;
}
if ( $msg == '' ) {
$gateway = 'Recharge' ;
$channel = $admin [ 'fullname' ];
$cust = User :: _info ( $id_customer );
$plan = ORM :: for_table ( 'tbl_plans' ) -> find_one ( $planId );
2024-03-15 10:38:05 +07:00
list ( $bills , $add_cost ) = User :: getBills ( $id_customer );
2024-03-14 12:14:11 +07:00
if ( $using == 'balance' && $config [ 'enable_balance' ] == 'yes' ) {
if ( ! $cust ) {
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'Customer not found' ));
}
if ( ! $plan ) {
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'Plan not found' ));
}
if ( $cust [ 'balance' ] < ( $plan [ 'price' ] + $add_cost )) {
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'insufficient balance' ));
}
$gateway = 'Recharge Balance' ;
}
if ( $using == 'zero' ) {
$zero = 1 ;
$gateway = 'Recharge Zero' ;
}
$ui -> assign ( 'bills' , $bills );
$ui -> assign ( 'add_cost' , $add_cost );
$ui -> assign ( 'cust' , $cust );
$ui -> assign ( 'gateway' , $gateway );
$ui -> assign ( 'channel' , $channel );
$ui -> assign ( 'server' , $server );
$ui -> assign ( 'using' , $using );
$ui -> assign ( 'plan' , $plan );
$ui -> display ( 'recharge-confirm.tpl' );
} else {
r2 ( U . 'prepaid/recharge' , 'e' , $msg );
}
2021-08-19 13:38:29 +07:00
break ;
2017-03-11 02:51:06 +07:00
case 'recharge-post' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2017-03-11 02:51:06 +07:00
$id_customer = _post ( 'id_customer' );
$server = _post ( 'server' );
2024-03-12 15:09:00 +07:00
$planId = _post ( 'plan' );
$using = _post ( 'using' );
2021-08-19 13:38:29 +07:00
$msg = '' ;
2024-03-14 12:14:11 +07:00
if ( $id_customer == '' or $server == '' or $planId == '' or $using == '' ) {
2024-03-12 15:09:00 +07:00
$msg .= Lang :: T ( 'All field is required' ) . '<br>' ;
2021-08-19 13:38:29 +07:00
}
if ( $msg == '' ) {
2024-03-12 15:09:00 +07:00
$gateway = 'Recharge' ;
$channel = $admin [ 'fullname' ];
$cust = User :: _info ( $id_customer );
2024-03-15 10:38:05 +07:00
list ( $bills , $add_cost ) = User :: getBills ( $id_customer );
2024-03-14 12:14:11 +07:00
if ( $using == 'balance' && $config [ 'enable_balance' ] == 'yes' ) {
2024-03-12 15:09:00 +07:00
$plan = ORM :: for_table ( 'tbl_plans' ) -> find_one ( $planId );
if ( ! $cust ) {
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'Customer not found' ));
}
if ( ! $plan ) {
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'Plan not found' ));
}
2024-03-14 12:14:11 +07:00
if ( $cust [ 'balance' ] < ( $plan [ 'price' ] + $add_cost )) {
2024-03-12 15:09:00 +07:00
r2 ( U . 'prepaid/recharge' , 'e' , Lang :: T ( 'insufficient balance' ));
}
$gateway = 'Recharge Balance' ;
}
if ( $using == 'zero' ) {
2024-03-14 12:14:11 +07:00
$add_cost = 0 ;
2024-03-12 15:09:00 +07:00
$zero = 1 ;
$gateway = 'Recharge Zero' ;
}
if ( Package :: rechargeUser ( $id_customer , $server , $planId , $gateway , $channel )) {
if ( $using == 'balance' ) {
2024-03-14 12:14:11 +07:00
Balance :: min ( $cust [ 'id' ], $plan [ 'price' ] + $add_cost );
2024-03-12 15:09:00 +07:00
}
$in = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'username' , $cust [ 'username' ]) -> order_by_desc ( 'id' ) -> find_one ();
2024-02-26 11:10:52 +07:00
Package :: createInvoice ( $in );
2023-08-15 17:01:48 +07:00
$ui -> display ( 'invoice.tpl' );
2024-03-12 15:09:00 +07:00
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Recharge ' . $cust [ 'username' ] . ' [' . $in [ 'plan_name' ] . '][' . Lang :: moneyFormat ( $in [ 'price' ]) . ']' , $admin [ 'user_type' ], $admin [ 'id' ]);
2023-08-23 12:11:07 +07:00
} else {
2023-08-15 17:01:48 +07:00
r2 ( U . 'prepaid/recharge' , 'e' , " Failed to recharge account " );
2022-09-05 15:11:22 +07:00
}
2021-08-19 13:38:29 +07:00
} else {
2017-03-11 02:51:06 +07:00
r2 ( U . 'prepaid/recharge' , 'e' , $msg );
}
break ;
2021-08-19 13:38:29 +07:00
2023-10-18 17:49:54 +07:00
case 'view' :
$id = $routes [ '2' ];
2024-02-26 11:10:52 +07:00
$in = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'id' , $id ) -> find_one ();
$ui -> assign ( 'in' , $in );
2023-10-24 12:27:30 +07:00
if ( ! empty ( $routes [ '3' ]) && $routes [ '3' ] == 'send' ) {
2024-02-26 11:10:52 +07:00
$c = ORM :: for_table ( 'tbl_customers' ) -> where ( 'username' , $in [ 'username' ]) -> find_one ();
2023-10-24 12:27:30 +07:00
if ( $c ) {
2024-02-26 11:10:52 +07:00
Message :: sendInvoice ( $c , $in );
2023-10-24 12:27:30 +07:00
r2 ( U . 'prepaid/view/' . $id , 's' , " Success send to customer " );
2023-10-20 13:57:12 +07:00
}
2023-10-24 12:27:30 +07:00
r2 ( U . 'prepaid/view/' . $id , 'd' , " Customer not found " );
2023-10-18 17:49:54 +07:00
}
2024-02-26 11:10:52 +07:00
Package :: createInvoice ( $in );
2023-10-20 13:57:12 +07:00
$ui -> assign ( '_title' , 'View Invoice' );
2023-10-18 17:49:54 +07:00
$ui -> display ( 'invoice.tpl' );
break ;
2021-08-19 13:38:29 +07:00
case 'print' :
2024-02-16 16:54:13 +07:00
$content = $_POST [ 'content' ];
2024-02-19 16:28:55 +07:00
if ( ! empty ( $content )) {
2024-02-26 11:10:52 +07:00
if ( $_POST [ 'nux' ] == 'print' ) {
2024-02-22 18:27:18 +07:00
//header("Location: nux://print?text=".urlencode($content));
2024-02-26 11:10:52 +07:00
$ui -> assign ( 'nuxprint' , " nux://print?text= " . urlencode ( $content ));
2024-02-22 18:27:18 +07:00
}
2024-02-16 16:54:13 +07:00
$ui -> assign ( 'content' , $content );
2024-02-19 16:28:55 +07:00
} else {
2024-02-16 16:54:13 +07:00
$id = _post ( 'id' );
$d = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'id' , $id ) -> find_one ();
$ui -> assign ( 'in' , $d );
$ui -> assign ( 'date' , Lang :: dateAndTimeFormat ( $d [ 'recharged_on' ], $d [ 'recharged_time' ]));
}
2021-08-19 13:38:29 +07:00
2022-09-18 00:00:40 +07:00
run_hook ( 'print_invoice' ); #HOOK
2017-03-11 02:51:06 +07:00
$ui -> display ( 'invoice-print.tpl' );
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'edit' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2017-03-11 02:51:06 +07:00
$id = $routes [ '2' ];
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> find_one ( $id );
2021-08-19 13:38:29 +07:00
if ( $d ) {
$ui -> assign ( 'd' , $d );
2024-03-12 15:09:00 +07:00
if ( in_array ( $admin [ 'user_type' ], array ( 'SuperAdmin' , 'Admin' ))) {
2024-03-12 13:58:42 +07:00
$p = ORM :: for_table ( 'tbl_plans' ) -> where_not_equal ( 'type' , 'Balance' ) -> find_many ();
2024-03-12 15:09:00 +07:00
} else {
2024-03-12 13:58:42 +07:00
$p = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' ) -> where_not_equal ( 'type' , 'Balance' ) -> find_many ();
}
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'p' , $p );
2022-09-18 00:00:40 +07:00
run_hook ( 'view_edit_customer_plan' ); #HOOK
2024-02-02 13:38:22 +07:00
$ui -> assign ( '_title' , 'Edit Plan' );
2017-03-11 02:51:06 +07:00
$ui -> display ( 'prepaid-edit.tpl' );
2021-08-19 13:38:29 +07:00
} else {
2017-03-11 02:51:06 +07:00
r2 ( U . 'services/list' , 'e' , $_L [ 'Account_Not_Found' ]);
}
break ;
case 'delete' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2017-03-11 02:51:06 +07:00
$id = $routes [ '2' ];
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> find_one ( $id );
2021-08-19 13:38:29 +07:00
if ( $d ) {
2022-09-18 00:00:40 +07:00
run_hook ( 'delete_customer_active_plan' ); #HOOK
2023-10-04 16:11:55 +07:00
$p = ORM :: for_table ( 'tbl_plans' ) -> find_one ( $d [ 'plan_id' ]);
if ( $p [ 'is_radius' ]) {
Radius :: customerDeactivate ( $d [ 'username' ]);
} else {
$mikrotik = Mikrotik :: info ( $d [ 'routers' ]);
if ( $d [ 'type' ] == 'Hotspot' ) {
2022-09-18 00:52:39 +07:00
$client = Mikrotik :: getClient ( $mikrotik [ 'ip_address' ], $mikrotik [ 'username' ], $mikrotik [ 'password' ]);
2023-09-07 09:20:31 +07:00
Mikrotik :: removeHotspotUser ( $client , $d [ 'username' ]);
2023-09-13 09:37:05 +07:00
Mikrotik :: removeHotspotActiveUser ( $client , $d [ 'username' ]);
2023-10-04 16:11:55 +07:00
} else {
2022-09-18 00:52:39 +07:00
$client = Mikrotik :: getClient ( $mikrotik [ 'ip_address' ], $mikrotik [ 'username' ], $mikrotik [ 'password' ]);
2023-09-07 09:20:31 +07:00
Mikrotik :: removePpoeUser ( $client , $d [ 'username' ]);
2023-09-13 09:37:05 +07:00
Mikrotik :: removePpoeActive ( $client , $d [ 'username' ]);
2021-08-19 13:38:29 +07:00
}
}
2023-10-04 16:11:55 +07:00
$d -> delete ();
2024-02-12 17:02:43 +07:00
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Delete Plan for Customer ' . $c [ 'username' ] . ' [' . $in [ 'plan_name' ] . '][' . Lang :: moneyFormat ( $in [ 'price' ]) . ']' , $admin [ 'user_type' ], $admin [ 'id' ]);
2024-02-13 13:54:01 +07:00
r2 ( U . 'prepaid/list' , 's' , Lang :: T ( 'Data Deleted Successfully' ));
2017-03-11 02:51:06 +07:00
}
break ;
case 'edit-post' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2017-03-11 02:51:06 +07:00
$username = _post ( 'username' );
$id_plan = _post ( 'id_plan' );
$recharged_on = _post ( 'recharged_on' );
2021-08-19 13:38:29 +07:00
$expiration = _post ( 'expiration' );
2023-08-28 08:57:33 +07:00
$time = _post ( 'time' );
2017-03-11 02:51:06 +07:00
$id = _post ( 'id' );
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> find_one ( $id );
2021-08-19 13:38:29 +07:00
if ( $d ) {
} else {
2024-02-13 13:54:01 +07:00
$msg .= Lang :: T ( 'Data Not Found' ) . '<br>' ;
2017-03-11 02:51:06 +07:00
}
2023-11-21 13:47:10 +07:00
$p = ORM :: for_table ( 'tbl_plans' ) -> where ( 'id' , $id_plan ) -> where ( 'enabled' , '1' ) -> find_one ();
2023-11-15 11:08:18 +07:00
if ( $d ) {
} else {
$msg .= ' Plan Not Found<br>' ;
}
2021-08-19 13:38:29 +07:00
if ( $msg == '' ) {
2022-09-18 00:00:40 +07:00
run_hook ( 'edit_customer_plan' ); #HOOK
2017-03-11 02:51:06 +07:00
$d -> username = $username ;
$d -> plan_id = $id_plan ;
2024-02-02 13:38:22 +07:00
$d -> namebp = $p [ 'name_plan' ];
2023-08-28 09:44:57 +07:00
//$d->recharged_on = $recharged_on;
2017-03-11 02:51:06 +07:00
$d -> expiration = $expiration ;
2023-08-28 08:57:33 +07:00
$d -> time = $time ;
2024-02-19 16:28:55 +07:00
if ( $d [ 'status' ] == 'off' ) {
if ( strtotime ( $expiration . ' ' . $time ) > time ()) {
2024-02-02 13:38:22 +07:00
$d -> status = 'on' ;
}
2024-01-29 11:34:07 +07:00
}
2024-02-19 16:28:55 +07:00
if ( $p [ 'is_radius' ]) {
2023-11-20 09:02:33 +07:00
$d -> routers = 'radius' ;
2024-02-19 16:28:55 +07:00
} else {
2023-11-20 09:02:33 +07:00
$d -> routers = $p [ 'routers' ];
}
2017-03-11 02:51:06 +07:00
$d -> save ();
2024-02-19 16:28:55 +07:00
if ( $d [ 'status' ] == 'on' ) {
2024-02-02 13:39:03 +07:00
Package :: changeTo ( $username , $id_plan , $id );
}
2024-02-12 17:02:43 +07:00
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Edit Plan for Customer ' . $d [ 'username' ] . ' to [' . $d [ 'namebp' ] . '][' . Lang :: moneyFormat ( $p [ 'price' ]) . ']' , $admin [ 'user_type' ], $admin [ 'id' ]);
2024-02-13 13:54:01 +07:00
r2 ( U . 'prepaid/list' , 's' , Lang :: T ( 'Data Updated Successfully' ));
2021-08-19 13:38:29 +07:00
} else {
r2 ( U . 'prepaid/edit/' . $id , 'e' , $msg );
2017-03-11 02:51:06 +07:00
}
break ;
2021-08-19 13:38:29 +07:00
case 'voucher' :
2024-03-13 16:59:12 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Vouchers' ));
2024-02-20 16:49:03 +07:00
$limit = 10 ;
$page = _get ( 'p' , 0 );
$pageNow = $page * $limit ;
$search = _req ( 'search' );
if ( $search != '' ) {
if ( in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
$d = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where_like ( 'tbl_voucher.code' , '%' . $search . '%' )
-> offset ( $pageNow )
-> limit ( $limit )
-> findArray ();
} else if ( $admin [ 'user_type' ] == 'Agent' ) {
$sales = [];
$sls = ORM :: for_table ( 'tbl_users' ) -> select ( 'id' ) -> where ( 'root' , $admin [ 'id' ]) -> findArray ();
foreach ( $sls as $s ) {
$sales [] = $s [ 'id' ];
}
$sales [] = $admin [ 'id' ];
$d = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where_in ( 'generated_by' , $sales )
-> where_like ( 'tbl_voucher.code' , '%' . $search . '%' )
-> offset ( $pageNow )
-> limit ( $limit )
-> findArray ();
}
2021-08-19 13:38:29 +07:00
} else {
2024-02-20 16:49:03 +07:00
if ( in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
$d = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> offset ( $pageNow )
-> limit ( $limit )
-> findArray ();
} else if ( $admin [ 'user_type' ] == 'Agent' ) {
$sales = [];
$sls = ORM :: for_table ( 'tbl_users' ) -> select ( 'id' ) -> where ( 'root' , $admin [ 'id' ]) -> findArray ();
foreach ( $sls as $s ) {
$sales [] = $s [ 'id' ];
}
$sales [] = $admin [ 'id' ];
$d = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where_in ( 'generated_by' , $sales )
-> offset ( $pageNow )
-> limit ( $limit )
-> findArray ();
}
2021-08-19 13:38:29 +07:00
}
2024-02-16 16:54:13 +07:00
// extract admin
$admins = [];
foreach ( $d as $k ) {
2024-02-19 16:28:55 +07:00
if ( ! empty ( $k [ 'generated_by' ])) {
2024-02-16 16:54:13 +07:00
$admins [] = $k [ 'generated_by' ];
}
}
2024-02-19 16:28:55 +07:00
if ( count ( $admins ) > 0 ) {
2024-02-16 16:54:13 +07:00
$adms = ORM :: for_table ( 'tbl_users' ) -> where_in ( 'id' , $admins ) -> find_many ();
unset ( $admins );
2024-02-19 16:28:55 +07:00
foreach ( $adms as $adm ) {
2024-02-16 16:54:13 +07:00
$tipe = $adm [ 'user_type' ];
2024-02-19 16:28:55 +07:00
if ( $tipe == 'Sales' ) {
2024-02-16 16:54:13 +07:00
$tipe = ' [S]' ;
2024-02-19 16:28:55 +07:00
} else if ( $tipe == 'Agent' ) {
2024-02-16 16:54:13 +07:00
$tipe = ' [A]' ;
2024-02-19 16:28:55 +07:00
} else {
2024-02-16 16:54:13 +07:00
$tipe == '' ;
}
2024-02-19 16:28:55 +07:00
$admins [ $adm [ 'id' ]] = $adm [ 'fullname' ] . $tipe ;
2024-02-16 16:54:13 +07:00
}
}
$ui -> assign ( 'admins' , $admins );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'd' , $d );
2024-02-20 16:49:03 +07:00
$ui -> assign ( 'search' , $search );
$ui -> assign ( 'page' , $page );
2022-09-18 00:00:40 +07:00
run_hook ( 'view_list_voucher' ); #HOOK
2021-08-19 13:38:29 +07:00
$ui -> display ( 'voucher.tpl' );
2017-03-11 02:51:06 +07:00
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'add-voucher' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2024-02-13 13:54:01 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Add Vouchers' ));
2021-08-19 13:38:29 +07:00
$c = ORM :: for_table ( 'tbl_customers' ) -> find_many ();
$ui -> assign ( 'c' , $c );
2022-09-08 14:00:46 +07:00
$p = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' ) -> find_many ();
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'p' , $p );
2022-09-08 14:00:46 +07:00
$r = ORM :: for_table ( 'tbl_routers' ) -> where ( 'enabled' , '1' ) -> find_many ();
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'r' , $r );
2022-09-18 00:00:40 +07:00
run_hook ( 'view_add_voucher' ); #HOOK
2017-03-11 02:51:06 +07:00
$ui -> display ( 'voucher-add.tpl' );
break ;
2021-08-19 13:38:29 +07:00
2023-12-19 11:45:18 +07:00
case 'remove-voucher' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2023-12-19 11:45:18 +07:00
$d = ORM :: for_table ( 'tbl_voucher' ) -> where_equal ( 'status' , '1' ) -> findMany ();
if ( $d ) {
2024-01-08 16:12:21 +07:00
$jml = 0 ;
foreach ( $d as $v ) {
2024-02-19 16:28:55 +07:00
if ( ! ORM :: for_table ( 'tbl_user_recharges' ) -> where_equal ( " method " , 'Voucher - ' . $v [ 'code' ]) -> findOne ()) {
2024-01-08 16:12:21 +07:00
$v -> delete ();
$jml ++ ;
}
}
2024-02-19 16:28:55 +07:00
r2 ( U . 'prepaid/voucher' , 's' , " $jml " . Lang :: T ( 'Data Deleted Successfully' ));
2023-12-19 11:45:18 +07:00
}
2021-08-19 13:38:29 +07:00
case 'print-voucher' :
2023-09-26 13:50:02 +07:00
$from_id = _post ( 'from_id' );
$planid = _post ( 'planid' );
$pagebreak = _post ( 'pagebreak' );
$limit = _post ( 'limit' );
2023-10-18 17:24:00 +07:00
$vpl = _post ( 'vpl' );
2023-10-24 12:27:30 +07:00
if ( empty ( $vpl )) {
2023-10-18 17:24:00 +07:00
$vpl = 3 ;
}
if ( $pagebreak < 1 ) $pagebreak = 12 ;
2021-08-19 13:38:29 +07:00
if ( $limit < 1 ) $limit = $pagebreak * 2 ;
2023-10-24 12:27:30 +07:00
if ( empty ( $from_id )) {
2023-10-18 17:24:00 +07:00
$from_id = 0 ;
}
2021-08-19 13:38:29 +07:00
if ( $from_id > 0 && $planid > 0 ) {
$v = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> where ( 'tbl_plans.id' , $planid )
-> where_gt ( 'tbl_voucher.id' , $from_id )
2024-02-20 16:49:03 +07:00
-> limit ( $limit );
2021-08-19 13:38:29 +07:00
$vc = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> where ( 'tbl_plans.id' , $planid )
2024-02-20 16:49:03 +07:00
-> where_gt ( 'tbl_voucher.id' , $from_id );
2021-08-19 13:38:29 +07:00
} else if ( $from_id == 0 && $planid > 0 ) {
$v = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> where ( 'tbl_plans.id' , $planid )
2024-02-20 16:49:03 +07:00
-> limit ( $limit );
2021-08-19 13:38:29 +07:00
$vc = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
2024-02-20 16:49:03 +07:00
-> where ( 'tbl_plans.id' , $planid );
2021-08-19 13:38:29 +07:00
} else if ( $from_id > 0 && $planid == 0 ) {
$v = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> where_gt ( 'tbl_voucher.id' , $from_id )
2024-02-20 16:49:03 +07:00
-> limit ( $limit );
2021-08-19 13:38:29 +07:00
$vc = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
2024-02-20 16:49:03 +07:00
-> where_gt ( 'tbl_voucher.id' , $from_id );
2021-08-19 13:38:29 +07:00
} else {
$v = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
2024-02-20 16:49:03 +07:00
-> limit ( $limit );
2021-08-19 13:38:29 +07:00
$vc = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
2024-02-20 16:49:03 +07:00
-> where ( 'tbl_voucher.status' , '0' );
}
if ( in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
$v = $v -> find_many ();
$vc = $vc -> count ();
} else {
$sales = [];
$sls = ORM :: for_table ( 'tbl_users' ) -> select ( 'id' ) -> where ( 'root' , $admin [ 'id' ]) -> findArray ();
foreach ( $sls as $s ) {
$sales [] = $s [ 'id' ];
}
$sales [] = $admin [ 'id' ];
$v = $v -> where_in ( 'generated_by' , $sales ) -> find_many ();
$vc = $vc -> where_in ( 'generated_by' , $sales ) -> count ();
2021-08-19 13:38:29 +07:00
}
2023-10-18 17:24:00 +07:00
$template = file_get_contents ( " pages/Voucher.html " );
$template = str_replace ( '[[company_name]]' , $config [ 'CompanyName' ], $template );
2021-08-19 13:38:29 +07:00
2024-02-13 13:54:01 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Hotspot Voucher' ));
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'from_id' , $from_id );
2023-10-18 17:24:00 +07:00
$ui -> assign ( 'vpl' , $vpl );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'pagebreak' , $pagebreak );
$plans = ORM :: for_table ( 'tbl_plans' ) -> find_many ();
$ui -> assign ( 'plans' , $plans );
$ui -> assign ( 'limit' , $limit );
$ui -> assign ( 'planid' , $planid );
2023-10-18 17:24:00 +07:00
$voucher = [];
2023-10-20 14:31:56 +07:00
$n = 1 ;
2023-10-18 17:24:00 +07:00
foreach ( $v as $vs ) {
$temp = $template ;
$temp = str_replace ( '[[qrcode]]' , '<img src="qrcode/?data=' . $vs [ 'code' ] . '">' , $temp );
$temp = str_replace ( '[[price]]' , Lang :: moneyFormat ( $vs [ 'price' ]), $temp );
$temp = str_replace ( '[[voucher_code]]' , $vs [ 'code' ], $temp );
$temp = str_replace ( '[[plan]]' , $vs [ 'name_plan' ], $temp );
2023-10-20 14:31:56 +07:00
$temp = str_replace ( '[[counter]]' , $n , $temp );
2023-10-18 17:24:00 +07:00
$voucher [] = $temp ;
2023-10-20 14:31:56 +07:00
$n ++ ;
2023-10-18 17:24:00 +07:00
}
2023-10-24 12:27:30 +07:00
$ui -> assign ( 'voucher' , $voucher );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'vc' , $vc );
//for counting pagebreak
$ui -> assign ( 'jml' , 0 );
2022-09-18 00:00:40 +07:00
run_hook ( 'view_print_voucher' ); #HOOK
2021-08-19 13:38:29 +07:00
$ui -> display ( 'print-voucher.tpl' );
2017-03-15 02:37:15 +07:00
break ;
2017-03-11 02:51:06 +07:00
case 'voucher-post' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2017-03-11 02:51:06 +07:00
$type = _post ( 'type' );
2021-08-19 13:38:29 +07:00
$plan = _post ( 'plan' );
2024-01-08 15:39:04 +07:00
$voucher_format = _post ( 'voucher_format' );
$prefix = _post ( 'prefix' );
2021-08-19 13:38:29 +07:00
$server = _post ( 'server' );
$numbervoucher = _post ( 'numbervoucher' );
$lengthcode = _post ( 'lengthcode' );
$msg = '' ;
if ( $type == '' or $plan == '' or $server == '' or $numbervoucher == '' or $lengthcode == '' ) {
2024-02-13 13:54:01 +07:00
$msg .= Lang :: T ( 'All field is required' ) . '<br>' ;
2021-08-19 13:38:29 +07:00
}
if ( Validator :: UnsignedNumber ( $numbervoucher ) == false ) {
$msg .= 'The Number of Vouchers must be a number' . '<br>' ;
2017-03-11 02:51:06 +07:00
}
2021-08-19 13:38:29 +07:00
if ( Validator :: UnsignedNumber ( $lengthcode ) == false ) {
$msg .= 'The Length Code must be a number' . '<br>' ;
2017-03-11 02:51:06 +07:00
}
2021-08-19 13:38:29 +07:00
if ( $msg == '' ) {
2024-02-19 16:28:55 +07:00
if ( ! empty ( $prefix )) {
2024-01-08 16:12:21 +07:00
$d = ORM :: for_table ( 'tbl_appconfig' ) -> where ( 'setting' , 'voucher_prefix' ) -> find_one ();
if ( $d ) {
$d -> value = $prefix ;
$d -> save ();
} else {
$d = ORM :: for_table ( 'tbl_appconfig' ) -> create ();
$d -> setting = 'voucher_prefix' ;
$d -> value = $prefix ;
$d -> save ();
}
}
2022-09-18 00:00:40 +07:00
run_hook ( 'create_voucher' ); #HOOK
2021-08-19 13:38:29 +07:00
for ( $i = 0 ; $i < $numbervoucher ; $i ++ ) {
$code = strtoupper ( substr ( md5 ( time () . rand ( 10000 , 99999 )), 0 , $lengthcode ));
2024-01-08 15:39:04 +07:00
if ( $voucher_format == 'low' ) {
2023-10-18 17:24:00 +07:00
$code = strtolower ( $code );
2024-01-08 15:39:04 +07:00
} else if ( $voucher_format == 'rand' ) {
2023-10-18 17:24:00 +07:00
$code = Lang :: randomUpLowCase ( $code );
}
2021-08-19 13:38:29 +07:00
$d = ORM :: for_table ( 'tbl_voucher' ) -> create ();
$d -> type = $type ;
$d -> routers = $server ;
$d -> id_plan = $plan ;
2024-02-19 16:28:55 +07:00
$d -> code = $prefix . $code ;
2021-08-19 13:38:29 +07:00
$d -> user = '0' ;
$d -> status = '0' ;
2024-02-07 10:11:30 +07:00
$d -> generated_by = $admin [ 'id' ];
2021-08-19 13:38:29 +07:00
$d -> save ();
}
2024-02-19 16:28:55 +07:00
if ( $numbervoucher == 1 ) {
r2 ( U . 'prepaid/voucher-view/' . $d -> id (), 's' , Lang :: T ( 'Create Vouchers Successfully' ));
2024-02-16 16:54:13 +07:00
}
2021-08-19 13:38:29 +07:00
2024-02-13 13:54:01 +07:00
r2 ( U . 'prepaid/voucher' , 's' , Lang :: T ( 'Create Vouchers Successfully' ));
2021-08-19 13:38:29 +07:00
} else {
r2 ( U . 'prepaid/add-voucher/' . $id , 'e' , $msg );
2017-03-11 02:51:06 +07:00
}
break ;
2021-08-19 13:38:29 +07:00
2024-02-16 16:54:13 +07:00
case 'voucher-view' :
2024-02-20 16:49:03 +07:00
$id = $routes [ 2 ];
if ( in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-16 16:54:13 +07:00
$voucher = ORM :: for_table ( 'tbl_voucher' ) -> find_one ( $id );
2024-02-19 16:28:55 +07:00
} else {
2024-02-20 16:49:03 +07:00
$sales = [];
$sls = ORM :: for_table ( 'tbl_users' ) -> select ( 'id' ) -> where ( 'root' , $admin [ 'id' ]) -> findArray ();
foreach ( $sls as $s ) {
$sales [] = $s [ 'id' ];
}
$sales [] = $admin [ 'id' ];
$voucher = ORM :: for_table ( 'tbl_voucher' )
-> find_one ( $id );
if ( ! in_array ( $voucher [ 'generated_by' ], $sales )) {
r2 ( U . 'prepaid/voucher/' , 'e' , Lang :: T ( 'Voucher Not Found' ));
}
}
if ( ! $voucher ) {
r2 ( U . 'prepaid/voucher/' , 'e' , Lang :: T ( 'Voucher Not Found' ));
2024-02-16 16:54:13 +07:00
}
$plan = ORM :: for_table ( 'tbl_plans' ) -> find_one ( $d [ 'id_plan' ]);
if ( $voucher && $plan ) {
2024-02-19 16:28:55 +07:00
$content = Lang :: pad ( $config [ 'CompanyName' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( $config [ 'address' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( $config [ 'phone' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( " " , '=' ) . " \n " ;
$content .= Lang :: pads ( 'ID' , $voucher [ 'id' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Code' ), $voucher [ 'code' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Plan Name' ), $plan [ 'name_plan' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Type' ), $voucher [ 'type' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Plan Price' ), Lang :: moneyFormat ( $plan [ 'price' ]), ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Sales' ), $admin [ 'fullname' ] . ' #' . $admin [ 'id' ], ' ' ) . " \n " ;
$content .= Lang :: pad ( " " , '=' ) . " \n " ;
$content .= Lang :: pad ( $config [ 'note' ], ' ' , 2 ) . " \n " ;
2024-02-16 16:54:13 +07:00
$ui -> assign ( 'print' , $content );
$config [ 'printer_cols' ] = 30 ;
2024-02-19 16:28:55 +07:00
$content = Lang :: pad ( $config [ 'CompanyName' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( $config [ 'address' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( $config [ 'phone' ], ' ' , 2 ) . " \n " ;
$content .= Lang :: pad ( " " , '=' ) . " \n " ;
$content .= Lang :: pads ( 'ID' , $voucher [ 'id' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Code' ), $voucher [ 'code' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Plan Name' ), $plan [ 'name_plan' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Type' ), $voucher [ 'type' ], ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Plan Price' ), Lang :: moneyFormat ( $plan [ 'price' ]), ' ' ) . " \n " ;
$content .= Lang :: pads ( Lang :: T ( 'Sales' ), $admin [ 'fullname' ] . ' #' . $admin [ 'id' ], ' ' ) . " \n " ;
$content .= Lang :: pad ( " " , '=' ) . " \n " ;
$content .= Lang :: pad ( $config [ 'note' ], ' ' , 2 ) . " \n " ;
2024-02-16 16:54:13 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'View' ));
2024-02-26 11:10:52 +07:00
$ui -> assign ( 'whatsapp' , urlencode ( " ``` $content ``` " ));
2024-02-16 16:54:13 +07:00
$ui -> display ( 'voucher-view.tpl' );
2024-02-19 16:28:55 +07:00
} else {
2024-02-16 16:54:13 +07:00
r2 ( U . 'prepaid/voucher/' , 'e' , Lang :: T ( 'Voucher Not Found' ));
}
break ;
2017-03-11 02:51:06 +07:00
case 'voucher-delete' :
2024-02-16 14:52:49 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
2024-02-26 11:10:52 +07:00
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-16 14:52:49 +07:00
}
2017-03-11 02:51:06 +07:00
$id = $routes [ '2' ];
2022-09-18 00:00:40 +07:00
run_hook ( 'delete_voucher' ); #HOOK
2017-03-11 02:51:06 +07:00
$d = ORM :: for_table ( 'tbl_voucher' ) -> find_one ( $id );
2021-08-19 13:38:29 +07:00
if ( $d ) {
2017-03-11 02:51:06 +07:00
$d -> delete ();
2024-02-13 13:54:01 +07:00
r2 ( U . 'prepaid/voucher' , 's' , Lang :: T ( 'Data Deleted Successfully' ));
2017-03-11 02:51:06 +07:00
}
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'refill' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
2024-02-13 13:54:01 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Refill Account' ));
2022-09-18 00:00:40 +07:00
run_hook ( 'view_refill' ); #HOOK
2017-03-11 02:51:06 +07:00
$ui -> display ( 'refill.tpl' );
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'refill-post' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2021-08-19 13:38:29 +07:00
$code = _post ( 'code' );
2023-08-30 09:01:37 +07:00
$user = ORM :: for_table ( 'tbl_customers' ) -> where ( 'id' , _post ( 'id_customer' )) -> find_one ();
2021-08-19 13:38:29 +07:00
$v1 = ORM :: for_table ( 'tbl_voucher' ) -> where ( 'code' , $code ) -> where ( 'status' , 0 ) -> find_one ();
2022-09-18 00:00:40 +07:00
run_hook ( 'refill_customer' ); #HOOK
2021-08-19 13:38:29 +07:00
if ( $v1 ) {
2024-01-08 16:12:21 +07:00
if ( Package :: rechargeUser ( $user [ 'id' ], $v1 [ 'routers' ], $v1 [ 'id_plan' ], " Voucher " , $code )) {
2021-08-19 13:38:29 +07:00
$v1 -> status = " 1 " ;
2023-08-30 09:01:37 +07:00
$v1 -> user = $user [ 'username' ];
2021-08-19 13:38:29 +07:00
$v1 -> save ();
2023-08-30 09:01:37 +07:00
$in = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'username' , $user [ 'username' ]) -> order_by_desc ( 'id' ) -> find_one ();
2024-02-26 11:10:52 +07:00
Package :: createInvoice ( $in );
2023-08-15 17:01:48 +07:00
$ui -> display ( 'invoice.tpl' );
2023-08-23 12:11:07 +07:00
} else {
2023-08-15 17:01:48 +07:00
r2 ( U . 'prepaid/refill' , 'e' , " Failed to refill account " );
2021-08-19 13:38:29 +07:00
}
} else {
2024-02-13 13:54:01 +07:00
r2 ( U . 'prepaid/refill' , 'e' , Lang :: T ( 'Voucher Not Valid' ));
2021-08-19 13:38:29 +07:00
}
2017-03-11 02:51:06 +07:00
break ;
2023-08-15 17:01:48 +07:00
case 'deposit' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2023-08-15 17:01:48 +07:00
$ui -> assign ( '_title' , Lang :: T ( 'Refill Balance' ));
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
2024-03-13 14:32:10 +07:00
if ( in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' ])) {
$ui -> assign ( 'p' , ORM :: for_table ( 'tbl_plans' ) -> where ( 'type' , 'Balance' ) -> find_many ());
2024-03-14 12:14:11 +07:00
} else {
2024-03-13 14:32:10 +07:00
$ui -> assign ( 'p' , ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' ) -> where ( 'type' , 'Balance' ) -> find_many ());
}
2023-08-15 17:01:48 +07:00
run_hook ( 'view_deposit' ); #HOOK
$ui -> display ( 'deposit.tpl' );
break ;
case 'deposit-post' :
2024-02-26 11:10:52 +07:00
if ( ! in_array ( $admin [ 'user_type' ], [ 'SuperAdmin' , 'Admin' , 'Agent' , 'Sales' ])) {
_alert ( Lang :: T ( 'You do not have permission to access this page' ), 'danger' , " dashboard " );
2024-02-26 11:01:54 +07:00
}
2023-08-15 17:01:48 +07:00
$user = _post ( 'id_customer' );
$plan = _post ( 'id_plan' );
run_hook ( 'deposit_customer' ); #HOOK
if ( ! empty ( $user ) && ! empty ( $plan )) {
2023-08-23 12:11:07 +07:00
if ( Package :: rechargeUser ( $user , 'balance' , $plan , " Deposit " , $admin [ 'fullname' ])) {
2023-08-15 17:01:48 +07:00
$c = ORM :: for_table ( 'tbl_customers' ) -> where ( 'id' , $user ) -> find_one ();
$in = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'username' , $c [ 'username' ]) -> order_by_desc ( 'id' ) -> find_one ();
2024-02-26 11:10:52 +07:00
Package :: createInvoice ( $in );
2023-08-15 17:01:48 +07:00
$ui -> display ( 'invoice.tpl' );
2023-08-23 12:11:07 +07:00
} else {
2023-08-15 17:01:48 +07:00
r2 ( U . 'prepaid/refill' , 'e' , " Failed to refill account " );
}
} else {
r2 ( U . 'prepaid/refill' , 'e' , " All field is required " );
}
break ;
2017-03-11 02:51:06 +07:00
default :
2023-09-27 15:01:48 +07:00
$ui -> display ( 'a404.tpl' );
2021-08-19 13:38:29 +07:00
}