create or replace procedure     S_OTB_LS 
  
( 
  v_dBeg date, 
  v_dEnd date,   
  v_nacValID number,   
  v_filialID number,   
  v_svID number, 
  v_Mode number default 1  
) 
AS                   
 v_DateAct date;   
 v_DateTo date;   
 v_D4000 date;   
 v_D1900 date; 
 v_SID number(12); 
 v_DDACT_SUBSCR date; 
 v_dLastSPOD date; 
 v_nActCount number(12); 
 v_nCount number(12);  
 v_dMax_S_MDT date;
 v_dMax_S_MDT_RKO date;
 v_dMax_S_MDT_Last date;
  
BEGIN  
  
  v_D4000 := to_date('12.12.4000','DD.MM.YYYY');   
  v_D1900 := to_date('01.01.1900','dd.mm.yyyy'); 
  v_DateTo := v_dEnd; 
  v_SID := to_number(sys_context('USERENV','SID')); 
  S_PROGRESS (1000001,'  Анализ даты актуальности по остаткам РКО',0,0);  
  /* Получим дату актуальности по остаткам РКО */ 
  select nvl(DACT,v_D4000) into v_DDACT_SUBSCR from OTB_SUBSCRIBER;
  
  select count(1) into v_nCount from OTB_RKO_DAYCLOSE_DOC where FILIAL = v_filialID;
  select max(S_MDT) into v_dMax_S_MDT_RKO from RKO_DAYCLOSE_DOC where DD < v_DDACT_SUBSCR and S_S > 1;

  if v_nCount = 0 then
    insert into OTB_RKO_DAYCLOSE_DOC(ID,FILIAL,MAX_S_MDT)
    values(mainid.nextval,v_filialID,v_dMax_S_MDT_RKO); 
  end if;
    
  select max(MAX_S_MDT) into v_dMax_S_MDT_Last from OTB_RKO_DAYCLOSE_DOC where FILIAL = v_filialID;
  v_dMax_S_MDT := v_dMax_S_MDT_Last;
  if v_dMax_S_MDT_RKO > v_dMax_S_MDT_Last then
    v_dMax_S_MDT := v_dMax_S_MDT_RKO;
  end if;  

  v_DateAct := dbo.S_OTB_GET_ACT_DD(v_dMax_S_MDT,v_dEnd,v_DDACT_SUBSCR,v_filialID,1);

  update OTB_RKO_DAYCLOSE_DOC set
    MAX_S_MDT = v_dMax_S_MDT
  where FILIAL = v_filialID  
    and MAX_S_MDT < v_dMax_S_MDT;

  /* Получим дату актуальности по остаткам РКО */
 /* select count(*) into v_nCount from all_objects a where a.object_type = 'TABLE' and a.object_name = 'DEF_VARS_V_MF';
  
  if v_nCount = 0 then

    select min(B.ACT_DATE) into v_DateAct  
    from RKO_ACT_LS B 
    where B.ACT_TYPE = 1 and B.ACT_PL = 0;
  
  end if;

  if v_nCount > 0 then

    select min(B.ACT_DATE) into v_DateAct  
    from RKO_ACT_LS B 
    where B.ACT_TYPE = 1 and B.ACT_PL = 0
      and B.LSID in (select ID from RKO_LS);

  
  end if;    sym переход на анализ опердней      */

  
  if v_DateAct is null then
  
    begin
      select DLOADFIRST into v_DateAct from SV_OSS where ID = v_filialID;
    exception
      when NO_DATA_FOUND then
        v_DateAct := v_dEnd; /* чтобы не производить загрузку */
    end;    
  
  end if;

/*  select min(nvl(B.ACT_DATE,A.DOPN-1)) into v_DateAct  
  from RKO_LS A  
  left join RKO_ACT_LS B on B.LSID = A.ID and B.ACT_TYPE = 1 and B.ACT_PL = 0  
  where A.PARENTID is null;    */

 /* Отбор счетов, из-зи которых "падает" дата актуальности */

