[FIX] 補貨單歸屬改以機台所綁公司為準

1. storeReplenishment / autoReplenishment 建單時 company_id 由「建立者」改為「機台的 company_id」
2. 修正系統管理員為他公司機台建單時 company_id 被寫成 null,導致篩選列選公司及租戶帳號登入皆看不到補貨單
3. 新增 migration 依機台回填既有舊單據的 company_id

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
sky121113 2026-06-23 15:27:31 +08:00
parent 9e9ed4bc9e
commit 62d3242db5
2 changed files with 37 additions and 6 deletions

View File

@ -877,7 +877,10 @@ class WarehouseController extends Controller
'items.*.quantity.min' => __('Please enter valid quantity for item :num', ['num' => ':position']), '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') . '-'; $prefix = 'RP-' . now()->format('Ymd') . '-';
$lastOrder = ReplenishmentOrder::withoutGlobalScopes() $lastOrder = ReplenishmentOrder::withoutGlobalScopes()
->withTrashed() ->withTrashed()
@ -889,10 +892,10 @@ class WarehouseController extends Controller
$order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT); $order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
$order = ReplenishmentOrder::create([ $order = ReplenishmentOrder::create([
'company_id' => Auth::user()->company_id, 'company_id' => $machine->company_id,
'order_no' => $order_no, 'order_no' => $order_no,
'warehouse_id' => $validated['warehouse_id'], 'warehouse_id' => $validated['warehouse_id'],
'machine_id' => $validated['machine_id'], 'machine_id' => $machine->id,
'status' => ReplenishmentOrder::STATUS_PENDING, 'status' => ReplenishmentOrder::STATUS_PENDING,
'note' => $validated['note'] ?? null, 'note' => $validated['note'] ?? null,
'created_by' => Auth::id(), 'created_by' => Auth::id(),
@ -1011,7 +1014,7 @@ class WarehouseController extends Controller
} }
// 有 items = 確認建單 // 有 items = 確認建單
DB::transaction(function () use ($validated) { DB::transaction(function () use ($validated, $machine) {
$prefix = 'RP-' . now()->format('Ymd') . '-'; $prefix = 'RP-' . now()->format('Ymd') . '-';
$lastOrder = ReplenishmentOrder::withoutGlobalScopes() $lastOrder = ReplenishmentOrder::withoutGlobalScopes()
->withTrashed() ->withTrashed()
@ -1023,10 +1026,10 @@ class WarehouseController extends Controller
$order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT); $order_no = $prefix . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
$order = ReplenishmentOrder::create([ $order = ReplenishmentOrder::create([
'company_id' => Auth::user()->company_id, 'company_id' => $machine->company_id,
'order_no' => $order_no, 'order_no' => $order_no,
'warehouse_id' => $validated['warehouse_id'], 'warehouse_id' => $validated['warehouse_id'],
'machine_id' => $validated['machine_id'], 'machine_id' => $machine->id,
'status' => ReplenishmentOrder::STATUS_PENDING, 'status' => ReplenishmentOrder::STATUS_PENDING,
'note' => $validated['note'] ?? __('Auto Replenishment'), 'note' => $validated['note'] ?? __('Auto Replenishment'),
'created_by' => Auth::id(), 'created_by' => Auth::id(),

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
/**
* 補貨單歸屬以「機台所綁公司」為準。
* 既有單據特別是系統管理員建立、company_id 寫成建立者 null 的)依機台回填,
* 否則篩選列選公司或租戶帳號登入皆看不到這些補貨單。
*/
return new class extends Migration
{
public function up(): void
{
// 以 machines.company_id 校正 replenishment_orders.company_id含 NULL 安全比較)
DB::statement(<<<'SQL'
UPDATE replenishment_orders AS ro
INNER JOIN machines AS m ON m.id = ro.machine_id
SET ro.company_id = m.company_id
WHERE NOT (ro.company_id <=> m.company_id)
SQL);
}
public function down(): void
{
// 資料校正,無法可靠還原
}
};