diff --git a/app/Services/ItemsService.php b/app/Services/ItemsService.php index a83aa91..17e1ce0 100644 --- a/app/Services/ItemsService.php +++ b/app/Services/ItemsService.php @@ -236,15 +236,33 @@ private function fetchPrices(string $itemType, int $itemId, ?int $clientId, ?str } /** - * 품목 생성 (Product 전용) + * 품목 생성 (타입에 따라 Products 또는 Materials 테이블에 저장) + * + * - FG, PT → Products 테이블 + * - SM, RM, CS → Materials 테이블 * * @param array $data 검증된 데이터 */ - public function createItem(array $data): Product + public function createItem(array $data): Product|Material { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); + $itemType = strtoupper($data['product_type'] ?? 'FG'); + // Materials 타입 (SM, RM, CS) + if (in_array($itemType, ['SM', 'RM', 'CS'])) { + return $this->createMaterial($data, $tenantId, $userId, $itemType); + } + + // Products 타입 (FG, PT) - 기본값 + return $this->createProduct($data, $tenantId, $userId); + } + + /** + * Product 생성 (FG, PT) + */ + private function createProduct(array $data, int $tenantId, int $userId): Product + { // 품목 코드 중복 시 자동 증가 $data['code'] = $this->resolveUniqueCode($data['code'], $tenantId); @@ -259,6 +277,82 @@ public function createItem(array $data): Product return Product::create($payload); } + /** + * Material 생성 (SM, RM, CS) + */ + private function createMaterial(array $data, int $tenantId, int $userId, string $materialType): Material + { + // 품목 코드 중복 시 자동 증가 (Materials용) + $code = $data['code'] ?? $data['material_code'] ?? ''; + $data['material_code'] = $this->resolveUniqueMaterialCode($code, $tenantId); + + $payload = [ + 'tenant_id' => $tenantId, + 'created_by' => $userId, + 'material_type' => $materialType, + 'material_code' => $data['material_code'], + 'name' => $data['name'], + 'unit' => $data['unit'], + 'category_id' => $data['category_id'] ?? null, + 'item_name' => $data['item_name'] ?? $data['name'], + 'specification' => $data['specification'] ?? $data['description'] ?? null, + 'is_inspection' => $data['is_inspection'] ?? 'N', + 'search_tag' => $data['search_tag'] ?? null, + 'remarks' => $data['remarks'] ?? null, + 'attributes' => $data['attributes'] ?? null, + 'options' => $data['options'] ?? null, + 'is_active' => $data['is_active'] ?? true, + ]; + + return Material::create($payload); + } + + /** + * Material 코드 중복 체크 및 고유 코드 생성 + */ + private function resolveUniqueMaterialCode(string $code, int $tenantId): string + { + $exists = Material::withTrashed() + ->where('tenant_id', $tenantId) + ->where('material_code', $code) + ->exists(); + + if (! $exists) { + return $code; + } + + // 마지막이 숫자인지 확인 + if (preg_match('/^(.+?)(\d+)$/', $code, $matches)) { + $prefix = $matches[1]; + $number = (int) $matches[2]; + $digits = strlen($matches[2]); + + do { + $number++; + $newCode = $prefix.str_pad($number, $digits, '0', STR_PAD_LEFT); + $exists = Material::withTrashed() + ->where('tenant_id', $tenantId) + ->where('material_code', $newCode) + ->exists(); + } while ($exists); + + return $newCode; + } + + // 마지막이 문자면 -001 추가 + $suffix = 1; + do { + $newCode = $code.'-'.str_pad($suffix, 3, '0', STR_PAD_LEFT); + $exists = Material::withTrashed() + ->where('tenant_id', $tenantId) + ->where('material_code', $newCode) + ->exists(); + $suffix++; + } while ($exists); + + return $newCode; + } + /** * 중복되지 않는 고유 코드 생성 * @@ -287,7 +381,7 @@ private function resolveUniqueCode(string $code, int $tenantId): string // 숫자 증가하며 고유 코드 찾기 do { $number++; - $newCode = $prefix . str_pad($number, $digits, '0', STR_PAD_LEFT); + $newCode = $prefix.str_pad($number, $digits, '0', STR_PAD_LEFT); $exists = Product::withTrashed() ->where('tenant_id', $tenantId) ->where('code', $newCode) @@ -300,7 +394,7 @@ private function resolveUniqueCode(string $code, int $tenantId): string // 마지막이 문자면 -001 추가 $suffix = 1; do { - $newCode = $code . '-' . str_pad($suffix, 3, '0', STR_PAD_LEFT); + $newCode = $code.'-'.str_pad($suffix, 3, '0', STR_PAD_LEFT); $exists = Product::withTrashed() ->where('tenant_id', $tenantId) ->where('code', $newCode)