-- ============================================================
-- 발주서 + 입고관리 — 구매관리 입고 3메뉴 + 발주서관리 의존 테이블
-- 원본: 운영DB 211.115.91.141:11133/waceplm
--   purchase_order_master 1건 (mail_send_yn='Y', status='create')
--   purchase_order_part    1건 (RPS26-0401-01 / C3P50L22)
--   arrival_plan           1건 (receipt_qty=1, receipt_date=2026-04-01)
-- 추출일: 2026-05-15
-- 적용대상: vexplor_rps (11134)
--
-- 운영 ↔ RPS 타입 차이:
--   part_objid: 운영 varchar(64) → RPS bigint (part_mng.objid bigint 호환)
--
-- 매퍼:
--   deliveryMngPartList:  wace_plm/src/com/pms/mapper/purchaseOrder.xml:6309-6543
--   purchaseCloseList:    wace_plm/src/com/pms/mapper/purchaseOrder.xml:6549-6765
--   projectPurchaseStat:  wace_plm/src/com/pms/mapper/purchaseOrder.xml:6768-6951
--
-- 함정:
--   1) wace 매퍼는 PROJECT_MGMT.OBJID = POM.CONTRACT_MGMT_OBJID 로 LEFT JOIN
--      (즉 contract_mgmt_objid 컬럼명이 실제로는 project_mgmt 키를 저장)
--   2) WHERE: POM.MAIL_SEND_DATE IS NOT NULL AND POM.STATUS='create'
-- ============================================================

-- ── 1. purchase_order_master 보충 컬럼 (10개) ───────────────────
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS mail_send_yn        varchar;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS mail_send_date      varchar;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS form_type           varchar(20);
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS sales_mng_user_id2  varchar(50);
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS request_content     text;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS purchase_close_date varchar(10);
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS shipment            varchar;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS packing             varchar;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS validity            varchar;
ALTER TABLE purchase_order_master ADD COLUMN IF NOT EXISTS attn_to             varchar;

-- ── 2. purchase_order_part (운영 43 cols 1:1, part_objid 만 bigint) ─
CREATE TABLE IF NOT EXISTS purchase_order_part (
    objid                       varchar(64) NOT NULL,
    purchase_order_master_objid varchar(64),
    part_objid                  bigint,
    order_qty                   varchar,
    partner_price               varchar,
    remark                      varchar,
    writer                      varchar,
    regdate                     timestamp,
    status                      varchar,
    part_name                   varchar,
    do_no                       varchar,
    thickness                   varchar,
    width                       varchar,
    height                      varchar,
    out_diameter                varchar,
    length                      varchar,
    in_diameter                 varchar,
    inven_total_qty             varchar,
    ld_part_objid               varchar,
    spec                        varchar,
    maker                       varchar,
    supply_unit_price           varchar,
    unit                        varchar,
    price1                      varchar,
    price2                      varchar,
    price3                      varchar,
    part_no                     varchar,
    supply_unit_vat_price       varchar,
    price4                      varchar,
    supply_unit_vat_sum_price   varchar,
    total_order_qty             varchar,
    stock_qty                   varchar,
    real_order_qty              varchar,
    update_date                 timestamp,
    modifier                    varchar,
    real_supply_price           varchar,
    bom_qty                     varchar,
    qty                         varchar,
    part_delivery_place         varchar(50),
    product_name                varchar(200),
    work_order_no               varchar(50),
    delivery_request_date       varchar(20),
    currency                    varchar,
    CONSTRAINT purchase_order_part_pkey PRIMARY KEY (objid)
);

CREATE INDEX IF NOT EXISTS idx_pop_master ON purchase_order_part (purchase_order_master_objid);
CREATE INDEX IF NOT EXISTS idx_pop_part   ON purchase_order_part (part_objid);

-- ── 3. arrival_plan (운영 37 cols 1:1, part_objid bigint) ───────
CREATE TABLE IF NOT EXISTS arrival_plan (
    objid                varchar(64) NOT NULL,
    parent_objid         varchar(64),
    order_part_objid     varchar(64),
    part_objid           bigint,
    arrival_plan_date    varchar,
    re_arrival_plan_date varchar,
    arrival_qty          varchar,
    receipt_qty          varchar,
    genuine_qty          varchar,
    receipt_date         varchar,
    inspection_date      varchar,
    location             varchar,
    error_qty            varchar,
    error_reason         varchar,
    attribution          varchar,
    status               varchar,
    assembly_status      varchar,
    writer               varchar,
    group_seq            varchar,
    seq                  varchar,
    defect_content       varchar,
    defect_action        varchar,
    defect_note          varchar,
    defect_action_date   varchar,
    defect_action_title  varchar,
    inventory_status     varchar,
    sub_location         varchar,
    receiver_id          varchar,
    purchase_close_date  varchar,
    foreign_type         varchar(10),
    exchange_rate        numeric(15,2),
    duty                 numeric(15,2),
    import_vat           numeric(15,2),
    tax_invoice_date     varchar(10),
    export_decl_no       varchar(100),
    loading_date         varchar(10),
    tax_type             varchar(20),
    CONSTRAINT arrival_plan_pkey PRIMARY KEY (objid)
);

CREATE INDEX IF NOT EXISTS idx_arrival_parent     ON arrival_plan (parent_objid);
CREATE INDEX IF NOT EXISTS idx_arrival_order_part ON arrival_plan (order_part_objid);
CREATE INDEX IF NOT EXISTS idx_arrival_part       ON arrival_plan (part_objid);
