-- 영업관리 계약관리 공통코드 등록 SQL (완전버전)
-- 문서: docs/영업_계약_수정.md 기준
-- 사용자 제공 정확한 테이블 구조 반영

-- 1. 계약구분 부모 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000167', NULL, '계약구분', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 계약구분 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000167001', '0000167', '개발', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000167002', '0000167', '변형', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000167003', '0000167', '소모품', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000167004', '0000167', '재주문', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000167005', '0000167', 'A/S', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000167006', '0000167', '연구과제', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 2. 영업진행단계 부모 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000932', NULL, '영업진행단계', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 영업진행단계 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000932001', '0000932', '사양협의', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000932002', '0000932', '원가검토', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000932003', '0000932', '견적제출', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 3. 수주결과 부모 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000963', NULL, '수주결과', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 수주결과 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000963001', '0000963', '수주', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000963002', '0000963', '실패', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 4. 실패사유 부모 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000964', NULL, '실패사유', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 실패사유 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000964001', '0000964', '회신없음', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000964002', '0000964', '예산문제', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000964003', '0000964', '탈락', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000964004', '0000964', '유찰', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000964005', '0000964', '취소', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000964006', '0000964', '부적합', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 5. 통화단위 부모 코드 (신규)
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000965', NULL, '통화단위', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 통화단위 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000965001', '0000965', 'KRW', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000965002', '0000965', 'USD', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000965003', '0000965', 'EUR', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000965004', '0000965', 'JPY', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000965005', '0000965', 'CNY', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 6. 계약방식 부모 코드 (신규)
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES ('0000966', NULL, '계약방식', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 계약방식 하위 코드
INSERT INTO comm_code (code_id, parent_code_id, code_name, id, code_cd, ext_val, writer, regdate, status) 
VALUES 
('0000966001', '0000966', '조달', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000966002', '0000966', '민자', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000966003', '0000966', '대리점', NULL, NULL, NULL, 'plm_admin', NOW(), 'active'),
('0000966004', '0000966', '재로비', NULL, NULL, NULL, 'plm_admin', NOW(), 'active');

-- 확인 쿼리
SELECT * FROM comm_code WHERE code_id IN ('0000167', '0000932', '0000963', '0000964', '0000965', '0000966') 
ORDER BY code_id, parent_code_id; 