diff --git a/system/autoload/Message.php b/system/autoload/Message.php new file mode 100644 index 0000000..4f72dcb --- /dev/null +++ b/system/autoload/Message.php @@ -0,0 +1,212 @@ + 4 && substr($config['sms_url'], 0, 4) != "http") { + if (strlen($txt) > 160) { + $txts = str_split($txt, 160); + try { + $mikrotik = Mikrotik::info($config['sms_url']); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + foreach ($txts as $txt) { + Mikrotik::sendSMS($client, $phone, $txt); + } + } catch (Exception $e) { + // ignore, add to logs + _log("Failed to send SMS using Mikrotik.\n" . $e->getMessage(), 'SMS', 0); + } + } else { + try { + $mikrotik = Mikrotik::info($config['sms_url']); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::sendSMS($client, $phone, $txt); + } catch (Exception $e) { + // ignore, add to logs + _log("Failed to send SMS using Mikrotik.\n" . $e->getMessage(), 'SMS', 0); + } + } + } else { + $smsurl = str_replace('[number]', urlencode($phone), $config['sms_url']); + $smsurl = str_replace('[text]', urlencode($txt), $smsurl); + return Http::getData($smsurl); + } + } + } + + public static function sendWhatsapp($phone, $txt) + { + global $config; + if(empty($txt)){ + return ""; + } + run_hook('send_whatsapp'); #HOOK + if (!empty($config['wa_url'])) { + $waurl = str_replace('[number]', urlencode(Lang::phoneFormat($phone)), $config['wa_url']); + $waurl = str_replace('[text]', urlencode($txt), $waurl); + return Http::getData($waurl); + } + } + + public static function sendEmail($to, $subject, $body) + { + global $config; + if(empty($body)){ + return ""; + } + run_hook('send_email'); #HOOK + if (empty($config['smtp_host'])) { + $attr = ""; + if (!empty($config['mail_from'])) { + $attr .= "From: " . $config['mail_from'] . "\r\n"; + } + if (!empty($config['mail_reply_to'])) { + $attr .= "Reply-To: " . $config['mail_reply_to'] . "\r\n"; + } + mail($to, $subject, $body, $attr); + } else { + $mail = new PHPMailer(); + $mail->isSMTP(); + $mail->SMTPDebug = SMTP::DEBUG_SERVER; + $mail->Host = $config['smtp_host']; + $mail->SMTPAuth = true; + $mail->Username = $config['smtp_user']; + $mail->Password = $config['smtp_pass']; + $mail->SMTPSecure = $config['smtp_ssltls']; + $mail->Port = $config['smtp_port']; + if (!empty($config['mail_from'])) { + $mail->setFrom($config['mail_from']); + } + if (!empty($config['mail_reply_to'])) { + $mail->addReplyTo($config['mail_reply_to']); + } + $mail->isHTML(false); + $mail->addAddress($to); + $mail->Subject = $subject; + $mail->Body = $body; + $mail->send(); + } + } + + public static function sendPackageNotification($customer, $package, $price, $message, $via) + { + global $ds; + if(empty($message)){ + return ""; + } + $msg = str_replace('[[name]]', $customer['fullname'], $message); + $msg = str_replace('[[username]]', $customer['username'], $msg); + $msg = str_replace('[[plan]]', $package, $msg); + $msg = str_replace('[[package]]', $package, $msg); + $msg = str_replace('[[price]]', Lang::moneyFormat($price), $msg); + list($bills, $add_cost) = User::getBills($customer['id']); + if($add_cost>0){ + $note = ""; + foreach ($bills as $k => $v) { + $note .= $k . " : " . Lang::moneyFormat($v) . "\n"; + } + $note .= "Total : " . Lang::moneyFormat($add_cost+$price) . "\n"; + $msg = str_replace('[[bills]]', $note, $msg); + }else{ + $msg = str_replace('[[bills]]', '', $msg); + } + if ($ds) { + $msg = str_replace('[[expired_date]]', Lang::dateAndTimeFormat($ds['expiration'], $ds['time']), $msg); + }else{ + $msg = str_replace('[[expired_date]]', "", $msg); + } + if ( + !empty($customer['phonenumber']) && strlen($customer['phonenumber']) > 5 + && !empty($message) && in_array($via, ['sms', 'wa']) + ) { + if ($via == 'sms') { + echo Message::sendSMS($customer['phonenumber'], $msg); + } else if ($via == 'wa') { + echo Message::sendWhatsapp($customer['phonenumber'], $msg); + } + } + return "$via: $msg"; + } + + public static function sendBalanceNotification($phone, $name, $balance, $balance_now, $message, $via) + { + $msg = str_replace('[[name]]', $name, $message); + $msg = str_replace('[[current_balance]]', Lang::moneyFormat($balance_now), $msg); + $msg = str_replace('[[balance]]', Lang::moneyFormat($balance), $msg); + if ( + !empty($phone) && strlen($phone) > 5 + && !empty($message) && in_array($via, ['sms', 'wa']) + ) { + if ($via == 'sms') { + Message::sendSMS($phone, $msg); + } else if ($via == 'wa') { + Message::sendWhatsapp($phone, $msg); + } + } + return "$via: $msg"; + } + + public static function sendInvoice($cust, $trx) + { + global $config; + $textInvoice = Lang::getNotifText('invoice_paid'); + $textInvoice = str_replace('[[company_name]]', $config['CompanyName'], $textInvoice); + $textInvoice = str_replace('[[address]]', $config['address'], $textInvoice); + $textInvoice = str_replace('[[phone]]', $config['phone'], $textInvoice); + $textInvoice = str_replace('[[invoice]]', $trx['invoice'], $textInvoice); + $textInvoice = str_replace('[[date]]', Lang::dateAndTimeFormat($trx['recharged_on'], $trx['recharged_time']), $textInvoice); + if (!empty($trx['note'])) { + $textInvoice = str_replace('[[note]]', $trx['note'], $textInvoice); + } + $gc = explode("-", $trx['method']); + $textInvoice = str_replace('[[payment_gateway]]', trim($gc[0]), $textInvoice); + $textInvoice = str_replace('[[payment_channel]]', trim($gc[1]), $textInvoice); + $textInvoice = str_replace('[[type]]', $trx['type'], $textInvoice); + $textInvoice = str_replace('[[plan_name]]', $trx['plan_name'], $textInvoice); + $textInvoice = str_replace('[[plan_price]]', Lang::moneyFormat($trx['price']), $textInvoice); + $textInvoice = str_replace('[[name]]', $cust['fullname'], $textInvoice); + $textInvoice = str_replace('[[note]]', $cust['note'], $textInvoice); + $textInvoice = str_replace('[[user_name]]', $trx['username'], $textInvoice); + $textInvoice = str_replace('[[user_password]]', $cust['password'], $textInvoice); + $textInvoice = str_replace('[[username]]', $trx['username'], $textInvoice); + $textInvoice = str_replace('[[password]]', $cust['password'], $textInvoice); + $textInvoice = str_replace('[[expired_date]]', Lang::dateAndTimeFormat($trx['expiration'], $trx['time']), $textInvoice); + $textInvoice = str_replace('[[footer]]', $config['note'], $textInvoice); + + if ($config['user_notification_payment'] == 'sms') { + Message::sendSMS($cust['phonenumber'], $textInvoice); + } else if ($config['user_notification_payment'] == 'wa') { + Message::sendWhatsapp($cust['phonenumber'], $textInvoice); + } + } +} diff --git a/system/autoload/Meta.php b/system/autoload/Meta.php new file mode 100644 index 0000000..5846529 --- /dev/null +++ b/system/autoload/Meta.php @@ -0,0 +1,118 @@ +set(1, 'point', '24'); + * it means tbl_plans with id 1 have point value 24, customer will get 24 point for loyalty if buy plan with id 1 + * You need to create the logic for that, Meta only hold the data + * + * Example to get data + * $point = Meta::for("tbl_plans")->get(1, 'point'); + * this will return the point value of plan with id 1 + * + * to get all key value + * $metas = Meta::for("tbl_plans")->getAll(1); + * + * to delete 1 data + * Meta::for("tbl_plans")->delete(1, 'point'); + * + * to delete all data + * Meta::for("tbl_plans")->deleteAll(1); + **/ + + +class Meta +{ + protected $table = ''; + + protected function __construct($table) + { + $this->table = $table; + } + + public static function for($table) + { + return new self($table); + } + + public function get($id, $key) + { + // get the Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->find_one()['value']; + } + + public function getAll($id) + { + //get all key Value + $metas = []; + $result = ORM::for_table('tbl_meta') + ->select('name') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->find_array(); + foreach ($result as $value) { + $metas[$value['name']] = $value['value']; + } + return $metas; + } + + public function set($id, $key, $value = '') + { + $meta = ORM::for_table('tbl_meta') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->find_one(); + if (!$meta) { + $meta = ORM::for_table('tbl_meta')->create(); + $meta->tbl = $this->table; + $meta->tbl_id = $id; + $meta->name = $key; + $meta->value = $value; + $meta->save(); + $result = $meta->id(); + if ($result) { + return $result; + } + } else { + $meta->value = $value; + $meta->save(); + return $meta['id']; + } + } + + public function delete($id, $key = '') + { + // get the Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->where('name', $key) + ->delete(); + } + + public function deleteAll($id) + { + //get all key Value + return ORM::for_table('tbl_meta') + ->select('value') + ->where('tbl', $this->table) + ->where('tbl_id', $id) + ->delete_many(); + } +} diff --git a/system/autoload/Mikrotik.php b/system/autoload/Mikrotik.php new file mode 100644 index 0000000..d917582 --- /dev/null +++ b/system/autoload/Mikrotik.php @@ -0,0 +1,562 @@ +where('name', $name)->find_one(); + } + + public static function getClient($ip, $user, $pass) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $iport = explode(":", $ip); + return new RouterOS\Client($iport[0], $user, $pass, ($iport[1]) ? $iport[1] : null); + } + + public static function isUserLogin($client, $username) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot active print', + RouterOS\Query::where('user', $username) + ); + return $client->sendSync($printRequest)->getProperty('.id'); + } + + public static function logMeIn($client, $user, $pass, $ip, $mac) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ip/hotspot/active/login'); + $client->sendSync( + $addRequest + ->setArgument('user', $user) + ->setArgument('password', $pass) + ->setArgument('ip', $ip) + ->setArgument('mac-address', $mac) + ); + } + + public static function logMeOut($client, $user) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot active print', + RouterOS\Query::where('user', $user) + ); + $id = $client->sendSync($printRequest)->getProperty('.id'); + $removeRequest = new RouterOS\Request('/ip/hotspot/active/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $id) + ); + } + + public static function addHotspotPlan($client, $name, $sharedusers, $rate) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ip/hotspot/user/profile/add'); + $client->sendSync( + $addRequest + ->setArgument('name', $name) + ->setArgument('shared-users', $sharedusers) + ->setArgument('rate-limit', $rate) + ); + } + + public static function setHotspotPlan($client, $name, $sharedusers, $rate) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot user profile print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $profileID = $client->sendSync($printRequest)->getProperty('.id'); + if (empty($profileID)) { + Mikrotik::addHotspotPlan($client, $name, $sharedusers, $rate); + } else { + $setRequest = new RouterOS\Request('/ip/hotspot/user/profile/set'); + $client->sendSync( + $setRequest + ->setArgument('numbers', $profileID) + ->setArgument('shared-users', $sharedusers) + ->setArgument('rate-limit', $rate) + ); + } + } + + public static function setHotspotExpiredPlan($client, $name, $pool) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot user profile print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $profileID = $client->sendSync($printRequest)->getProperty('.id'); + if (empty($profileID)) { + $addRequest = new RouterOS\Request('/ip/hotspot/user/profile/add'); + $client->sendSync( + $addRequest + ->setArgument('name', $name) + ->setArgument('shared-users', 3) + ->setArgument('address-pool', $pool) + ->setArgument('rate-limit', '512K/512K') + ); + } else { + $setRequest = new RouterOS\Request('/ip/hotspot/user/profile/set'); + $client->sendSync( + $setRequest + ->setArgument('numbers', $profileID) + ->setArgument('shared-users', 3) + ->setArgument('address-pool', $pool) + ->setArgument('rate-limit', '512K/512K') + ); + } + } + + public static function removeHotspotPlan($client, $name) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot user profile print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $profileID = $client->sendSync($printRequest)->getProperty('.id'); + + $removeRequest = new RouterOS\Request('/ip/hotspot/user/profile/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $profileID) + ); + } + + public static function removeHotspotUser($client, $username) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot user print .proplist=.id', + RouterOS\Query::where('name', $username) + ); + $userID = $client->sendSync($printRequest)->getProperty('.id'); + $removeRequest = new RouterOS\Request('/ip/hotspot/user/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $userID) + ); + } + + public static function addHotspotUser($client, $plan, $customer) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ip/hotspot/user/add'); + if ($plan['typebp'] == "Limited") { + if ($plan['limit_type'] == "Time_Limit") { + if ($plan['time_unit'] == 'Hrs') + $timelimit = $plan['time_limit'] . ":00:00"; + else + $timelimit = "00:" . $plan['time_limit'] . ":00"; + $client->sendSync( + $addRequest + ->setArgument('name', $customer['username']) + ->setArgument('profile', $plan['name_plan']) + ->setArgument('password', $customer['password']) + ->setArgument('comment', $customer['fullname']) + ->setArgument('email', $customer['email']) + ->setArgument('limit-uptime', $timelimit) + ); + } else if ($plan['limit_type'] == "Data_Limit") { + if ($plan['data_unit'] == 'GB') + $datalimit = $plan['data_limit'] . "000000000"; + else + $datalimit = $plan['data_limit'] . "000000"; + $client->sendSync( + $addRequest + ->setArgument('name', $customer['username']) + ->setArgument('profile', $plan['name_plan']) + ->setArgument('password', $customer['password']) + ->setArgument('comment', $customer['fullname']) + ->setArgument('email', $customer['email']) + ->setArgument('limit-bytes-total', $datalimit) + ); + } else if ($plan['limit_type'] == "Both_Limit") { + if ($plan['time_unit'] == 'Hrs') + $timelimit = $plan['time_limit'] . ":00:00"; + else + $timelimit = "00:" . $plan['time_limit'] . ":00"; + if ($plan['data_unit'] == 'GB') + $datalimit = $plan['data_limit'] . "000000000"; + else + $datalimit = $plan['data_limit'] . "000000"; + $client->sendSync( + $addRequest + ->setArgument('name', $customer['username']) + ->setArgument('profile', $plan['name_plan']) + ->setArgument('password', $customer['password']) + ->setArgument('comment', $customer['fullname']) + ->setArgument('email', $customer['email']) + ->setArgument('limit-uptime', $timelimit) + ->setArgument('limit-bytes-total', $datalimit) + ); + } + } else { + $client->sendSync( + $addRequest + ->setArgument('name', $customer['username']) + ->setArgument('profile', $plan['name_plan']) + ->setArgument('comment', $customer['fullname']) + ->setArgument('email', $customer['email']) + ->setArgument('password', $customer['password']) + ); + } + } + + public static function setHotspotUser($client, $user, $pass) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request('/ip/hotspot/user/print'); + $printRequest->setArgument('.proplist', '.id'); + $printRequest->setQuery(RouterOS\Query::where('name', $user)); + $id = $client->sendSync($printRequest)->getProperty('.id'); + + $setRequest = new RouterOS\Request('/ip/hotspot/user/set'); + $setRequest->setArgument('numbers', $id); + $setRequest->setArgument('password', $pass); + $client->sendSync($setRequest); + } + + public static function setHotspotUserPackage($client, $user, $plan) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request('/ip/hotspot/user/print'); + $printRequest->setArgument('.proplist', '.id'); + $printRequest->setQuery(RouterOS\Query::where('name', $user)); + $id = $client->sendSync($printRequest)->getProperty('.id'); + + $setRequest = new RouterOS\Request('/ip/hotspot/user/set'); + $setRequest->setArgument('numbers', $id); + $setRequest->setArgument('profile', $plan); + $client->sendSync($setRequest); + } + + public static function removeHotspotActiveUser($client, $username) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $onlineRequest = new RouterOS\Request('/ip/hotspot/active/print'); + $onlineRequest->setArgument('.proplist', '.id'); + $onlineRequest->setQuery(RouterOS\Query::where('user', $username)); + $id = $client->sendSync($onlineRequest)->getProperty('.id'); + + $removeRequest = new RouterOS\Request('/ip/hotspot/active/remove'); + $removeRequest->setArgument('numbers', $id); + $client->sendSync($removeRequest); + } + + public static function removePpoeUser($client, $username) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request('/ppp/secret/print'); + //$printRequest->setArgument('.proplist', '.id'); + $printRequest->setQuery(RouterOS\Query::where('name', $username)); + $id = $client->sendSync($printRequest)->getProperty('.id'); + $removeRequest = new RouterOS\Request('/ppp/secret/remove'); + $removeRequest->setArgument('numbers', $id); + $client->sendSync($removeRequest); + } + + public static function addPpoeUser($client, $plan, $customer) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ppp/secret/add'); + if (!empty($customer['pppoe_password'])) { + $pass = $customer['pppoe_password']; + } else { + $pass = $customer['password']; + } + $client->sendSync( + $addRequest + ->setArgument('name', $customer['username']) + ->setArgument('service', 'pppoe') + ->setArgument('profile', $plan['name_plan']) + ->setArgument('comment', $customer['fullname'] . ' | ' . $customer['email']) + ->setArgument('password', $pass) + ); + } + + public static function setPpoeUser($client, $user, $pass) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request('/ppp/secret/print'); + $printRequest->setArgument('.proplist', '.id'); + $printRequest->setQuery(RouterOS\Query::where('name', $user)); + $id = $client->sendSync($printRequest)->getProperty('.id'); + + $setRequest = new RouterOS\Request('/ppp/secret/set'); + $setRequest->setArgument('numbers', $id); + $setRequest->setArgument('password', $pass); + $client->sendSync($setRequest); + } + + public static function setPpoeUserPlan($client, $user, $plan) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request('/ppp/secret/print'); + $printRequest->setArgument('.proplist', '.id'); + $printRequest->setQuery(RouterOS\Query::where('name', $user)); + $id = $client->sendSync($printRequest)->getProperty('.id'); + + $setRequest = new RouterOS\Request('/ppp/secret/set'); + $setRequest->setArgument('numbers', $id); + $setRequest->setArgument('profile', $plan); + $client->sendSync($setRequest); + } + + public static function removePpoeActive($client, $username) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $onlineRequest = new RouterOS\Request('/ppp/active/print'); + $onlineRequest->setArgument('.proplist', '.id'); + $onlineRequest->setQuery(RouterOS\Query::where('name', $username)); + $id = $client->sendSync($onlineRequest)->getProperty('.id'); + + $removeRequest = new RouterOS\Request('/ppp/active/remove'); + $removeRequest->setArgument('numbers', $id); + $client->sendSync($removeRequest); + } + + public static function removePool($client, $name) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip pool print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $poolID = $client->sendSync($printRequest)->getProperty('.id'); + + $removeRequest = new RouterOS\Request('/ip/pool/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $poolID) + ); + } + + public static function addPool($client, $name, $ip_address) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ip/pool/add'); + $client->sendSync( + $addRequest + ->setArgument('name', $name) + ->setArgument('ranges', $ip_address) + ); + } + + public static function setPool($client, $name, $ip_address) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip pool print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $poolID = $client->sendSync($printRequest)->getProperty('.id'); + + if (empty($poolID)) { + self::addPool($client, $name, $ip_address); + } else { + $setRequest = new RouterOS\Request('/ip/pool/set'); + $client->sendSync( + $setRequest + ->setArgument('numbers', $poolID) + ->setArgument('ranges', $ip_address) + ); + } + } + + + public static function addPpoePlan($client, $name, $pool, $rate) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ppp/profile/add'); + $client->sendSync( + $addRequest + ->setArgument('name', $name) + ->setArgument('local-address', $pool) + ->setArgument('remote-address', $pool) + ->setArgument('rate-limit', $rate) + ); + } + + public static function setPpoePlan($client, $name, $pool, $rate) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ppp profile print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $profileID = $client->sendSync($printRequest)->getProperty('.id'); + if (empty($profileID)) { + self::addPpoePlan($client, $name, $pool, $rate); + } else { + $setRequest = new RouterOS\Request('/ppp/profile/set'); + $client->sendSync( + $setRequest + ->setArgument('numbers', $profileID) + ->setArgument('local-address', $pool) + ->setArgument('remote-address', $pool) + ->setArgument('rate-limit', $rate) + ); + } + } + + public static function removePpoePlan($client, $name) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ppp profile print .proplist=.id', + RouterOS\Query::where('name', $name) + ); + $profileID = $client->sendSync($printRequest)->getProperty('.id'); + + $removeRequest = new RouterOS\Request('/ppp/profile/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $profileID) + ); + } + + public static function sendSMS($client, $to, $message) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $smsRequest = new RouterOS\Request('/tool sms send'); + $smsRequest + ->setArgument('phone-number', $to) + ->setArgument('message', $message); + $client->sendSync($smsRequest); + } + + public static function getIpHotspotUser($client, $username){ + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip hotspot active print', + RouterOS\Query::where('user', $username) + ); + return $client->sendSync($printRequest)->getProperty('address'); + } + + public static function addIpToAddressList($client, $ip, $listName, $comment = '') + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $addRequest = new RouterOS\Request('/ip/firewall/address-list/add'); + $client->sendSync( + $addRequest + ->setArgument('address', $ip) + ->setArgument('comment', $comment) + ->setArgument('list', $listName) + ); + } + + public static function removeIpFromAddressList($client, $ip) + { + global $_app_stage; + if ($_app_stage == 'demo') { + return null; + } + $printRequest = new RouterOS\Request( + '/ip firewall address-list print .proplist=.id', + RouterOS\Query::where('address', $ip) + ); + $id = $client->sendSync($printRequest)->getProperty('.id'); + $removeRequest = new RouterOS\Request('/ip/firewall/address-list/remove'); + $client->sendSync( + $removeRequest + ->setArgument('numbers', $id) + ); + } +} diff --git a/system/autoload/Package.php b/system/autoload/Package.php new file mode 100644 index 0000000..a2210d2 --- /dev/null +++ b/system/autoload/Package.php @@ -0,0 +1,817 @@ +where('id', $id_customer)->find_one(); + $p = ORM::for_table('tbl_plans')->where('id', $plan_id)->find_one(); + + if ($c['status'] != 'Active') { + _alert(Lang::T('This account status') . ' : ' . Lang::T($c['status']), 'danger', ""); + } + + $add_cost = 0; + $bills = []; + // Zero cost recharge + if (isset($zero) && $zero == 1) { + $p['price'] = 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"; + } + $note .= $p['name_plan'] . " : " . Lang::moneyFormat($p['price']) . "\n"; + } + } + + + if (!$p['enabled']) { + if (!isset($admin) || !isset($admin['id']) || empty($admin['id'])) { + r2(U . 'home', 'e', Lang::T('Plan Not found')); + } + if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { + r2(U . 'dashboard', 'e', Lang::T('Plan Not found')); + } + } + + if ($p['validity_unit'] == 'Period') { + $day_exp = User::getAttribute("Expired Date", $c['id']); //ORM::for_table('tbl_customers_fields')->where('field_name', 'Expired Date')->where('customer_id', $c['id'])->find_one(); + if (!$day_exp) { + $day_exp = 20; + // $day_exp = date('d', strtotime($c['created_at'])); + // if (empty($day_exp) || $day_exp > 28) { + // $day_exp = 1; + // } + $f = ORM::for_table('tbl_customers_fields')->create(); + $f->customer_id = $c['id']; + $f->field_name = 'Expired Date'; + $f->field_value = $day_exp; + $f->save(); + } + } + + if ($router_name == 'balance') { + // insert table transactions + $t = ORM::for_table('tbl_transactions')->create(); + $t->invoice = $inv = "INV-" . Package::_raid(); + $t->username = $c['username']; + $t->plan_name = $p['name_plan']; + $t->price = $p['price']; + $t->recharged_on = $date_only; + $t->recharged_time = date("H:i:s"); + $t->expiration = $date_only; + $t->time = $time; + $t->method = "$gateway - $channel"; + $t->routers = $router_name; + $t->type = "Balance"; + if ($admin) { + $t->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $t->admin_id = '0'; + } + $t->save(); + + $balance_before = $c['balance']; + Balance::plus($id_customer, $p['price']); + $balance = $c['balance'] + $p['price']; + + $textInvoice = Lang::getNotifText('invoice_balance'); + $textInvoice = str_replace('[[company_name]]', $config['CompanyName'], $textInvoice); + $textInvoice = str_replace('[[address]]', $config['address'], $textInvoice); + $textInvoice = str_replace('[[phone]]', $config['phone'], $textInvoice); + $textInvoice = str_replace('[[invoice]]', $inv, $textInvoice); + $textInvoice = str_replace('[[date]]', Lang::dateTimeFormat($date_now), $textInvoice); + $textInvoice = str_replace('[[payment_gateway]]', $gateway, $textInvoice); + $textInvoice = str_replace('[[payment_channel]]', $channel, $textInvoice); + $textInvoice = str_replace('[[type]]', 'Balance', $textInvoice); + $textInvoice = str_replace('[[plan_name]]', $p['name_plan'], $textInvoice); + $textInvoice = str_replace('[[plan_price]]', Lang::moneyFormat($p['price']), $textInvoice); + $textInvoice = str_replace('[[name]]', $c['fullname'], $textInvoice); + $textInvoice = str_replace('[[user_name]]', $c['username'], $textInvoice); + $textInvoice = str_replace('[[user_password]]', $c['password'], $textInvoice); + $textInvoice = str_replace('[[footer]]', $config['note'], $textInvoice); + $textInvoice = str_replace('[[balance_before]]', Lang::moneyFormat($balance_before), $textInvoice); + $textInvoice = str_replace('[[balance]]', Lang::moneyFormat($balance), $textInvoice); + + if ($config['user_notification_payment'] == 'sms') { + Message::sendSMS($c['phonenumber'], $textInvoice); + } else if ($config['user_notification_payment'] == 'wa') { + Message::sendWhatsapp($c['phonenumber'], $textInvoice); + } + + return true; + } + + /** + * 1 Customer only can have 1 PPPOE and 1 Hotspot Plan, 1 prepaid and 1 postpaid + */ + $b = ORM::for_table('tbl_user_recharges') + ->select('tbl_user_recharges.id', 'id') + ->select('customer_id') + ->select('username') + ->select('plan_id') + ->select('namebp') + ->select('recharged_on') + ->select('recharged_time') + ->select('expiration') + ->select('time') + ->select('status') + ->select('method') + ->select('tbl_user_recharges.routers', 'routers') + ->select('tbl_user_recharges.type', 'type') + ->select('admin_id') + ->select('prepaid') + ->where('customer_id', $id_customer) + ->where('tbl_user_recharges.routers', $router_name) + ->where('tbl_user_recharges.Type', $p['type']) + # PPPOE or Hotspot only can have 1 per customer prepaid or postpaid + # because 1 customer can have 1 PPPOE and 1 Hotspot Plan in mikrotik + //->where('prepaid', $p['prepaid']) + ->left_outer_join('tbl_plans', array('tbl_plans.id', '=', 'tbl_user_recharges.plan_id')) + ->find_one(); + + run_hook("recharge_user"); + + + $mikrotik = Mikrotik::info($router_name); + if ($p['validity_unit'] == 'Months') { + $date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month')); + } else if ($p['validity_unit'] == 'Period') { + $date_tmp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month')); + $dt1 = new DateTime("$date_only"); + $dt2 = new DateTime("$date_tmp"); + $diff = $dt2->diff($dt1); + $sum = $diff->format("%a"); // => 453 + if ($sum >= 35 * $p['validity']) { + $date_exp = date("Y-m-$day_exp", strtotime('+0 month')); + } else { + $date_exp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month')); + }; + $time = date("23:59:00"); + } else if ($p['validity_unit'] == 'Days') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' day'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } else if ($p['validity_unit'] == 'Hrs') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' hour'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } else if ($p['validity_unit'] == 'Mins') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' minute'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } + $isChangePlan = false; + if ($p['type'] == 'Hotspot') { + if ($b) { + if ($plan_id != $b['plan_id']) { + $isChangePlan = true; + } + if ($config['extend_expiry'] === 'yes') { + if ($b['namebp'] == $p['name_plan'] && $b['status'] == 'on') { + // if it same internet plan, expired will extend + if ($p['validity_unit'] == 'Months') { + $date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months')); + $time = $b['time']; + } else if ($p['validity_unit'] == 'Period') { + $date_exp = date("Y-m-$day_exp", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months')); + $time = date("23:59:00"); + } else if ($p['validity_unit'] == 'Days') { + $date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' days')); + $time = $b['time']; + } else if ($p['validity_unit'] == 'Hrs') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' hours'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } else if ($p['validity_unit'] == 'Mins') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' minutes'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } + } + } + if ($isChangePlan || $b['status'] == 'off') { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, "$date_exp $time"); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removeHotspotUser($client, $c['username']); + Mikrotik::removeHotspotActiveUser($client, $c['username']); + Mikrotik::addHotspotUser($client, $p, $c); + } + } + + $b->customer_id = $id_customer; + $b->username = $c['username']; + $b->plan_id = $plan_id; + $b->namebp = $p['name_plan']; + $b->recharged_on = $date_only; + $b->recharged_time = $time_only; + $b->expiration = $date_exp; + $b->time = $time; + $b->status = "on"; + $b->method = "$gateway - $channel"; + $b->routers = $router_name; + $b->type = "Hotspot"; + if ($admin) { + $b->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $b->admin_id = '0'; + } + $b->save(); + + // insert table transactions + $t = ORM::for_table('tbl_transactions')->create(); + $t->invoice = $inv = "INV-" . Package::_raid(); + $t->username = $c['username']; + $t->plan_name = $p['name_plan']; + if ($p['validity_unit'] == 'Period') { + // Postpaid price from field + $add_inv = User::getAttribute("Invoice", $id_customer); + if (empty($add_inv) or $add_inv == 0) { + $t->price = $p['price'] + $add_cost; + } else { + $t->price = $add_inv + $add_cost; + } + } else { + $t->price = $p['price'] + $add_cost; + } + $t->recharged_on = $date_only; + $t->recharged_time = $time_only; + $t->expiration = $date_exp; + $t->time = $time; + $t->method = "$gateway - $channel"; + $t->routers = $router_name; + $t->note = $note; + $t->type = "Hotspot"; + if ($admin) { + $t->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $t->admin_id = '0'; + } + $t->save(); + + if ($p['validity_unit'] == 'Period') { + // insert price to fields for invoice next month + $fl = ORM::for_table('tbl_customers_fields')->where('field_name', 'Invoice')->where('customer_id', $c['id'])->find_one(); + if (!$fl) { + $fl = ORM::for_table('tbl_customers_fields')->create(); + $fl->customer_id = $c['id']; + $fl->field_name = 'Invoice'; + $fl->field_value = $p['price']; + $fl->save(); + } else { + $fl->customer_id = $c['id']; + $fl->field_value = $p['price']; + $fl->save(); + } + } + + + Message::sendTelegram("#u$c[username] $c[fullname] #recharge #Hotspot \n" . $p['name_plan'] . + "\nRouter: " . $router_name . + "\nGateway: " . $gateway . + "\nChannel: " . $channel . + "\nPrice: " . Lang::moneyFormat($p['price'] + $add_cost) . + "\nNote:\n" . $note); + } else { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, "$date_exp $time"); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removeHotspotUser($client, $c['username']); + Mikrotik::removeHotspotActiveUser($client, $c['username']); + Mikrotik::addHotspotUser($client, $p, $c); + } + + $d = ORM::for_table('tbl_user_recharges')->create(); + $d->customer_id = $id_customer; + $d->username = $c['username']; + $d->plan_id = $plan_id; + $d->namebp = $p['name_plan']; + $d->recharged_on = $date_only; + $d->recharged_time = $time_only; + $d->expiration = $date_exp; + $d->time = $time; + $d->status = "on"; + $d->method = "$gateway - $channel"; + $d->routers = $router_name; + $d->type = "Hotspot"; + if ($admin) { + $d->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $d->admin_id = '0'; + } + $d->save(); + + // insert table transactions + $t = ORM::for_table('tbl_transactions')->create(); + $t->invoice = $inv = "INV-" . Package::_raid(); + $t->username = $c['username']; + $t->plan_name = $p['name_plan']; + if ($p['validity_unit'] == 'Period') { + // Postpaid price always zero for first time + $t->price = 0 + $add_cost; + } else { + $t->price = $p['price'] + $add_cost; + } + $t->recharged_on = $date_only; + $t->recharged_time = $time_only; + $t->expiration = $date_exp; + $t->time = $time; + $t->method = "$gateway - $channel"; + $t->routers = $router_name; + $t->note = $note; + $t->type = "Hotspot"; + if ($admin) { + $t->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $t->admin_id = '0'; + } + $t->save(); + + if ($p['validity_unit'] == 'Period' && $p['price'] != 0) { + // insert price to fields for invoice next month + $fl = ORM::for_table('tbl_customers_fields')->where('field_name', 'Invoice')->where('customer_id', $c['id'])->find_one(); + if (!$fl) { + $fl = ORM::for_table('tbl_customers_fields')->create(); + $fl->customer_id = $c['id']; + $fl->field_name = 'Invoice'; + // Calculating Price + $sd = new DateTime("$date_only"); + $ed = new DateTime("$date_exp"); + $td = $ed->diff($sd); + $fd = $td->format("%a"); + $gi = ($p['price'] / (30 * $p['validity'])) * $fd; + if ($gi > $p['price']) { + $fl->field_value = $p['price']; + } else { + $fl->field_value = $gi; + } + $fl->save(); + } else { + $fl->customer_id = $c['id']; + $fl->field_value = $p['price']; + $fl->save(); + } + } + + Message::sendTelegram("#u$c[username] $c[fullname] #buy #Hotspot \n" . $p['name_plan'] . + "\nRouter: " . $router_name . + "\nGateway: " . $gateway . + "\nChannel: " . $channel . + "\nPrice: " . Lang::moneyFormat($p['price'] + $add_cost) . + "\nNote:\n" . $note); + } + } else { + + if ($b) { + if ($plan_id != $b['plan_id']) { + $isChangePlan = true; + } + if ($config['extend_expiry'] === 'yes') { + if ($b['namebp'] == $p['name_plan'] && $b['status'] == 'on') { + // if it same internet plan, expired will extend + if ($p['validity_unit'] == 'Months') { + $date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months')); + $time = $b['time']; + } else if ($p['validity_unit'] == 'Period') { + $date_exp = date("Y-m-$day_exp", strtotime($b['expiration'] . ' +' . $p['validity'] . ' months')); + $time = date("23:59:00"); + } else if ($p['validity_unit'] == 'Days') { + $date_exp = date("Y-m-d", strtotime($b['expiration'] . ' +' . $p['validity'] . ' days')); + $time = $b['time']; + } else if ($p['validity_unit'] == 'Hrs') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' hours'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } else if ($p['validity_unit'] == 'Mins') { + $datetime = explode(' ', date("Y-m-d H:i:s", strtotime($b['expiration'] . ' ' . $b['time'] . ' +' . $p['validity'] . ' minutes'))); + $date_exp = $datetime[0]; + $time = $datetime[1]; + } + } + } + + if ($isChangePlan || $b['status'] == 'off') { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, "$date_exp $time"); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removePpoeUser($client, $c['username']); + Mikrotik::removePpoeActive($client, $c['username']); + Mikrotik::addPpoeUser($client, $p, $c); + } + } + + $b->customer_id = $id_customer; + $b->username = $c['username']; + $b->plan_id = $plan_id; + $b->namebp = $p['name_plan']; + $b->recharged_on = $date_only; + $b->recharged_time = $time_only; + $b->expiration = $date_exp; + $b->time = $time; + $b->status = "on"; + $b->method = "$gateway - $channel"; + $b->routers = $router_name; + $b->type = "PPPOE"; + if ($admin) { + $b->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $b->admin_id = '0'; + } + $b->save(); + + // insert table transactions + $t = ORM::for_table('tbl_transactions')->create(); + $t->invoice = $inv = "INV-" . Package::_raid(); + $t->username = $c['username']; + $t->plan_name = $p['name_plan']; + if ($p['validity_unit'] == 'Period') { + // Postpaid price from field + $add_inv = User::getAttribute("Invoice", $id_customer); + if (empty($add_inv) or $add_inv == 0) { + $t->price = $p['price'] + $add_cost; + } else { + $t->price = $add_inv + $add_cost; + } + } else { + $t->price = $p['price'] + $add_cost; + } + $t->recharged_on = $date_only; + $t->recharged_time = $time_only; + $t->expiration = $date_exp; + $t->time = $time; + $t->method = "$gateway - $channel"; + $t->routers = $router_name; + $t->note = $note; + $t->type = "PPPOE"; + if ($admin) { + $t->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $t->admin_id = '0'; + } + $t->save(); + + if ($p['validity_unit'] == 'Period' && $p['price'] != 0) { + // insert price to fields for invoice next month + $fl = ORM::for_table('tbl_customers_fields')->where('field_name', 'Invoice')->where('customer_id', $c['id'])->find_one(); + if (!$fl) { + $fl = ORM::for_table('tbl_customers_fields')->create(); + $fl->customer_id = $c['id']; + $fl->field_name = 'Invoice'; + $fl->field_value = $p['price']; + $fl->save(); + } else { + $fl->customer_id = $c['id']; + $fl->field_value = $p['price']; + $fl->save(); + } + } + + Message::sendTelegram("#u$c[username] $c[fullname] #recharge #PPPOE \n" . $p['name_plan'] . + "\nRouter: " . $router_name . + "\nGateway: " . $gateway . + "\nChannel: " . $channel . + "\nPrice: " . Lang::moneyFormat($p['price'] + $add_cost) . + "\nNote:\n" . $note); + } else { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, "$date_exp $time"); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removePpoeUser($client, $c['username']); + Mikrotik::removePpoeActive($client, $c['username']); + Mikrotik::addPpoeUser($client, $p, $c); + } + + $d = ORM::for_table('tbl_user_recharges')->create(); + $d->customer_id = $id_customer; + $d->username = $c['username']; + $d->plan_id = $plan_id; + $d->namebp = $p['name_plan']; + $d->recharged_on = $date_only; + $d->recharged_time = $time_only; + $d->expiration = $date_exp; + $d->time = $time; + $d->status = "on"; + $d->method = "$gateway - $channel"; + $d->routers = $router_name; + $d->type = "PPPOE"; + if ($admin) { + $d->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $d->admin_id = '0'; + } + $d->save(); + + // insert table transactions + $t = ORM::for_table('tbl_transactions')->create(); + $t->invoice = $inv = "INV-" . Package::_raid(); + $t->username = $c['username']; + $t->plan_name = $p['name_plan']; + if ($p['validity_unit'] == 'Period') { + // Postpaid price always zero for first time + $note = ''; + $bills = []; + $t->price = 0; + } else { + $t->price = $p['price'] + $add_cost; + } + $t->recharged_on = $date_only; + $t->recharged_time = $time_only; + $t->expiration = $date_exp; + $t->time = $time; + $t->method = "$gateway - $channel"; + $t->note = $note; + $t->routers = $router_name; + if ($admin) { + $t->admin_id = ($admin['id']) ? $admin['id'] : '0'; + } else { + $t->admin_id = '0'; + } + $t->type = "PPPOE"; + $t->save(); + + if ($p['validity_unit'] == 'Period' && $p['price'] != 0) { + // insert price to fields for invoice next month + $fl = ORM::for_table('tbl_customers_fields')->where('field_name', 'Invoice')->where('customer_id', $c['id'])->find_one(); + if (!$fl) { + $fl = ORM::for_table('tbl_customers_fields')->create(); + $fl->customer_id = $c['id']; + $fl->field_name = 'Invoice'; + // Calculating Price + $sd = new DateTime("$date_only"); + $ed = new DateTime("$date_exp"); + $td = $ed->diff($sd); + $fd = $td->format("%a"); + $gi = ($p['price'] / (30 * $p['validity'])) * $fd; + if ($gi > $p['price']) { + $fl->field_value = $p['price']; + } else { + $fl->field_value = $gi; + } + $fl->save(); + } else { + $fl->customer_id = $c['id']; + $fl->field_value = $p['price']; + $fl->save(); + } + } + + Message::sendTelegram("#u$c[username] $c[fullname] #buy #PPPOE \n" . $p['name_plan'] . + "\nRouter: " . $router_name . + "\nGateway: " . $gateway . + "\nChannel: " . $channel . + "\nPrice: " . Lang::moneyFormat($p['price'] + $add_cost) . + "\nNote:\n" . $note); + } + } + if (is_array($bills) && count($bills) > 0) { + User::billsPaid($bills, $id_customer); + } + run_hook("recharge_user_finish"); + Message::sendInvoice($c, $t); + if ($trx) { + $trx->trx_invoice = $inv; + } + return $inv; + } + + public static function changeTo($username, $plan_id, $from_id) + { + $c = ORM::for_table('tbl_customers')->where('username', $username)->find_one(); + $p = ORM::for_table('tbl_plans')->where('id', $plan_id)->find_one(); + $b = ORM::for_table('tbl_user_recharges')->find_one($from_id); + if ($p['routers'] == $b['routers'] && $b['routers'] != 'radius') { + $mikrotik = Mikrotik::info($p['routers']); + } else { + $mikrotik = Mikrotik::info($b['routers']); + } + // delete first + if ($p['type'] == 'Hotspot') { + if ($b) { + if (!$p['is_radius']) { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removeHotspotUser($client, $c['username']); + Mikrotik::removeHotspotActiveUser($client, $c['username']); + } + } else { + if (!$p['is_radius']) { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removeHotspotUser($client, $c['username']); + Mikrotik::removeHotspotActiveUser($client, $c['username']); + } + } + } else { + if ($b) { + if (!$p['is_radius']) { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removePpoeUser($client, $c['username']); + Mikrotik::removePpoeActive($client, $c['username']); + } + } else { + if (!$p['is_radius']) { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::removePpoeUser($client, $c['username']); + Mikrotik::removePpoeActive($client, $c['username']); + } + } + } + // call the next mikrotik + if ($p['routers'] != $b['routers'] && $p['routers'] != 'radius') { + $mikrotik = Mikrotik::info($p['routers']); + } + if ($p['type'] == 'Hotspot') { + if ($b) { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, $b['expiration'] . '' . $b['time']); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::addHotspotUser($client, $p, $c); + } + } else { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p, $b['expiration'] . '' . $b['time']); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::addHotspotUser($client, $p, $c); + } + } + } else { + if ($b) { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::addPpoeUser($client, $p, $c); + } + } else { + if ($p['is_radius']) { + Radius::customerAddPlan($c, $p); + } else { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + Mikrotik::addPpoeUser($client, $p, $c); + } + } + } + } + + + public static function _raid() + { + return ORM::for_table('tbl_transactions')->max('id') + 1; + } + + /** + * @param in tbl_transactions + * @param string $router_name router name for this package + * @param int $plan_id plan id for this package + * @param string $gateway payment gateway name + * @param string $channel channel payment gateway + * @return boolean + */ + public static function createInvoice($in) + { + global $config, $admin, $ui; + $date = Lang::dateAndTimeFormat($in['recharged_on'], $in['recharged_time']); + if ($admin['id'] != $in['admin_id'] && $in['admin_id'] > 0) { + $_admin = Admin::_info($in['admin_id']); + // if admin not deleted + if ($_admin) $admin = $_admin; + } else { + $admin['fullname'] = 'Customer'; + } + $cust = ORM::for_table('tbl_customers')->where('username', $in['username'])->findOne(); + + $note = ''; + //print + $invoice = Lang::pad($config['CompanyName'], ' ', 2) . "\n"; + $invoice .= Lang::pad($config['address'], ' ', 2) . "\n"; + $invoice .= Lang::pad($config['phone'], ' ', 2) . "\n"; + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pads("Invoice", $in['invoice'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Date'), $date, ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Sales'), $admin['fullname'], ' ') . "\n"; + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pads(Lang::T('Type'), $in['type'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Plan Name'), $in['plan_name'], ' ') . "\n"; + if (!empty($in['note'])) { + $in['note'] = str_replace("\r", "", $in['note']); + $tmp = explode("\n", $in['note']); + foreach ($tmp as $t) { + if (strpos($t, " : ") === false) { + if (!empty($t)) { + $note .= "$t\n"; + } + } else { + $tmp2 = explode(" : ", $t); + $invoice .= Lang::pads($tmp2[0], $tmp2[1], ' ') . "\n"; + } + } + } + $invoice .= Lang::pads(Lang::T('Total'), Lang::moneyFormat($in['price']), ' ') . "\n"; + $method = explode("-", $in['method']); + $invoice .= Lang::pads($method[0], $method[1], ' ') . "\n"; + if (!empty($note)) { + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pad($note, ' ', 2) . "\n"; + } + $invoice .= Lang::pad("", '=') . "\n"; + if ($cust) { + $invoice .= Lang::pads(Lang::T('Full Name'), $cust['fullname'], ' ') . "\n"; + } + $invoice .= Lang::pads(Lang::T('Username'), $in['username'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Password'), '**********', ' ') . "\n"; + if ($in['type'] != 'Balance') { + $invoice .= Lang::pads(Lang::T('Created On'), Lang::dateAndTimeFormat($in['recharged_on'], $in['recharged_time']), ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Expires On'), Lang::dateAndTimeFormat($in['expiration'], $in['time']), ' ') . "\n"; + } + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pad($config['note'], ' ', 2) . "\n"; + $ui->assign('invoice', $invoice); + $config['printer_cols'] = 30; + //whatsapp + $invoice = Lang::pad($config['CompanyName'], ' ', 2) . "\n"; + $invoice .= Lang::pad($config['address'], ' ', 2) . "\n"; + $invoice .= Lang::pad($config['phone'], ' ', 2) . "\n"; + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pads("Invoice", $in['invoice'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Date'), $date, ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Sales'), $admin['fullname'], ' ') . "\n"; + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pads(Lang::T('Type'), $in['type'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Plan Name'), $in['plan_name'], ' ') . "\n"; + if (!empty($in['note'])) { + $invoice .= Lang::pad("", '=') . "\n"; + foreach ($tmp as $t) { + if (strpos($t, " : ") === false) { + if (!empty($t)) { + $invoice .= Lang::pad($t, ' ', 2) . "\n"; + } + } else { + $tmp2 = explode(" : ", $t); + $invoice .= Lang::pads($tmp2[0], $tmp2[1], ' ') . "\n"; + } + } + } + $invoice .= Lang::pads(Lang::T('Total'), Lang::moneyFormat($in['price']), ' ') . "\n"; + $invoice .= Lang::pads($method[0], $method[1], ' ') . "\n"; + if (!empty($note)) { + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pad($note, ' ', 2) . "\n"; + } + $invoice .= Lang::pad("", '=') . "\n"; + if ($cust) { + $invoice .= Lang::pads(Lang::T('Full Name'), $cust['fullname'], ' ') . "\n"; + } + $invoice .= Lang::pads(Lang::T('Username'), $in['username'], ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Password'), '**********', ' ') . "\n"; + if ($in['type'] != 'Balance') { + $invoice .= Lang::pads(Lang::T('Created On'), Lang::dateAndTimeFormat($in['recharged_on'], $in['recharged_time']), ' ') . "\n"; + $invoice .= Lang::pads(Lang::T('Expires On'), Lang::dateAndTimeFormat($in['expiration'], $in['time']), ' ') . "\n"; + } + $invoice .= Lang::pad("", '=') . "\n"; + $invoice .= Lang::pad($config['note'], ' ', 2) . "\n"; + $ui->assign('whatsapp', urlencode("```$invoice```")); + $ui->assign('in', $in); + } + public static function tax($price, $tax_rate = 1) + { + // Convert tax rate to decimal + $tax_rate_decimal = $tax_rate / 100; + $tax = $price * $tax_rate_decimal; + return $tax; + } +} diff --git a/system/autoload/Paginator.php b/system/autoload/Paginator.php new file mode 100644 index 0000000..85441b8 --- /dev/null +++ b/system/autoload/Paginator.php @@ -0,0 +1,366 @@ + 0) { + $url .= '&' . http_build_query($search); + } + $url .= '&p='; + $totalReq = $query->count(); + $lastpage = ceil($totalReq / $per_page); + $lpm1 = $lastpage - 1; + $limit = $per_page; + $startpoint = ($page * $limit) - $limit; + if ($lastpage >= 1) { + $pages = []; + if ($lastpage < 7 + ($adjacents * 2)) { + for ($counter = 1; $counter <= $lastpage; $counter++) { + $pages[] = $counter; + } + } elseif ($lastpage > 5 + ($adjacents * 2)) { + if ($page < 1 + ($adjacents * 2)) { + for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { + $pages[] = $counter; + } + $pages[] = "..."; + $pages[] = $lpm1; + $pages[] = $lastpage; + } elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { + $pages[] = "1"; + $pages[] = "2"; + $pages[] = "..."; + for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { + $pages[] = $counter; + } + $pages[] = "..."; + $pages[] = $lpm1; + $pages[] = $lastpage; + } else { + $pages[] = "1"; + $pages[] = "2"; + $pages[] = "..."; + for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { + $pages[] = $counter; + } + } + } + + $result = [ + 'count' => $lastpage, + 'limit' => $per_page, + 'startpoint' => $startpoint, + 'url' => $url, + 'page' => $page, + 'pages' => $pages, + 'prev' => ($page > 0) ? ($page - 1) : "0", + 'next' => ($page >= $lastpage) ? $lastpage : $page + 1 + ]; + if ($ui) { + $ui->assign('paginator', $result); + } + return $query->offset($startpoint)->limit($per_page)->find_many(); + } + } + + public static function build($table, $colVal = [], $query = '', $per_page = '10') + { + global $routes; + global $_L; + $url = U . implode('/', $routes); + $query = urlencode($query); + $adjacents = "2"; + $page = (int)(empty(_get('p')) ? 1 : _get('p')); + $pagination = ""; + foreach ($colVal as $k => $v) { + if (!is_array($v) && strpos($v, '%') === false) { + $table = $table->where($k, $v); + } else { + if (is_array($v)) { + $table = $table->where_in($k, $v); + } else { + $table = $table->where_like($k, $v); + } + } + } + $totalReq = $table->count(); + $page = ($page == 0 ? 1 : $page); + $next = $page + 1; + $lastpage = ceil($totalReq / $per_page); + $lpm1 = $lastpage - 1; + $limit = $per_page; + $startpoint = ($page * $limit) - $limit; + if ($lastpage >= 1) { + $pagination .= '