1. 新增員工卡 (staff_cards) 與員工卡感應日誌 (staff_card_logs) 模型與遷移檔。 2. 實作 StaffCardController 與相關管理介面,支援狀態切換與 CRUD。 3. 擴充 routes/web.php 加入員工卡管理路由。 4. 更新 api-docs.php 文檔,補全 B012 (Unified Sync) 的商品同步回應範例。 5. 實作 PaymentTypeSeeder 並整合至 DatabaseSeeder 以標準化支付類型。 6. 優化銷售中心訂單標籤與倉庫機台庫存顯示邏輯。 7. 側邊欄選單加入員工卡管理入口。 8. 重構 TransactionService 以支援員工卡工作階段 (session_token) 驗證。
221 lines
8.8 KiB
PHP
221 lines
8.8 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Transaction;
|
||
|
||
use App\Models\Transaction\Order;
|
||
use App\Models\Transaction\OrderItem;
|
||
use App\Models\Transaction\Invoice;
|
||
use App\Models\Transaction\DispenseRecord;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Models\Machine\Machine;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class TransactionService
|
||
{
|
||
/**
|
||
* Process a new transaction (B600).
|
||
*/
|
||
public function processTransaction(array $data): Order
|
||
{
|
||
$flowId = $data['flow_id'] ?? null;
|
||
|
||
if ($flowId) {
|
||
$existingOrder = Order::withoutGlobalScopes()
|
||
->where('flow_id', $flowId)
|
||
->first();
|
||
if ($existingOrder) {
|
||
return $existingOrder;
|
||
}
|
||
}
|
||
|
||
return DB::transaction(function () use ($data) {
|
||
$machine = Machine::where('serial_no', $data['serial_no'])->firstOrFail();
|
||
|
||
// Create Order
|
||
$order = Order::create([
|
||
'company_id' => $machine->company_id,
|
||
'flow_id' => $data['flow_id'] ?? null,
|
||
'order_no' => $data['order_no'] ?? $this->generateOrderNo(),
|
||
'machine_id' => $machine->id,
|
||
'member_id' => $data['member_id'] ?? null,
|
||
'total_amount' => $data['total_amount'],
|
||
'discount_amount' => $data['discount_amount'] ?? 0,
|
||
'pay_amount' => $data['pay_amount'],
|
||
'change_amount' => $data['change_amount'] ?? 0,
|
||
'points_used' => $data['points_used'] ?? 0,
|
||
'original_amount' => $data['original_amount'] ?? $data['total_amount'],
|
||
'payment_type' => (int)($data['payment_type'] ?? 0),
|
||
'payment_status' => $data['payment_status'] ?? 1,
|
||
'payment_request' => $data['payment_request'] ?? null,
|
||
'payment_response' => $data['payment_response'] ?? null,
|
||
'member_barcode' => $data['member_barcode'] ?? null,
|
||
'invoice_info' => $data['invoice_info'] ?? null,
|
||
'machine_time' => $data['machine_time'] ?? now(),
|
||
'payment_at' => now(),
|
||
'status' => 'completed',
|
||
'metadata' => $data['metadata'] ?? null,
|
||
]);
|
||
|
||
// Create Order Items
|
||
if (!empty($data['items'])) {
|
||
foreach ($data['items'] as $item) {
|
||
$productName = $item['product_name'] ?? null;
|
||
|
||
// 如果沒傳名稱,嘗試從資料庫抓取
|
||
if (empty($productName)) {
|
||
$product = \App\Models\Product\Product::find($item['product_id']);
|
||
$productName = $product?->name ?? 'Unknown';
|
||
}
|
||
|
||
$order->items()->create([
|
||
'product_id' => $item['product_id'],
|
||
'product_name' => $productName,
|
||
'barcode' => $item['barcode'] ?? null,
|
||
'price' => $item['price'],
|
||
'quantity' => $item['quantity'],
|
||
'subtotal' => $item['price'] * $item['quantity'],
|
||
]);
|
||
}
|
||
}
|
||
|
||
return $order;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Generate a unique order number.
|
||
*/
|
||
protected function generateOrderNo(): string
|
||
{
|
||
return 'ORD-' . now()->format('YmdHis') . '-' . strtoupper(bin2hex(random_bytes(3)));
|
||
}
|
||
|
||
/**
|
||
* Record Invoice (B601).
|
||
*/
|
||
public function recordInvoice(array $data): Invoice
|
||
{
|
||
return DB::transaction(function () use ($data) {
|
||
$machine = Machine::where('serial_no', $data['serial_no'])->firstOrFail();
|
||
|
||
$order = null;
|
||
if (!empty($data['flow_id'])) {
|
||
$order = Order::where('flow_id', $data['flow_id'])->first();
|
||
}
|
||
|
||
// 處理額外的發票資訊 (business_tax_id, carrier_type)
|
||
$metadata = $data['metadata'] ?? [];
|
||
if (isset($data['business_tax_id'])) $metadata['business_tax_id'] = $data['business_tax_id'];
|
||
if (isset($data['carrier_type'])) $metadata['carrier_type'] = $data['carrier_type'];
|
||
|
||
return Invoice::create([
|
||
'company_id' => $machine->company_id,
|
||
'order_id' => $order?->id ?? ($data['order_id'] ?? null),
|
||
'machine_id' => $machine->id,
|
||
'flow_id' => $data['flow_id'] ?? null,
|
||
'invoice_no' => $data['invoice_no'] ?? null,
|
||
'amount' => $data['amount'] ?? 0,
|
||
'carrier_id' => $data['carrier_id'] ?? null,
|
||
'invoice_date' => $data['invoice_date'] ?? null,
|
||
'machine_time' => $data['machine_time'] ?? null,
|
||
'random_number' => $data['random_number'] ?? ($data['random_no'] ?? null),
|
||
'love_code' => $data['love_code'] ?? null,
|
||
'rtn_code' => $data['rtn_code'] ?? null,
|
||
'rtn_msg' => $data['rtn_msg'] ?? null,
|
||
'metadata' => $metadata,
|
||
]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Record dispense result (B602).
|
||
*/
|
||
public function recordDispense(array $data): DispenseRecord
|
||
{
|
||
return DB::transaction(function () use ($data) {
|
||
$machine = Machine::where('serial_no', $data['serial_no'])->firstOrFail();
|
||
|
||
$order = null;
|
||
if (!empty($data['flow_id'])) {
|
||
$order = Order::where('flow_id', $data['flow_id'])->first();
|
||
}
|
||
|
||
return DispenseRecord::create([
|
||
'company_id' => $machine->company_id,
|
||
'order_id' => $order?->id ?? ($data['order_id'] ?? null),
|
||
'flow_id' => $data['flow_id'] ?? null,
|
||
'machine_id' => $machine->id,
|
||
'slot_no' => $data['slot_no'] ?? 'unknown',
|
||
'product_id' => $data['product_id'] ?? null,
|
||
'amount' => $data['amount'] ?? 0,
|
||
'remaining_stock' => $data['remaining_stock'] ?? null,
|
||
'dispense_status' => $data['dispense_status'] ?? 0,
|
||
'member_barcode' => $data['member_barcode'] ?? ($order?->member_barcode ?? null),
|
||
'machine_time' => $data['machine_time'] ?? now(),
|
||
'points_used' => $data['points_used'] ?? 0,
|
||
]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Finalize a complete transaction (Unified B600/B601/B602).
|
||
*/
|
||
public function finalizeTransaction(array $data): Order
|
||
{
|
||
$flowId = $data['order']['flow_id'] ?? null;
|
||
|
||
// 冪等性檢查:如果 flow_id 已經存在,直接回傳既有訂單
|
||
if ($flowId) {
|
||
$existingOrder = Order::withoutGlobalScopes()
|
||
->where('flow_id', $flowId)
|
||
->first();
|
||
if ($existingOrder) {
|
||
Log::info("Transaction already finalized, returning existing order", ['flow_id' => $flowId]);
|
||
return $existingOrder;
|
||
}
|
||
}
|
||
|
||
return DB::transaction(function () use ($data) {
|
||
$serialNo = $data['serial_no'];
|
||
|
||
// 1. Process Order (B600)
|
||
$orderData = $data['order'];
|
||
$orderData['serial_no'] = $serialNo;
|
||
|
||
// 確保提取 payment_type (可能在 root 或 order 內)
|
||
if (!isset($orderData['payment_type']) && isset($data['payment_type'])) {
|
||
$orderData['payment_type'] = $data['payment_type'];
|
||
}
|
||
$order = $this->processTransaction($orderData);
|
||
|
||
// 2. Record Invoice (B601) - Optional
|
||
if (!empty($data['invoice'])) {
|
||
$invoiceData = $data['invoice'];
|
||
$invoiceData['serial_no'] = $serialNo;
|
||
$invoiceData['flow_id'] = $order->flow_id;
|
||
$invoiceData['order_id'] = $order->id;
|
||
$this->recordInvoice($invoiceData);
|
||
}
|
||
|
||
// 3. Record Dispense Results (B602) - Optional/Multiple
|
||
if (!empty($data['dispense'])) {
|
||
$dispenseList = isset($data['dispense'][0]) ? $data['dispense'] : [$data['dispense']];
|
||
foreach ($dispenseList as $dispenseItem) {
|
||
$dispenseItem['serial_no'] = $serialNo;
|
||
$dispenseItem['flow_id'] = $order->flow_id;
|
||
$dispenseItem['order_id'] = $order->id;
|
||
|
||
// 自動繼承:如果出貨紀錄沒帶 Barcode,就用訂單的
|
||
if (empty($dispenseItem['member_barcode']) && !empty($order->member_barcode)) {
|
||
$dispenseItem['member_barcode'] = $order->member_barcode;
|
||
}
|
||
|
||
$this->recordDispense($dispenseItem);
|
||
}
|
||
}
|
||
|
||
return $order;
|
||
});
|
||
}
|
||
}
|