/*  insert into OTB_LS_ACT(LSID,DDACT,DD)
  select M.LSID,min(M.DDACT),M.DD
  from (
        select B.LSID as LSID, nvl(B.ACT_DATE,A.DOPN-1) as DDACT, sysdate as DD
        from RKO_LS A
        left join RKO_ACT_LS B on B.LSID = A.ID and B.ACT_TYPE = 1 and B.ACT_PL = 0
        where A.PARENTID is null
          and nvl(B.ACT_DATE,A.DOPN-1) < v_DDACT_SUBSCR
       ) M
  group by M.LSID,M.DD;*/

  /* Оповещение "подсписчиков" об изменении даты актуальности */ 
  update OTB_SUBSCRIBER set DACT = v_DateAct where nvl(DACT,v_D4000) > v_DateAct;   
  
  /* OnLine филиал или мультифилиальная база */ 
  IF v_Mode = 1 THEN  
  
    S_PROGRESS (1000001,'  Лицевые счета',0,0);  
    S_PROGRESS (1000001,'    Удаление неактуальных данных по л/с',0,0);  
     
    /* Определение даты последних СПОД */    
   /* S_PROGRESS (1000001,'  Определение даты последних СПОД',0,0);        
     
    select max(DD) 
    into v_dLastSPOD 
    from RKO_SVERT_DR  
    where S_S = 1000182 
      and year(DD) = year(v_dEnd); 
       
    if v_dLastSPOD is null then   
             
      select nvl(max(Q.LAST_DD),dbo.BOY(v_dEnd)) 
      into v_dLastSPOD 
      from ( 
            select  
              dbo.S_RKO_LS_DPD (ID,1000000,v_dEnd,4) as LAST_DD 
            from RKO_LS 
            where N20 like '707%'         
           ) Q 
      where Q.LAST_DD >= dbo.BOY(v_dEnd); 
     
    end if; 
     
    delete from OTB_LAST_SPOD 
    where FILIAL = v_filialID 
      and REPORT_YEAR = year(v_dEnd); 
        
    insert into OTB_LAST_SPOD(ID,FILIAL,REPORT_YEAR,LAST_SPOD_DD) 
    values(mainid.nextval,v_filialID,year(v_dEnd),v_dLastSPOD);  */ 
    insert into OTB_LS (FILIAL,VER_STAMP,GID,NATIVEID,NATIVE_S_MDT,CODE,NAME,DBEG,DEND,NBSOID,ORGID,VALUTID,ISKL,KKS_N20K,SK_TYPE,DATE_POGASH,DOVOSTR,PROCENT_V, 
                        F302NAZN,DOHRASH302,DOHRASH446,DOHRASHMODE,DDSSUD,VIDSSUD,CREDITFOR,NAMESECURITY,OVPVAL,RISK_LEVEL,LSTYPE,IMPSOURCETYPE,CODE_OKATO_OTD, 
                        ISP_AS_FIZLS,F345NO,TYPLS,TYPE_REZ,REPO,AKTIV_SS,L_BUSINESS)  
    select    
       v_filialID as FILIAL,  
       1, 
       A.GID,  
       A.ID as NATIVEID,  
       A.S_MDT,  
       nvl(A.N20,'б/н') as N20,    
       nvl(A.NAME,'б/н') as NAME,    
       A.DOPN,  
       A.DCLS,    
       P.OID as NBSOID,  
       A.ORGID,  
       A.VALUTID,    
       nvl(A.ISKL,0) as ISKL,  
       K.VNESHLS AS KKS_N20K,    
       A.SK_TYPE,  
       A.SROK as DATE_POGASH,  
       A.DOVOSTR,   
       A.PROCENT_V,  
       A.F302NAZN,    
       DR.CODE as DOHRASH302,  
       DR_446.CODE as DOHRASH446,  
       A.DOHRASHMODE as DOHRASHMODE,  
       A.DDSSUD as DDSSUD,  
       A.VIDSSUD as VIDSSUD,  
       A.CREDITFOR as CREDITFOR,  
       A.NAMESECURITY as NAMESECURITY,  
       A.OVPVAL as OVPVAL,  
       A.RISK_LEVEL as RISK_LEVEL,  
       A.LSTYPE as LSTYPE,  
       A.IMPSOURCETYPE as IMPSOURCETYPE,  
       O.CODE_OKATO as CODE_OKATO_OTD,  
       A.ISP_AS_FIZLS as ISP_AS_FIZLS,  
       A.F345NO as F345NO,  
       A.TYPLS as TYPLS,  
       A.TYPE_REZ as TYPE_REZ, 
       A.REPO, 
       A.AKTIV_SS, 
       nvl(A.L_BUSINESS,0) as L_BUSINESS   
    from RKO_LS A  
    left join COM_PLAN001 P on P.ID = A.NBSID    
    left join RKO_SVPLT K on K.LSID = A.ID    
    left join RKO_DOH_RAS302P DR on DR.OID = A.DOHRASHOID302    
    left join COM_DOH_RAS446P DR_446 on DR_446.OID = A.DOHRASHOID446   
    left join COM_OTDEL O on O.ID = A.OTDELENIE                
    where A.PARENTID is null    
      and P.PLANOID = 1000000   
      and not exists(select 1 from OTB_LS where NATIVEID = A.ID and FILIAL = v_filialID) ;   
            
    
    /* Анализ триггера актуальности */ 
    select count(1) into v_nActCount from RKO_SVACT_LS1 where ACT_FLAG = 1 and OBJID in (select ID from RKO_LS);  
     
    IF v_nActCount > 0 THEN  
     
        /* Удаление удаленных л/с */   
        delete from OTB_LS    
        where FILIAL = v_filialID 
          and not exists (select B.ID from RKO_LS B where B.ID = OTB_LS.NATIVEID);   
         
        /* Добавление новых л/с */   
        /* Изменение л/с */     
        S_PROGRESS (1000001,'    Добавление новых л/с',0,0);  
            
        MERGE into OTB_LS D   
        USING ( 
               select   
                 v_filialID as FILIAL, 
                 A.GID, 
                 A.ID as NATIVEID, 
                 A.S_MDT, 
                 nvl(A.N20,'б/н') as N20,   
                 nvl(A.NAME,'б/н') as NAME,   
                 A.DOPN, 
                 A.DCLS,   
                 P.OID as NBSOID, 
                 A.ORGID, 
                 A.VALUTID,   
                 nvl(A.ISKL,0) as ISKL, 
                 K.VNESHLS AS KKS_N20K,   
                 A.SK_TYPE, 
                 A.SROK as DATE_POGASH, 
                 A.DOVOSTR,  
                 A.PROCENT_V, 
                 A.F302NAZN,   
                 DR.CODE as DOHRASH302, 
                 DR_446.CODE as DOHRASH446, 
                 A.DOHRASHMODE as DOHRASHMODE, 
                 A.DDSSUD as DDSSUD, 
                 A.VIDSSUD as VIDSSUD, 
                 A.CREDITFOR as CREDITFOR, 
                 A.NAMESECURITY as NAMESECURITY, 
                 A.OVPVAL as OVPVAL, 
                 A.RISK_LEVEL as RISK_LEVEL, 
                 A.LSTYPE as LSTYPE, 
                 A.IMPSOURCETYPE as IMPSOURCETYPE, 
                 O.CODE_OKATO as CODE_OKATO_OTD, 
                 A.ISP_AS_FIZLS as ISP_AS_FIZLS, 
                 A.F345NO as F345NO, 
                 A.TYPLS as TYPLS, 
                 A.TYPE_REZ as TYPE_REZ 
                ,A.REPO  
                ,A.AKTIV_SS  
                ,nvl(A.L_BUSINESS,0) as L_BUSINESS  
               from RKO_LS A 
                 left join COM_PLAN001 P on P.ID = A.NBSID   
                 left join RKO_SVPLT K on K.LSID = A.ID   
                 left join RKO_DOH_RAS302P DR on DR.OID = A.DOHRASHOID302   
                 left join COM_DOH_RAS446P DR_446 on DR_446.OID = A.DOHRASHOID446  
                 left join COM_OTDEL O on O.ID = A.OTDELENIE               
               where A.PARENTID is null   
                 and P.PLANOID = 1000000  
              ) M   
        ON (D.NATIVEID = M.NATIVEID)   
        WHEN MATCHED THEN UPDATE SET                            
           D.VER_STAMP = case   
                           when (D.NATIVE_S_MDT = M.S_MDT)                                                 
                            and (nvl(D.CODE,'б/н') = nvl(M.N20,'б/н'))  
                            and (nvl(D.NAME,'б/н') = nvl(M.NAME,'б/н'))  
                            and (nvl(D.DBEG,v_D1900) = nvl(M.DOPN,v_D1900))  
                            and (nvl(D.DEND,v_D1900) = nvl(M.DCLS,v_D1900))  
                            and (nvl(D.NBSOID,0) = nvl(M.NBSOID,0))                         
                            and (nvl(D.ORGID,0) = nvl(M.ORGID,0))                           
                            and (nvl(D.VALUTID,0) = nvl(M.VALUTID,0)) 
                            and (nvl(D.ISKL,0) = nvl(M.ISKL,0))  
                            and (nvl(D.KKS_N20K,'0') = nvl(M.KKS_N20K,'0')) 
                            and (nvl(D.SK_TYPE,0) = nvl(M.SK_TYPE,0))  
                            and (nvl(D.DATE_POGASH,v_D1900) = nvl(M.DATE_POGASH,v_D1900))  
                            and (nvl(D.DOVOSTR,0) = nvl(M.DOVOSTR,0))                    
                            and (nvl(D.PROCENT_V,0) = nvl(M.PROCENT_V,0)) 
                            and (nvl(D.F302NAZN,0) = nvl(M.F302NAZN,0)) 
                            and (nvl(D.DOHRASH302,'0') = nvl(M.DOHRASH302,'0'))  
                            and (nvl(D.DOHRASH446,'0') = nvl(M.DOHRASH446,'0'))                          
                            and (nvl(D.DOHRASHMODE,'0') = nvl(M.DOHRASHMODE,'0')) 
                            and (nvl(D.DDSSUD,v_D1900) = nvl(M.DDSSUD,v_D1900)) 
                            and (nvl(D.VIDSSUD,0) = nvl(M.VIDSSUD,0)) 
                            and (nvl(D.CREDITFOR,'0') = nvl(M.CREDITFOR,'0')) 
                            and (nvl(D.NAMESECURITY,'0') = nvl(M.NAMESECURITY,'0')) 
                            and (nvl(D.OVPVAL,0) = nvl(M.OVPVAL,0)) 
                            and (nvl(D.RISK_LEVEL,0) = nvl(M.RISK_LEVEL,0)) 
                            and (nvl(D.LSTYPE,0) = nvl(M.LSTYPE,0)) 
                            and (nvl(D.IMPSOURCETYPE,'0') = nvl(M.IMPSOURCETYPE,'0')) 
                            and (nvl(D.CODE_OKATO_OTD,'0') = nvl(M.CODE_OKATO_OTD,'0')) 
                            and (nvl(D.ISP_AS_FIZLS,'0') = nvl(M.ISP_AS_FIZLS,'0')) 
                            and (nvl(D.F345NO,'0') = nvl(M.F345NO,'0')) 
                            and (nvl(D.TYPLS,0) = nvl(M.TYPLS,0)) 
                            and (nvl(D.TYPE_REZ,0) = nvl(M.TYPE_REZ,0)) 
                            and (nvl(D.REPO,0) = nvl(M.REPO,0)) 
                            and (nvl(D.AKTIV_SS,0) = nvl(M.AKTIV_SS,0)) 
                            and (nvl(D.L_BUSINESS,0) = nvl(M.L_BUSINESS,0))  
                           then 
                             nvl(D.VER_STAMP,1)    
                           else   
                             nvl(D.VER_STAMP,1) + 1  
                         end   
          ,D.GID = M.GID   
          ,D.NATIVE_S_MDT = M.S_MDT   
          ,D.CODE = M.N20   
          ,D.NAME = M.NAME   
          ,D.DBEG = M.DOPN   
          ,D.DEND = M.DCLS   
          ,D.NBSOID = M.NBSOID   
          ,D.ORGID = M.ORGID   
          ,D.VALUTID = M.VALUTID   
          ,D.ISKL = M.ISKL   
          ,D.KKS_N20K = M.KKS_N20K   
          ,D.SK_TYPE = M.SK_TYPE   
          ,D.DATE_POGASH = M.DATE_POGASH   
          ,D.DOVOSTR = M.DOVOSTR   
          ,D.PROCENT_V = M.PROCENT_V   
          ,D.F302NAZN = M.F302NAZN         
          ,D.DOHRASH302 = M.DOHRASH302 
          ,D.DOHRASH446 = M.DOHRASH446 
          ,D.DOHRASHMODE = M.DOHRASHMODE 
          ,D.DDSSUD = M.DDSSUD 
          ,D.VIDSSUD = M.VIDSSUD 
          ,D.CREDITFOR = M.CREDITFOR 
          ,D.NAMESECURITY = M.NAMESECURITY 
          ,D.OVPVAL = M.OVPVAL 
          ,D.RISK_LEVEL = M.RISK_LEVEL 
          ,D.LSTYPE = M.LSTYPE 
          ,D.IMPSOURCETYPE = M.IMPSOURCETYPE  
          ,D.CODE_OKATO_OTD = M.CODE_OKATO_OTD    
          ,D.ISP_AS_FIZLS = M.ISP_AS_FIZLS 
          ,D.F345NO = M.F345NO 
          ,D.TYPLS = M.TYPLS 
          ,D.TYPE_REZ = M.TYPE_REZ  
          ,D.REPO = M.REPO  
          ,D.AKTIV_SS = M.AKTIV_SS 
          ,D.L_BUSINESS = M.L_BUSINESS;  
