declare
 v47422 int;
 newID int;
 vV_LS int;
begin

 select A.ID into v47422
  from DG_PRODLS A
  where A.PRODID is null and A.S_CLASS=1004841 and NAME='47422';
  
 for q_ in (
            select D.ID,D.DPODP,D.ND,D.PRGROUPID,PL.NAME,PL.ID as LS_PRD,PL.KODVAL,PL.BSVALUE,PL.V_LS,LT.ID as TYPEID
             from DG_DOG D
             join DG_PRODLS PL on PL.PRODID=D.PRGROUPID
             join DG_LS_TYPES LT on LT.ID=PL.TYPLS
              where D.S_CLASS=1003737 and D.S_S=1000235 and D.DBEG>=sysdate and LT.SYSTYPE=40
              and not exists (select L.ID from DG_LS_DOG L where L.DOG_ID=D.ID and L.LS_PRD=PL.ID)
             ) LOOP
    
   select MainID.NextVal into newID from dual;                      
   insert into DG_LS_DOG (ID,TYPEID,DOG_ID,BDATE,NAME,LS_PRD,KODVAL,BSVALUE) 
   values (newID,q_.TYPEID,q_.ID,q_.DPODP,q_.NAME,q_.LS_PRD,q_.KODVAL,q_.BSVALUE);
   
   if not q_.V_LS is null then
    vV_LS := S_MBK_GET_V_LS( q_.ID, newID, q_.DPODP);                  
    if vV_LS<>-1 then                                                                                              
      insert into DG_PERIODIC_OBJ (ID,VNESHID,DDB   ,DDE    ,ATT   ,VAL_ID     ,VAL_STR,VAL_DATE,VAL_NUM)                                                                                                                      
      values          (MainId.Nextval,newID    ,q_.DPODP,dbo.D4000(),'LSID', vV_LS ,null   ,null    ,null);                                                                                                  
    end if;                                                                                                   
   end if;
              
 
 end LOOP; 
  

end;


