fix(WEB): 입고관리 저장 오류 3건 수정

- FormRequest에 manufacturer, material_no 규칙 추가 (validated()에서 누락 방지)
- store() 시 lot_no 자동 생성 (generateLotNo() 폴백)
- getOrCreateStock()에서 item_code 기반 2차 검색 추가 (unique key 충돌 방지)
  - 동일 item_code, 다른 item_id인 Stock 존재 시 item_id 업데이트
  - SoftDeletes 복원 로직 포함

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-22 03:50:24 +09:00
parent 16b8dbcc6f
commit 7eb5825d41
4 changed files with 32 additions and 5 deletions

View File

@@ -29,6 +29,8 @@ public function rules(): array
'lot_no' => ['nullable', 'string', 'max:50'],
'status' => ['nullable', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending'],
'remark' => ['nullable', 'string', 'max:1000'],
'manufacturer' => ['nullable', 'string', 'max:100'],
'material_no' => ['nullable', 'string', 'max:50'],
];
}

View File

@@ -31,6 +31,8 @@ public function rules(): array
'inspection_status' => ['nullable', 'string', 'max:10'],
'inspection_date' => ['nullable', 'date'],
'inspection_result' => ['nullable', 'string', 'max:20'],
'manufacturer' => ['nullable', 'string', 'max:100'],
'material_no' => ['nullable', 'string', 'max:50'],
];
}

View File

@@ -199,7 +199,7 @@ public function store(array $data): Receiving
$receiving->due_date = $data['due_date'] ?? null;
$receiving->receiving_qty = $data['receiving_qty'] ?? null;
$receiving->receiving_date = $data['receiving_date'] ?? null;
$receiving->lot_no = $data['lot_no'] ?? null;
$receiving->lot_no = $data['lot_no'] ?? $this->generateLotNo();
$receiving->status = $data['status'] ?? 'receiving_pending';
$receiving->remark = $data['remark'] ?? null;

View File

@@ -525,18 +525,41 @@ public function getOrCreateStock(int $itemId, ?Receiving $receiving = null): Sto
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$stock = Stock::where('tenant_id', $tenantId)
$item = Item::where('tenant_id', $tenantId)
->findOrFail($itemId);
// 1차: item_id로 조회 (SoftDeletes 포함)
$stock = Stock::withTrashed()
->where('tenant_id', $tenantId)
->where('item_id', $itemId)
->first();
// 2차: item_code로 조회 (unique key 기준, item_id가 다를 수 있음)
if (! $stock) {
$stock = Stock::withTrashed()
->where('tenant_id', $tenantId)
->where('item_code', $item->code)
->first();
// item_id가 변경된 경우 업데이트
if ($stock && $stock->item_id !== $itemId) {
$stock->item_id = $itemId;
}
}
if ($stock) {
if ($stock->trashed()) {
$stock->restore();
$stock->status = 'out';
}
$stock->item_name = $item->name;
$stock->updated_by = $userId;
$stock->save();
return $stock;
}
// Stock이 없으면 새로 생성
$item = Item::where('tenant_id', $tenantId)
->findOrFail($itemId);
$stock = new Stock;
$stock->tenant_id = $tenantId;
$stock->item_id = $itemId;