/*        WHEN NOT MATCHED THEN  
          INSERT   
            ( 
             D.FILIAL, 
             D.VER_STAMP, 
             D.GID, 
             D.NATIVEID, 
             D.NATIVE_S_MDT, 
             D.CODE, 
             D.NAME, 
             D.DBEG, 
             D.DEND, 
             D.NBSOID, 
             D.ORGID, 
             D.VALUTID, 
             D.ISKL, 
             D.KKS_N20K, 
             D.SK_TYPE, 
             D.DATE_POGASH, 
             D.DOVOSTR, 
             D.PROCENT_V, 
             D.F302NAZN, 
             D.DOHRASH302, 
             D.DOHRASH446, 
             D.DOHRASHMODE, 
             D.DDSSUD, 
             D.VIDSSUD, 
             D.CREDITFOR, 
             D.NAMESECURITY, 
             D.OVPVAL, 
             D.RISK_LEVEL, 
             D.LSTYPE, 
             D.IMPSOURCETYPE, 
             D.CODE_OKATO_OTD, 
             D.ISP_AS_FIZLS, 
             D.F345NO, 
             D.TYPLS, 
             D.TYPE_REZ, 
             D.REPO, 
             D.AKTIV_SS, 
             D.L_BUSINESS) 
          VALUES  
            ( 
             M.FILIAL, 
             1, 
             M.GID, 
             M.NATIVEID, 
             M.S_MDT, 
             M.N20, 
             M.NAME, 
             M.DOPN, 
             M.DCLS, 
             M.NBSOID, 
             M.ORGID, 
             M.VALUTID, 
             M.ISKL, 
             M.KKS_N20K, 
             M.SK_TYPE, 
             M.DATE_POGASH, 
             M.DOVOSTR, 
             M.PROCENT_V, 
             M.F302NAZN, 
             M.DOHRASH302, 
             M.DOHRASH446, 
             M.DOHRASHMODE, 
             M.DDSSUD, 
             M.VIDSSUD, 
             M.CREDITFOR, 
             M.NAMESECURITY, 
             M.OVPVAL, 
             M.RISK_LEVEL, 
             M.LSTYPE, 
             M.IMPSOURCETYPE, 
             M.CODE_OKATO_OTD, 
             M.ISP_AS_FIZLS, 
             M.F345NO, 
             M.TYPLS, 
             M.TYPE_REZ, 
             M.REPO, 
             M.AKTIV_SS, 
             M.L_BUSINESS); */ 
              
      /* Установим флаг актуальности в источнике */ 
      update RKO_SVACT_LS1 set ACT_FLAG = 0 where ACT_FLAG = 1 and OBJID in (select ID from RKO_LS); 
      /* Сбросим актуальность для Ядра */        
      update OTB_ACT_TABLES set ACT_FLAG = 1 where TABLENAME = 'RKO_LS' and FILIAL = v_filialID;   
              
    END IF;          
     
    /* saow: считаем отдельно атрибуты, которые тянутся с балансовых счетов: */ 
    MERGE into OTB_LS D   
    USING ( 
           select   
             A.ID as NATIVEID, 
             nvl(A.RISKGR,P.RISKGR) as RISKGR, 
             nvl(A.PRRISK,P.PRRISK) as PRRISK, 
             nvl(A.PRRISK_FONDRUB,P.PRRISK_FONDRUB) as PRRISK_FONDRUB, 
             nvl(A.PRRISK_FONDVAL,P.PRRISK_FONDVAL) as PRRISK_FONDVAL 
           from RKO_LS A 
             left join COM_PLAN001 P on P.ID = A.NBSID             
           where A.PARENTID is null   
             and P.PLANOID = 1000000  
          ) M   
    ON (D.NATIVEID = M.NATIVEID)   
    WHEN MATCHED THEN UPDATE SET                            
       D.VER_STAMP = case   
                       when (nvl(D.RISKGR,0) = nvl(M.RISKGR,0))   
                        and (nvl(D.PRRISK,0) = nvl(M.PRRISK,0)) 
                        and (nvl(D.PRRISK_FONDRUB,0) = nvl(M.PRRISK_FONDRUB,0)) 
                        and (nvl(D.PRRISK_FONDVAL,0) = nvl(M.PRRISK_FONDVAL,0)) 
                       then 
                         nvl(D.VER_STAMP,1)    
                       else   
                         nvl(D.VER_STAMP,1) + 1  
                     end  
      ,D.RISKGR = M.RISKGR   
      ,D.PRRISK = M.PRRISK 
      ,D.PRRISK_FONDRUB = M.PRRISK_FONDRUB 
      ,D.PRRISK_FONDVAL = M.PRRISK_FONDVAL; 
    /* Атрибуты, хранимые по времени */ 
