From 43ccd1e6e0e48834ab06db41817da3c6a6a7bb1d Mon Sep 17 00:00:00 2001 From: kent Date: Fri, 26 Dec 2025 15:45:33 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20H-1=20=EC=9E=85=EA=B3=A0=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ReceivingController: CRUD 및 목록 조회 API - ReceivingService: 입고 비즈니스 로직 - Receiving 모델: 다중 테넌트 지원 - FormRequest 검증 클래스 - Swagger 문서화 - receivings 테이블 마이그레이션 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../Api/V1/ReceivingController.php | 99 +++++ .../V1/Receiving/ProcessReceivingRequest.php | 36 ++ .../V1/Receiving/StoreReceivingRequest.php | 43 +++ .../V1/Receiving/UpdateReceivingRequest.php | 38 ++ app/Models/Tenants/Receiving.php | 107 ++++++ app/Services/ReceivingService.php | 298 +++++++++++++++ app/Swagger/v1/ReceivingApi.php | 360 ++++++++++++++++++ ...5_12_26_131150_create_receivings_table.php | 58 +++ 8 files changed, 1039 insertions(+) create mode 100644 app/Http/Controllers/Api/V1/ReceivingController.php create mode 100644 app/Http/Requests/V1/Receiving/ProcessReceivingRequest.php create mode 100644 app/Http/Requests/V1/Receiving/StoreReceivingRequest.php create mode 100644 app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php create mode 100644 app/Models/Tenants/Receiving.php create mode 100644 app/Services/ReceivingService.php create mode 100644 app/Swagger/v1/ReceivingApi.php create mode 100644 database/migrations/2025_12_26_131150_create_receivings_table.php diff --git a/app/Http/Controllers/Api/V1/ReceivingController.php b/app/Http/Controllers/Api/V1/ReceivingController.php new file mode 100644 index 0000000..a5d2e33 --- /dev/null +++ b/app/Http/Controllers/Api/V1/ReceivingController.php @@ -0,0 +1,99 @@ +only([ + 'search', + 'status', + 'start_date', + 'end_date', + 'sort_by', + 'sort_dir', + 'per_page', + 'page', + ]); + + $receivings = $this->service->index($params); + + return ApiResponse::success($receivings, __('message.fetched')); + } + + /** + * 입고 통계 + */ + public function stats() + { + $stats = $this->service->stats(); + + return ApiResponse::success($stats, __('message.fetched')); + } + + /** + * 입고 등록 + */ + public function store(StoreReceivingRequest $request) + { + $receiving = $this->service->store($request->validated()); + + return ApiResponse::success($receiving, __('message.created'), [], 201); + } + + /** + * 입고 상세 + */ + public function show(int $id) + { + $receiving = $this->service->show($id); + + return ApiResponse::success($receiving, __('message.fetched')); + } + + /** + * 입고 수정 + */ + public function update(int $id, UpdateReceivingRequest $request) + { + $receiving = $this->service->update($id, $request->validated()); + + return ApiResponse::success($receiving, __('message.updated')); + } + + /** + * 입고 삭제 + */ + public function destroy(int $id) + { + $this->service->destroy($id); + + return ApiResponse::success(null, __('message.deleted')); + } + + /** + * 입고처리 (상태 변경 + 입고 정보 입력) + */ + public function process(int $id, ProcessReceivingRequest $request) + { + $receiving = $this->service->process($id, $request->validated()); + + return ApiResponse::success($receiving, __('message.receiving.processed')); + } +} diff --git a/app/Http/Requests/V1/Receiving/ProcessReceivingRequest.php b/app/Http/Requests/V1/Receiving/ProcessReceivingRequest.php new file mode 100644 index 0000000..582e381 --- /dev/null +++ b/app/Http/Requests/V1/Receiving/ProcessReceivingRequest.php @@ -0,0 +1,36 @@ + ['required', 'numeric', 'min:0'], + 'receiving_date' => ['nullable', 'date'], + 'lot_no' => ['nullable', 'string', 'max:50'], + 'supplier_lot' => ['nullable', 'string', 'max:50'], + 'receiving_location' => ['required', 'string', 'max:100'], + 'receiving_manager' => ['nullable', 'string', 'max:50'], + 'remark' => ['nullable', 'string', 'max:1000'], + ]; + } + + public function messages(): array + { + return [ + 'receiving_qty.required' => __('validation.required', ['attribute' => '입고수량']), + 'receiving_qty.numeric' => __('validation.numeric', ['attribute' => '입고수량']), + 'receiving_qty.min' => __('validation.min.numeric', ['attribute' => '입고수량', 'min' => 0]), + 'receiving_location.required' => __('validation.required', ['attribute' => '입고위치']), + ]; + } +} diff --git a/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php b/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php new file mode 100644 index 0000000..e7a9c05 --- /dev/null +++ b/app/Http/Requests/V1/Receiving/StoreReceivingRequest.php @@ -0,0 +1,43 @@ + ['nullable', 'string', 'max:30'], + 'order_date' => ['nullable', 'date'], + 'item_id' => ['nullable', 'integer'], + 'item_code' => ['required', 'string', 'max:50'], + 'item_name' => ['required', 'string', 'max:200'], + 'specification' => ['nullable', 'string', 'max:200'], + 'supplier' => ['required', 'string', 'max:100'], + 'order_qty' => ['required', 'numeric', 'min:0'], + 'order_unit' => ['nullable', 'string', 'max:20'], + 'due_date' => ['nullable', 'date'], + 'status' => ['nullable', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending'], + 'remark' => ['nullable', 'string', 'max:1000'], + ]; + } + + public function messages(): array + { + return [ + 'item_code.required' => __('validation.required', ['attribute' => '품목코드']), + 'item_name.required' => __('validation.required', ['attribute' => '품목명']), + 'supplier.required' => __('validation.required', ['attribute' => '공급업체']), + 'order_qty.required' => __('validation.required', ['attribute' => '발주수량']), + 'order_qty.numeric' => __('validation.numeric', ['attribute' => '발주수량']), + 'order_qty.min' => __('validation.min.numeric', ['attribute' => '발주수량', 'min' => 0]), + ]; + } +} diff --git a/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php b/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php new file mode 100644 index 0000000..1d73d79 --- /dev/null +++ b/app/Http/Requests/V1/Receiving/UpdateReceivingRequest.php @@ -0,0 +1,38 @@ + ['nullable', 'string', 'max:30'], + 'order_date' => ['nullable', 'date'], + 'item_code' => ['sometimes', 'string', 'max:50'], + 'item_name' => ['sometimes', 'string', 'max:200'], + 'specification' => ['nullable', 'string', 'max:200'], + 'supplier' => ['sometimes', 'string', 'max:100'], + 'order_qty' => ['sometimes', 'numeric', 'min:0'], + 'order_unit' => ['nullable', 'string', 'max:20'], + 'due_date' => ['nullable', 'date'], + 'status' => ['sometimes', 'string', 'in:order_completed,shipping,inspection_pending,receiving_pending'], + 'remark' => ['nullable', 'string', 'max:1000'], + ]; + } + + public function messages(): array + { + return [ + 'order_qty.numeric' => __('validation.numeric', ['attribute' => '발주수량']), + 'order_qty.min' => __('validation.min.numeric', ['attribute' => '발주수량', 'min' => 0]), + ]; + } +} diff --git a/app/Models/Tenants/Receiving.php b/app/Models/Tenants/Receiving.php new file mode 100644 index 0000000..e370ba1 --- /dev/null +++ b/app/Models/Tenants/Receiving.php @@ -0,0 +1,107 @@ + 'date', + 'due_date' => 'date', + 'receiving_date' => 'date', + 'order_qty' => 'decimal:2', + 'receiving_qty' => 'decimal:2', + 'item_id' => 'integer', + ]; + + /** + * 상태 목록 + */ + public const STATUSES = [ + 'order_completed' => '발주완료', + 'shipping' => '배송중', + 'inspection_pending' => '검사대기', + 'receiving_pending' => '입고대기', + 'completed' => '입고완료', + ]; + + /** + * 품목 관계 + */ + public function item(): BelongsTo + { + return $this->belongsTo(\App\Models\Items\Item::class); + } + + /** + * 생성자 관계 + */ + public function creator(): BelongsTo + { + return $this->belongsTo(\App\Models\Members\User::class, 'created_by'); + } + + /** + * 상태 라벨 + */ + public function getStatusLabelAttribute(): string + { + return self::STATUSES[$this->status] ?? $this->status; + } + + /** + * 수정 가능 여부 + */ + public function canEdit(): bool + { + return $this->status !== 'completed'; + } + + /** + * 삭제 가능 여부 + */ + public function canDelete(): bool + { + return $this->status !== 'completed'; + } + + /** + * 입고처리 가능 여부 + */ + public function canProcess(): bool + { + return in_array($this->status, ['order_completed', 'shipping', 'inspection_pending', 'receiving_pending']); + } +} diff --git a/app/Services/ReceivingService.php b/app/Services/ReceivingService.php new file mode 100644 index 0000000..fef0e4e --- /dev/null +++ b/app/Services/ReceivingService.php @@ -0,0 +1,298 @@ +tenantId(); + + $query = Receiving::query() + ->where('tenant_id', $tenantId); + + // 검색어 필터 + if (! empty($params['search'])) { + $search = $params['search']; + $query->where(function ($q) use ($search) { + $q->where('order_no', 'like', "%{$search}%") + ->orWhere('item_code', 'like', "%{$search}%") + ->orWhere('item_name', 'like', "%{$search}%") + ->orWhere('supplier', 'like', "%{$search}%"); + }); + } + + // 상태 필터 + if (! empty($params['status'])) { + if ($params['status'] === 'receiving_pending') { + // 입고대기: receiving_pending + inspection_pending + $query->whereIn('status', ['receiving_pending', 'inspection_pending']); + } elseif ($params['status'] === 'completed') { + $query->where('status', 'completed'); + } else { + $query->where('status', $params['status']); + } + } + + // 날짜 범위 필터 + if (! empty($params['start_date'])) { + $query->where('receiving_date', '>=', $params['start_date']); + } + if (! empty($params['end_date'])) { + $query->where('receiving_date', '<=', $params['end_date']); + } + + // 정렬 + $sortBy = $params['sort_by'] ?? 'created_at'; + $sortDir = $params['sort_dir'] ?? 'desc'; + $query->orderBy($sortBy, $sortDir); + + // 페이지네이션 + $perPage = $params['per_page'] ?? 20; + + return $query->paginate($perPage); + } + + /** + * 입고 통계 조회 + */ + public function stats(): array + { + $tenantId = $this->tenantId(); + $today = now()->toDateString(); + + $receivingPendingCount = Receiving::where('tenant_id', $tenantId) + ->where('status', 'receiving_pending') + ->count(); + + $shippingCount = Receiving::where('tenant_id', $tenantId) + ->where('status', 'shipping') + ->count(); + + $inspectionPendingCount = Receiving::where('tenant_id', $tenantId) + ->where('status', 'inspection_pending') + ->count(); + + $todayReceivingCount = Receiving::where('tenant_id', $tenantId) + ->where('status', 'completed') + ->whereDate('receiving_date', $today) + ->count(); + + return [ + 'receiving_pending_count' => $receivingPendingCount, + 'shipping_count' => $shippingCount, + 'inspection_pending_count' => $inspectionPendingCount, + 'today_receiving_count' => $todayReceivingCount, + ]; + } + + /** + * 입고 상세 조회 + */ + public function show(int $id): Receiving + { + $tenantId = $this->tenantId(); + + return Receiving::query() + ->where('tenant_id', $tenantId) + ->with(['creator:id,name']) + ->findOrFail($id); + } + + /** + * 입고 등록 + */ + public function store(array $data): Receiving + { + $tenantId = $this->tenantId(); + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($data, $tenantId, $userId) { + // 입고번호 자동 생성 + $receivingNumber = $this->generateReceivingNumber($tenantId); + + $receiving = new Receiving; + $receiving->tenant_id = $tenantId; + $receiving->receiving_number = $receivingNumber; + $receiving->order_no = $data['order_no'] ?? null; + $receiving->order_date = $data['order_date'] ?? null; + $receiving->item_id = $data['item_id'] ?? null; + $receiving->item_code = $data['item_code']; + $receiving->item_name = $data['item_name']; + $receiving->specification = $data['specification'] ?? null; + $receiving->supplier = $data['supplier']; + $receiving->order_qty = $data['order_qty']; + $receiving->order_unit = $data['order_unit'] ?? 'EA'; + $receiving->due_date = $data['due_date'] ?? null; + $receiving->status = $data['status'] ?? 'order_completed'; + $receiving->remark = $data['remark'] ?? null; + $receiving->created_by = $userId; + $receiving->updated_by = $userId; + $receiving->save(); + + return $receiving; + }); + } + + /** + * 입고 수정 + */ + public function update(int $id, array $data): Receiving + { + $tenantId = $this->tenantId(); + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($id, $data, $tenantId, $userId) { + $receiving = Receiving::query() + ->where('tenant_id', $tenantId) + ->findOrFail($id); + + if (! $receiving->canEdit()) { + throw new \Exception(__('error.receiving.cannot_edit')); + } + + if (isset($data['order_no'])) { + $receiving->order_no = $data['order_no']; + } + if (isset($data['order_date'])) { + $receiving->order_date = $data['order_date']; + } + if (isset($data['item_code'])) { + $receiving->item_code = $data['item_code']; + } + if (isset($data['item_name'])) { + $receiving->item_name = $data['item_name']; + } + if (array_key_exists('specification', $data)) { + $receiving->specification = $data['specification']; + } + if (isset($data['supplier'])) { + $receiving->supplier = $data['supplier']; + } + if (isset($data['order_qty'])) { + $receiving->order_qty = $data['order_qty']; + } + if (isset($data['order_unit'])) { + $receiving->order_unit = $data['order_unit']; + } + if (array_key_exists('due_date', $data)) { + $receiving->due_date = $data['due_date']; + } + if (isset($data['status'])) { + $receiving->status = $data['status']; + } + if (array_key_exists('remark', $data)) { + $receiving->remark = $data['remark']; + } + + $receiving->updated_by = $userId; + $receiving->save(); + + return $receiving->fresh(); + }); + } + + /** + * 입고 삭제 + */ + public function destroy(int $id): bool + { + $tenantId = $this->tenantId(); + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($id, $tenantId, $userId) { + $receiving = Receiving::query() + ->where('tenant_id', $tenantId) + ->findOrFail($id); + + if (! $receiving->canDelete()) { + throw new \Exception(__('error.receiving.cannot_delete')); + } + + $receiving->deleted_by = $userId; + $receiving->save(); + $receiving->delete(); + + return true; + }); + } + + /** + * 입고처리 (상태 변경 + 입고 정보 입력) + */ + public function process(int $id, array $data): Receiving + { + $tenantId = $this->tenantId(); + $userId = $this->apiUserId(); + + return DB::transaction(function () use ($id, $data, $tenantId, $userId) { + $receiving = Receiving::query() + ->where('tenant_id', $tenantId) + ->findOrFail($id); + + if (! $receiving->canProcess()) { + throw new \Exception(__('error.receiving.cannot_process')); + } + + // LOT번호 생성 (없으면 자동 생성) + $lotNo = $data['lot_no'] ?? $this->generateLotNo(); + + $receiving->receiving_qty = $data['receiving_qty']; + $receiving->receiving_date = $data['receiving_date'] ?? now()->toDateString(); + $receiving->lot_no = $lotNo; + $receiving->supplier_lot = $data['supplier_lot'] ?? null; + $receiving->receiving_location = $data['receiving_location']; + $receiving->receiving_manager = $data['receiving_manager'] ?? null; + $receiving->status = 'completed'; + $receiving->remark = $data['remark'] ?? $receiving->remark; + $receiving->updated_by = $userId; + $receiving->save(); + + return $receiving->fresh(); + }); + } + + /** + * 입고번호 자동 생성 + */ + private function generateReceivingNumber(int $tenantId): string + { + $prefix = 'RV'.date('Ymd'); + + $lastReceiving = Receiving::query() + ->where('tenant_id', $tenantId) + ->where('receiving_number', 'like', $prefix.'%') + ->orderBy('receiving_number', 'desc') + ->first(); + + if ($lastReceiving) { + $lastSeq = (int) substr($lastReceiving->receiving_number, -4); + $newSeq = $lastSeq + 1; + } else { + $newSeq = 1; + } + + return $prefix.str_pad($newSeq, 4, '0', STR_PAD_LEFT); + } + + /** + * LOT번호 자동 생성 + */ + private function generateLotNo(): string + { + $now = now(); + $year = $now->format('y'); + $month = $now->format('m'); + $day = $now->format('d'); + $seq = str_pad(rand(1, 99), 2, '0', STR_PAD_LEFT); + + return "{$year}{$month}{$day}-{$seq}"; + } +} diff --git a/app/Swagger/v1/ReceivingApi.php b/app/Swagger/v1/ReceivingApi.php new file mode 100644 index 0000000..7865835 --- /dev/null +++ b/app/Swagger/v1/ReceivingApi.php @@ -0,0 +1,360 @@ +id(); + $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID'); + $table->string('receiving_number', 30)->comment('입고번호'); + $table->string('order_no', 30)->nullable()->comment('발주번호'); + $table->date('order_date')->nullable()->comment('발주일자'); + $table->unsignedBigInteger('item_id')->nullable()->comment('품목 ID'); + $table->string('item_code', 50)->comment('품목코드'); + $table->string('item_name', 200)->comment('품목명'); + $table->string('specification', 200)->nullable()->comment('규격'); + $table->string('supplier', 100)->comment('공급업체'); + $table->decimal('order_qty', 15, 2)->comment('발주수량'); + $table->string('order_unit', 20)->default('EA')->comment('발주단위'); + $table->date('due_date')->nullable()->comment('납기일'); + $table->decimal('receiving_qty', 15, 2)->nullable()->comment('입고수량'); + $table->date('receiving_date')->nullable()->comment('입고일자'); + $table->string('lot_no', 50)->nullable()->comment('입고 LOT번호'); + $table->string('supplier_lot', 50)->nullable()->comment('공급업체 LOT'); + $table->string('receiving_location', 100)->nullable()->comment('입고위치'); + $table->string('receiving_manager', 50)->nullable()->comment('입고담당'); + $table->string('status', 30)->default('order_completed')->comment('상태: order_completed/shipping/inspection_pending/receiving_pending/completed'); + $table->text('remark')->nullable()->comment('비고'); + $table->unsignedBigInteger('created_by')->nullable()->comment('생성자'); + $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자'); + $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자'); + $table->softDeletes(); + $table->timestamps(); + + // 인덱스 + $table->unique(['tenant_id', 'receiving_number'], 'uk_tenant_receiving_number'); + $table->index(['tenant_id', 'status'], 'idx_tenant_status'); + $table->index(['tenant_id', 'receiving_date'], 'idx_tenant_receiving_date'); + $table->index('item_id', 'idx_item'); + $table->index('order_no', 'idx_order_no'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('receivings'); + } +};