diff --git a/app/Http/Controllers/Admin/SalesController.php b/app/Http/Controllers/Admin/SalesController.php index e328e4c..ff536d3 100644 --- a/app/Http/Controllers/Admin/SalesController.php +++ b/app/Http/Controllers/Admin/SalesController.php @@ -194,7 +194,10 @@ class SalesController extends Controller // 1. 取貨碼列表 (list) if (!$isAjax || $tab === 'list') { - $query = PickupCode::with(['machine.slots.product', 'creator', 'order'])->latest(); + // 僅顯示目前帳號可存取機台的取貨碼(機台授權以 machine_user 為準,whereHas 會套用 Machine 的 machine_access 全域 scope) + $query = PickupCode::with(['machine.slots.product', 'creator', 'order']) + ->whereHas('machine') + ->latest(); if ($request->search) { $query->where(function ($q) use ($request) { @@ -219,7 +222,12 @@ class SalesController extends Controller // 2. 操作紀錄 (logs) if (!$isAjax || $tab === 'logs') { - $logQuery = PickupCodeLog::with(['user:id,name', 'machine:id,name,serial_no', 'pickupCode', 'order']); + // 同上:操作紀錄只顯示可存取機台的資料(machine_id 為 null 的非機台紀錄仍保留) + $logQuery = PickupCodeLog::with(['user:id,name', 'machine:id,name,serial_no', 'pickupCode', 'order']) + ->where(function ($q) { + $q->whereNull('machine_id') + ->orWhereHas('machine'); + }); if ($request->filled('search_log')) { $search = $request->input('search_log'); @@ -362,6 +370,11 @@ class SalesController extends Controller */ public function destroyPickupCode(PickupCode $pickupCode) { + // 機台授權以 machine_user 為準:非系統管理員若無法存取該取貨碼所屬機台則拒絕 + if (!Auth::user()->isSystemAdmin() && !Machine::whereKey($pickupCode->machine_id)->exists()) { + abort(403); + } + $oldValues = $pickupCode->toArray(); $pickupCode->update(['status' => 'cancelled']); @@ -411,7 +424,10 @@ class SalesController extends Controller // 1. 通行碼列表 (list) if (!$isAjax || $tab === 'list') { - $query = PassCode::with(['machine', 'creator'])->latest(); + // 僅顯示目前帳號可存取機台的通行碼(機台授權以 machine_user 為準,whereHas 會套用 Machine 的 machine_access 全域 scope) + $query = PassCode::with(['machine', 'creator']) + ->whereHas('machine') + ->latest(); if ($request->search) { $query->where(function ($q) use ($request) { @@ -447,7 +463,9 @@ class SalesController extends Controller // 2. 操作紀錄 (logs) if (!$isAjax || $tab === 'logs') { - $logQuery = PassCodeLog::with(['user:id,name', 'machine:id,name,serial_no', 'passCode']); + // 同上:操作紀錄只顯示可存取機台的資料(pass_code_logs.machine_id 為 NOT NULL) + $logQuery = PassCodeLog::with(['user:id,name', 'machine:id,name,serial_no', 'passCode']) + ->whereHas('machine'); if ($request->filled('search_log')) { $search = $request->input('search_log'); @@ -587,6 +605,11 @@ class SalesController extends Controller */ public function destroyPassCode(PassCode $passCode) { + // 機台授權以 machine_user 為準:非系統管理員若無法存取該通行碼所屬機台則拒絕 + if (!Auth::user()->isSystemAdmin() && !Machine::whereKey($passCode->machine_id)->exists()) { + abort(403); + } + $oldValues = $passCode->toArray(); $passCode->update(['status' => 'disabled']); @@ -618,7 +641,10 @@ class SalesController extends Controller // 1. 來店禮列表 (list) if (!$isAjax || $tab === 'list') { - $query = WelcomeGift::with(['machine', 'creator'])->latest(); + // 僅顯示目前帳號可存取機台的來店禮(機台授權以 machine_user 為準,whereHas 會套用 Machine 的 machine_access 全域 scope) + $query = WelcomeGift::with(['machine', 'creator']) + ->whereHas('machine') + ->latest(); if ($request->search) { $query->where(function ($q) use ($request) { @@ -654,7 +680,9 @@ class SalesController extends Controller // 2. 操作紀錄 (logs) if (!$isAjax || $tab === 'logs') { - $logQuery = WelcomeGiftLog::with(['machine:id,name,serial_no', 'welcomeGift', 'order:id,order_no']); + // 同上:操作紀錄只顯示可存取機台的資料(welcome_gift_logs.machine_id 為 NOT NULL) + $logQuery = WelcomeGiftLog::with(['machine:id,name,serial_no', 'welcomeGift', 'order:id,order_no']) + ->whereHas('machine'); if ($request->filled('search_log')) { $search = $request->input('search_log'); @@ -846,6 +874,11 @@ class SalesController extends Controller */ public function destroyWelcomeGift(WelcomeGift $welcomeGift) { + // 機台授權以 machine_user 為準:非系統管理員若無法存取該來店禮所屬機台則拒絕 + if (!Auth::user()->isSystemAdmin() && !Machine::whereKey($welcomeGift->machine_id)->exists()) { + abort(403); + } + $oldValues = $welcomeGift->toArray(); $welcomeGift->update(['status' => 'disabled']); diff --git a/resources/views/admin/sales/pass-codes/partials/tab-list.blade.php b/resources/views/admin/sales/pass-codes/partials/tab-list.blade.php index b1543f7..ef79895 100644 --- a/resources/views/admin/sales/pass-codes/partials/tab-list.blade.php +++ b/resources/views/admin/sales/pass-codes/partials/tab-list.blade.php @@ -109,7 +109,7 @@
{{ - $code->machine->name }}
+ $code->machine?->name ?? '-' }}
- {{ $code->machine->name }} + {{ $code->machine?->name ?? '-' }}
{{ $code->slot_no }} @php - $slotMobile = $code->machine->slots->where('slot_no', + $slotMobile = $code->machine?->slots->where('slot_no', $code->slot_no)->first(); $prodMobile = $slotMobile ? ($slotMobile->product?->name ?? __('Empty')) : __('Empty'); diff --git a/resources/views/admin/sales/welcome-gifts/partials/tab-list.blade.php b/resources/views/admin/sales/welcome-gifts/partials/tab-list.blade.php index 9655d27..84dbd89 100644 --- a/resources/views/admin/sales/welcome-gifts/partials/tab-list.blade.php +++ b/resources/views/admin/sales/welcome-gifts/partials/tab-list.blade.php @@ -108,7 +108,7 @@
{{ __('Machine') }}
- {{ $gift->machine->name }}
+ {{ $gift->machine?->name ?? '-' }}