-- ============================================================
-- M-BOM 관리 화면 의존 테이블 신설 (PR-A0)
-- 운영: 211.115.91.141:11133/waceplm
-- 대상: 211.115.91.141:11134/vexplor_rps
--
-- 1) mbom_history          — M-BOM 변경 이력 (8 cols, FK→mbom_header)
-- 2) sales_request_master  — 구매요청서 마스터 (27 cols, FK→mbom_header via mbom_header_objid)
-- 3) client_mng            — 거래처 마스터 (117 cols) — 그리드 CUSTOMER_NAME 매칭용
--
-- 생성 명령:
--   PGPASSWORD='vexplor0909!!' psql -h 211.115.91.141 -p 11134 -U postgres -d vexplor_rps -f 401_mbom_dependencies.sql
-- ============================================================

SET statement_timeout = 0;
SET client_encoding = 'UTF8';

-- ── 1. mbom_history ────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS public.mbom_history (
    objid              character varying(64) NOT NULL,
    mbom_header_objid  character varying(64) NOT NULL,
    change_type        character varying(50),
    change_description text,
    before_data        jsonb,
    after_data         jsonb,
    change_user        character varying(50),
    change_date        timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT mbom_history_pkey PRIMARY KEY (objid),
    CONSTRAINT fk_mbom_history_header FOREIGN KEY (mbom_header_objid)
        REFERENCES public.mbom_header(objid) ON DELETE CASCADE
);

COMMENT ON TABLE  public.mbom_history IS 'M-BOM 변경 이력 테이블';

CREATE INDEX IF NOT EXISTS idx_mbom_history_header_objid ON public.mbom_history (mbom_header_objid);
CREATE INDEX IF NOT EXISTS idx_mbom_history_change_date  ON public.mbom_history (change_date);


-- ── 2. sales_request_master ────────────────────────────────────
CREATE TABLE IF NOT EXISTS public.sales_request_master (
    objid                 character varying NOT NULL,
    request_mng_no        character varying,
    request_cd            character varying,
    project_no            character varying,
    release_date          character varying,
    request_reasons       character varying,
    request_user_id       character varying,
    delivery_request_date character varying,
    unit_name             character varying,
    status                character varying,
    receipt_user_id       character varying,
    receipt_date          character varying,
    writer                character varying,
    regdate               timestamp without time zone,
    remark                character varying,
    purchase_type         character varying(50),
    order_type            character varying(50),
    product_name          character varying(50),
    area_cd               character varying(50),
    customer_objid        character varying(50),
    paid_type             character varying(20),
    mbom_header_objid     character varying(50),
    doc_type              character varying(50),
    recipient_ref         character varying(500),
    executor              character varying(100),
    execution_date        date,
    title                 character varying(500),
    CONSTRAINT sales_request_master_pkey PRIMARY KEY (objid)
);

COMMENT ON TABLE  public.sales_request_master                       IS '구매요청서 마스터';
COMMENT ON COLUMN public.sales_request_master.request_mng_no        IS '요청번호';
COMMENT ON COLUMN public.sales_request_master.request_cd            IS '구분';
COMMENT ON COLUMN public.sales_request_master.project_no            IS '프로젝트번호';
COMMENT ON COLUMN public.sales_request_master.release_date          IS '출고일';
COMMENT ON COLUMN public.sales_request_master.request_reasons       IS '요청사유';
COMMENT ON COLUMN public.sales_request_master.request_user_id       IS '요청인';
COMMENT ON COLUMN public.sales_request_master.delivery_request_date IS '입고요청일';
COMMENT ON COLUMN public.sales_request_master.unit_name             IS '유닛명';
COMMENT ON COLUMN public.sales_request_master.status                IS '상태';
COMMENT ON COLUMN public.sales_request_master.receipt_user_id       IS '접수자';
COMMENT ON COLUMN public.sales_request_master.receipt_date          IS '접수일';
COMMENT ON COLUMN public.sales_request_master.writer                IS '작성자';
COMMENT ON COLUMN public.sales_request_master.regdate               IS '작성일';
COMMENT ON COLUMN public.sales_request_master.remark                IS '비고';
COMMENT ON COLUMN public.sales_request_master.mbom_header_objid     IS 'M-BOM 헤더 OBJID (NULL: 수동작성, 값 있음: M-BOM 자동생성)';
COMMENT ON COLUMN public.sales_request_master.doc_type              IS '문서유형 (PURCHASE_REQUEST: 구매요청서, PROPOSAL: 품의서)';
COMMENT ON COLUMN public.sales_request_master.recipient_ref         IS '수신및참조';
COMMENT ON COLUMN public.sales_request_master.executor              IS '시행자';
COMMENT ON COLUMN public.sales_request_master.execution_date        IS '시행일자';
COMMENT ON COLUMN public.sales_request_master.title                 IS '제목';

CREATE INDEX IF NOT EXISTS idx_sales_request_master_doc_type     ON public.sales_request_master (doc_type);
CREATE INDEX IF NOT EXISTS idx_sales_request_master_mbom_header  ON public.sales_request_master (mbom_header_objid);


