diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index c773cce..d55ac9c 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -3,8 +3,10 @@ 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 { @@ -16,12 +18,37 @@ class DashboardController extends Controller $perPage = 10; // 從資料庫獲取真實統計數據 - $totalRevenue = \App\Models\Member\MemberWallet::sum('balance'); $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) { @@ -33,18 +60,22 @@ class DashboardController extends Controller ->withSum('slots', 'max_stock') ->withSum(['orders as today_sales_sum' => function($query) { $query->whereDate('payment_at', now()->toDateString()) - ->where('payment_status', 'paid'); + ->where('payment_status', Order::PAYMENT_STATUS_SUCCESS); }], 'pay_amount') ->orderByDesc('last_heartbeat_at') ->paginate($perPage) ->withQueryString(); return view('admin.dashboard', compact( - 'totalRevenue', 'activeMachines', 'offlineMachines', 'alertsPending', 'memberCount', + 'todayRevenue', + 'yesterdayRevenue', + 'dayBeforeRevenue', + 'monthlyRevenue', + 'yesterdayTrend', 'machines' )); } diff --git a/app/Models/Transaction/Order.php b/app/Models/Transaction/Order.php index 03cf58e..740745d 100644 --- a/app/Models/Transaction/Order.php +++ b/app/Models/Transaction/Order.php @@ -13,6 +13,10 @@ class Order extends Model { use HasFactory, SoftDeletes, TenantScoped; + // 支付狀態 + public const PAYMENT_STATUS_FAILED = 0; + public const PAYMENT_STATUS_SUCCESS = 1; + protected $fillable = [ 'company_id', 'flow_id', diff --git a/lang/en.json b/lang/en.json index 301e3da..26f473a 100644 --- a/lang/en.json +++ b/lang/en.json @@ -796,6 +796,7 @@ "Monitor events and system activity across your vending fleet.": "Monitor events and system activity across your vending fleet.", "Monitor warehouse stock summary": "Monitor warehouse stock summary", "Monthly Transactions": "Monthly Transactions", + "Monthly Cumulative Revenue": "Monthly Cumulative Revenue", "Monthly cumulative revenue overview": "Monthly cumulative revenue overview", "Motor not stopped": "Motor not stopped", "Movement History": "Movement History", diff --git a/lang/ja.json b/lang/ja.json index 8984029..b448124 100644 --- a/lang/ja.json +++ b/lang/ja.json @@ -789,6 +789,7 @@ "Monitor events and system activity across your vending fleet.": "Monitor events and system activity across your vending fleet.", "Monitor warehouse stock summary": "Monitor warehouse stock summary", "Monthly Transactions": "Monthly Transactions", + "Monthly Cumulative Revenue": "今月の累計収益", "Monthly cumulative revenue overview": "Monthly cumulative revenue overview", "Motor not stopped": "Motor not stopped", "Movement History": "移動履歴", diff --git a/lang/zh_TW.json b/lang/zh_TW.json index 2a7047a..cbba53e 100644 --- a/lang/zh_TW.json +++ b/lang/zh_TW.json @@ -895,6 +895,7 @@ "Monitor events and system activity across your vending fleet.": "跨機台連線動態與系統日誌監控。", "Monitor warehouse stock summary": "監控倉庫庫存摘要", "Monthly Transactions": "本月交易統計", + "Monthly Cumulative Revenue": "本月累計營收", "Monthly cumulative revenue overview": "本月累計營收概況", "Motor not stopped": "電機未停止", "Movement History": "異動紀錄", diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php index 801cc55..c03c76c 100644 --- a/resources/views/admin/dashboard.blade.php +++ b/resources/views/admin/dashboard.blade.php @@ -73,7 +73,7 @@
{{ __('Monthly cumulative revenue overview') }}
+{{ __('Monthly Cumulative Revenue') }}: ${{ number_format($monthlyRevenue, 0) }}
{{ __("Today Cumulative Sales") }}
- ${{ number_format($totalRevenue / 30, 0) }}
+ ${{ number_format($todayRevenue, 0) }}{{ __('vs Yesterday') }}
${{ number_format($totalRevenue - / 25, 0) }}
+${{ number_format($yesterdayRevenue, 0) }}
@@ -146,8 +145,7 @@ -${{ number_format($totalRevenue - / 40, 0) }}
+${{ number_format($dayBeforeRevenue, 0) }}