[RELEASE] demo → main 正式上線:遠端管理租戶 500 修復與電子發票調整

1. [FIX] 遠端管理歷史紀錄套用機台授權過濾(whereHas('machine')),修復租戶帳號進入遠端管理與庫存歷史時 machine 為 null 導致的 500,並對 Blade 加上 null-safe 防禦;系統管理員不受影響。
2. [FIX] 開立電子發票前檢查機台電子發票開關(tax_invoice_enabled),未開啟者不開立。
3. [STYLE] 發票操作按鈕配置調整,作廢確認框改用自製 UI。
This commit is contained in:
sky121113 2026-06-17 11:17:38 +08:00
commit 159fa38fc0
6 changed files with 55 additions and 64 deletions

View File

@ -42,7 +42,10 @@ class RemoteController extends Controller
$machines = $machineQuery->paginate($request->input('per_page', 10), ['*'], 'machine_page'); $machines = $machineQuery->paginate($request->input('per_page', 10), ['*'], 'machine_page');
// --- 2. 歷史紀錄處理 (Operation Records Tab) --- // --- 2. 歷史紀錄處理 (Operation Records Tab) ---
// whereHas('machine') 會套用 Machine 的 machine_access 全域作用域,
// 確保租戶僅能看到自己被授權機台的指令紀錄(系統管理員不受作用域限制,仍可看全部)。
$historyQuery = RemoteCommand::where('command_type', '!=', 'reload_stock') $historyQuery = RemoteCommand::where('command_type', '!=', 'reload_stock')
->whereHas('machine')
->with(['machine', 'user']); ->with(['machine', 'user']);
if ($request->filled('search') && ($request->input('tab') === 'history' || !$request->has('tab'))) { if ($request->filled('search') && ($request->input('tab') === 'history' || !$request->has('tab'))) {
@ -233,8 +236,9 @@ class RemoteController extends Controller
->paginate($request->input('per_page', 10), ['*'], 'machine_page'); ->paginate($request->input('per_page', 10), ['*'], 'machine_page');
// 2. 歷史紀錄查詢與分頁 // 2. 歷史紀錄查詢與分頁
// whereHas('machine') 套用 machine_access 作用域,租戶僅能看到自己授權機台的補貨紀錄。
$historyQuery = RemoteCommand::with(['machine', 'user']); $historyQuery = RemoteCommand::with(['machine', 'user']);
$historyQuery->where('command_type', 'reload_stock'); $historyQuery->whereHas('machine')->where('command_type', 'reload_stock');
if ($request->filled('search')) { if ($request->filled('search')) {
$search = $request->input('search'); $search = $request->input('search');

View File

@ -371,7 +371,9 @@ class TransactionService
// 2. Record Invoice — 後台開立版finalize 帶 invoice 輸入(status=pending)時建 pending 發票, // 2. Record Invoice — 後台開立版finalize 帶 invoice 輸入(status=pending)時建 pending 發票,
// commit 後派 IssueInvoiceJob 去綠界開立(不在交易內等 ECPay避免長交易 // commit 後派 IssueInvoiceJob 去綠界開立(不在交易內等 ECPay避免長交易
if (!empty($data['invoice'])) { // 開立條件:機台「電子發票開關」開啟 AND 上報帶 invoice兩者皆成立才開立。
// 機台關閉電子發票時略過開立(不建發票、不送綠界),僅留 warning log 供日後追查。
if (!empty($data['invoice']) && $order->machine?->tax_invoice_enabled) {
$invoiceData = $data['invoice']; $invoiceData = $data['invoice'];
$invoiceData['serial_no'] = $serialNo; $invoiceData['serial_no'] = $serialNo;
$invoiceData['flow_id'] = $order->flow_id; $invoiceData['flow_id'] = $order->flow_id;
@ -384,6 +386,13 @@ class TransactionService
\App\Jobs\Transaction\IssueInvoiceJob::dispatch($invoice->id); \App\Jobs\Transaction\IssueInvoiceJob::dispatch($invoice->id);
}); });
} }
} elseif (!empty($data['invoice'])) {
// 上報帶了 invoice但機台「電子發票開關」為關閉 → 略過開立,留軌跡供追查。
Log::warning('機台已關閉電子發票,略過開立', [
'machine_id' => $order->machine_id,
'flow_id' => $order->flow_id,
'order_id' => $order->id,
]);
} }
// 3. Record Dispense Results (B602) - Optional/Multiple // 3. Record Dispense Results (B602) - Optional/Multiple

View File