-- ── 3. client_mng ──────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS public.client_mng (
    comp_code          character varying(50),
    client_cd          character varying(50),
    client_nm          character varying(200),
    tr_nmk             character varying(200),
    client_nmk         character varying(200),
    attr_nmk           character varying(100),
    client_type        character varying(50),
    bus_reg_no         character varying(50),
    resident_no        character varying(50),
    ceo_nm             character varying(100),
    ceo_nmk            character varying(100),
    bus_type           character varying(100),
    bus_item           character varying(100),
    post_no            character varying(20),
    addr1              character varying(300),
    addr2              character varying(300),
    addr_fg            character varying(50),
    tel_no             character varying(50),
    fax_no             character varying(50),
    homepage           character varying(200),
    email              character varying(100),
    liq_rs             character varying(100),
    tr_fg              character varying(100),
    country_nm         character varying(100),
    class_cd           character varying(50),
    class_nm           character varying(100),
    grade_cd           character varying(50),
    grade_nm           character varying(100),
    collect_client_cd  character varying(50),
    collect_client_nm  character varying(200),
    region_cd          character varying(50),
    region_nm          character varying(100),
    trade_start_dt     character varying(20),
    trade_end_dt       character varying(20),
    use_yn             character varying(10),
    contract_start_dt  character varying(20),
    contract_end_dt    character varying(20),
    trade_type         character varying(50),
    discount_rate      character varying(20),
    contract_amt       character varying(20),
    monthly_fee        character varying(20),
    payment_term       character varying(200),
    rcp_tp             character varying(50),
    credit_limit       character varying(20),
    limit_return_day   character varying(20),
    pur_bank_cd        character varying(50),
    pur_bank_nm        character varying(200),
    pur_branch_nm      character varying(200),
    pur_account_no     character varying(100),
    pur_account_holder character varying(100),
    pur_pay_plan       character varying(200),
    pur_slip_type      character varying(50),
    pur_tax_type       character varying(50),
    sale_bank_cd       character varying(50),
    sale_bank_nm       character varying(200),
    sale_branch_nm     character varying(200),
    sale_account_no    character varying(100),
    sale_collect_plan  character varying(200),
    sale_slip_type     character varying(50),
    sale_tax_type      character varying(50),
    vendor_dept_nm     character varying(100),
    vendor_position    character varying(50),
    vendor_duty        character varying(100),
    vendor_manager_nm  character varying(100),
    vendor_tel         character varying(50),
    vendor_ext         character varying(20),
    vendor_mobile      character varying(50),
    vendor_email       character varying(100),
    mgr_dept_cd        character varying(50),
    mgr_dept_nm        character varying(100),
    mgr_position       character varying(50),
    mgr_duty           character varying(100),
    mgr_emp_cd         character varying(50),
    mgr_emp_nm         character varying(100),
    mgr_tel            character varying(50),
    mgr_ext            character varying(20),
    mgr_mobile         character varying(50),
    mgr_email          character varying(100),
    mgr_remark         text,
    rec_remark         text,
    rec_post_no        character varying(20),
    rec_addr1          character varying(300),
    rec_addr2          character varying(300),
    rec_addr_fg        character varying(50),
    rec_tel            character varying(50),
    rec_fax            character varying(50),
    project_cd         character varying(50),
    project_nm         character varying(200),
    pjt_nmk            character varying(200),
    ext_data_cd        character varying(100),
    e_tax_yn           character varying(10),
    unit_report_client character varying(200),
    sub_bus_no         character varying(50),
    procurement_yn     character varying(10),
    user_def_dc1       character varying(200),
    user_def_dc2       character varying(200),
    use_fg             character varying(50),
    use_nm             character varying(100),
    bizcon_fg          character varying(50),
    bizcon_nm          character varying(100),
    ship_tp            character varying(50),
    ship_nm            character varying(100),
    plan_day_type      character varying(50),
    plan_day           character varying(20),
    purpose_type       character varying(50),
    for_yn             character varying(10),
    check_data         character varying(200),
    check_state        character varying(100),
    check_order        character varying(100),
    fixed_order        character varying(100),
    insert_id          character varying(50),
    insert_ip          character varying(50),
    insert_dt          timestamp without time zone DEFAULT now(),
    modify_id          character varying(50),
    modify_ip          character varying(50),
    modify_dt          timestamp without time zone,
    objid              character varying,
    CONSTRAINT uk_client_mng_client_cd UNIQUE (client_cd)
);

CREATE INDEX IF NOT EXISTS idx_client_mng_client_cd ON public.client_mng (client_cd);
CREATE INDEX IF NOT EXISTS idx_client_mng_client_nm ON public.client_mng (client_nm);
CREATE INDEX IF NOT EXISTS idx_client_mng_use_yn    ON public.client_mng (use_yn);


-- ── 4. user_name() 함수 ─────────────────────────────────────────
-- 그리드 WRITER_NAME / MBOM_EDITOR 표시용 (운영 1:1).
-- 매퍼 productionplanning.mBomMgmtGridList: user_name(MH.WRITER) / user_name(COALESCE(MH.EDITER, MH.WRITER))

CREATE OR REPLACE FUNCTION public.user_name(v_user_id character varying)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
DECLARE
    v_user_name varchar;
BEGIN
    SELECT user_name INTO v_user_name
    FROM user_info
    WHERE user_id = v_user_id;
    RETURN v_user_name;
END;
$function$;