/*    S_PROGRESS (1000001,'  Атрибуты, хранимые по времени',0,0);  
    S_PROGRESS (1000001,'    Удаление атрибутов',0,0);  
    delete from OTB_LS_ATTRS  
    where VNESHID in (select NATIVEID from OTB_LS where FILIAL = v_filialID)  
      and ATT not in ('QCAT_PORT','NOMINAL');  
   
    S_PROGRESS (1000001,'    Добавление атрибутов',0,0);  
    insert into OTB_LS_ATTRS (ID,VNESHID,DDB,ATT,VAL_STR,VAL_ID,VAL_DATE,VAL_NUM)   
    select mainid.nextval,A.VNESHID,A.DDB,A.ATT,A.VAL_STR,A.VAL_ID,A.VAL_DATE,A.VAL_NUM   
    from RKO_LS_ATTRS A   
    left join RKO_LS LS on LS.ID = A.VNESHID   
    where A.ATT in ('LIMOVER','MINOST','MAXOST','VIDSECURITY','QCAT','QCAT_PRRISK','CATEGORY_UCH') 
      and LS.PARENTID is null;   */ 
    /* Если категории качества нет на лицевом счете, то берем с балансового счета */ 
/*    insert into OTB_LS_ATTRS (ID,VNESHID,DDB,ATT,VAL_ID) 
    select mainid.nextval,LS.ID,LS.DOPN,'QCAT',P.QCAT 
    from RKO_LS LS 
    left join COM_PLAN001 P on P.ID = LS.NBSID 
    where P.QCAT is not null 
      and not exists (select VAL_ID 
                        from RKO_LS_ATTRS 
                       where VNESHID = LS.ID 
                         and ATT = 'QCAT');   */ 
   
    /* Если категории качества нет на балансовом счете, то берем с контрагента */ 