@ -162,7 +162,7 @@
@foreach ($history as $item) @foreach ($history as $item)
<tr <tr
class="group hover:bg-slate-50/80 dark:hover:bg-slate-800/40 transition-all duration-300"> class="group hover:bg-slate-50/80 dark:hover:bg-slate-800/40 transition-all duration-300">
<td class="px-6 py-6 cursor-pointer" @click="selectMachine({{ Js::from($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])) }})"> <td class="px-6 py-6 cursor-pointer" @click="selectMachine({{ Js::from(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)) }})">
<div class="flex items-center gap-4"> <div class="flex items-center gap-4">
<div <div
class="w-12 h-12 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 shadow-sm overflow-hidden shrink-0"> class="w-12 h-12 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 shadow-sm overflow-hidden shrink-0">
@ -295,7 +295,7 @@
<div class="xl:hidden grid grid-cols-1 md:grid-cols-2 gap-6"> <div class="xl:hidden grid grid-cols-1 md:grid-cols-2 gap-6">
@forelse ($history as $item) @forelse ($history as $item)
<div class="luxury-card p-6 rounded-[2rem] border border-slate-100 dark:border-slate-800 bg-white/50 dark:bg-slate-900/50 transition-all duration-300 group" <div class="luxury-card p-6 rounded-[2rem] border border-slate-100 dark:border-slate-800 bg-white/50 dark:bg-slate-900/50 transition-all duration-300 group"
@click="selectMachine({{ Js::from($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])) }})"> @click="selectMachine({{ Js::from(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)) }})">
<div class="flex items-start justify-between gap-4 mb-6"> <div class="flex items-start justify-between gap-4 mb-6">
<div class="flex items-center gap-4 min-w-0"> <div class="flex items-center gap-4 min-w-0">
<div class="w-14 h-14 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 overflow-hidden shadow-sm shrink-0"> <div class="w-14 h-14 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 overflow-hidden shadow-sm shrink-0">
@ -379,7 +379,7 @@
</div> </div>
<div class="flex items-center gap-3"> <div class="flex items-center gap-3">
<button type="button" @click.stop="selectMachine({{ Js::from($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])) }})" <button type="button" @click.stop="selectMachine({{ Js::from(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)) }})"
class="flex-1 flex items-center justify-center gap-2 py-3 rounded-xl bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-black text-[10px] sm:text-xs tracking-widest hover:bg-slate-200 dark:hover:bg-slate-700 transition-all duration-300 border border-slate-200 dark:border-slate-700 shadow-sm"> class="flex-1 flex items-center justify-center gap-2 py-3 rounded-xl bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-black text-[10px] sm:text-xs tracking-widest hover:bg-slate-200 dark:hover:bg-slate-700 transition-all duration-300 border border-slate-200 dark:border-slate-700 shadow-sm">
<svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" /></svg> <svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" /></svg>
{{ __('New Command') }} {{ __('New Command') }}

View File

@ -129,7 +129,7 @@
<tbody class="divide-y divide-slate-50 dark:divide-slate-800/80"> <tbody class="divide-y divide-slate-50 dark:divide-slate-800/80">
@foreach ($history as $item) @foreach ($history as $item)
<tr class="group hover:bg-slate-50/80 dark:hover:bg-slate-800/40 transition-all duration-300"> <tr class="group hover:bg-slate-50/80 dark:hover:bg-slate-800/40 transition-all duration-300">
<td class="px-6 py-6 cursor-pointer" @click="selectMachine(@js($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])))"> <td class="px-6 py-6 cursor-pointer" @click="selectMachine(@js(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)))">
<div class="flex items-center gap-4"> <div class="flex items-center gap-4">
<div <div
class="w-12 h-12 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 shadow-sm overflow-hidden"> class="w-12 h-12 rounded-2xl bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400 border border-slate-200 dark:border-slate-700 group-hover:bg-cyan-500 group-hover:text-white transition-all duration-300 shadow-sm overflow-hidden">
@ -236,7 +236,7 @@
<div class="xl:hidden grid grid-cols-1 md:grid-cols-2 gap-6"> <div class="xl:hidden grid grid-cols-1 md:grid-cols-2 gap-6">
@forelse ($history as $item) @forelse ($history as $item)
<div class="luxury-card p-6 rounded-[2rem] border border-slate-100 dark:border-slate-800 bg-white/50 dark:bg-slate-900/50 transition-all duration-300 group" <div class="luxury-card p-6 rounded-[2rem] border border-slate-100 dark:border-slate-800 bg-white/50 dark:bg-slate-900/50 transition-all duration-300 group"
@click="selectMachine(@js($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])))"> @click="selectMachine(@js(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)))">
<div class="flex items-start justify-between gap-4 mb-6"> <div class="flex items-start justify-between gap-4 mb-6">
<div class="flex items-center gap-4 min-w-0"> <div class="flex items-center gap-4 min-w-0">
<div <div
@ -314,7 +314,7 @@
</div> </div>
<div class="flex items-center gap-3"> <div class="flex items-center gap-3">
<button type="button" @click.stop="selectMachine(@js($item->machine->only(['id', 'name', 'serial_no', 'image_urls'])))" <button type="button" @click.stop="selectMachine(@js(($item->machine?->only(['id', 'name', 'serial_no', 'image_urls']) ?? null)))"
class="flex-1 flex items-center justify-center gap-2 py-3 rounded-xl bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-black text-[10px] sm:text-xs tracking-widest hover:bg-slate-200 dark:hover:bg-slate-700 transition-all duration-300 border border-slate-200 dark:border-slate-700 shadow-sm"> class="flex-1 flex items-center justify-center gap-2 py-3 rounded-xl bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-black text-[10px] sm:text-xs tracking-widest hover:bg-slate-200 dark:hover:bg-slate-700 transition-all duration-300 border border-slate-200 dark:border-slate-700 shadow-sm">
<svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" <path stroke-linecap="round" stroke-linejoin="round"

