diff --git a/app/Http/Controllers/Admin/WarehouseController.php b/app/Http/Controllers/Admin/WarehouseController.php index 50aa134..101ab38 100644 --- a/app/Http/Controllers/Admin/WarehouseController.php +++ b/app/Http/Controllers/Admin/WarehouseController.php @@ -487,7 +487,18 @@ class WarehouseController extends Controller 'created_by' => Auth::id(), ]); + // 預先取得商品資訊以建立快照 + $productIds = collect($validated['items'])->pluck('product_id')->unique()->toArray(); + $products = \App\Models\Product\Product::whereIn('id', $productIds)->get()->keyBy('id'); + foreach ($validated['items'] as $item) { + $product = $products->get($item['product_id']); + if ($product) { + $item['snapshot_product_name'] = $product->name; + $item['snapshot_product_name_key'] = $product->name_dictionary_key; + $item['snapshot_product_barcode'] = $product->barcode; + $item['snapshot_product_image_url'] = $product->image_url; + } $order->items()->create($item); } }); @@ -798,9 +809,14 @@ class WarehouseController extends Controller 'created_by' => Auth::id(), ]); + // 預先取得商品資訊以建立快照 + $productIds = collect($validated['items'])->pluck('product_id')->unique()->toArray(); + $products = \App\Models\Product\Product::whereIn('id', $productIds)->get()->keyBy('id'); + // 批量寫入明細(遵循 DB Best Practice: 不在迴圈中逐筆 create) $itemsData = []; foreach ($validated['items'] as $item) { + $product = $products->get($item['product_id']); $itemsData[] = [ 'replenishment_order_id' => $order->id, 'product_id' => $item['product_id'], @@ -808,6 +824,10 @@ class WarehouseController extends Controller 'quantity' => $item['quantity'], 'current_stock' => $item['current_stock'] ?? 0, 'max_stock' => $item['max_stock'] ?? 0, + 'snapshot_product_name' => $product ? $product->name : null, + 'snapshot_product_name_key' => $product ? $product->name_dictionary_key : null, + 'snapshot_product_barcode' => $product ? $product->barcode : null, + 'snapshot_product_image_url' => $product ? $product->image_url : null, 'created_at' => now(), 'updated_at' => now(), ]; @@ -923,8 +943,13 @@ class WarehouseController extends Controller 'created_by' => Auth::id(), ]); + // 預先取得商品資訊以建立快照 + $productIds = collect($validated['items'])->pluck('product_id')->unique()->toArray(); + $products = \App\Models\Product\Product::whereIn('id', $productIds)->get()->keyBy('id'); + $itemsData = []; foreach ($validated['items'] as $item) { + $product = $products->get($item['product_id']); $itemsData[] = [ 'replenishment_order_id' => $order->id, 'product_id' => $item['product_id'], @@ -932,6 +957,10 @@ class WarehouseController extends Controller 'quantity' => $item['quantity'], 'current_stock' => $item['current_stock'] ?? 0, 'max_stock' => $item['max_stock'] ?? 0, + 'snapshot_product_name' => $product ? $product->name : null, + 'snapshot_product_name_key' => $product ? $product->name_dictionary_key : null, + 'snapshot_product_barcode' => $product ? $product->barcode : null, + 'snapshot_product_image_url' => $product ? $product->image_url : null, 'created_at' => now(), 'updated_at' => now(), ]; @@ -1205,14 +1234,14 @@ class WarehouseController extends Controller ->where('id', $id) ->firstOrFail(); - $items = ReplenishmentOrderItem::with('product.translations') + $items = ReplenishmentOrderItem::with(['translations', 'product.translations']) ->where('replenishment_order_id', $id) ->get() ->map(function ($item) { return [ 'product_id' => $item->product_id, - 'product_name' => $item->product?->localized_name ?? 'Unknown', - 'image_url' => $item->product?->image_url, + 'product_name' => $item->localized_name ?: __('Unknown'), + 'image_url' => $item->image_url, 'quantity' => $item->quantity, 'slot_no' => $item->slot_no, 'current_stock' => $item->current_stock, @@ -1376,14 +1405,14 @@ class WarehouseController extends Controller ->where('id', $id) ->firstOrFail(); - $items = \App\Models\Warehouse\TransferOrderItem::with('product.translations') + $items = \App\Models\Warehouse\TransferOrderItem::with(['translations', 'product.translations']) ->where('transfer_order_id', $id) ->get() ->map(function($item) { return [ 'product_id' => $item->product_id, - 'product_name' => $item->product?->localized_name ?? __('Unknown'), - 'image_url' => $item->product?->image_url, + 'product_name' => $item->localized_name ?: __('Unknown'), + 'image_url' => $item->image_url, 'quantity' => (int)$item->quantity, ]; }); diff --git a/app/Models/Warehouse/ReplenishmentOrderItem.php b/app/Models/Warehouse/ReplenishmentOrderItem.php index 13fb7ae..cd907ef 100644 --- a/app/Models/Warehouse/ReplenishmentOrderItem.php +++ b/app/Models/Warehouse/ReplenishmentOrderItem.php @@ -16,6 +16,10 @@ class ReplenishmentOrderItem extends Model 'quantity', 'current_stock', 'max_stock', + 'snapshot_product_name', + 'snapshot_product_name_key', + 'snapshot_product_barcode', + 'snapshot_product_image_url', ]; protected $casts = [ @@ -24,6 +28,11 @@ class ReplenishmentOrderItem extends Model 'max_stock' => 'integer', ]; + /** + * 自動附加到 JSON/陣列輸出的屬性 + */ + protected $appends = ['localized_name', 'barcode', 'image_url']; + /** * 所屬補貨單 */ @@ -33,10 +42,85 @@ class ReplenishmentOrderItem extends Model } /** - * 對應商品 + * 對應商品 (包含被軟刪除的商品以相容舊資料) */ public function product() { - return $this->belongsTo(\App\Models\Product\Product::class); + return $this->belongsTo(\App\Models\Product\Product::class)->withTrashed(); + } + + /** + * 取得快照商品名稱的字典翻譯 + */ + public function translations() + { + return $this->hasMany(\App\Models\System\Translation::class, 'key', 'snapshot_product_name_key') + ->where('group', 'product'); + } + + /** + * 取得商品名稱。 + * 回退順序:快照字典語系翻譯 → 快照字典 zh_TW → 快照名稱 → 關聯商品名稱 → 空值 + */ + public function getLocalizedNameAttribute(): string + { + if ($this->snapshot_product_name_key) { + if ($this->relationLoaded('translations') && $this->translations->isNotEmpty()) { + $locale = app()->getLocale(); + // 優先找當前語系 + $translation = $this->translations->firstWhere('locale', $locale); + if ($translation) { + return $translation->value; + } + // 回退至 zh_TW + $fallback = $this->translations->firstWhere('locale', 'zh_TW'); + if ($fallback) { + return $fallback->value; + } + } + } + + if ($this->snapshot_product_name) { + return $this->snapshot_product_name; + } + + if ($this->product) { + return $this->product->localized_name; + } + + return ''; + } + + /** + * 取得商品條碼。 + * 回退順序:快照條碼 → 關聯商品條碼 → 空值 + */ + public function getBarcodeAttribute(): string + { + if ($this->snapshot_product_barcode) { + return $this->snapshot_product_barcode; + } + + if ($this->product) { + return $this->product->barcode ?? ''; + } + + return ''; + } + + /** + * 取得商品圖片。 + * 回退順序:快照圖片 → 關聯商品圖片 → 空值 + */ + public function getImageUrlAttribute(): ?string + { + $url = $this->snapshot_product_image_url ?: ($this->product ? $this->product->image_url : null); + + // 防禦性過濾無效路徑或空值 + if (empty($url) || trim($url) === '' || in_array(rtrim($url, '/'), ['/storage', '/storage/products'])) { + return null; + } + + return $url; } } diff --git a/app/Models/Warehouse/TransferOrderItem.php b/app/Models/Warehouse/TransferOrderItem.php index f08d17d..1235ebf 100644 --- a/app/Models/Warehouse/TransferOrderItem.php +++ b/app/Models/Warehouse/TransferOrderItem.php @@ -13,12 +13,21 @@ class TransferOrderItem extends Model 'transfer_order_id', 'product_id', 'quantity', + 'snapshot_product_name', + 'snapshot_product_name_key', + 'snapshot_product_barcode', + 'snapshot_product_image_url', ]; protected $casts = [ 'quantity' => 'integer', ]; + /** + * 自動附加到 JSON/陣列輸出的屬性 + */ + protected $appends = ['localized_name', 'barcode', 'image_url']; + /** * 所屬調撥單 */ @@ -28,10 +37,85 @@ class TransferOrderItem extends Model } /** - * 對應商品 + * 對應商品 (包含被軟刪除的商品以相容舊資料) */ public function product() { - return $this->belongsTo(\App\Models\Product\Product::class); + return $this->belongsTo(\App\Models\Product\Product::class)->withTrashed(); + } + + /** + * 取得快照商品名稱的字典翻譯 + */ + public function translations() + { + return $this->hasMany(\App\Models\System\Translation::class, 'key', 'snapshot_product_name_key') + ->where('group', 'product'); + } + + /** + * 取得商品名稱。 + * 回退順序:快照字典語系翻譯 → 快照字典 zh_TW → 快照名稱 → 關聯商品名稱 → 空值 + */ + public function getLocalizedNameAttribute(): string + { + if ($this->snapshot_product_name_key) { + if ($this->relationLoaded('translations') && $this->translations->isNotEmpty()) { + $locale = app()->getLocale(); + // 優先找當前語系 + $translation = $this->translations->firstWhere('locale', $locale); + if ($translation) { + return $translation->value; + } + // 回退至 zh_TW + $fallback = $this->translations->firstWhere('locale', 'zh_TW'); + if ($fallback) { + return $fallback->value; + } + } + } + + if ($this->snapshot_product_name) { + return $this->snapshot_product_name; + } + + if ($this->product) { + return $this->product->localized_name; + } + + return ''; + } + + /** + * 取得商品條碼。 + * 回退順序:快照條碼 → 關聯商品條碼 → 空值 + */ + public function getBarcodeAttribute(): string + { + if ($this->snapshot_product_barcode) { + return $this->snapshot_product_barcode; + } + + if ($this->product) { + return $this->product->barcode ?? ''; + } + + return ''; + } + + /** + * 取得商品圖片。 + * 回退順序:快照圖片 → 關聯商品圖片 → 空值 + */ + public function getImageUrlAttribute(): ?string + { + $url = $this->snapshot_product_image_url ?: ($this->product ? $this->product->image_url : null); + + // 防禦性過濾無效路徑或空值 + if (empty($url) || trim($url) === '' || in_array(rtrim($url, '/'), ['/storage', '/storage/products'])) { + return null; + } + + return $url; } } diff --git a/database/migrations/2026_05_21_161722_add_snapshots_to_order_items_tables.php b/database/migrations/2026_05_21_161722_add_snapshots_to_order_items_tables.php new file mode 100644 index 0000000..b017cb8 --- /dev/null +++ b/database/migrations/2026_05_21_161722_add_snapshots_to_order_items_tables.php @@ -0,0 +1,52 @@ +string('snapshot_product_name')->nullable()->after('quantity'); + $table->string('snapshot_product_name_key')->nullable()->after('snapshot_product_name'); + $table->string('snapshot_product_barcode')->nullable()->after('snapshot_product_name_key'); + $table->string('snapshot_product_image_url')->nullable()->after('snapshot_product_barcode'); + }); + + Schema::table('replenishment_order_items', function (Blueprint $table) { + $table->string('snapshot_product_name')->nullable()->after('max_stock'); + $table->string('snapshot_product_name_key')->nullable()->after('snapshot_product_name'); + $table->string('snapshot_product_barcode')->nullable()->after('snapshot_product_name_key'); + $table->string('snapshot_product_image_url')->nullable()->after('snapshot_product_barcode'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('transfer_order_items', function (Blueprint $table) { + $table->dropColumn([ + 'snapshot_product_name', + 'snapshot_product_name_key', + 'snapshot_product_barcode', + 'snapshot_product_image_url' + ]); + }); + + Schema::table('replenishment_order_items', function (Blueprint $table) { + $table->dropColumn([ + 'snapshot_product_name', + 'snapshot_product_name_key', + 'snapshot_product_barcode', + 'snapshot_product_image_url' + ]); + }); + } +};