From be73e7c7a0bb30319a315fd23de806742c379758 Mon Sep 17 00:00:00 2001
From: twsystem1004
Date: Fri, 3 Jul 2026 04:35:15 +0000
Subject: [PATCH] =?UTF-8?q?feat(sales):=20=E7=8F=BE=E9=87=91=E6=94=AF?=
=?UTF-8?q?=E4=BB=98=E9=9D=A2=E9=A1=8D=E6=98=8E=E7=B4=B0=EF=BC=88=E6=94=B6?=
=?UTF-8?q?=E5=90=84=E9=9D=A2=E9=A1=8D=E5=BC=B5=E6=95=B8+=E6=89=BE?=
=?UTF-8?q?=E9=9B=B6=EF=BC=89=E5=9B=9E=E5=82=B3=E4=B8=A6=E6=96=BC=E9=8A=B7?=
=?UTF-8?q?=E5=94=AE=E7=B4=80=E9=8C=84=E9=A1=AF=E7=A4=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- orders 新增 cash_detail JSON 欄(migration)
- Order model fillable/casts + cash_received_summary accessor
(顯示「收:總額(只列有收到幣別明細) 找:x」)
- TransactionService create/transition 兩處寫入 cash_detail
- 銷售列表 blade 桌機/手機各加一列現金明細
Co-Authored-By: Claude Opus 4.8
---
app/Models/Transaction/Order.php | 38 +++++++++++++++++++
.../Transaction/TransactionService.php | 4 ++
...120000_add_cash_detail_to_orders_table.php | 35 +++++++++++++++++
.../admin/sales/partials/tab-orders.blade.php | 10 +++++
4 files changed, 87 insertions(+)
create mode 100644 database/migrations/2026_07_03_120000_add_cash_detail_to_orders_table.php
diff --git a/app/Models/Transaction/Order.php b/app/Models/Transaction/Order.php
index 3a235e7..29e5c06 100644
--- a/app/Models/Transaction/Order.php
+++ b/app/Models/Transaction/Order.php
@@ -60,6 +60,7 @@ class Order extends Model
'discount_amount',
'pay_amount',
'change_amount',
+ 'cash_detail',
'points_used',
'original_amount',
'payment_status',
@@ -82,6 +83,7 @@ class Order extends Model
'discount_amount' => 'decimal:2',
'pay_amount' => 'decimal:2',
'change_amount' => 'decimal:2',
+ 'cash_detail' => 'array',
'original_amount' => 'decimal:2',
'payment_at' => 'datetime',
'machine_time' => 'datetime',
@@ -90,6 +92,42 @@ class Order extends Model
'delivery_status' => 'integer',
];
+ /**
+ * 現金收款摘要字串:「收:{總收金額}(只列有收到的面額明細) 找:{找零}」。
+ * 例:收:120(佰:1、10:2) 找:30。無 cash_detail(非現金或舊資料)或全為 0 時回 null。
+ * 總收金額由各面額張數加總得出,與括號內明細一致。
+ */
+ public function getCashReceivedSummaryAttribute(): ?string
+ {
+ $cd = $this->cash_detail;
+ if (empty($cd)) {
+ return null;
+ }
+ // 面額顯示標籤 + 幣值(依大到小)
+ $denoms = [
+ 'b1000' => ['label' => '仟', 'unit' => 1000],
+ 'b500' => ['label' => '伍佰', 'unit' => 500],
+ 'b100' => ['label' => '佰', 'unit' => 100],
+ 'c50' => ['label' => '50', 'unit' => 50],
+ 'c10' => ['label' => '10', 'unit' => 10],
+ 'c5' => ['label' => '5', 'unit' => 5],
+ 'c1' => ['label' => '1', 'unit' => 1],
+ ];
+ $received = 0;
+ $parts = [];
+ foreach ($denoms as $key => $d) {
+ $n = (int) ($cd[$key] ?? 0);
+ if ($n > 0) {
+ $parts[] = $d['label'] . ':' . $n; // 只列有收到的幣別
+ $received += $n * $d['unit'];
+ }
+ }
+ if (empty($parts)) {
+ return null;
+ }
+ return '收:' . $received . '(' . implode('、', $parts) . ') 找:' . number_format($this->change_amount, 0);
+ }
+
/**
* 取得所有支付類型的對照標籤
*/
diff --git a/app/Services/Transaction/TransactionService.php b/app/Services/Transaction/TransactionService.php
index 3833cf6..f066534 100644
--- a/app/Services/Transaction/TransactionService.php
+++ b/app/Services/Transaction/TransactionService.php
@@ -71,6 +71,8 @@ class TransactionService
'discount_amount' => $data['discount_amount'] ?? 0,
'pay_amount' => $data['pay_amount'],
'change_amount' => $data['change_amount'] ?? 0,
+ // 現金面額明細(僅現金 payment_type=9 會帶;其餘為 null)
+ 'cash_detail' => $data['cash_detail'] ?? null,
'points_used' => $data['points_used'] ?? 0,
'original_amount' => $data['original_amount'] ?? $data['total_amount'],
'payment_type' => (int) ($data['payment_type'] ?? 0),
@@ -124,6 +126,8 @@ class TransactionService
'original_amount' => $data['original_amount'] ?? $order->original_amount,
'discount_amount' => $data['discount_amount'] ?? $order->discount_amount,
'change_amount' => $data['change_amount'] ?? $order->change_amount,
+ // 現金面額明細:completed finalize 才帶(pending 階段無投幣結果)
+ 'cash_detail' => $data['cash_detail'] ?? $order->cash_detail,
'points_used' => $data['points_used'] ?? $order->points_used,
'invoice_info' => $data['invoice_info'] ?? $order->invoice_info,
'delivery_status' => isset($data['delivery_status'])
diff --git a/database/migrations/2026_07_03_120000_add_cash_detail_to_orders_table.php b/database/migrations/2026_07_03_120000_add_cash_detail_to_orders_table.php
new file mode 100644
index 0000000..2ab4645
--- /dev/null
+++ b/database/migrations/2026_07_03_120000_add_cash_detail_to_orders_table.php
@@ -0,0 +1,35 @@
+json('cash_detail')->nullable()
+ ->comment('現金收款面額明細 {b1000,b500,b100,c50,c10,c5,c1}')
+ ->after('change_amount');
+ }
+ });
+ }
+
+ public function down(): void
+ {
+ Schema::table('orders', function (Blueprint $table) {
+ if (Schema::hasColumn('orders', 'cash_detail')) {
+ $table->dropColumn('cash_detail');
+ }
+ });
+ }
+};
diff --git a/resources/views/admin/sales/partials/tab-orders.blade.php b/resources/views/admin/sales/partials/tab-orders.blade.php
index d99a7c9..34fe611 100644
--- a/resources/views/admin/sales/partials/tab-orders.blade.php
+++ b/resources/views/admin/sales/partials/tab-orders.blade.php
@@ -285,6 +285,11 @@
{{ __('Discounted') }} ${{ number_format($order->discount_amount, 0) }}
@endif
+ @if($order->cash_received_summary)
+
+ {{ $order->cash_received_summary }}
+
+ @endif
@if((int) $order->payment_type === 6 && !empty($order->member_barcode))
{{-- 取貨碼交易:於支付金額欄換行加註取貨碼序號 --}}
@@ -468,6 +473,11 @@
{{
$paymentTypes[$order->payment_type] ?? '??' }}
+ @if($order->cash_received_summary)
+
+ {{ $order->cash_received_summary }}
+
+ @endif
@if($order->payment_type == 41 && $order->staffCardLog && $order->staffCardLog->staffCard)