create or replace package body cuttoship_lots is

  procedure prod_run(p_w_day date) as
  begin
    delete cuttoship_lot;
    commit;
    prod_erp(p_w_day);
    prod_szbgerp(p_w_day);
    prod_shs(p_w_day);
    prod_subedit;
    prod_ins_rpt;
    prod_update_0;
  end;  
  
  procedure prod_erp(p_w_day date) as
  begin
    --panel qty
       for c in (select 'erp' as site,lotno,max(gen_date) w_day
                  from facd321
                  where gen_date>=trunc(p_w_day) and gen_date<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
       end loop;
       
       --trf to sewing qty
       for c in (select 'erp' as site,lotno,max(import_time) w_day
                  from facd512
                  where import_time>=trunc(p_w_day) and import_time<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
       end loop;
       
       --output qty
       for c in (select 'erp' as site,lotno,max(lrrq) w_day
                  from facd105
                  where lrrq>=trunc(p_w_day) and lrrq<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
       end loop;
       
       --leftover garments
       for c in (select 'erp' as site,lotno,max(lrrq) w_day
                  from facd550
                  where lrrq>=trunc(p_w_day) and lrrq<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
       end loop;
       
       --reason qty
       for c in (select 'erp' as site,lotno,max(lastupdate) w_day
                  from facd343
                  where lastupdate>=trunc(p_w_day) and lastupdate<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
       end loop;
       
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_erp',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_erp',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
      commit;
  end; 
  
  procedure prod_szbgerp(p_w_day date) as
    p_ck number;
  begin
       p_ck:=0;
       --panel qty
       for c in (select 'szbgerp' as site,lotno,max(gen_date) w_day
                  from facd321@szbgerp
                  where gen_date>=trunc(p_w_day) and gen_date<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('szbgerp');
         p_ck:=0;
       end if;
       
       --trf to sewing qty
       for c in (select 'szbgerp' as site,lotno,max(import_time) w_day
                  from facd512@szbgerp
                  where import_time>=trunc(p_w_day) and import_time<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('szbgerp');
         p_ck:=0;
       end if;
       
       --output qty
       for c in (select 'szbgerp' as site,lotno,max(lrrq) w_day
                  from facd105@szbgerp
                  where lrrq>=trunc(p_w_day) and lrrq<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('szbgerp');
         p_ck:=0;
       end if;
       
       --leftover garments
       for c in (select 'szbgerp' as site,lotno,max(w_day) w_day
                  from facd340@szbgerp
                  where w_day>=trunc(p_w_day) and w_day<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('szbgerp');
         p_ck:=0;
       end if;
       
       --reason qty
       for c in (select 'szbgerp' as site,lotno,max(lastupdate) w_day
                  from facd343@szbgerp
                  where lastupdate>=trunc(p_w_day) and lastupdate<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('szbgerp');
       end if;
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_szbgerp',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_szbgerp',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
      commit;
  end; 
  
  procedure prod_shs(p_w_day date) as
    p_ck number;
  begin
       p_ck:=0;
       
       --panel qty
       for c in (select 'shs' as site,lotno,max(gen_date) w_day
                  from facd321@shs
                  where gen_date>=trunc(p_w_day) and gen_date<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('shs');
         p_ck:=0;
       end if;
       
       
       --trf to sewing qty
       for c in (select 'shs' as site,lotno,max(import_time) w_day
                  from facd512@shs
                  where import_time>=trunc(p_w_day) and import_time<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('shs');
         p_ck:=0;
       end if;
       
       
       --output qty
       for c in (select 'shs' as site,lotno,max(lrrq) w_day
                  from facd105@shs
                  where lrrq>=trunc(p_w_day) and lrrq<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('shs');
         p_ck:=0;
       end if;
       
       --leftover garments
       for c in (select 'shs' as site,lotno,max(w_day) w_day
                  from facd340@shs
                  where w_day>=trunc(p_w_day) and w_day<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('shs');
         p_ck:=0;
       end if;
       
       --reason qty
       for c in (select 'shs' as site,lotno,max(lastupdate) w_day
                  from facd343@shs
                  where lastupdate>=trunc(p_w_day) and lastupdate<trunc(p_w_day+1)
                  group by lotno) loop
           insert into cuttoship_lot
           values(c.site,c.lotno,c.w_day);
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
         dbms_session.close_database_link('shs');
       end if;
       
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_shs',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_shs',
         trunc(p_w_day),
         SYSDATE,
         get_OS('OS_USER'));
      commit;
  end; 
  
  procedure prod_subedit as
    maxday date;
    p_ck number;
  begin
       --delete the smae lotno records
       for c in (select site,lotno,day
                  from cuttoship_lot
                  where site='erp') loop
           select count(0) into p_ck 
           from  cuttoship_lot
           where site='szbgerp' and lotno=c.lotno;
           if p_ck>0 then
             select day into maxday
             from  cuttoship_lot
             where site='szbgerp' and lotno=c.lotno;
             if maxday>c.day then
                delete cuttoship_lot where site=c.site and lotno=c.lotno;
             elsif maxday<c.day then
                delete cuttoship_lot where site='szbgerp' and lotno=c.lotno;
             end if;
           end if;
           commit;
       end loop;
       
       for c in (select site,lotno,day
                  from cuttoship_lot
                  where site in('erp','szbgerp')) loop
           select count(0) into p_ck 
           from  cuttoship_lot
           where site='shs' and lotno=c.lotno;
           if p_ck>0 then
             select day into maxday
             from  cuttoship_lot
             where site='shs' and lotno=c.lotno;
             if maxday>c.day then
                delete cuttoship_lot where site=c.site and lotno=c.lotno;
             elsif maxday<c.day then
                delete cuttoship_lot where site='shs' and lotno=c.lotno;
             end if;
           end if;
           commit;
       end loop;
       
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_subedit',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_subedit',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
      commit;
  end; 
  
  procedure prod_ins_rpt as
  begin 
    prod_ins_rpt_erp;
    prod_ins_rpt_szbgerp;
    prod_ins_rpt_shs;
    prod_ins_rpt_protex;
  end; 

  procedure prod_ins_rpt_erp as
  begin
       --lot info
       for c1 in (select t1.lotno,lot_status,clot,wrk_no,customer,styleno,rmk_qty 
                 from facd101 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 ) loop
           update cuttoship set lotstatus=c1.lot_status, clot=c1.clot, workshop=c1.wrk_no, 
           customer=c1.customer, styleno=c1.styleno, vpoqty=c1.rmk_qty
           where lotno=c1.lotno;
           
           if sql%notfound then
             insert into cuttoship
             (lotno, lotstatus, clot, workshop, customer, styleno, vpoqty)
             values
             (c1.lotno,c1.lot_status,c1.clot,c1.wrk_no,c1.customer,c1.styleno,c1.rmk_qty);
           end if;
           commit;
       end loop;
       --Mfg. Order Qty
       for c2 in (select t1.lotno,sum(nvl(qty, 0)) mfgqty
                 from facd302 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set mfgqty=c2.mfgqty
           where lotno=c2.lotno;
           commit;
       end loop;
       --Panel Qty 
       for c3 in (select t1.lotno,sum(nvl(qty, 0)) panelqty
                 from facd321 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 where gen_date is not null and nvl(isactive, '0') = '0'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set panelqty=c3.panelqty
           where lotno=c3.lotno;
           commit;
       end loop;
       --Trf to Sewing & Panel Received  
       for c4 in (select t1.lotno,sum(nvl(qty, 0)) trftosewingqty 
                 from facd512 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 where op='888'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set trftosewingqty=c4.trftosewingqty,panelreceived=c4.trftosewingqty
           where lotno=c4.lotno;
           commit;
       end loop;
       --Output Qty 
       for c5 in (select t1.lotno,sum(nvl(qty, 0)) outputqty 
                 from facd105 t1
                 inner join facd102 t2 on t1.lotno=t2.lotno and t1.operno=t2.lineno 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t2.ch_po='FOF'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set outputqty=c5.outputqty
           where lotno=c5.lotno;
           commit;
       end loop;
       --Leftover Garments Stored in WH 
       --1.A Quality,2.B Quality 
       for c6 in (select A.lotno,(A.defect222-B.defect223) defectqty,A.deadqty
                  from
                   (select t1.lotno,sum(nvl(repqty,0)-nvl(rplqty,0)) defect222,sum(nvl(damqty, 0)) deadqty 
                 from facd550 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 where operno='222'
                 group by t1.lotno) A,
                  (select t1.lotno,sum(nvl(repqty,0)) defect223 
                 from facd550 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t2 on t1.lotno=t2.lotno
                 where operno='223'
                 group by t1.lotno) B
                 where A.lotno=B.lotno(+)
                 ) loop
           update cuttoship 
           set defectqty=c6.defectqty,deadqty=c6.deadqty
           where lotno=c6.lotno;
           commit;
       end loop;
       --reason qty
       for c7 in (select distinct S.lotno,qwlqty,wlpzqty,zgpzqty,khyyqty,zlwtqty,ysqty
                  from
                 (SELECT t1.lotno from facd343 t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno)S,
                 (select t1.lotno,nvl(t1.qty,0) qwlqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='A')A,
                 (select t1.lotno,nvl(t1.qty,0) wlpzqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='B')B,
                 (select t1.lotno,nvl(t1.qty,0) zgpzqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='C')C,
                 (select t1.lotno,nvl(t1.qty,0) khyyqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='D')D,
                 (select t1.lotno,nvl(t1.qty,0) zlwtqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='E')E,
                 (select t1.lotno,nvl(t1.qty,0) ysqty 
                 from facd343 t1 
                 inner join facd343_rea t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='erp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='F')F
                 where S.lotno=A.lotno(+)
                 and S.lotno=B.lotno(+)
                 and S.lotno=C.lotno(+)
                 and S.lotno=D.lotno(+)
                 and S.lotno=E.lotno(+)
                 and S.lotno=F.lotno(+)
                 ) loop
           update cuttoship 
           set qwlqty=c7.qwlqty,wlpzqty=c7.wlpzqty,zgpzqty=c7.zgpzqty,khyyqty=c7.khyyqty,
           zlwtqty=c7.zlwtqty,ysqty=c7.ysqty,
           subqty=c7.qwlqty+c7.wlpzqty+c7.zgpzqty+c7.khyyqty+c7.zlwtqty+c7.ysqty
           where lotno=c7.lotno;
           commit;
       end loop;
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_ins_rpt_erp',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_ins_rpt_erp',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
      commit;
  end;
  
  procedure prod_ins_rpt_szbgerp as
    p_ck number;
  begin
       p_ck:=0;
       
       --lot info
       for c1 in (select t1.lotno,lot_status,clot,wrk_no,customer,styleno,rmk_qty 
                 from facd101@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t2 on t1.lotno=t2.lotno
                 ) loop
           update cuttoship set lotstatus=c1.lot_status, clot=c1.clot, workshop=c1.wrk_no, 
           customer=c1.customer, styleno=c1.styleno, vpoqty=c1.rmk_qty
           where lotno=c1.lotno;
           
           if sql%notfound then
             insert into cuttoship
             (lotno, lotstatus, clot, workshop, customer, styleno, vpoqty)
             values
             (c1.lotno,c1.lot_status,c1.clot,c1.wrk_no,c1.customer,c1.styleno,c1.rmk_qty);
           end if;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --Mfg. Order Qty
       for c2 in (select t1.lotno,sum(nvl(qty, 0)) mfgqty
                 from facd302@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t2 on t1.lotno=t2.lotno
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set mfgqty=c2.mfgqty
           where lotno=c2.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --Panel Qty 
       for c3 in (select t1.lotno,sum(nvl(qty, 0)) panelqty
                 from facd321@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t2 on t1.lotno=t2.lotno
                 where gen_date is not null and nvl(isactive, '0') = '0'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set panelqty=c3.panelqty
           where lotno=c3.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --Trf to Sewing & Panel Received  
       for c4 in (select t1.lotno,sum(nvl(qty, 0)) trftosewingqty 
                 from facd512@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t2 on t1.lotno=t2.lotno
                 where op='888'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set trftosewingqty=c4.trftosewingqty,panelreceived=c4.trftosewingqty
           where lotno=c4.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --Output Qty 
       for c5 in (select t1.lotno,sum(nvl(qty, 0)) outputqty 
                 from facd105@szbgerp t1
                 inner join facd102@szbgerp t2 on t1.lotno=t2.lotno and t1.operno=t2.lineno 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t2.ch_po='FOF'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set outputqty=c5.outputqty
           where lotno=c5.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --Leftover Garments 
       --1.A Quality,2.B Quality 
       for c6 in (select t1.lotno, sum(nvl(defect, 0)) defectqty, sum(nvl(dead, 0)) deadqty
                 from facd340@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t2 on t1.lotno=t2.lotno
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set defectqty=c6.defectqty,deadqty=c6.deadqty
           where lotno=c6.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
          p_ck:=0;
       end if;
       
       --reason qty
       for c7 in (select distinct S.lotno,qwlqty,wlpzqty,zgpzqty,khyyqty,zlwtqty,ysqty
                  from
                 (SELECT t1.lotno from facd343@szbgerp t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno)S,
                 (select t1.lotno,nvl(t1.qty,0) qwlqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='A')A,
                 (select t1.lotno,nvl(t1.qty,0) wlpzqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='B')B,
                 (select t1.lotno,nvl(t1.qty,0) zgpzqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='C')C,
                 (select t1.lotno,nvl(t1.qty,0) khyyqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='D')D,
                 (select t1.lotno,nvl(t1.qty,0) zlwtqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='E')E,
                 (select t1.lotno,nvl(t1.qty,0) ysqty 
                 from facd343@szbgerp t1 
                 inner join facd343_rea@szbgerp t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='szbgerp') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='F')F
                 where S.lotno=A.lotno(+)
                 and S.lotno=B.lotno(+)
                 and S.lotno=C.lotno(+)
                 and S.lotno=D.lotno(+)
                 and S.lotno=E.lotno(+)
                 and S.lotno=F.lotno(+)
                 ) loop
           update cuttoship 
           set qwlqty=c7.qwlqty,wlpzqty=c7.wlpzqty,zgpzqty=c7.zgpzqty,khyyqty=c7.khyyqty,
           zlwtqty=c7.zlwtqty,ysqty=c7.ysqty,
           subqty=c7.qwlqty+c7.wlpzqty+c7.zgpzqty+c7.khyyqty+c7.zlwtqty+c7.ysqty
           where lotno=c7.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       /**/
       --must have data before close database link!
       if p_ck>0 then
          dbms_session.close_database_link('szbgerp');
       end if;
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_ins_rpt_szbgerp',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_ins_rpt_szbgerp',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
      commit;
  end;
  
  procedure prod_ins_rpt_shs as
    p_ck number;
  begin
       p_ck:=0;
       --lot info
       for c1 in (select t1.lotno,lot_status,clot,wrk_no,customer,styleno,rmk_qty 
                 from facd101@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t2 on t1.lotno=t2.lotno
                 ) loop
           update cuttoship set lotstatus=c1.lot_status, clot=c1.clot, workshop=c1.wrk_no, 
           customer=c1.customer, styleno=c1.styleno, vpoqty=c1.rmk_qty
           where lotno=c1.lotno;
           
           if sql%notfound then
             insert into cuttoship
             (lotno, lotstatus, clot, workshop, customer, styleno, vpoqty)
             values
             (c1.lotno,c1.lot_status,c1.clot,c1.wrk_no,c1.customer,c1.styleno,c1.rmk_qty);
           end if;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --Mfg. Order Qty
       for c2 in (select t1.lotno,sum(nvl(qty, 0)) mfgqty
                 from facd302@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t2 on t1.lotno=t2.lotno
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set mfgqty=c2.mfgqty
           where lotno=c2.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --Panel Qty 
       for c3 in (select t1.lotno,sum(nvl(qty, 0)) panelqty
                 from facd321@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t2 on t1.lotno=t2.lotno
                 where gen_date is not null and nvl(isactive, '0') = '0'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set panelqty=c3.panelqty
           where lotno=c3.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --Trf to Sewing & Panel Received  
       for c4 in (select t1.lotno,sum(nvl(qty, 0)) trftosewingqty 
                 from facd512@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t2 on t1.lotno=t2.lotno
                 where op='888'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set trftosewingqty=c4.trftosewingqty,panelreceived=c4.trftosewingqty
           where lotno=c4.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --Output Qty 
       for c5 in (select t1.lotno,sum(nvl(qty, 0)) outputqty 
                 from facd105@shs t1
                 inner join facd102@shs t2 on t1.lotno=t2.lotno and t1.operno=t2.lineno 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t2.ch_po='FOF'
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set outputqty=c5.outputqty
           where lotno=c5.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --Leftover Garments 
       --1.A Quality,2.B Quality 
       for c6 in (select t1.lotno, sum(nvl(defect, 0)) defectqty, sum(nvl(dead, 0)) deadqty
                 from facd340@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t2 on t1.lotno=t2.lotno
                 group by t1.lotno
                 ) loop
           update cuttoship 
           set defectqty=c6.defectqty,deadqty=c6.deadqty
           where lotno=c6.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
          p_ck:=0;
       end if;
       --reason qty
       for c7 in (select distinct S.lotno,qwlqty,wlpzqty,zgpzqty,khyyqty,zlwtqty,ysqty
                  from
                 (SELECT t1.lotno from facd343@shs t1
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno)S,
                 (select t1.lotno,nvl(t1.qty,0) qwlqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='A')A,
                 (select t1.lotno,nvl(t1.qty,0) wlpzqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='B')B,
                 (select t1.lotno,nvl(t1.qty,0) zgpzqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='C')C,
                 (select t1.lotno,nvl(t1.qty,0) khyyqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='D')D,
                 (select t1.lotno,nvl(t1.qty,0) zlwtqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='E')E,
                 (select t1.lotno,nvl(t1.qty,0) ysqty 
                 from facd343@shs t1 
                 inner join facd343_rea@shs t2 on t1.reasoncode=t2.reasoncode 
                 inner join (select distinct site,lotno
                 from cuttoship_lot
                 where site='shs') t3 on t1.lotno=t3.lotno
                 where t1.isconfirm='Y' and t1.reasoncode='F')F
                 where S.lotno=A.lotno(+)
                 and S.lotno=B.lotno(+)
                 and S.lotno=C.lotno(+)
                 and S.lotno=D.lotno(+)
                 and S.lotno=E.lotno(+)
                 and S.lotno=F.lotno(+)
                 ) loop
           update cuttoship 
           set qwlqty=c7.qwlqty,wlpzqty=c7.wlpzqty,zgpzqty=c7.zgpzqty,khyyqty=c7.khyyqty,
           zlwtqty=c7.zlwtqty,ysqty=c7.ysqty,
           subqty=c7.qwlqty+c7.wlpzqty+c7.zgpzqty+c7.khyyqty+c7.zlwtqty+c7.ysqty
           where lotno=c7.lotno;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('shs');
       end if;
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_ins_rpt_shs',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_ins_rpt_shs',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
      commit;
  end;
  
  procedure prod_ins_rpt_protex as
    p_ck number;
  begin
       p_ck:=0;
       --shipmentdate
       for c1 in (select t2.cstord,max(t1.plcomn),to_date(cast(DT#5Y as nvarchar2(4))||'-'||cast(DT#5M as nvarchar2(2))||'-'||cast(DT#5D as nvarchar2(2))) shipmentdate
                 from proda201.ORFPLCA@db2gzbg t1
                 left join proda201.ORFPLFB@db2gzbg t2 on t1.plcomn=t2.plcomn
                 inner join (select distinct lotno from cuttoship_lot) t3 on t2.cstord=t3.lotno
                 group by t2.cstord,DT#5Y,DT#5M,DT#5D  
                 ) loop
           update cuttoship 
           set shipmentdate=c1.shipmentdate
           where lotno=c1.cstord;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('db2gzbg');
          p_ck:=0;
       end if;
       --sample qty
       /*
       for c2 in (select t1.cstord,nvl(sum(qty),0) sampleqty
                 from proda201.orforbc@db2gzbg t1
                 inner join (select distinct lotno from cuttoship_lot) t2 on t1.cstord=t2.lotno
                 group by t1.cstord 
                 ) loop
           update cuttoship 
           set sampleqty=c2.sampleqty
           where lotno=c2.cstord;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('db2gzbg');
          p_ck:=0;
       end if;
       */
       --shipped qty
       for c3 in (select t1.cstord,nvl(sum(qtypck),0) shippedqty
                 from proda201.orfplfb@db2gzbg t1
                 inner join (select distinct lotno from cuttoship_lot) t2 on t1.cstord=t2.lotno
                 group by t1.cstord 
                 ) loop
           update cuttoship 
           set shippedqty=c3.shippedqty
           where lotno=c3.cstord;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('db2gzbg');
          p_ck:=0;
       end if;
       --Leftover Garments Stored in WH 
       --1.A Quality,2.B Quality 
       for c4 in (select S.cstord,astq,bstq
                  from
                  (select t1.cstord
                 from proda201.fgfstka@db2gzbg t1
                 inner join (select distinct lotno from cuttoship_lot) t2 on t1.cstord=t2.lotno
                  ) S,
                  (select t1.cstord,nvl(sum(stq),0) astq
                 from proda201.fgfstka@db2gzbg t1
                 inner join (select distinct lotno from cuttoship_lot) t2 on t1.cstord=t2.lotno
                 group by t1.cstord) A, 
                  (select t1.cstord,nvl(sum(stq),0) bstq
                 from proda201.fgfstka@db2gzbg t1
                 inner join (select distinct lotno from cuttoship_lot) t2 on t1.cstord=t2.lotno
                 group by t1.cstord) B
                  where S.cstord=A.cstord(+)
                  and S.cstord=B.cstord(+)
                 ) loop
           update cuttoship 
           set astq=c4.astq,bstq=c4.bstq
           where lotno=c4.cstord;
           commit;
           p_ck:=p_ck+1;
       end loop;
       if p_ck>0 then
          dbms_session.close_database_link('db2gzbg');
       end if;
       --radio,balabce,variance
       update cuttoship 
       set radio=decode(vpoqty,0,0,round(1-shippedqty/vpoqty,4)),
       balance=panelreceived-sampleqty-shippedqty-astq-bstq,
       variance=panelqty-sampleqty-shippedqty-astq-bstq;
       commit;
       --log
       insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
       values
        ('Success',
         'cuttoship_lots',
         'prod_ins_rpt_protex',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
       commit;
  exception
    when others then
      insert into facd610
        (op_type, info_type, prod_name, w_day, op_date, os_user)
      values
        ('Failure',
         'cuttoship_lots',
         'prod_ins_rpt_protex',
         trunc(sysdate),
         sysdate,
         get_OS('OS_USER'));
      commit;
  end;
  
  procedure prod_update_0 as
  begin
    update cuttoship set vpoqty=0 where vpoqty is null;
    update cuttoship set mfgqty=0 where mfgqty is null;
    update cuttoship set panelqty=0 where panelqty is null;
    update cuttoship set trftosewingqty=0 where trftosewingqty is null;
    update cuttoship set panelreceived=0 where panelreceived is null;
    update cuttoship set sampleqty=0 where sampleqty is null;
    update cuttoship set outputqty=0 where outputqty is null;
    update cuttoship set defectqty=0 where defectqty is null;
    update cuttoship set deadqty=0 where deadqty is null;
    update cuttoship set shippedqty=0 where shippedqty is null;
    update cuttoship set radio=0 where radio is null;
    update cuttoship set balance=0 where balance is null;
    update cuttoship set astq=0 where astq is null;
    update cuttoship set bstq=0 where bstq is null;
    update cuttoship set variance=0 where variance is null;
    update cuttoship set qwlqty=0 where qwlqty is null;
    update cuttoship set wlpzqty=0 where wlpzqty is null;
    update cuttoship set zgpzqty=0 where zgpzqty is null;
    update cuttoship set khyyqty=0 where khyyqty is null;
    update cuttoship set zlwtqty=0 where zlwtqty is null;
    update cuttoship set ysqty=0 where ysqty is null;
    update cuttoship set subqty=0 where subqty is null;
    commit;
  end; 
end cuttoship_lots;

posted on 2014-04-03 14:26  vinsonLu  阅读(663)  评论(0编辑  收藏  举报