2017-03-11 02:51:06 +07:00
< ? php
/**
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 ();
2022-10-13 14:00:54 +07:00
$ui -> assign ( '_title' , $_L [ 'Recharge_Account' ]);
2017-03-11 02:51:06 +07:00
$ui -> assign ( '_system_menu' , 'prepaid' );
$action = $routes [ '1' ];
$admin = Admin :: _info ();
$ui -> assign ( '_admin' , $admin );
2021-08-19 13:38:29 +07:00
if ( $admin [ 'user_type' ] != 'Admin' and $admin [ 'user_type' ] != 'Sales' ) {
r2 ( U . " dashboard " , 'e' , $_L [ 'Do_Not_Access' ]);
2017-03-11 02:51:06 +07:00
}
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' :
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-12 15:47:45 +07:00
if ( $plan [ 'routers' ] == 'radius' ){
2023-10-13 16:23:01 +07:00
Radius :: customerAddPlan ( $c , $p , $plan [ 'expiration' ] . ' ' . $plan [ 'time' ]);
2023-10-12 15:47:45 +07:00
} else {
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> " ;
}
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>' );
2023-09-15 12:13:12 +07:00
$ui -> assign ( '_title' , $_L [ 'Customers' ]);
2021-08-19 13:38:29 +07:00
$username = _post ( 'username' );
if ( $username != '' ) {
$paginator = Paginator :: bootstrap ( 'tbl_user_recharges' , 'username' , '%' . $username . '%' );
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> where_like ( 'username' , '%' . $username . '%' ) -> offset ( $paginator [ 'startpoint' ]) -> limit ( $paginator [ 'limit' ]) -> order_by_desc ( 'id' ) -> find_many ();
} else {
$paginator = Paginator :: bootstrap ( 'tbl_user_recharges' );
$d = ORM :: for_table ( 'tbl_user_recharges' ) -> offset ( $paginator [ 'startpoint' ]) -> limit ( $paginator [ 'limit' ]) -> order_by_desc ( 'id' ) -> find_many ();
}
$ui -> assign ( 'd' , $d );
2023-04-06 09:50:03 +07:00
$ui -> assign ( 'cari' , $username );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'paginator' , $paginator );
2022-09-18 00:00:40 +07:00
run_hook ( 'view_list_billing' ); #HOOK
2021-08-19 13:38:29 +07:00
$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' :
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
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 );
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 ;
case 'recharge-user' :
2021-08-19 13:38:29 +07:00
$id = $routes [ '2' ];
$ui -> assign ( 'id' , $id );
2017-03-11 02:51:06 +07:00
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_recharge_customer' ); #HOOK
2017-03-11 02:51:06 +07:00
$ui -> display ( 'recharge-user.tpl' );
2021-08-19 13:38:29 +07:00
break ;
2017-03-11 02:51:06 +07:00
case 'recharge-post' :
$id_customer = _post ( 'id_customer' );
2021-08-19 13:38:29 +07:00
$type = _post ( 'type' );
2017-03-11 02:51:06 +07:00
$server = _post ( 'server' );
2021-08-19 13:38:29 +07:00
$plan = _post ( 'plan' );
$date_only = date ( " Y-m-d " );
$time = date ( " H:i:s " );
$msg = '' ;
if ( $id_customer == '' or $type == '' or $server == '' or $plan == '' ) {
$msg .= 'All field is required' . '<br>' ;
}
if ( $msg == '' ) {
2023-08-23 12:11:07 +07:00
if ( Package :: rechargeUser ( $id_customer , $server , $plan , " Recharge " , $admin [ 'fullname' ])) {
2023-08-15 17:01:48 +07:00
$c = ORM :: for_table ( 'tbl_customers' ) -> where ( 'id' , $id_customer ) -> find_one ();
$in = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'username' , $c [ 'username' ]) -> order_by_desc ( 'id' ) -> find_one ();
$ui -> assign ( 'in' , $in );
$ui -> assign ( 'date' , date ( " Y-m-d H:i:s " ));
$ui -> display ( 'invoice.tpl' );
2023-08-23 12:11:07 +07:00
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Recharge ' . $c [ 'username' ] . ' [' . $in [ 'plan_name' ] . '][' . Lang :: moneyFormat ( $in [ 'price' ]) . ']' , 'Admin' , $admin [ 'id' ]);
} 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
case 'print' :
$id = _post ( 'id' );
$d = ORM :: for_table ( 'tbl_transactions' ) -> where ( 'id' , $id ) -> find_one ();
$ui -> assign ( 'd' , $d );
2023-08-15 17:01:48 +07:00
$ui -> assign ( 'date' , date ( " Y-m-d H:i:s " ));
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' :
$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 );
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-18 00:00:40 +07:00
run_hook ( 'view_edit_customer_plan' ); #HOOK
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' :
$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 ();
2023-08-23 12:11:07 +07:00
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Delete Plan for Customer ' . $c [ 'username' ] . ' [' . $in [ 'plan_name' ] . '][' . Lang :: moneyFormat ( $in [ 'price' ]) . ']' , 'Admin' , $admin [ 'id' ]);
2017-03-11 02:51:06 +07:00
r2 ( U . 'prepaid/list' , 's' , $_L [ 'Delete_Successfully' ]);
}
break ;
case 'edit-post' :
$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 {
$msg .= $_L [ 'Data_Not_Found' ] . '<br>' ;
2017-03-11 02:51:06 +07:00
}
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 ;
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 ;
2017-03-11 02:51:06 +07:00
$d -> save ();
2023-08-23 12:11:07 +07:00
Package :: changeTo ( $username , $id_plan );
_log ( '[' . $admin [ 'username' ] . ']: ' . 'Edit Plan for Customer ' . $d [ 'username' ] . ' to [' . $d [ 'plan_name' ] . '][' . Lang :: moneyFormat ( $d [ 'price' ]) . ']' , 'Admin' , $admin [ 'id' ]);
2017-03-11 02:51:06 +07:00
r2 ( U . 'prepaid/list' , 's' , $_L [ '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' :
$ui -> assign ( 'xfooter' , '<script type="text/javascript" src="ui/lib/c/voucher.js"></script>' );
$code = _post ( 'code' );
if ( $code != '' ) {
$ui -> assign ( 'code' , $code );
$paginator = Paginator :: bootstrap ( 'tbl_voucher' , 'code' , '%' . $code . '%' );
2022-09-08 14:00:46 +07:00
$d = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' )
2021-08-19 13:38:29 +07:00
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
2023-04-06 09:47:26 +07:00
-> where_like ( 'tbl_voucher.code' , '%' . $code . '%' )
2021-08-19 13:38:29 +07:00
-> offset ( $paginator [ 'startpoint' ])
-> limit ( $paginator [ 'limit' ])
-> find_many ();
} else {
$paginator = Paginator :: bootstrap ( 'tbl_voucher' );
2022-09-08 14:00:46 +07:00
$d = ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' )
2021-08-19 13:38:29 +07:00
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> offset ( $paginator [ 'startpoint' ])
-> limit ( $paginator [ 'limit' ]) -> find_many ();
}
$ui -> assign ( 'd' , $d );
2023-04-06 09:47:26 +07:00
$ui -> assign ( '_code' , $code );
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'paginator' , $paginator );
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' :
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
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' );
2021-08-19 13:38:29 +07:00
if ( $pagebreak < 1 ) $pagebreak = 6 ;
if ( $limit < 1 ) $limit = $pagebreak * 2 ;
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 )
-> limit ( $limit )
-> find_many ();
$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 )
-> where_gt ( 'tbl_voucher.id' , $from_id )
-> count ();
} 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 )
-> limit ( $limit )
-> find_many ();
$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 )
-> count ();
} 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 )
-> limit ( $limit )
-> find_many ();
$vc = 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 )
-> count ();
} else {
$v = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> limit ( $limit )
-> find_many ();
$vc = ORM :: for_table ( 'tbl_plans' )
-> join ( 'tbl_voucher' , array ( 'tbl_plans.id' , '=' , 'tbl_voucher.id_plan' ))
-> where ( 'tbl_voucher.status' , '0' )
-> count ();
}
2022-10-13 14:00:54 +07:00
$ui -> assign ( '_title' , $_L [ 'Voucher_Hotspot' ]);
2021-08-19 13:38:29 +07:00
$ui -> assign ( 'from_id' , $from_id );
$ui -> assign ( 'pagebreak' , $pagebreak );
$plans = ORM :: for_table ( 'tbl_plans' ) -> find_many ();
$ui -> assign ( 'plans' , $plans );
$ui -> assign ( 'limit' , $limit );
$ui -> assign ( 'planid' , $planid );
$ui -> assign ( 'v' , $v );
$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' :
$type = _post ( 'type' );
2021-08-19 13:38:29 +07:00
$plan = _post ( 'plan' );
$server = _post ( 'server' );
$numbervoucher = _post ( 'numbervoucher' );
$lengthcode = _post ( 'lengthcode' );
$msg = '' ;
if ( $type == '' or $plan == '' or $server == '' or $numbervoucher == '' or $lengthcode == '' ) {
$msg .= $_L [ 'All_field_is_required' ] . '<br>' ;
}
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 == '' ) {
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 ));
2022-09-18 00:00:40 +07:00
//TODO: IMPLEMENT Voucher Generator
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 ;
$d -> code = $code ;
$d -> user = '0' ;
$d -> status = '0' ;
$d -> save ();
}
2017-03-11 02:51:06 +07:00
r2 ( U . 'prepaid/voucher' , 's' , $_L [ 'Voucher_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
2017-03-11 02:51:06 +07:00
case 'voucher-delete' :
$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 ();
r2 ( U . 'prepaid/voucher' , 's' , $_L [ 'Delete_Successfully' ]);
}
break ;
2021-08-19 13:38:29 +07:00
2017-03-11 02:51:06 +07:00
case 'refill' :
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
2023-08-15 17:01:48 +07:00
$ui -> assign ( '_title' , $_L [ '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' :
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 ) {
2023-08-30 09:01:37 +07:00
if ( Package :: rechargeUser ( $user [ 'id' ], $v1 [ 'routers' ], $v1 [ 'id_plan' ], " Refill " , " Voucher " )) {
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 ();
2023-08-15 17:01:48 +07:00
$ui -> assign ( 'in' , $in );
$ui -> assign ( 'date' , date ( " Y-m-d H:i:s " ));
$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 {
r2 ( U . 'prepaid/refill' , 'e' , $_L [ 'Voucher_Not_Valid' ]);
}
2017-03-11 02:51:06 +07:00
break ;
2023-08-15 17:01:48 +07:00
case 'deposit' :
$ui -> assign ( '_title' , Lang :: T ( 'Refill Balance' ));
2023-08-23 12:11:07 +07:00
$ui -> assign ( 'xfooter' , $select2_customer );
2023-08-15 17:01:48 +07:00
$ui -> assign ( 'p' , ORM :: for_table ( 'tbl_plans' ) -> where ( 'enabled' , '1' ) -> where ( 'type' , 'Balance' ) -> find_many ());
run_hook ( 'view_deposit' ); #HOOK
$ui -> display ( 'deposit.tpl' );
break ;
case 'deposit-post' :
$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 ();
$ui -> assign ( 'in' , $in );
$ui -> assign ( 'date' , date ( " Y-m-d H:i:s " ));
$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
}