diff --git a/app/Http/Controllers/Admin/WarehouseController.php b/app/Http/Controllers/Admin/WarehouseController.php index 6b603f7..5c6411e 100644 --- a/app/Http/Controllers/Admin/WarehouseController.php +++ b/app/Http/Controllers/Admin/WarehouseController.php @@ -877,7 +877,10 @@ class WarehouseController extends Controller 'items.*.quantity.min' => __('Please enter valid quantity for item :num', ['num' => ':position']), ]); - DB::transaction(function () use ($validated) { + // 補貨針對機台,單據歸屬以「機台所綁公司」為準(系統管理員可為他公司機台建單) + $machine = Machine::findOrFail($validated['machine_id']); + + DB::transaction(function () use ($validated, $machine) { $prefix = 'RP-' . now()->format('Ymd') . '-'; $lastOrder = ReplenishmentOrder::withoutGlobalScopes() ->withTrashed() @@ -889,10 +892,10 @@ class WarehouseController extends Controller $order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT); $order = ReplenishmentOrder::create([ - 'company_id' => Auth::user()->company_id, + 'company_id' => $machine->company_id, 'order_no' => $order_no, 'warehouse_id' => $validated['warehouse_id'], - 'machine_id' => $validated['machine_id'], + 'machine_id' => $machine->id, 'status' => ReplenishmentOrder::STATUS_PENDING, 'note' => $validated['note'] ?? null, 'created_by' => Auth::id(), @@ -1011,7 +1014,7 @@ class WarehouseController extends Controller } // 有 items = 確認建單 - DB::transaction(function () use ($validated) { + DB::transaction(function () use ($validated, $machine) { $prefix = 'RP-' . now()->format('Ymd') . '-'; $lastOrder = ReplenishmentOrder::withoutGlobalScopes() ->withTrashed() @@ -1023,10 +1026,10 @@ class WarehouseController extends Controller $order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT); $order = ReplenishmentOrder::create([ - 'company_id' => Auth::user()->company_id, + 'company_id' => $machine->company_id, 'order_no' => $order_no, 'warehouse_id' => $validated['warehouse_id'], - 'machine_id' => $validated['machine_id'], + 'machine_id' => $machine->id, 'status' => ReplenishmentOrder::STATUS_PENDING, 'note' => $validated['note'] ?? __('Auto Replenishment'), 'created_by' => Auth::id(), diff --git a/database/migrations/2026_06_23_120000_backfill_replenishment_orders_company_by_machine.php b/database/migrations/2026_06_23_120000_backfill_replenishment_orders_company_by_machine.php new file mode 100644 index 0000000..f49e5bf --- /dev/null +++ b/database/migrations/2026_06_23_120000_backfill_replenishment_orders_company_by_machine.php @@ -0,0 +1,28 @@ + m.company_id) + SQL); + } + + public function down(): void + { + // 資料校正,無法可靠還原 + } +};