phpnuxbill/system/controllers/dashboard.php

198 lines
6.6 KiB
PHP
Raw Normal View History

2017-03-11 03:51:06 +08:00
<?php
2023-09-07 11:54:20 +08:00
2017-03-11 03:51:06 +08:00
/**
2023-10-12 16:55:42 +08:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2023-09-07 11:54:20 +08:00
**/
2023-10-12 16:55:42 +08:00
2017-03-11 03:51:06 +08:00
_admin();
2024-02-13 14:54:01 +08:00
$ui->assign('_title', Lang::T('Dashboard'));
2017-03-11 03:51:06 +08:00
$ui->assign('_admin', $admin);
$fdate = date('Y-m-01');
$tdate = date('Y-m-t');
//first day of month
$first_day_month = date('Y-m-01');
$mdate = date('Y-m-d');
$month_n = date('n');
$iday = ORM::for_table('tbl_transactions')
->where('recharged_on', $mdate)
->where_not_equal('method', 'Customer - Balance')
2024-03-13 16:34:57 +08:00
->where_not_equal('method', 'Recharge Balance - Administrator')
->sum('price');
2023-09-07 11:54:20 +08:00
if ($iday == '') {
2017-03-11 03:51:06 +08:00
$iday = '0.00';
}
2023-09-07 11:54:20 +08:00
$ui->assign('iday', $iday);
2017-03-11 03:51:06 +08:00
2024-03-13 16:34:57 +08:00
$imonth = ORM::for_table('tbl_transactions')->where_not_equal('method', 'Customer - Balance')->where_not_equal('method', 'Recharge Balance - Administrator')->where_gte('recharged_on', $first_day_month)->where_lte('recharged_on', $mdate)->sum('price');
2023-09-07 11:54:20 +08:00
if ($imonth == '') {
2017-03-11 03:51:06 +08:00
$imonth = '0.00';
}
2023-09-07 11:54:20 +08:00
$ui->assign('imonth', $imonth);
2017-03-11 03:51:06 +08:00
2023-09-07 11:54:20 +08:00
$u_act = ORM::for_table('tbl_user_recharges')->where('status', 'on')->count();
2023-09-13 16:50:20 +08:00
if (empty($u_act)) {
2017-03-11 03:51:06 +08:00
$u_act = '0';
}
2023-09-07 11:54:20 +08:00
$ui->assign('u_act', $u_act);
2017-03-11 03:51:06 +08:00
$u_all = ORM::for_table('tbl_user_recharges')->count();
2023-09-13 16:50:20 +08:00
if (empty($u_all)) {
2017-03-11 03:51:06 +08:00
$u_all = '0';
}
2023-09-07 11:54:20 +08:00
$ui->assign('u_all', $u_all);
$c_all = ORM::for_table('tbl_customers')->count();
2023-09-13 16:50:20 +08:00
if (empty($c_all)) {
2023-09-07 11:54:20 +08:00
$c_all = '0';
}
2023-09-13 16:50:20 +08:00
$ui->assign('c_all', $c_all);
2023-09-07 11:54:20 +08:00
2024-02-26 15:38:04 +08:00
if ($config['hide_uet'] != 'yes') {
2024-02-07 13:02:39 +08:00
//user expire
$paginator = Paginator::build(ORM::for_table('tbl_user_recharges'));
$expire = ORM::for_table('tbl_user_recharges')
->where_lte('expiration', $mdate)
->offset($paginator['startpoint'])
->limit($paginator['limit'])
->order_by_desc('expiration')
->find_many();
// Get the total count of expired records for pagination
$totalCount = ORM::for_table('tbl_user_recharges')
->where_lte('expiration', $mdate)
->count();
// Pass the total count and current page to the paginator
$paginator['total_count'] = $totalCount;
// Assign the pagination HTML to the template variable
$ui->assign('paginator', $paginator);
$ui->assign('expire', $expire);
}
2017-03-11 03:51:06 +08:00
//activity log
$dlog = ORM::for_table('tbl_logs')->limit(5)->order_by_desc('id')->find_many();
2023-09-07 11:54:20 +08:00
$ui->assign('dlog', $dlog);
2017-03-11 03:51:06 +08:00
$log = ORM::for_table('tbl_logs')->count();
2023-09-07 11:54:20 +08:00
$ui->assign('log', $log);
2017-03-11 03:51:06 +08:00
2024-02-06 17:46:50 +08:00
2024-02-26 15:38:04 +08:00
if ($config['hide_vs'] != 'yes') {
$cacheStocksfile = $CACHE_PATH . File::pathFixer('/VoucherStocks.temp');
$cachePlanfile = $CACHE_PATH . File::pathFixer('/VoucherPlans.temp');
2024-02-07 13:02:39 +08:00
//Cache for 5 minutes
2024-02-26 15:38:04 +08:00
if (file_exists($cacheStocksfile) && time() - filemtime($cacheStocksfile) < 600) {
2024-02-07 13:02:39 +08:00
$stocks = json_decode(file_get_contents($cacheStocksfile), true);
$plans = json_decode(file_get_contents($cachePlanfile), true);
2024-02-26 15:38:04 +08:00
} else {
2024-02-07 13:02:39 +08:00
// Count stock
$tmp = $v = ORM::for_table('tbl_plans')->select('id')->select('name_plan')->find_many();
$plans = array();
$stocks = array("used" => 0, "unused" => 0);
$n = 0;
foreach ($tmp as $plan) {
$unused = ORM::for_table('tbl_voucher')
->where('id_plan', $plan['id'])
->where('status', 0)->count();
$used = ORM::for_table('tbl_voucher')
->where('id_plan', $plan['id'])
->where('status', 1)->count();
if ($unused > 0 || $used > 0) {
$plans[$n]['name_plan'] = $plan['name_plan'];
$plans[$n]['unused'] = $unused;
$plans[$n]['used'] = $used;
$stocks["unused"] += $unused;
$stocks["used"] += $used;
$n++;
}
2024-02-06 17:46:50 +08:00
}
2024-02-07 13:02:39 +08:00
file_put_contents($cacheStocksfile, json_encode($stocks));
file_put_contents($cachePlanfile, json_encode($plans));
}
}
2024-02-26 15:38:04 +08:00
$cacheMRfile = File::pathFixer('/monthlyRegistered.temp');
2024-02-06 17:41:26 +08:00
//Cache for 1 hour
2024-02-26 15:38:04 +08:00
if (file_exists($cacheMRfile) && time() - filemtime($cacheMRfile) < 3600) {
2024-02-06 17:41:26 +08:00
$monthlyRegistered = json_decode(file_get_contents($cacheMRfile), true);
2024-02-26 15:38:04 +08:00
} else {
2024-02-06 17:41:26 +08:00
//Monthly Registered Customers
$result = ORM::for_table('tbl_customers')
->select_expr('MONTH(created_at)', 'month')
->select_expr('COUNT(*)', 'count')
->where_raw('YEAR(created_at) = YEAR(NOW())')
->group_by_expr('MONTH(created_at)')
->find_many();
$monthlyRegistered = [];
foreach ($result as $row) {
$monthlyRegistered[] = [
'date' => $row->month,
'count' => $row->count
];
}
file_put_contents($cacheMRfile, json_encode($monthlyRegistered));
}
2024-02-26 15:38:04 +08:00
$cacheMSfile = $CACHE_PATH . File::pathFixer('/monthlySales.temp');
2024-02-06 17:43:06 +08:00
//Cache for 12 hours
2024-02-26 15:38:04 +08:00
if (file_exists($cacheMSfile) && time() - filemtime($cacheMSfile) < 43200) {
2024-02-06 17:41:26 +08:00
$monthlySales = json_decode(file_get_contents($cacheMSfile), true);
2024-02-26 15:38:04 +08:00
} else {
2024-02-06 17:41:26 +08:00
// Query to retrieve monthly data
$results = ORM::for_table('tbl_transactions')
->select_expr('MONTH(recharged_on)', 'month')
->select_expr('SUM(price)', 'total')
->where_raw("YEAR(recharged_on) = YEAR(CURRENT_DATE())") // Filter by the current year
->where_not_equal('method', 'Customer - Balance')
2024-03-13 16:34:57 +08:00
->where_not_equal('method', 'Recharge Balance - Administrator')
2024-02-06 17:41:26 +08:00
->group_by_expr('MONTH(recharged_on)')
->find_many();
// Create an array to hold the monthly sales data
$monthlySales = array();
// Iterate over the results and populate the array
foreach ($results as $result) {
$month = $result->month;
$totalSales = $result->total;
$monthlySales[$month] = array(
'month' => $month,
2024-02-06 17:41:26 +08:00
'totalSales' => $totalSales
);
}
2024-02-06 17:41:26 +08:00
// Fill in missing months with zero sales
for ($month = 1; $month <= 12; $month++) {
if (!isset($monthlySales[$month])) {
$monthlySales[$month] = array(
'month' => $month,
'totalSales' => 0
);
}
}
// Sort the array by month
ksort($monthlySales);
2024-02-06 17:41:26 +08:00
// Reindex the array
$monthlySales = array_values($monthlySales);
file_put_contents($cacheMSfile, json_encode($monthlySales));
}
// Assign the monthly sales data to Smarty
$ui->assign('monthlySales', $monthlySales);
$ui->assign('xfooter', '');
2024-02-06 17:41:26 +08:00
$ui->assign('monthlyRegistered', $monthlyRegistered);
2023-09-07 11:54:20 +08:00
$ui->assign('stocks', $stocks);
$ui->assign('plans', $plans);
2022-09-18 01:00:40 +08:00
run_hook('view_dashboard'); #HOOK
2023-09-07 11:54:20 +08:00
$ui->display('dashboard.tpl');