1. 修復儀表板營收統計,將模擬數據替換為從 Order 模型撈取的真實資料。 2. 修正 DashboardController 中的支付狀態篩選邏輯,從字串 'paid' 改為正確的 tinyInteger 狀態碼 1。 3. 在 Order 模型中新增 PAYMENT_STATUS_SUCCESS 與 PAYMENT_STATUS_FAILED 常數。 4. 新增「本月累計營收」的多語系翻譯(中、英、日)。 5. 優化儀表板前端介面,包含動態顯示昨日增長趨勢百分比與顏色標籤。 6. 修正儀表板機台列表中的「今日銷售額」統計邏輯。
83 lines
2.9 KiB
PHP
83 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Transaction\Order;
|
|
use App\Models\Machine\Machine;
|
|
use Illuminate\Http\Request;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
// 每頁顯示筆數限制 (預設為 10)
|
|
$perPage = (int) request()->input('per_page', 10);
|
|
if ($perPage <= 0)
|
|
$perPage = 10;
|
|
|
|
// 從資料庫獲取真實統計數據
|
|
$activeMachines = Machine::online()->count();
|
|
$offlineMachines = Machine::offline()->count();
|
|
$alertsPending = Machine::hasError()->count();
|
|
$memberCount = \App\Models\Member\Member::count();
|
|
|
|
// 交易營收統計 (真實數據)
|
|
$todayRevenue = Order::where('payment_status', Order::PAYMENT_STATUS_SUCCESS)
|
|
->whereDate('payment_at', Carbon::today())
|
|
->sum('pay_amount');
|
|
|
|
$yesterdayRevenue = Order::where('payment_status', Order::PAYMENT_STATUS_SUCCESS)
|
|
->whereDate('payment_at', Carbon::yesterday())
|
|
->sum('pay_amount');
|
|
|
|
$dayBeforeRevenue = Order::where('payment_status', Order::PAYMENT_STATUS_SUCCESS)
|
|
->whereDate('payment_at', Carbon::yesterday()->subDay())
|
|
->sum('pay_amount');
|
|
|
|
$monthlyRevenue = Order::where('payment_status', Order::PAYMENT_STATUS_SUCCESS)
|
|
->whereMonth('payment_at', Carbon::now()->month)
|
|
->whereYear('payment_at', Carbon::now()->year)
|
|
->sum('pay_amount');
|
|
|
|
// 計算昨日增長趨勢 (百分比)
|
|
$yesterdayTrend = 0;
|
|
if ($yesterdayRevenue > 0) {
|
|
$yesterdayTrend = (($todayRevenue - $yesterdayRevenue) / $yesterdayRevenue) * 100;
|
|
} elseif ($todayRevenue > 0) {
|
|
$yesterdayTrend = 100;
|
|
}
|
|
|
|
// 獲取機台列表 (分頁)
|
|
$machines = Machine::when($request->search, function ($query, $search) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('serial_no', 'like', "%{$search}%");
|
|
});
|
|
})
|
|
->withSum('slots', 'stock')
|
|
->withSum('slots', 'max_stock')
|
|
->withSum(['orders as today_sales_sum' => function($query) {
|
|
$query->whereDate('payment_at', now()->toDateString())
|
|
->where('payment_status', Order::PAYMENT_STATUS_SUCCESS);
|
|
}], 'pay_amount')
|
|
->orderByDesc('last_heartbeat_at')
|
|
->paginate($perPage)
|
|
->withQueryString();
|
|
|
|
return view('admin.dashboard', compact(
|
|
'activeMachines',
|
|
'offlineMachines',
|
|
'alertsPending',
|
|
'memberCount',
|
|
'todayRevenue',
|
|
'yesterdayRevenue',
|
|
'dayBeforeRevenue',
|
|
'monthlyRevenue',
|
|
'yesterdayTrend',
|
|
'machines'
|
|
));
|
|
}
|
|
}
|