-- ============================================================
-- 발주/입고 운영 sample 데이터 → RPS 이관
-- 운영: 211.115.91.141:11133/waceplm
--   purchase_order_master 1건 / purchase_order_part 1건 / arrival_plan 1건
-- 대상: 211.115.91.141:11134/vexplor_rps
--
-- FK 매칭 (확인):
--   sales_request_objid='-233034270' → RPS sales_request_master.objid (있음)
--   contract_mgmt_objid='-1752090174' → 운영DB project_mgmt.objid (RPS contract_mgmt 미매칭, project_mgmt 매칭)
--   part_objid=1868260552 → RPS part_mng (있음)
--   partner_objid='0000000007' → RPS client_mng 서울반도체(주) (있음)
--
-- 멱등성: ON CONFLICT DO NOTHING
-- ============================================================

-- ── purchase_order_master (RPS 이미 존재하면 mail_send_* 만 보강) ──
-- PK constraint 없어 ON CONFLICT 사용 불가 → WHERE NOT EXISTS 패턴
INSERT INTO purchase_order_master
  (objid, purchase_order_no, partner_objid, contract_mgmt_objid, sales_request_objid,
   regdate, writer, status, mail_send_yn, mail_send_date,
   sales_mng_user_id, payment_terms)
SELECT
  '-2135417309','RPS26-0401-01','0000000007','-1752090174','-233034270',
   '2026-04-01 07:20:58.687075','ady1225','create','Y','2026-04-03',
   'ish0312','0001069'
WHERE NOT EXISTS (SELECT 1 FROM purchase_order_master WHERE objid='-2135417309');

-- 이미 있던 행에는 매퍼 필수 필드(mail_send_*) 보강
UPDATE purchase_order_master
   SET mail_send_yn='Y', mail_send_date='2026-04-03'
 WHERE objid='-2135417309'
   AND COALESCE(mail_send_yn,'') = '';

-- ── purchase_order_part ───────────────────────────────────────
INSERT INTO purchase_order_part
  (objid, purchase_order_master_objid, part_objid, order_qty, partner_price,
   remark, writer, regdate, part_name, spec, supply_unit_price, unit,
   part_no, qty, part_delivery_place, delivery_request_date)
VALUES
  ('-192149597','-2135417309',1868260552,'1','10000',
   'W/M ASSY (RWMR1070-NO07 LH) / HOLDER','ady1225','2026-04-01 07:20:58.687075',
   'Ti(GR5)','Ø50*22','10000','0001400','C3P50L22','1','RPS','2026-04-03')
ON CONFLICT (objid) DO NOTHING;

-- ── arrival_plan ──────────────────────────────────────────────
INSERT INTO arrival_plan
  (objid, parent_objid, order_part_objid, part_objid,
   arrival_qty, receipt_qty, receipt_date, location,
   writer, group_seq, seq, inventory_status, sub_location, receiver_id)
VALUES
  ('1030275443','-2135417309','-192149597',1868260552,
   '1','1','2026-04-01','L101',
   'ady1225','1','1','Y','1490000','ady1225')
ON CONFLICT (objid) DO NOTHING;

-- 검증: 매퍼 WHERE (mail_send_date IS NOT NULL AND status='create') 통과 여부
-- SELECT pom.purchase_order_no, pop.part_no, ap.receipt_date
--   FROM purchase_order_master pom
--   JOIN purchase_order_part pop ON pop.purchase_order_master_objid = pom.objid
--   LEFT JOIN arrival_plan ap ON ap.parent_objid = pom.objid AND ap.part_objid = pop.part_objid
--  WHERE pom.mail_send_date IS NOT NULL AND pom.status = 'create';
