-- ====================================================================
-- estimate_template / estimate_template_item
-- ====================================================================
-- 출처: wace_plm 운영 DB 211.115.91.141:11133/waceplm (PG 16.8)
-- 추출일: 2026-05-07
-- 견적관리(estimateList_new)의 메인 테이블 — 신규 견적의 헤더+라인을 담당.
-- contract_objid 로 contract_mgmt에 묶여 1 contract → N templates (template_type별).
-- ====================================================================

CREATE TABLE IF NOT EXISTS estimate_template (
    objid             VARCHAR(50)  PRIMARY KEY,
    contract_objid    VARCHAR(50)  NOT NULL,                 -- → contract_mgmt(objid)
    template_type     VARCHAR(10)  NOT NULL,                 -- '1'=일반, '2'=장비
    executor          VARCHAR(200),                          -- 발주자
    recipient         VARCHAR(200),                          -- 수신자
    estimate_no       VARCHAR(100),                          -- 견적번호
    contact_person    VARCHAR(100),                          -- 담당자
    greeting_text     TEXT,                                  -- 인사말
    model_name        VARCHAR(200),
    model_code        VARCHAR(100),
    executor_date     VARCHAR(50),
    note1             VARCHAR(500),
    note2             VARCHAR(500),
    note3             VARCHAR(500),
    note4             VARCHAR(500),
    categories_json   TEXT,                                  -- 그룹/소그룹 구조 JSON
    writer            VARCHAR(50),
    regdate           TIMESTAMP    DEFAULT now(),
    chg_user_id       VARCHAR(50),
    chgdate           TIMESTAMP    DEFAULT now(),
    notes_content     TEXT,                                  -- 본문 비고
    validity_period   VARCHAR(50),                           -- 유효기간
    total_amount      VARCHAR(50),
    total_amount_krw  VARCHAR(50),
    manager_name      VARCHAR(100),                          -- 담당자 이름
    manager_contact   VARCHAR(100),                          -- 담당자 연락처
    note_remarks      TEXT,                                  -- 테이블 내 비고 (품목 하단)
    show_total_row    VARCHAR(1)   DEFAULT 'Y',              -- 계 행 표시 여부 (Y/N)
    group1_subtotal   VARCHAR(50),
    part_name         VARCHAR(200),
    part_objid        VARCHAR(50)                            -- → item_info(legacy mapping)
);

CREATE INDEX IF NOT EXISTS idx_estimate_contract ON estimate_template(contract_objid);
CREATE INDEX IF NOT EXISTS idx_estimate_type     ON estimate_template(template_type);

COMMENT ON TABLE estimate_template IS '견적 템플릿 (estimateList_new의 메인)';
COMMENT ON COLUMN estimate_template.manager_name    IS '담당자 이름';
COMMENT ON COLUMN estimate_template.manager_contact IS '담당자 연락처';
COMMENT ON COLUMN estimate_template.note_remarks    IS '테이블 내 비고 (품목 하단)';
COMMENT ON COLUMN estimate_template.show_total_row  IS '계 행 표시 여부 (Y: 표시, N: 숨김)';

-- --------------------------------------------------------------------

CREATE SEQUENCE IF NOT EXISTS estimate_template_item_objid_seq;

CREATE TABLE IF NOT EXISTS estimate_template_item (
    objid           INTEGER      PRIMARY KEY DEFAULT nextval('estimate_template_item_objid_seq'),
    template_objid  VARCHAR(50)  NOT NULL,                  -- → estimate_template(objid)
    seq             INTEGER      NOT NULL,
    category        VARCHAR(100),                           -- 그룹명
    description     VARCHAR(500),                           -- 품명
    specification   TEXT,                                   -- 규격
    quantity        VARCHAR(50),
    unit            VARCHAR(50),
    unit_price      VARCHAR(50),
    amount          VARCHAR(50),
    note            VARCHAR(500),
    remark          VARCHAR(500),
    part_objid      VARCHAR
);

ALTER SEQUENCE estimate_template_item_objid_seq OWNED BY estimate_template_item.objid;

CREATE INDEX IF NOT EXISTS idx_item_template ON estimate_template_item(template_objid);
CREATE INDEX IF NOT EXISTS idx_item_seq      ON estimate_template_item(template_objid, seq);

COMMENT ON TABLE estimate_template_item IS '견적 템플릿 라인';
