2025-12-30 15:03:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
namespace App\Modules\Inventory\Controllers;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
use App\Http\Controllers\Controller;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
use App\Modules\Inventory\Models\InventoryTransferOrder;
|
2026-01-26 10:37:47 +08:00
|
|
|
|
use App\Modules\Inventory\Models\Warehouse;
|
2026-02-02 09:34:24 +08:00
|
|
|
|
use App\Modules\Inventory\Models\Inventory;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
use App\Modules\Inventory\Services\TransferService;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
use Illuminate\Http\Request;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
use Inertia\Inertia;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
class TransferOrderController extends Controller
|
|
|
|
|
|
{
|
2026-01-28 18:04:45 +08:00
|
|
|
|
protected $transferService;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct(TransferService $transferService)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->transferService = $transferService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
$query = InventoryTransferOrder::query()
|
|
|
|
|
|
->with(['fromWarehouse', 'toWarehouse', 'createdBy', 'postedBy']);
|
|
|
|
|
|
|
|
|
|
|
|
// 篩選:若有選定倉庫,則顯示該倉庫作為來源或目的地的調撥單
|
|
|
|
|
|
if ($request->filled('warehouse_id')) {
|
|
|
|
|
|
$query->where(function ($q) use ($request) {
|
|
|
|
|
|
$q->where('from_warehouse_id', $request->warehouse_id)
|
|
|
|
|
|
->orWhere('to_warehouse_id', $request->warehouse_id);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 17:24:34 +08:00
|
|
|
|
$perPage = $request->input('per_page', 10);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
$orders = $query->orderByDesc('created_at')
|
2026-02-03 17:24:34 +08:00
|
|
|
|
->paginate($perPage)
|
|
|
|
|
|
->withQueryString()
|
2026-01-28 18:04:45 +08:00
|
|
|
|
->through(function ($order) {
|
|
|
|
|
|
return [
|
|
|
|
|
|
'id' => (string) $order->id,
|
|
|
|
|
|
'doc_no' => $order->doc_no,
|
|
|
|
|
|
'from_warehouse_name' => $order->fromWarehouse->name,
|
|
|
|
|
|
'to_warehouse_name' => $order->toWarehouse->name,
|
|
|
|
|
|
'status' => $order->status,
|
|
|
|
|
|
'created_at' => $order->created_at->format('Y-m-d H:i'),
|
|
|
|
|
|
'posted_at' => $order->posted_at ? $order->posted_at->format('Y-m-d H:i') : '-',
|
|
|
|
|
|
'created_by' => $order->createdBy?->name,
|
|
|
|
|
|
];
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return Inertia::render('Inventory/Transfer/Index', [
|
|
|
|
|
|
'orders' => $orders,
|
|
|
|
|
|
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
2026-02-03 17:24:34 +08:00
|
|
|
|
'filters' => $request->only(['warehouse_id', 'per_page']),
|
2026-01-28 18:04:45 +08:00
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
public function store(Request $request)
|
|
|
|
|
|
{
|
2026-02-02 09:27:02 +08:00
|
|
|
|
// 兼容前端不同的參數命名 (from/source, to/target)
|
|
|
|
|
|
$fromId = $request->input('from_warehouse_id') ?? $request->input('sourceWarehouseId');
|
|
|
|
|
|
$toId = $request->input('to_warehouse_id') ?? $request->input('targetWarehouseId');
|
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
$validated = $request->validate([
|
2026-02-02 09:27:02 +08:00
|
|
|
|
'from_warehouse_id' => 'required_without:sourceWarehouseId|exists:warehouses,id',
|
|
|
|
|
|
'to_warehouse_id' => 'required_without:targetWarehouseId|exists:warehouses,id|different:from_warehouse_id',
|
2026-01-28 18:04:45 +08:00
|
|
|
|
'remarks' => 'nullable|string',
|
2026-02-02 09:27:02 +08:00
|
|
|
|
'notes' => 'nullable|string',
|
|
|
|
|
|
'instant_post' => 'boolean',
|
|
|
|
|
|
// 支援單筆商品直接建立 (撥補單模式)
|
|
|
|
|
|
'product_id' => 'nullable|exists:products,id',
|
|
|
|
|
|
'quantity' => 'nullable|numeric|min:0.01',
|
|
|
|
|
|
'batch_number' => 'nullable|string',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
2026-02-02 09:27:02 +08:00
|
|
|
|
$remarks = $validated['remarks'] ?? $validated['notes'] ?? null;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
$order = $this->transferService->createOrder(
|
2026-02-02 09:27:02 +08:00
|
|
|
|
$fromId,
|
|
|
|
|
|
$toId,
|
|
|
|
|
|
$remarks,
|
2026-01-28 18:04:45 +08:00
|
|
|
|
auth()->id()
|
|
|
|
|
|
);
|
2026-02-04 13:24:33 +08:00
|
|
|
|
|
2026-02-02 09:27:02 +08:00
|
|
|
|
if ($request->input('instant_post') === true) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$this->transferService->post($order, auth()->id());
|
2026-02-04 13:24:33 +08:00
|
|
|
|
|
2026-02-02 09:27:02 +08:00
|
|
|
|
return redirect()->back()->with('success', '撥補成功,庫存已更新');
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
// 如果過帳失敗,雖然單據已建立,但應回報錯誤
|
|
|
|
|
|
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-28 18:04:45 +08:00
|
|
|
|
return redirect()->route('inventory.transfer.show', [$order->id])
|
|
|
|
|
|
->with('success', '已建立調撥單');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function show(InventoryTransferOrder $order)
|
|
|
|
|
|
{
|
|
|
|
|
|
$order->load(['items.product.baseUnit', 'fromWarehouse', 'toWarehouse', 'createdBy', 'postedBy']);
|
|
|
|
|
|
|
|
|
|
|
|
$orderData = [
|
|
|
|
|
|
'id' => (string) $order->id,
|
|
|
|
|
|
'doc_no' => $order->doc_no,
|
|
|
|
|
|
'from_warehouse_id' => (string) $order->from_warehouse_id,
|
|
|
|
|
|
'from_warehouse_name' => $order->fromWarehouse->name,
|
|
|
|
|
|
'to_warehouse_id' => (string) $order->to_warehouse_id,
|
|
|
|
|
|
'to_warehouse_name' => $order->toWarehouse->name,
|
2026-02-09 16:52:35 +08:00
|
|
|
|
'to_warehouse_type' => $order->toWarehouse->type->value, // 用於判斷是否為販賣機
|
2026-01-28 18:04:45 +08:00
|
|
|
|
'status' => $order->status,
|
|
|
|
|
|
'remarks' => $order->remarks,
|
|
|
|
|
|
'created_at' => $order->created_at->format('Y-m-d H:i'),
|
|
|
|
|
|
'created_by' => $order->createdBy?->name,
|
2026-02-02 09:34:24 +08:00
|
|
|
|
'items' => $order->items->map(function ($item) use ($order) {
|
|
|
|
|
|
// 獲取來源倉庫的當前庫存
|
|
|
|
|
|
$stock = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
|
|
|
|
|
->where('product_id', $item->product_id)
|
|
|
|
|
|
->where('batch_number', $item->batch_number)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
2026-01-28 18:04:45 +08:00
|
|
|
|
return [
|
|
|
|
|
|
'id' => (string) $item->id,
|
|
|
|
|
|
'product_id' => (string) $item->product_id,
|
|
|
|
|
|
'product_name' => $item->product->name,
|
|
|
|
|
|
'product_code' => $item->product->code,
|
|
|
|
|
|
'batch_number' => $item->batch_number,
|
2026-02-05 11:45:08 +08:00
|
|
|
|
'expiry_date' => $stock && $stock->expiry_date ? $stock->expiry_date->format('Y-m-d') : null,
|
2026-01-28 18:04:45 +08:00
|
|
|
|
'unit' => $item->product->baseUnit?->name,
|
|
|
|
|
|
'quantity' => (float) $item->quantity,
|
2026-02-09 16:52:35 +08:00
|
|
|
|
'position' => $item->position,
|
2026-02-05 09:33:36 +08:00
|
|
|
|
'max_quantity' => $item->snapshot_quantity ? (float) $item->snapshot_quantity : ($stock ? (float) $stock->quantity : 0.0),
|
2026-01-28 18:04:45 +08:00
|
|
|
|
'notes' => $item->notes,
|
|
|
|
|
|
];
|
|
|
|
|
|
}),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
return Inertia::render('Inventory/Transfer/Show', [
|
|
|
|
|
|
'order' => $orderData,
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, InventoryTransferOrder $order)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($order->status !== 'draft') {
|
|
|
|
|
|
return redirect()->back()->with('error', '只能修改草稿狀態的單據');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 16:52:35 +08:00
|
|
|
|
// 1. 先更新資料 (如果請求中包含 items,則先執行儲存)
|
2026-02-05 09:33:36 +08:00
|
|
|
|
$itemsChanged = false;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
if ($request->has('items')) {
|
2026-02-09 16:52:35 +08:00
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
|
'items' => 'array',
|
|
|
|
|
|
'items.*.product_id' => 'required|exists:products,id',
|
|
|
|
|
|
'items.*.quantity' => 'required|numeric|min:0.01',
|
|
|
|
|
|
'items.*.batch_number' => 'nullable|string',
|
|
|
|
|
|
'items.*.position' => 'nullable|string',
|
|
|
|
|
|
'items.*.notes' => 'nullable|string',
|
|
|
|
|
|
]);
|
2026-02-05 09:33:36 +08:00
|
|
|
|
$itemsChanged = $this->transferService->updateItems($order, $validated['items']);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 16:52:35 +08:00
|
|
|
|
$remarksChanged = false;
|
|
|
|
|
|
if ($request->has('remarks')) {
|
|
|
|
|
|
$remarksChanged = $order->remarks !== $request->input('remarks');
|
|
|
|
|
|
$order->remarks = $request->input('remarks');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 09:33:36 +08:00
|
|
|
|
if ($itemsChanged || $remarksChanged) {
|
|
|
|
|
|
// [IMPORTANT] 使用 touch() 確保即便只有品項異動,也會因為 updated_at 變更而觸發自動日誌
|
|
|
|
|
|
$order->touch();
|
|
|
|
|
|
$message = '儲存成功';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$message = '資料未變更';
|
|
|
|
|
|
}
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
2026-02-04 17:51:29 +08:00
|
|
|
|
// 2. 判斷是否需要過帳
|
|
|
|
|
|
if ($request->input('action') === 'post') {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$this->transferService->post($order, auth()->id());
|
|
|
|
|
|
return redirect()->route('inventory.transfer.index')
|
|
|
|
|
|
->with('success', '調撥單已過帳完成');
|
2026-02-09 16:52:35 +08:00
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
|
return redirect()->back()->withErrors($e->errors());
|
2026-02-04 17:51:29 +08:00
|
|
|
|
} catch (\Exception $e) {
|
2026-02-09 16:52:35 +08:00
|
|
|
|
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
|
2026-02-04 17:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-04 13:25:49 +08:00
|
|
|
|
|
2026-02-05 09:33:36 +08:00
|
|
|
|
return redirect()->back()->with('success', $message);
|
2026-01-28 18:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function destroy(InventoryTransferOrder $order)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($order->status !== 'draft') {
|
|
|
|
|
|
return redirect()->back()->with('error', '只能刪除草稿狀態的單據');
|
|
|
|
|
|
}
|
2026-02-04 13:25:49 +08:00
|
|
|
|
|
2026-01-28 18:04:45 +08:00
|
|
|
|
$order->items()->delete();
|
|
|
|
|
|
$order->delete();
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('inventory.transfer.index')
|
|
|
|
|
|
->with('success', '調撥單已刪除');
|
2025-12-30 15:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-28 18:04:45 +08:00
|
|
|
|
* 獲取特定倉庫的庫存列表 (API) - 保留給前端選擇商品用
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function getWarehouseInventories(Warehouse $warehouse)
|
|
|
|
|
|
{
|
|
|
|
|
|
$inventories = $warehouse->inventories()
|
2026-01-08 16:32:10 +08:00
|
|
|
|
->with(['product.baseUnit', 'product.category'])
|
2026-01-28 18:04:45 +08:00
|
|
|
|
->where('quantity', '>', 0)
|
2025-12-30 15:03:19 +08:00
|
|
|
|
->get()
|
|
|
|
|
|
->map(function ($inv) {
|
|
|
|
|
|
return [
|
2026-01-26 14:59:24 +08:00
|
|
|
|
'product_id' => (string) $inv->product_id,
|
|
|
|
|
|
'product_name' => $inv->product->name,
|
2026-02-06 16:36:14 +08:00
|
|
|
|
'product_code' => $inv->product->code,
|
|
|
|
|
|
'product_barcode' => $inv->product->barcode,
|
2026-01-26 14:59:24 +08:00
|
|
|
|
'batch_number' => $inv->batch_number,
|
|
|
|
|
|
'quantity' => (float) $inv->quantity,
|
2026-01-28 18:04:45 +08:00
|
|
|
|
'unit_cost' => (float) $inv->unit_cost,
|
2026-01-26 14:59:24 +08:00
|
|
|
|
'unit_name' => $inv->product->baseUnit?->name ?? '個',
|
|
|
|
|
|
'expiry_date' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
|
2025-12-30 15:03:19 +08:00
|
|
|
|
];
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return response()->json($inventories);
|
|
|
|
|
|
}
|
2026-02-09 16:52:35 +08:00
|
|
|
|
public function importItems(Request $request, InventoryTransferOrder $order)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($order->status !== 'draft') {
|
|
|
|
|
|
return redirect()->back()->with('error', '只能在草稿狀態下匯入明細');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
|
'file' => 'required|file|mimes:xlsx,xls,csv',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
\Maatwebsite\Excel\Facades\Excel::import(new \App\Modules\Inventory\Imports\InventoryTransferItemImport($order), $request->file('file'));
|
|
|
|
|
|
return redirect()->back()->with('success', '匯入成功');
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
return redirect()->back()->with('error', '匯入失敗:' . $e->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function template()
|
|
|
|
|
|
{
|
|
|
|
|
|
return \Maatwebsite\Excel\Facades\Excel::download(
|
|
|
|
|
|
new \App\Modules\Inventory\Exports\InventoryTransferTemplateExport(),
|
|
|
|
|
|
'調撥單明細匯入範本.xlsx'
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-12-30 15:03:19 +08:00
|
|
|
|
}
|
2026-02-09 16:52:35 +08:00
|
|
|
|
|