diff --git a/app/Models/Products/Price.php b/app/Models/Products/Price.php index 1128146..5f45f29 100644 --- a/app/Models/Products/Price.php +++ b/app/Models/Products/Price.php @@ -29,16 +29,8 @@ class Price extends Model public const STATUS_FINALIZED = 'finalized'; - // 품목 유형 (items.item_type) - public const ITEM_TYPE_FG = 'FG'; // Finished Goods (완제품) - - public const ITEM_TYPE_PT = 'PT'; // Part (부품) - - public const ITEM_TYPE_RM = 'RM'; // Raw Material (원자재) - - public const ITEM_TYPE_SM = 'SM'; // Semi-finished (반제품) - - public const ITEM_TYPE_CS = 'CS'; // Consumables/Supplies (소모품) + // 품목 유형은 common_codes 테이블의 code_group='item_type'에서 관리 + // FG(완제품), PT(부품), RM(원자재), SM(반제품), CS(소모품) protected $fillable = [ 'tenant_id', diff --git a/app/Services/PricingService.php b/app/Services/PricingService.php index 3e062ee..eebefc9 100644 --- a/app/Services/PricingService.php +++ b/app/Services/PricingService.php @@ -385,8 +385,15 @@ public function getCost(array $params): array 'price_id' => null, ]; - // 1순위: 자재인 경우 수입검사 입고단가 조회 - if ($itemType === 'MATERIAL') { + // 1순위: 자재(is_material = true)인 경우 수입검사 입고단가 조회 + // common_codes의 attributes.is_material 플래그로 자재 여부 판단 + $isMaterial = DB::table('common_codes') + ->where('code_group', 'item_type') + ->where('code', $itemType) + ->whereJsonContains('attributes->is_material', true) + ->exists(); + + if ($isMaterial) { $receipt = MaterialReceipt::query() ->where('material_id', $itemId) ->where('receipt_date', '<=', $date) diff --git a/database/migrations/2025_12_21_165524_update_prices_item_type_code_to_actual_item_type.php b/database/migrations/2025_12_21_165524_update_prices_item_type_code_to_actual_item_type.php new file mode 100644 index 0000000..aebcf58 --- /dev/null +++ b/database/migrations/2025_12_21_165524_update_prices_item_type_code_to_actual_item_type.php @@ -0,0 +1,76 @@ +whereIn('item_type_code', ['PRODUCT', 'MATERIAL']) + ->get(); + + $updated = 0; + $failed = 0; + + foreach ($legacyPrices as $price) { + // items 테이블에서 실제 item_type 조회 + $item = DB::table('items') + ->where('id', $price->item_id) + ->whereNull('deleted_at') + ->first(); + + if ($item && $item->item_type) { + DB::table('prices') + ->where('id', $price->id) + ->update(['item_type_code' => $item->item_type]); + $updated++; + + Log::info('Price item_type_code updated', [ + 'price_id' => $price->id, + 'old_value' => $price->item_type_code, + 'new_value' => $item->item_type, + 'item_id' => $price->item_id, + ]); + } else { + $failed++; + Log::warning('Price item_type_code update failed - item not found', [ + 'price_id' => $price->id, + 'item_type_code' => $price->item_type_code, + 'item_id' => $price->item_id, + ]); + } + } + + Log::info('Migration completed: update_prices_item_type_code_to_actual_item_type', [ + 'total_legacy' => $legacyPrices->count(), + 'updated' => $updated, + 'failed' => $failed, + ]); + } + + /** + * Reverse the migrations. + * 주의: 원본 값 복원이 불가능하므로 롤백하지 않음 + */ + public function down(): void + { + // 롤백 불가 (원본 PRODUCT/MATERIAL 정보 손실) + Log::warning('Rollback not supported for update_prices_item_type_code_to_actual_item_type'); + } +}; diff --git a/database/migrations/2025_12_21_171944_add_is_material_to_common_codes_item_type.php b/database/migrations/2025_12_21_171944_add_is_material_to_common_codes_item_type.php new file mode 100644 index 0000000..5a6140b --- /dev/null +++ b/database/migrations/2025_12_21_171944_add_is_material_to_common_codes_item_type.php @@ -0,0 +1,82 @@ +updateAttributes($code, false); + } + + foreach ($materialTypes as $code) { + $this->updateAttributes($code, true); + } + } + + /** + * attributes JSON에 is_material 플래그 추가 + */ + private function updateAttributes(string $code, bool $isMaterial): void + { + $record = DB::table('common_codes') + ->where('code_group', 'item_type') + ->where('code', $code) + ->first(); + + if ($record) { + $attributes = json_decode($record->attributes ?? '{}', true); + $attributes['is_material'] = $isMaterial; + + DB::table('common_codes') + ->where('id', $record->id) + ->update(['attributes' => json_encode($attributes)]); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // is_material 플래그 제거 + $codes = ['FG', 'PT', 'SM', 'RM', 'CS']; + + foreach ($codes as $code) { + $record = DB::table('common_codes') + ->where('code_group', 'item_type') + ->where('code', $code) + ->first(); + + if ($record) { + $attributes = json_decode($record->attributes ?? '{}', true); + unset($attributes['is_material']); + + DB::table('common_codes') + ->where('id', $record->id) + ->update(['attributes' => json_encode($attributes)]); + } + } + } +};