1. 新增發票狀態機(pending/issued/failed/void)與冪等鍵 relate_number,並補上 last_checked_at、retry_count、voided_at、void_reason 欄位(migration 全欄位 nullable/有預設,對既有資料零影響並回填既有狀態)。 2. finalize 帶入發票輸入時改為先建 pending 發票,commit 後派發 IssueInvoiceJob 非同步向綠界開立,避免在交易內等待 ECPay 造成長交易。 3. 新增 EcpayInvoiceService 封裝綠界 B2C API(GetIssue 查詢、Issue 補開、Invalid 作廢),金鑰仍取自各機台 payment_configs。 4. 新增 invoices:reconcile 排程指令,每 5 分鐘對 pending 發票以 RelateNumber 向綠界 GetIssue 查證,補登 issued 或標記 failed 待補開。 5. SalesController 新增 reconcileInvoice/reissueInvoice/voidInvoice 三個後台操作端點與對應路由。 6. 發票列表改以 created_at 篩選(pending/failed 尚無 invoice_date 也能顯示),新增狀態過濾器與查詢/補開/作廢操作按鈕,狀態徽章改以狀態機顯示。 7. recordInvoice 改以 flow_id updateOrCreate 收斂,避免重送產生重複發票列;查詢機台/訂單時加上 withoutGlobalScopes。 8. 同步新增相關三語系字串並加入 ecpay_invoice base_url 設定。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
473 lines
21 KiB
PHP
473 lines
21 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 App\Models\StaffCardLog;
|
||
use App\Models\Transaction\PickupCode;
|
||
use App\Models\Transaction\PickupCodeLog;
|
||
use App\Models\Transaction\PassCodeLog;
|
||
use App\Models\Transaction\WelcomeGift;
|
||
use App\Models\Transaction\WelcomeGiftLog;
|
||
use Illuminate\Support\Facades\Log;
|
||
use App\Services\Machine\MachineService;
|
||
|
||
|
||
class TransactionService
|
||
{
|
||
protected $machineService;
|
||
|
||
public function __construct(MachineService $machineService)
|
||
{
|
||
$this->machineService = $machineService;
|
||
}
|
||
|
||
/**
|
||
* 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,
|
||
'code_id' => $data['code_id'] ?? null,
|
||
'welcome_gift_id' => $data['welcome_gift_id'] ?? null,
|
||
'invoice_info' => $data['invoice_info'] ?? null,
|
||
'machine_time' => $data['machine_time'] ?? now(),
|
||
'payment_at' => now(),
|
||
'status' => 'completed',
|
||
'metadata' => $data['metadata'] ?? null,
|
||
'delivery_status' => (int) ($data['delivery_status'] ?? Order::DELIVERY_STATUS_SUCCESS),
|
||
]);
|
||
|
||
// 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::withoutGlobalScopes()->where('serial_no', $data['serial_no'])->firstOrFail();
|
||
|
||
$order = null;
|
||
if (!empty($data['flow_id'])) {
|
||
$order = Order::withoutGlobalScopes()->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'];
|
||
|
||
$invoiceNo = $data['invoice_no'] ?? null;
|
||
$rtnCode = $data['rtn_code'] ?? null;
|
||
|
||
// 狀態:優先採用 App 已判定的 status(新 finalize 流程一定帶 status=pending);否則才推導。
|
||
// 此推導分支目前僅 MQTT 'invoice' 指令(ProcessInvoice)會走,App 暫未使用,屬防禦性。
|
||
// 重點:有發票號就代表已開立,除非「明確」收到失敗碼才算 failed
|
||
//(部分上報格式只帶 invoice_no、不帶 rtn_code,不可因缺 rtn_code 就誤判失敗)。
|
||
// 綠界 RtnCode == "1" 為成功;其餘非空值才視為明確失敗。
|
||
$status = $data['status'] ?? null;
|
||
if (empty($status)) {
|
||
$explicitFailure = !empty($rtnCode) && (string) $rtnCode !== '1';
|
||
if (!empty($invoiceNo)) {
|
||
$status = $explicitFailure ? 'failed' : 'issued';
|
||
} else {
|
||
// 無發票號:明確失敗→failed;否則待開立/待查證→pending
|
||
$status = $explicitFailure ? 'failed' : 'pending';
|
||
}
|
||
}
|
||
|
||
// RelateNumber 冪等鍵:App 未帶時由後台以 serial + flow_id 推導(綠界上限 30 字)
|
||
$relateNumber = $data['relate_number'] ?? null;
|
||
if (empty($relateNumber) && !empty($data['flow_id'])) {
|
||
$relateNumber = substr($machine->serial_no . $data['flow_id'], 0, 30);
|
||
}
|
||
|
||
$attributes = [
|
||
'company_id' => $machine->company_id,
|
||
'order_id' => $order?->id ?? ($data['order_id'] ?? null),
|
||
'machine_id' => $machine->id,
|
||
'invoice_no' => $invoiceNo,
|
||
'status' => $status,
|
||
'relate_number' => $relateNumber,
|
||
'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' => $rtnCode,
|
||
'rtn_msg' => $data['rtn_msg'] ?? null,
|
||
'metadata' => $metadata,
|
||
];
|
||
|
||
// 去重:同一 flow_id(一張訂單一張發票)以 updateOrCreate 收斂,
|
||
// 避免 finalize 重送 / 補開回寫產生重複發票列。
|
||
if (!empty($data['flow_id'])) {
|
||
// 終態守衛(防禦縱深):已開立/已作廢的發票為終態,不得被後續寫入覆蓋,
|
||
// 避免任何重送(含 ProcessInvoice/MQTT invoice 指令)把 issued 打回 pending、清掉發票號。
|
||
$existing = Invoice::withoutGlobalScopes()->where('flow_id', $data['flow_id'])->first();
|
||
if ($existing && in_array($existing->status, [Invoice::STATUS_ISSUED, Invoice::STATUS_VOID], true)) {
|
||
return $existing;
|
||
}
|
||
return Invoice::withoutGlobalScopes()->updateOrCreate(
|
||
['flow_id' => $data['flow_id']],
|
||
$attributes
|
||
);
|
||
}
|
||
|
||
return Invoice::create(array_merge($attributes, ['flow_id' => null]));
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Record dispense result (B602).
|
||
*/
|
||
public function recordDispense(array $data): DispenseRecord
|
||
{
|
||
return DB::transaction(function () use ($data) {
|
||
// Use withoutGlobalScopes for background job safety
|
||
$machine = Machine::withoutGlobalScopes()->where('serial_no', $data['serial_no'])->firstOrFail();
|
||
|
||
$order = null;
|
||
if (!empty($data['flow_id'])) {
|
||
$order = Order::withoutGlobalScopes()->where('flow_id', $data['flow_id'])->first();
|
||
}
|
||
|
||
// Find slot with robust matching (handle "1" vs "01")
|
||
$slotNo = $data['slot_no'] ?? null;
|
||
$slot = null;
|
||
if ($slotNo) {
|
||
$slot = $machine->slots()->where('slot_no', (string) $slotNo)->first();
|
||
if (!$slot) {
|
||
$normalized = ltrim((string) $slotNo, '0');
|
||
if ($normalized === '')
|
||
$normalized = '0';
|
||
$slot = $machine->slots()->get()->first(function ($s) use ($normalized) {
|
||
$sn = ltrim($s->slot_no, '0');
|
||
if ($sn === '')
|
||
$sn = '0';
|
||
return $sn === $normalized;
|
||
});
|
||
}
|
||
}
|
||
|
||
$record = 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' => $slotNo ?? 'unknown',
|
||
'product_id' => ($data['product_id'] ?? 0) > 0 ? $data['product_id'] : ($slot ? $slot->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,
|
||
]);
|
||
|
||
// 執行實體庫存扣除 (真實交易化)
|
||
if ((int) ($data['dispense_status'] ?? 0) === 1) {
|
||
if ($slot) {
|
||
$slot->decrement('stock');
|
||
|
||
$type = \App\Models\Machine\MachineStockMovement::TYPE_SALE;
|
||
|
||
$this->machineService->recordStockMovement(
|
||
$slot,
|
||
-1,
|
||
$type,
|
||
$order ?? $record,
|
||
$order ? "Sale Order: #{$order->order_no}" : "Dispense Record: #{$record->id}",
|
||
['order_id' => $order?->id, 'dispense_id' => $record->id]
|
||
);
|
||
} else {
|
||
// Log warning if slot matching fails to help diagnostic
|
||
\Log::warning("Dispense record created but slot not found for decrement", [
|
||
'machine_id' => $machine->id,
|
||
'slot_no' => $slotNo
|
||
]);
|
||
}
|
||
}
|
||
|
||
return $record;
|
||
});
|
||
}
|
||
|
||
|
||
/**
|
||
* Finalize a complete transaction (Unified B600/B601/B602).
|
||
*/
|
||
public function finalizeTransaction(array $data): Order
|
||
{
|
||
// 優先從 Root 讀取,再從 order 內讀取
|
||
$flowId = $data['flow_id'] ?? ($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;
|
||
}
|
||
} else {
|
||
throw new \Exception("Flow ID is required for finalization");
|
||
}
|
||
|
||
return DB::transaction(function () use ($data, $flowId) {
|
||
$serialNo = $data['serial_no'];
|
||
|
||
// 1. Process Order (B600)
|
||
$orderData = $data['order'];
|
||
$orderData['serial_no'] = $serialNo;
|
||
|
||
// 確保提取 payment_type 與 flow_id (可能在 root 或 order 內)
|
||
$orderData['flow_id'] = $flowId;
|
||
if (!isset($orderData['delivery_status']) && !empty($data['dispense'])) {
|
||
$orderData['delivery_status'] = $this->resolveDeliveryStatusFromDispense($data['dispense']);
|
||
}
|
||
if (!isset($orderData['payment_type']) && isset($data['payment_type'])) {
|
||
$orderData['payment_type'] = $data['payment_type'];
|
||
}
|
||
$order = $this->processTransaction($orderData);
|
||
|
||
// 2. Record Invoice — 後台開立版:finalize 帶 invoice 輸入(status=pending)時建 pending 發票,
|
||
// commit 後派 IssueInvoiceJob 去綠界開立(不在交易內等 ECPay,避免長交易)。
|
||
if (!empty($data['invoice'])) {
|
||
$invoiceData = $data['invoice'];
|
||
$invoiceData['serial_no'] = $serialNo;
|
||
$invoiceData['flow_id'] = $order->flow_id;
|
||
$invoiceData['order_id'] = $order->id;
|
||
$invoice = $this->recordInvoice($invoiceData);
|
||
|
||
// 待開立 → commit 後非同步開立(已開/失敗則不重複觸發)
|
||
if ($invoice->status === Invoice::STATUS_PENDING) {
|
||
DB::afterCommit(function () use ($invoice) {
|
||
\App\Jobs\Transaction\IssueInvoiceJob::dispatch($invoice->id);
|
||
});
|
||
}
|
||
}
|
||
|
||
// 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);
|
||
}
|
||
}
|
||
|
||
// 4. 閉環勾稽 (Closed Loop Check)
|
||
$codeId = $data['order']['code_id'] ?? ($data['code_id'] ?? null);
|
||
$paymentType = (int) $order->payment_type;
|
||
|
||
if ($codeId) {
|
||
switch ($paymentType) {
|
||
case 41: // 員工卡 (Staff Card)
|
||
// 建立員工卡核銷日誌
|
||
StaffCardLog::create([
|
||
'company_id' => $order->company_id,
|
||
'machine_id' => $order->machine_id,
|
||
'staff_card_id' => $codeId,
|
||
'payment_type' => 41,
|
||
'code_id' => $codeId,
|
||
'order_id' => $order->id,
|
||
'action' => 'consume',
|
||
'remark' => "MQTT 交易完成自動核銷,訂單: #{$order->id}"
|
||
]);
|
||
break;
|
||
|
||
case 6: // 取貨碼 (Pickup Code)
|
||
$pickupCode = PickupCode::where('machine_id', $order->machine_id)
|
||
->where('id', $codeId)
|
||
->where('status', 'active')
|
||
->first();
|
||
if ($pickupCode) {
|
||
$newCount = $pickupCode->usage_count + 1;
|
||
$isUsedUp = $newCount >= $pickupCode->usage_limit;
|
||
|
||
$pickupCode->update([
|
||
'order_id' => $order->id,
|
||
'usage_count' => $newCount,
|
||
'status' => $isUsedUp ? 'used' : 'active',
|
||
'used_at' => $isUsedUp ? now() : $pickupCode->used_at
|
||
]);
|
||
|
||
// 建立核銷日誌 (閉環最後一哩路)
|
||
PickupCodeLog::create([
|
||
'company_id' => $order->company_id,
|
||
'machine_id' => $order->machine_id,
|
||
'pickup_code_id' => $pickupCode->id,
|
||
'order_id' => $order->id,
|
||
'action' => $isUsedUp ? 'used' : 'consume',
|
||
'remark' => "MQTT 交易完成自動核銷 (" . ($newCount) . "/" . $pickupCode->usage_limit . "),訂單: #{$order->id}",
|
||
'raw_data' => [
|
||
'slot' => $pickupCode->slot_no,
|
||
'code' => $pickupCode->code,
|
||
'count' => $newCount,
|
||
'limit' => $pickupCode->usage_limit
|
||
]
|
||
]);
|
||
}
|
||
break;
|
||
|
||
case 5: // 通行碼 (Pass Code)
|
||
// 建立通行碼核銷日誌
|
||
PassCodeLog::create([
|
||
'company_id' => $order->company_id,
|
||
'machine_id' => $order->machine_id,
|
||
'pass_code_id' => $codeId,
|
||
'order_id' => $order->id,
|
||
'action' => 'completed',
|
||
'remark' => "MQTT 交易完成自動紀錄取貨,訂單: #{$order->id}"
|
||
]);
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 處理來店禮 (Welcome Gift) 自動核銷
|
||
$welcomeGiftId = $data['order']['welcome_gift_id'] ?? ($data['welcome_gift_id'] ?? null);
|
||
if ($welcomeGiftId) {
|
||
$welcomeGift = WelcomeGift::where('machine_id', $order->machine_id)
|
||
->where('id', $welcomeGiftId)
|
||
->where('status', 'active')
|
||
->first();
|
||
if ($welcomeGift) {
|
||
$newCount = $welcomeGift->usage_count + 1;
|
||
$isUsedUp = ($welcomeGift->usage_type === 'once' && $newCount >= ($welcomeGift->usage_limit ?? 1));
|
||
|
||
$welcomeGift->update([
|
||
'usage_count' => $newCount,
|
||
'status' => $isUsedUp ? 'used' : 'active',
|
||
]);
|
||
|
||
WelcomeGiftLog::create([
|
||
'company_id' => $order->company_id,
|
||
'machine_id' => $order->machine_id,
|
||
'welcome_gift_id' => $welcomeGift->id,
|
||
'order_id' => $order->id,
|
||
'action' => 'consume',
|
||
'remark' => "MQTT 交易完成自動核銷,訂單: #{$order->id}",
|
||
'raw_data' => [
|
||
'code' => $welcomeGift->code,
|
||
'count' => $newCount,
|
||
'type' => $welcomeGift->usage_type
|
||
]
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
return $order;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Resolve order-level delivery status from item-level dispense results.
|
||
*/
|
||
protected function resolveDeliveryStatusFromDispense(array $dispense): int
|
||
{
|
||
$dispenseList = isset($dispense[0]) ? $dispense : [$dispense];
|
||
$statuses = collect($dispenseList)
|
||
->filter(fn ($item) => is_array($item) && array_key_exists('dispense_status', $item))
|
||
->map(fn ($item) => (int) $item['dispense_status']);
|
||
|
||
if ($statuses->isEmpty()) {
|
||
return Order::DELIVERY_STATUS_SUCCESS;
|
||
}
|
||
|
||
$successCount = $statuses
|
||
->filter(fn ($status) => $status === Order::DELIVERY_STATUS_SUCCESS)
|
||
->count();
|
||
|
||
if ($successCount === $statuses->count()) {
|
||
return Order::DELIVERY_STATUS_SUCCESS;
|
||
}
|
||
|
||
if ($successCount === 0) {
|
||
return Order::DELIVERY_STATUS_FAILED;
|
||
}
|
||
|
||
return Order::DELIVERY_STATUS_PARTIAL;
|
||
}
|
||
}
|