View File

@ -309,57 +309,29 @@
</div> </div>
</div> </div>
<div class="flex items-center gap-2"> @if($invStatus === 'issued')
@if($invStatus === 'issued') <div class="flex items-center gap-3">
{{-- 列印(捐贈發票無紙本,僅非捐贈可列印) --}} {{-- 列印(捐贈發票無紙本,僅非捐贈可列印) --}}
@if($invPrintable) @if($invPrintable)
<form method="POST" action="{{ route('admin.sales.invoices.print', $order->invoice) }}" target="_blank"> <form method="POST" action="{{ route('admin.sales.invoices.print', $order->invoice) }}" target="_blank">
@csrf @csrf
<button type="submit" title="{{ __('Print Invoice') }}" <button type="submit" class="flex-1 sm:flex-none px-6 py-3 rounded-xl bg-white dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-bold text-xs border border-slate-200 dark:border-slate-700 hover:bg-indigo-500 hover:text-white hover:border-indigo-500 transition-all duration-300 shadow-sm">
class="p-3 rounded-xl bg-white dark:bg-slate-800 text-slate-500 dark:text-slate-400 border border-slate-200 dark:border-slate-700 hover:bg-indigo-500 hover:text-white hover:border-indigo-500 transition-all duration-300 shadow-sm"> {{ __('Print Invoice') }}
<svg class="w-4 h-4 stroke-[2.2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M6.72 13.829c-.24.03-.48.062-.72.096m.72-.096a42.415 42.415 0 0 1 10.56 0m-10.56 0L6.34 18m10.94-4.171c.24.03.48.062.72.096m-.72-.096L17.66 18m0 0 .229 2.523a1.125 1.125 0 0 1-1.12 1.227H7.231c-.662 0-1.18-.568-1.12-1.227L6.34 18m11.318 0h1.091A2.25 2.25 0 0 0 21 15.75V9.456c0-1.081-.768-2.015-1.837-2.175a48.055 48.055 0 0 0-1.913-.247M6.34 18H5.25A2.25 2.25 0 0 1 3 15.75V9.456c0-1.081.768-2.015 1.837-2.175a48.041 48.041 0 0 1 1.913-.247m10.5 0a48.536 48.536 0 0 0-10.5 0m10.5 0V3.375c0-.621-.504-1.125-1.125-1.125h-8.25c-.621 0-1.125.504-1.125 1.125v3.659M18 10.5h.008v.008H18V10.5Z" />
</svg>
</button>
</form>
@else
<span class="text-[11px] font-bold text-slate-400 dark:text-slate-500 px-2">{{ __('Donation invoices cannot be printed') }}</span>
@endif
{{-- 作廢:使用自製確認框 UI不用瀏覽器原生 confirm --}}
<form id="invoice-void-form-{{ $order->invoice->id }}" method="POST" action="{{ route('admin.sales.invoices.void', $order->invoice) }}" class="hidden">
@csrf
</form>
<button type="button" title="{{ __('Void Invoice') }}"
@click="confirmVoidInvoice('invoice-void-form-{{ $order->invoice->id }}')"
class="p-3 rounded-xl bg-white dark:bg-slate-800 text-slate-500 dark:text-slate-400 border border-slate-200 dark:border-slate-700 hover:bg-rose-500 hover:text-white hover:border-rose-500 transition-all duration-300 shadow-sm">
<svg class="w-4 h-4 stroke-[2.2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
</button>
@elseif(in_array($invStatus, ['pending', 'failed']))
{{-- 查詢(對帳) --}}
<form method="POST" action="{{ route('admin.sales.invoices.reconcile', $order->invoice) }}">
@csrf
<button type="submit" title="{{ __('Query') }}"
class="p-3 rounded-xl bg-white dark:bg-slate-800 text-slate-500 dark:text-slate-400 border border-slate-200 dark:border-slate-700 hover:bg-amber-500 hover:text-white hover:border-amber-500 transition-all duration-300 shadow-sm">
<svg class="w-4 h-4 stroke-[2.2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</button>
</form>
{{-- 補開 --}}
<form id="invoice-reissue-form-{{ $order->invoice->id }}" method="POST" action="{{ route('admin.sales.invoices.reissue', $order->invoice) }}" class="hidden">
@csrf
</form>
<button type="button" title="{{ __('Re-issue') }}"
@click="confirmReissueInvoice('invoice-reissue-form-{{ $order->invoice->id }}')"
class="p-3 rounded-xl bg-white dark:bg-slate-800 text-slate-500 dark:text-slate-400 border border-slate-200 dark:border-slate-700 hover:bg-cyan-500 hover:text-white hover:border-cyan-500 transition-all duration-300 shadow-sm">
<svg class="w-4 h-4 stroke-[2.2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
</button> </button>
</form>
@else
<span class="text-[11px] font-bold text-slate-400 dark:text-slate-500 px-2">{{ __('Donation invoices cannot be printed') }}</span>
@endif @endif
{{-- 作廢:使用自製確認框 UI不用瀏覽器原生 confirm --}}
<form id="invoice-void-form-{{ $order->invoice->id }}" method="POST" action="{{ route('admin.sales.invoices.void', $order->invoice) }}" class="hidden">
@csrf
</form>
<button type="button" @click="confirmVoidInvoice('invoice-void-form-{{ $order->invoice->id }}')"
class="flex-1 sm:flex-none px-6 py-3 rounded-xl bg-white dark:bg-slate-800 text-slate-600 dark:text-slate-300 font-bold text-xs border border-slate-200 dark:border-slate-700 hover:bg-rose-500 hover:text-white hover:border-rose-500 transition-all duration-300 shadow-sm">
{{ __('Void Invoice') }}
</button>
</div> </div>
@endif
</div> </div>
</div> </div>
</section> </section>

