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

This commit is contained in:
sky121113 2026-06-23 15:34:22 +08:00
commit 0da3887aa6
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']),
]);
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(),

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
{
// 資料校正,無法可靠還原
}
};