/*    insert into OTB_LS_ATTRS (ID,VNESHID,DDB,ATT,VAL_ID)                           
    select mainid.nextval,LS.ID,C.DDB,C.ATT,C.VAL_INT 
    from RKO_LS LS 
    left join COM_ORG_AV C on C.VNESHID = LS.ORGID 
    where not exists( 
                     select ID 
                     from OTB_LS_ATTRS 
                     where VNESHID = LS.ID 
                       and ATT = 'QCAT' 
                    ) 
      and C.ATT = 'QCAT';                    
    insert into OTB_LS_ATTRS (ID,VNESHID,DDB,ATT,VAL_NUM)                           
    select mainid.nextval,LS.ID,C.DDB,C.ATT,C.VAL_DEC 
    from RKO_LS LS 
    left join COM_ORG_AV C on C.VNESHID = LS.ORGID 
    where not exists( 
                     select ID 
                     from OTB_LS_ATTRS 
                     where VNESHID = LS.ID 
                       and ATT = 'QCAT_PRRISK' 
                    ) 
      and C.ATT = 'QCAT_PRRISK';    */ 
       
    /* Атрибуты, хранимые по времени */       
    S_PROGRESS (1000001,'  Атрибуты, хранимые по времени',0,0);  
    S_PROGRESS (1000001,'    Удаление атрибутов',0,0);  
     
    delete from OTB_LS_ATTRS    
     where VNESHID in (select NATIVEID from OTB_LS where FILIAL = v_filialID)    
       and ATT in ('LIMOVER','MINOST','MAXOST','VIDSECURITY','QCAT','QCAT_PRRISK','CATEGORY_UCH','SPRAV_STOIM','SUM_NENADL_AKT','KOEF_SPRAV_STOIM','NVPI_VAL' 
                  ,'PRRISK_FONDRUB_H','PRRISK_FONDVAL_H','L_NACHPC')   
       and NATIVEID is not null   
       and not exists(select ID from RKO_LS_ATTRS where ID = OTB_LS_ATTRS.NATIVEID); 
    /*teoi Тип учета TYPEUCH грузим всегда заново, т.к. поле может заполняться из договоров и ЦБ*/    
    delete from OTB_LS_ATTRS    
    where VNESHID in (select NATIVEID from OTB_LS where FILIAL = v_filialID)    
      and ATT = 'TYPEUCH'; 
         
    S_PROGRESS (1000001,'    Добавление и изменение атрибутов',0,0);  
    merge into OTB_LS_ATTRS T  
    using (  
           select A.ID as NATIVEID,A.VNESHID,A.DDB,A.ATT,A.VAL_STR,A.VAL_ID,A.VAL_DATE,A.VAL_NUM  
           from RKO_LS LS   
           left join RKO_LS_ATTRS A on A.VNESHID = LS.ID   
           where A.ATT in ('LIMOVER','MINOST','MAXOST','VIDSECURITY','QCAT','QCAT_PRRISK','CATEGORY_UCH','SPRAV_STOIM','SUM_NENADL_AKT','KOEF_SPRAV_STOIM','NVPI_VAL' 
                          ,'PRRISK_FONDRUB_H','PRRISK_FONDVAL_H','L_NACHPC','TYPEUCH')    
             and LS.PARENTID is null       
          ) S  
    on (T.NATIVEID = S.NATIVEID and T.ATT in ('LIMOVER','MINOST','MAXOST','VIDSECURITY','QCAT','QCAT_PRRISK','CATEGORY_UCH','SPRAV_STOIM','SUM_NENADL_AKT' 
                                             ,'KOEF_SPRAV_STOIM','NVPI_VAL','PRRISK_FONDRUB_H','PRRISK_FONDVAL_H','L_NACHPC','TYPEUCH'))          
    when MATCHED then UPDATE set                                 
      T.DDB = S.DDB  
     ,T.VAL_STR = S.VAL_STR  
     ,T.VAL_ID = S.VAL_ID  
     ,T.VAL_DATE = S.VAL_DATE  
     ,T.VAL_NUM = S.VAL_NUM  
    when NOT MATCHED then  
      insert        
        (T.ID,T.VNESHID,T.DDB,T.ATT,T.VAL_STR,T.VAL_ID,T.VAL_DATE,T.VAL_NUM,T.NATIVEID)      
      values       
        (otb_ls_attrs_id.nextval,S.VNESHID,S.DDB,S.ATT,S.VAL_STR,S.VAL_ID,S.VAL_DATE,S.VAL_NUM,S.NATIVEID);      
    if v_DateAct < v_dEnd then 
      /* Удаление остатков по удаленным л/с */ 
      S_PROGRESS (1000001,'  Остатки по лицевым счетам',0,0);  
      S_PROGRESS (1000001,'    Удаление по удаленным л/с',0,0);  
       
      delete from OTB_LLC  
      where LSID in (select NATIVEID from OTB_LS where FILIAL = v_filialID)  
        and not exists(select B.ID from RKO_LS B where B.ID = OTB_LLC.LSID);  
      /* Удаление остатков по л/с от даты актуальности до DateTo */ 
      S_PROGRESS (1000001,'    Удаление неактуальных данных',0,0);  
      delete from OTB_LLC  
       where LSID in (select NATIVEID from OTB_LS where FILIAL = v_filialID)  
         and OTB_LLC.DD >= v_DateAct;  
     
      /* Добавление остатков по л/с от даты актуальности до DateTo */ 
      S_PROGRESS (1000001,'    Актуализация остатков л/с',0,0);  
       
     select count(*) into v_nCount from all_objects a where a.object_type = 'TABLE' and a.object_name = 'DEF_VARS_V_MF'; 
         
      if v_nCount = 0 then   
        insert into OTB_LLC (PLANOID,PL,LSID,DD,SALDO,DE,KR)   
        select A.PLOID    
              ,case A.TYP when 1 then 1 when 2 then 2 when 7 then 3 when 4 then 11 when 5 then 12 end AS PL    
              ,A.LSID    
              ,A.DD    
              ,A.SALDO    
              ,A.DE    
              ,A.KR    
         from RKO_LC A    
         where A.DD >= v_DateAct    
           and A.DD <= v_DateTo    
           and A.PLOID = 1000000    
           and A.TYP in (1,2,4,5,7); 
          
      end if;    
      /* Для мультифилиальной версии нуждно обязательно наличие счета в RKO_LS (sym 02.06.17) */ 
      if v_nCount > 0 then 
        insert into OTB_LLC (PLANOID,PL,LSID,DD,SALDO,DE,KR)   
        select A.PLOID   
              ,case A.TYP when 1 then 1 when 2 then 2 when 7 then 3 when 4 then 11 when 5 then 12 end AS PL   
              ,B.ID   
              ,A.DD   
              ,A.SALDO   
              ,A.DE   
              ,A.KR   
         from RKO_LC A   
         left join RKO_LS B on B.ID = A.LSID   
         where B.PARENTID is null   
           and A.DD >= v_DateAct   
           and A.DD <= v_DateTo   
           and A.PLOID = 1000000   
           and (A.TYP in (1,2,4,5,7))   
           and B.ID is not null;     
            
      end if; 
       
      /* Зафиксируем дату актуальности */  
      update RKO_ACT_LS   
      set ACT_DATE = v_DateTo   
      where (ACT_TYPE = 1) and (ACT_PL = 0) and (ACT_DATE < v_DateTo) 
        and LSID in (select ID from RKO_LS);   
   
      insert into RKO_ACT_LS (LSID,ACT_TYPE,ACT_PL,ACT_DATE)   
      select A.ID,1,0,v_DateTo   
      from RKO_LS A   
      where A.PARENTID is null 
        and A.DOPN < v_DateTo /* NDA 28.12.10 */ 
        and not exists(select B.LSID from RKO_ACT_LS B   
                       where B.LSID = A.ID   
                         and B.ACT_TYPE = 1   
                         and B.ACT_PL = 0);  
  
    end if; 
     
  /* Перенос привязок */ 
  /* Удаление удаленных */ 
      
