-- ============================================================
-- project_mgmt — 판매·매출관리 메인 테이블
-- ----------------------------------------------------------------
-- 출처: wace_plm 운영 DB (211.115.91.141:11133/waceplm), 75 컬럼 / 89건
-- 추출: information_schema.columns 직접 조회 (2026-05-07)
-- 역할: contract_item이 수주 확정되면 라인당 1행이 project_mgmt에 생성됨
--       PROJECT_NO 형식: [R/S/T 등]-[CT/BS/AC/DS/...]-[YYMMDD]-[NNN]
-- ============================================================

CREATE TABLE IF NOT EXISTS project_mgmt (
    objid                       VARCHAR        NOT NULL,
    contract_objid              VARCHAR        NOT NULL,
    category_cd                 VARCHAR,
    customer_objid              VARCHAR,
    product                     VARCHAR,
    customer_project_name       VARCHAR,
    status_cd                   VARCHAR,
    due_date                    VARCHAR,
    location                    VARCHAR,
    setup                       VARCHAR,
    facility                    VARCHAR,
    facility_qty                VARCHAR,
    facility_type               VARCHAR,
    facility_depth              VARCHAR,
    production_no               VARCHAR,
    bus_cal_cd                  VARCHAR,
    category1_cd                VARCHAR,
    chg_user_id                 VARCHAR,
    plan_date                   VARCHAR,
    complete_date               VARCHAR,
    result_cd                   VARCHAR,
    project_no                  VARCHAR,
    pm_user_id                  VARCHAR,
    contract_price              VARCHAR,
    regdate                     TIMESTAMP WITHOUT TIME ZONE,
    writer                      VARCHAR,
    contract_no                 VARCHAR,
    customer_equip_name         VARCHAR,
    req_del_date                VARCHAR,
    contract_del_date           VARCHAR,
    contract_company            VARCHAR,
    contract_date               VARCHAR,
    po_no                       VARCHAR,
    manufacture_plant           VARCHAR,
    contract_result             VARCHAR,
    project_name                VARCHAR,
    spec_user_id                VARCHAR,
    spec_plan_date              VARCHAR,
    spec_comp_date              VARCHAR,
    spec_result_cd              VARCHAR,
    est_plan_date               VARCHAR,
    est_user_id                 VARCHAR,
    est_comp_date               VARCHAR,
    est_result_cd               VARCHAR,
    area_cd                     VARCHAR,
    contract_price_currency     VARCHAR,
    contract_currency           VARCHAR,
    mechanical_type             VARCHAR,
    is_temp                     VARCHAR,
    overhaul_order              VARCHAR,
    part_objid                  VARCHAR,
    part_no                     VARCHAR(100),
    part_name                   VARCHAR(200),
    quantity                    VARCHAR(50),
    ebom_status                 VARCHAR(50),
    mbom_status                 VARCHAR(50),
    receiving_rate              VARCHAR(10),
    production_team_12          VARCHAR(50),
    production_team_3           VARCHAR(50),
    sales_status                VARCHAR,
    bom_report_objid            VARCHAR(100),
    mbom_version                INTEGER         DEFAULT 0,
    mbom_regdate                TIMESTAMP WITHOUT TIME ZONE,
    mbom_writer                 VARCHAR(100),
    sales_deadline_date         VARCHAR(10),
    source_bom_type             VARCHAR(20),
    source_ebom_objid           VARCHAR(64),
    source_mbom_objid           VARCHAR(64),
    tax_type                    VARCHAR(20),
    tax_invoice_date            VARCHAR(10),
    export_decl_no              VARCHAR(100),
    loading_date                VARCHAR(10),
    sales_slip_date             VARCHAR(8),
    sales_slip_menu_sq          INTEGER,
    contract_item_objid         VARCHAR(50),
    CONSTRAINT project_mgmt_pkey PRIMARY KEY (objid)
);

CREATE INDEX IF NOT EXISTS idx_project_mgmt_contract_objid ON project_mgmt(contract_objid);
CREATE INDEX IF NOT EXISTS idx_project_mgmt_contract_part  ON project_mgmt(contract_objid, part_objid);
CREATE INDEX IF NOT EXISTS idx_project_mgmt_project_no     ON project_mgmt(project_no);
CREATE INDEX IF NOT EXISTS idx_project_mgmt_customer       ON project_mgmt(customer_objid);
CREATE INDEX IF NOT EXISTS idx_project_mgmt_regdate        ON project_mgmt(regdate DESC);

COMMENT ON TABLE  project_mgmt IS '프로젝트(수주 라인 단위) 헤더 — 판매·매출관리 메인';
COMMENT ON COLUMN project_mgmt.objid               IS 'PK (wace 동일)';
COMMENT ON COLUMN project_mgmt.project_no          IS '프로젝트번호 (예: R-CT-260323-001) — 화면 표시 식별자';
COMMENT ON COLUMN project_mgmt.contract_objid      IS 'contract_mgmt.objid 참조';
COMMENT ON COLUMN project_mgmt.contract_item_objid IS 'contract_item.objid 참조';
COMMENT ON COLUMN project_mgmt.part_objid          IS 'item_info.id 참조';
COMMENT ON COLUMN project_mgmt.sales_status        IS '판매상태 (NULL/등록/완료 등)';
COMMENT ON COLUMN project_mgmt.sales_deadline_date IS '매출마감일 (YYYY-MM-DD)';
