tenantId(); // 1. SystemFields에서 materials 테이블 고정 컬럼 $systemFields = SystemFields::getReservedKeys(SystemFields::SOURCE_TABLE_MATERIALS); // 2. ItemField에서 storage_type='column'인 필드의 field_key 조회 $columnFields = ItemField::where('tenant_id', $tenantId) ->where('source_table', 'materials') ->where('storage_type', 'column') ->whereNotNull('field_key') ->pluck('field_key') ->toArray(); // 3. 추가적인 API 전용 필드 (DB 컬럼이 아니지만 API에서 사용하는 필드) $apiFields = ['item_type', 'type_code', 'bom']; return array_unique(array_merge($systemFields, $columnFields, $apiFields)); } /** * 정의된 필드 외의 동적 필드를 options로 추출 */ private function extractDynamicOptions(array $params): array { $knownFields = $this->getKnownFields(); $dynamicOptions = []; foreach ($params as $key => $value) { if (! in_array($key, $knownFields) && $value !== null && $value !== '') { $dynamicOptions[$key] = $value; } } return $dynamicOptions; } /** * 기존 options 배열과 동적 필드를 병합 * - 기존 options가 [{label, value}] 배열이면 동적 필드를 배열 항목으로 추가 * - 기존 options가 {key: value} 맵이면 동적 필드를 맵에 병합 */ private function mergeOptionsWithDynamic($existingOptions, array $dynamicOptions): array { if (! is_array($existingOptions) || empty($existingOptions)) { // 기존 options가 없으면 동적 필드만 반환 return $dynamicOptions; } // 기존 options가 연관 배열(맵)인지 판별 $isAssoc = array_keys($existingOptions) !== range(0, count($existingOptions) - 1); if ($isAssoc) { // 맵 형태면 단순 병합 return array_merge($existingOptions, $dynamicOptions); } // 배열 형태 [{label, value}]면 동적 필드를 배열 항목으로 추가 foreach ($dynamicOptions as $key => $value) { $existingOptions[] = ['label' => $key, 'value' => $value]; } return $existingOptions; } /** 공통 검증 헬퍼 */ protected function v(array $input, array $rules) { $v = Validator::make($input, $rules); if ($v->fails()) { return ['error' => $v->errors()->first(), 'code' => 422]; } return $v->validated(); } /** 목록 */ public function getMaterials(array $params) { $tenantId = $this->tenantId(); $p = $this->v($params, [ 'q' => 'nullable|string|max:100', 'category' => 'nullable|integer|min:1', 'page' => 'nullable|integer|min:1', 'per_page' => 'nullable|integer|min:1|max:200', ]); if (isset($p['error'])) { return $p; } $q = Material::query() ->where('tenant_id', $tenantId); // SoftDeletes가 있으면 기본적으로 deleted_at IS NULL if (! empty($p['category'])) { $q->where('category_id', (int) $p['category']); } if (! empty($p['q'])) { $kw = '%'.$p['q'].'%'; $q->where(function ($w) use ($kw) { $w->where('item_name', 'like', $kw) ->orWhere('name', 'like', $kw) ->orWhere('material_code', 'like', $kw) ->orWhere('search_tag', 'like', $kw); }); } $q->orderBy('id'); $perPage = $p['per_page'] ?? 20; $page = $p['page'] ?? null; return $q->paginate($perPage, ['*'], 'page', $page); } /** 단건 조회 */ public function getMaterial(int $id) { $tenantId = $this->tenantId(); /** @var Material|null $row */ $row = Material::query() ->where('tenant_id', $tenantId) ->find($id); if (! $row) { return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404]; } // 모델에서 casts가 없을 수 있으니 안전하게 배열화 $row->attributes = is_array($row->attributes) ? $row->attributes : ($row->attributes ? json_decode($row->attributes, true) : null); $row->options = is_array($row->options) ? $row->options : ($row->options ? json_decode($row->options, true) : null); return $row; } /** 등록 */ public function setMaterial(array $params) { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); // 동적 필드를 options에 병합 $dynamicOptions = $this->extractDynamicOptions($params); if (! empty($dynamicOptions)) { $existingOptions = $params['options'] ?? []; $params['options'] = $this->mergeOptionsWithDynamic($existingOptions, $dynamicOptions); } $p = $this->v($params, [ 'category_id' => 'nullable|integer|min:1', 'name' => 'required|string|max:100', 'unit' => 'required|string|max:10', 'is_inspection' => 'nullable|in:Y,N', 'search_tag' => 'nullable|string', 'remarks' => 'nullable|string', 'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map 'options' => 'nullable|array', // [{label,value,unit}] 또는 map 'material_code' => 'nullable|string|max:50', 'specification' => 'nullable|string|max:100', ]); if (isset($p['error'])) { return $p; } // material_code 중복 체크 (삭제된 레코드 포함 - DB unique 제약은 deleted_at 무시) if (! empty($p['material_code'])) { $duplicate = Material::withTrashed() ->where('tenant_id', $tenantId) ->where('material_code', $p['material_code']) ->first(['id', 'name', 'deleted_at']); if ($duplicate) { if ($duplicate->deleted_at) { return [ 'error' => "자재코드 '{$p['material_code']}'가 삭제된 자재에서 사용 중입니다. 해당 자재를 복구하거나 완전 삭제 후 다시 시도하세요.", 'code' => 422, 'deleted_material_id' => $duplicate->id, ]; } return ['error' => "자재코드 '{$p['material_code']}'가 이미 존재합니다.", 'code' => 422]; } } // 기존 normalizeAttributes 사용(그대로), options는 새 normalizeOptions 사용 $attributes = $this->normalizeAttributes($p['attributes'] ?? null); $options = $this->normalizeOptions($p['options'] ?? null); $itemName = $this->buildItemName($p['name'], $attributes); $specText = $p['specification'] ?? $this->buildSpecText($attributes); $m = new Material; $m->tenant_id = $tenantId; $m->category_id = $p['category_id'] ?? null; $m->name = $p['name']; $m->item_name = $itemName; $m->specification = $specText; $m->material_code = $p['material_code'] ?? null; $m->unit = $p['unit']; $m->is_inspection = $p['is_inspection'] ?? 'N'; $m->search_tag = $p['search_tag'] ?? null; $m->remarks = $p['remarks'] ?? null; $m->attributes = $attributes ?? null; $m->options = $options ?? null; $m->created_by = $userId ?? 0; $m->updated_by = $userId ?? null; $m->save(); return $this->getMaterial($m->id); } /** 수정 */ public function updateMaterial(int $id, array $params = []) { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); /** @var Material|null $exists */ $exists = Material::query()->where('tenant_id', $tenantId)->find($id); if (! $exists) { return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404]; } // 동적 필드를 options에 병합 $dynamicOptions = $this->extractDynamicOptions($params); if (! empty($dynamicOptions)) { $existingOptions = $params['options'] ?? []; $params['options'] = $this->mergeOptionsWithDynamic($existingOptions, $dynamicOptions); } $p = $this->v($params, [ 'category_id' => 'nullable|integer|min:1', 'name' => 'nullable|string|max:100', 'unit' => 'nullable|string|max:10', 'is_inspection' => 'nullable|in:Y,N', 'search_tag' => 'nullable|string', 'remarks' => 'nullable|string', 'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map 'options' => 'nullable|array', // [{label,value,unit}] 또는 map 'material_code' => 'nullable|string|max:50', 'specification' => 'nullable|string|max:100', ]); if (isset($p['error'])) { return $p; } // material_code 중복 체크 (삭제된 레코드 포함 - DB unique 제약은 deleted_at 무시) $finalMaterialCode = $p['material_code'] ?? $exists->material_code; if (! empty($finalMaterialCode)) { $duplicate = Material::withTrashed() ->where('tenant_id', $tenantId) ->where('material_code', $finalMaterialCode) ->where('id', '!=', $id) ->first(['id', 'name', 'deleted_at']); if ($duplicate) { if ($duplicate->deleted_at) { return [ 'error' => "자재코드 '{$finalMaterialCode}'가 삭제된 자재에서 사용 중입니다. 해당 자재를 복구하거나 완전 삭제 후 다시 시도하세요.", 'code' => 422, 'deleted_material_id' => $duplicate->id, ]; } return ['error' => "자재코드 '{$finalMaterialCode}'가 이미 존재합니다.", 'code' => 422]; } } $currentAttrs = is_array($exists->attributes) ? $exists->attributes : ($exists->attributes ? json_decode($exists->attributes, true) : null); $currentOpts = is_array($exists->options) ? $exists->options : ($exists->options ? json_decode($exists->options, true) : null); // 변경 점만 정규화 $attrs = array_key_exists('attributes', $p) ? $this->normalizeAttributes($p['attributes']) : $currentAttrs; $opts = array_key_exists('options', $p) ? $this->normalizeOptions($p['options']) : $currentOpts; $baseName = array_key_exists('name', $p) ? ($p['name'] ?? $exists->name) : $exists->name; $exists->category_id = $p['category_id'] ?? $exists->category_id; $exists->name = $baseName; $exists->item_name = $this->buildItemName($baseName, $attrs); $exists->specification = array_key_exists('specification', $p) ? ($p['specification'] ?? null) : ($exists->specification ?: $this->buildSpecText($attrs)); $exists->material_code = $p['material_code'] ?? $exists->material_code; $exists->unit = $p['unit'] ?? $exists->unit; $exists->is_inspection = $p['is_inspection'] ?? $exists->is_inspection; $exists->search_tag = $p['search_tag'] ?? $exists->search_tag; $exists->remarks = $p['remarks'] ?? $exists->remarks; if (array_key_exists('attributes', $p)) { $exists->attributes = $attrs; } if (array_key_exists('options', $p)) { $exists->options = $opts; } $exists->updated_by = $userId ?? $exists->updated_by; $exists->save(); return $this->getMaterial($exists->id); } /** 삭제(소프트) */ public function destroyMaterial(int $id) { $tenantId = $this->tenantId(); /** @var Material|null $row */ $row = Material::query() ->where('tenant_id', $tenantId) ->find($id); if (! $row) { return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404]; } // 사용 중인 자재 삭제 방지 $usageCheck = $this->checkMaterialUsage($id, $tenantId); if ($usageCheck['in_use']) { return [ 'error' => '사용 중인 자재는 삭제할 수 없습니다.', 'code' => 422, 'usage' => $usageCheck['details'], ]; } $row->delete(); return ['id' => $id, 'deleted_at' => now()->toDateTimeString()]; } /** * 자재 사용 여부 체크 * - material_receipts: 입고 내역 * - lots: 로트 관리 * - product_components: BOM 구성품 (ref_type='MATERIAL') */ private function checkMaterialUsage(int $materialId, int $tenantId): array { $details = []; // 1. 입고 내역 체크 $receiptCount = \App\Models\Materials\MaterialReceipt::where('tenant_id', $tenantId) ->where('material_id', $materialId) ->count(); if ($receiptCount > 0) { $details['receipts'] = $receiptCount; } // 2. 로트 체크 $lotCount = \App\Models\Qualitys\Lot::where('tenant_id', $tenantId) ->where('material_id', $materialId) ->count(); if ($lotCount > 0) { $details['lots'] = $lotCount; } // 3. BOM 구성품 체크 (ref_type='MATERIAL', ref_id=material_id) $bomCount = \App\Models\Products\ProductComponent::where('tenant_id', $tenantId) ->where('ref_type', 'MATERIAL') ->where('ref_id', $materialId) ->count(); if ($bomCount > 0) { $details['bom_components'] = $bomCount; } return [ 'in_use' => ! empty($details), 'details' => $details, ]; } /* ------------------------- 헬퍼: 규격/품목명 빌더 attributes 예시: [ {"label":"두께","value":"10","unit":"T"}, {"label":"길이","value":"150","unit":"CM"} ] → item_name: "철판 10T 150CM" → specification: "두께 10T, 길이 150CM" ------------------------- */ private function normalizeAttributes(?array $attrs): ?array { if (! $attrs) { return null; } $out = []; foreach ($attrs as $a) { if (! is_array($a)) { continue; } $label = trim((string) ($a['label'] ?? '')); $value = trim((string) ($a['value'] ?? '')); $unit = trim((string) ($a['unit'] ?? '')); if ($label === '' && $value === '' && $unit === '') { continue; } $out[] = ['label' => $label, 'value' => $value, 'unit' => $unit]; } return $out ?: null; } private function buildItemName(string $name, ?array $attrs): string { if (! $attrs || count($attrs) === 0) { return $name; } $parts = []; foreach ($attrs as $a) { $value = (string) ($a['value'] ?? ''); $unit = (string) ($a['unit'] ?? ''); $chunk = trim($value.$unit); if ($chunk !== '') { $parts[] = $chunk; } } return trim($name.' '.implode(' ', $parts)); } private function buildSpecText(?array $attrs): ?string { if (! $attrs || count($attrs) === 0) { return null; } $parts = []; foreach ($attrs as $a) { $label = (string) ($a['label'] ?? ''); $value = (string) ($a['value'] ?? ''); $unit = (string) ($a['unit'] ?? ''); $valueWithUnit = trim($value.$unit); if ($label !== '' && $valueWithUnit !== '') { $parts[] = "{$label} {$valueWithUnit}"; } elseif ($valueWithUnit !== '') { $parts[] = $valueWithUnit; } } return $parts ? implode(', ', $parts) : null; } /** * options 입력을 [{label, value, unit}] 형태로 정규화. * - 맵 형태 {"key": "value"}도 배열로 변환 * - 항상 [{label, value, unit}] 형태로 저장 */ private function normalizeOptions(?array $in): ?array { if (! $in) { return null; } // 연관 맵 형태인지 간단 판별 $isAssoc = array_keys($in) !== range(0, count($in) - 1); if ($isAssoc) { // 맵 형태를 [{label, value, unit}] 배열로 변환 $out = []; foreach ($in as $k => $v) { $label = trim((string) $k); $value = is_scalar($v) ? trim((string) $v) : json_encode($v, JSON_UNESCAPED_UNICODE); if ($label !== '' || $value !== '') { $out[] = ['label' => $label, 'value' => $value, 'unit' => '']; } } return $out ?: null; } // 리스트(triple) 정규화 $out = []; foreach ($in as $a) { if (! is_array($a)) { continue; } $label = trim((string) ($a['label'] ?? '')); $value = trim((string) ($a['value'] ?? '')); $unit = trim((string) ($a['unit'] ?? '')); if ($label === '' && $value === '') { continue; } $out[] = ['label' => $label, 'value' => $value, 'unit' => $unit]; } return $out ?: null; } }