/*biiw перенос привязок перенесен в отдельную ППД S_OTB_LS_BINDING_DEF*/      
/*  S_PROGRESS (1000001,'  Документы привязок',0,0);  
  S_PROGRESS (1000001,'    Удаление привязок',0,0);  
   
  delete from OTB_BINDING_V 
  where FILIAL = v_filialID 
    and not exists(select ID from DEF_BINDING_V where ID = OTB_BINDING_V.NATIVEID) 
    and BINDOID in (select OID from DEF_BINDING where PL0 = 1000029 and BINDOID = 1000029) 
    and nvl(FMODULE,'DEF') = 'DEF';  */ 
   
  /* Запись новых */ 
/*  S_PROGRESS (1000001,'    Изменение привязок',0,0);  
  merge into OTB_BINDING_V T  
  using (  
         select ID as NATIVEID,BINDOID,DBEG,PL0,PL1,PL2,PL3,PL4,PL5,PL100 
         from DEF_BINDING_V 
         where BINDOID in ( 
                           select OID 
                           from DEF_BINDING  
                           where (PL0 = 1000029 and BINDOID = 1000029)  
                             and exists(select ID from RKO_LS where ID = DEF_BINDING_V.PL0) 
                             and exists(select ID from RKO_LS where ID = DEF_BINDING_V.PL100) 
                          ) 
        ) S  
  on (T.NATIVEID = S.NATIVEID and nvl(T.FMODULE,'DEF') = 'DEF')          
  when MATCHED then UPDATE set                                 
    T.DBEG = S.DBEG  
   ,T.PL0 = S.PL0 
   ,T.PL1 = S.PL1 
   ,T.PL2 = S.PL2 
   ,T.PL3 = S.PL3 
   ,T.PL4 = S.PL4 
   ,T.PL5 = S.PL5 
   ,T.PL100 = S.PL100 
  when NOT MATCHED then  
    insert        
      (T.ID,T.NATIVEID,T.FILIAL,T.BINDOID,T.DBEG,T.PL0,T.PL1,T.PL2,T.PL3,T.PL4,T.PL5,T.PL100,T.FMODULE)      
    values       
      (otb_mainid.nextval,S.NATIVEID,v_filialID,S.BINDOID,S.DBEG,S.PL0,S.PL1,S.PL2,S.PL3,S.PL4,S.PL5,S.PL100,'DEF'); */     
     
  END IF; 
   
END S_OTB_LS;   
 