View File

@ -238,21 +238,27 @@
<td class="px-6 py-6 text-right" @click.stop> <td class="px-6 py-6 text-right" @click.stop>
<div class="flex items-center justify-end gap-1.5"> <div class="flex items-center justify-end gap-1.5">
@if(in_array($st, ['pending', 'failed'])) @if(in_array($st, ['pending', 'failed']))
{{-- 查詢(對帳) --}}
<form method="POST" action="{{ route('admin.sales.invoices.reconcile', $invoice) }}"> <form method="POST" action="{{ route('admin.sales.invoices.reconcile', $invoice) }}">
@csrf @csrf
<button type="submit" title="{{ __('Query ECPay') }}" <button type="submit" title="{{ __('Query ECPay') }}"
class="px-2.5 py-1.5 rounded-lg text-[11px] font-bold text-amber-600 dark:text-amber-400 bg-amber-500/5 hover:bg-amber-500/10 border border-amber-500/20 transition-all"> class="p-2 rounded-xl text-slate-400 hover:text-amber-500 hover:bg-amber-500/5 dark:hover:bg-amber-500/10 border border-transparent hover:border-amber-500/20 transition-all">
{{ __('Query') }} <svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</button> </button>
</form> </form>
<form method="POST" action="{{ route('admin.sales.invoices.reissue', $invoice) }}" {{-- 補開:使用自製確認框 UI --}}
onsubmit="return confirm('{{ __('Re-issue this invoice?') }}')"> <form id="invoice-reissue-form-{{ $invoice->id }}" method="POST" action="{{ route('admin.sales.invoices.reissue', $invoice) }}" class="hidden">
@csrf @csrf
<button type="submit" title="{{ __('Re-issue') }}"
class="px-2.5 py-1.5 rounded-lg text-[11px] font-bold text-cyan-600 dark:text-cyan-400 bg-cyan-500/5 hover:bg-cyan-500/10 border border-cyan-500/20 transition-all">
{{ __('Re-issue') }}
</button>
</form> </form>
<button type="button" title="{{ __('Re-issue') }}"
@click="confirmReissueInvoice('invoice-reissue-form-{{ $invoice->id }}')"
class="p-2 rounded-xl text-slate-400 hover:text-cyan-500 hover:bg-cyan-500/5 dark:hover:bg-cyan-500/10 border border-transparent hover:border-cyan-500/20 transition-all">
<svg class="w-4 h-4 stroke-[2.5]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
</button>
@endif @endif
{{-- 作廢按鈕僅在「訂單詳情」提供,列表操作欄不再顯示 --}} {{-- 作廢按鈕僅在「訂單詳情」提供,列表操作欄不再顯示 --}}
<button @click="openDetail({{ $invoice->order_id }})" <button @click="openDetail({{ $invoice->order_id }})"