CREATE OR REPLACE procedure MES_CX_YDM_BC_INITIAL_hand is
      a number(4);
      b varchar(200);

 CURSOR OPOrder_Cursor IS


select --t1.bal_year_month 记账日期,
 substr(t1.bal_year_month, 0, 4) || '-' || substr(t1.bal_year_month, 5, 2) bal_year_month,
  --记账日期
 '天淮中间库' ckmc,
 t1.judge_stove_no, 
 t2.material_code material_no,
  --物料编码,
 t2.material_name material_name,
  --物料名称,
 spec_code,
  --产品规格代码,
 t2.outer_diam || '*' || t2.wall_thickness spec_name,
  -- 产品规格描述,
 std_code,
  --执行标准代码,
 std_name,
  --执行标准名称,
 produccode,
  --品名代码,
 t2.prod_name producname,
  --品名描述,
 gradecode,
  --钢种代码,
 t2.sg_grade gradename,
  --钢种名称,
 steelcode,
  --钢级代码,
 t2.sg_sign steelname,
  --钢级名称,
 act_count,
  --期初库存支数,
 act_weight,
  --期初库存重量,
 0 act_in_count,
  --本期入库支数,
 0 act_in_weight,
  --本期入库重量,
 0 act_out_count,
  --本期出库支数,
 0 act_out_weight,
  --本期出库重量,
 --0 期末库存支数,
 --0 期末库存重量,
 t1.INSTOCK_TIME, --入库时间
 getislljg(t1.judge_stove_no,
            nvl((t1.order_no || t1.order_seq),
                replace(get_cp_order(t1.judge_stove_no), '/', ''))) is_lljg,
 (case
   when t1.process_cdoe = 'D' then
    '热轧'
   when t1.process_cdoe = 'E' then
    '镦拔扩'
   when t1.process_cdoe = 'F' then
    '热处理'
   else
    t1.process_cdoe
 end) process_name,
  --工序,
 --nvl(t1.process_cdoe,(case when t1.pline_code = 'C072' then '热轧'when  t1.pline_code = 'C091' then '热处理' else t1.pline_code end)) 工序,
 --(case when t1.pline_code = 'C072' then '热轧'when  t1.pline_code = 'C091' then '热处理' else t1.pline_code end) 工序,
 
 --'否' is_ckc,
  --是否出口材
      (case
       when nvl(t2.if_export,'内贸') ='内贸' then
        '否'
       when nvl(t2.if_export,'内贸') ='外贸' then
        '是'
       else
        '否'
      end) is_ckc,
  
 dimater,
  --外径
 height,
  --壁厚
 nvl((t1.order_no || t1.order_seq),
      replace(get_cp_order(t1.judge_stove_no), '/', '')) orderid,
  --合同号
 model_desc,
  -- 扣型描述
 get_cp_buyer(get_cp_order(t1.judge_stove_no)) buyer,
  --客户名称
 get_cp_pass(get_cp_order(t1.judge_stove_no)) pass --孔型
 
  from ydm_bc_initial t1, mvm_order_matno_info t2 --添加MVM

 where nvl((t1.order_no || t1.order_seq),
           replace(get_cp_order(t1.judge_stove_no), '/', '')) =
       t2.contractno_num(+)

           
   and
      --t1.bal_year_month = to_char(sysdate,'yyyymm')
       t1.bal_year_month in ('{0}')
   and t1.belong_code = '002023005'
   and nvl((t1.order_no || t1.order_seq),
           replace(get_cp_order(t1.judge_stove_no), '/', ''))
    
       not in ('XSXPQ-21A1666001',
               'XSXPQ-21A0068001',
               'XSXPQ-21A0067001',
               'XSXPQ-20A2850002',
               'XSXPQ-20A2496001',
               'XSXPQ-20A2487001',
               'XSXPQ-20A2458002',
               'XSXPQ-20A2398001',
               'XSXPQ-20A2378001',
               'XSXPQ-20A2292001',
               'XSXPQ-20A2247001',
               'XSXPQ-20A1104001',
               'GMXPQ-20A2235002',
               'GMXPQ-20A2235001',
               'EXSPQ-200072X001',
               'EXMGQ-210634E003',
               'EXMGQ-210604E012',
               'EXMGQ-210263E001',
               'EXMGQ-200457E001',
               'ZZBY-D230116007')
and nvl((t1.order_no || t1.order_seq),
               replace(get_cp_order(t1.judge_stove_no), '/', '')) not like 'ZZBY%'
;

curoporder oporder_cursor%rowtype;

begin
  open oporder_cursor;
  loop
    fetch oporder_cursor
      into curoporder;
    exit when oporder_cursor%notfound;

    insert into cx_ydm_bc_initial
      (bal_year_month, ckmc, material_no, material_name, spec_code,
       spec_name, std_code, std_name, produccode, producname, gradecode,
       gradename, steelcode, steelname, act_count, act_weight, act_in_count,
       act_in_weight, act_out_count, act_out_weight, is_lljg, process_name,
       is_ckc, dimater, height, pass, model_desc, buyer, orderid, bc_type,
       judge_stove_no,INSTOCK_TIME,INSERT_TIME)
    values
      (curoporder.bal_year_month, curoporder.ckmc, curoporder.material_no,
       curoporder.material_name, curoporder.spec_code, curoporder.spec_name,
       curoporder.std_code, curoporder.std_name, curoporder.produccode,
       curoporder.producname, curoporder.gradecode, curoporder.gradename,
       curoporder.steelcode, curoporder.steelname, curoporder.act_count,
       curoporder.act_weight, curoporder.act_in_count,
       curoporder.act_in_weight, curoporder.act_out_count,
       curoporder.act_out_weight, curoporder.is_lljg,
       curoporder.process_name, curoporder.is_ckc, curoporder.dimater,
       curoporder.height, curoporder.pass, curoporder.model_desc,
       curoporder.buyer, curoporder.orderid, '半成品期初库存',
       curoporder.judge_stove_no,curOPOrder.INSTOCK_TIME,sysdate);
  end loop;
  close oporder_cursor;
  commit;

end mes_cx_ydm_bc_initial_hand;