diff --git a/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php b/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php index 56594ee..56904de 100644 --- a/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php +++ b/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php @@ -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'], ]; } diff --git a/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php b/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php index 61e2de9..4273062 100644 --- a/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php +++ b/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php @@ -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'], ]; } diff --git a/app/Services/ReceivingService.php b/app/Services/ReceivingService.php index ed005c6..6ecf6ff 100644 --- a/app/Services/ReceivingService.php +++ b/app/Services/ReceivingService.php @@ -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; diff --git a/app/Services/StockService.php b/app/Services/StockService.php index e3569cf..d980a30 100644 --- a/app/Services/StockService.php +++ b/app/Services/StockService.php @@ -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;