/* проводки по Дт счёта */
select substr(dbo.GS_COM_ORG_AV('CODE_INN',O.ID,:D$DEnd),1,12) as "ИНН клиента"
      ,LS.N20 as "Номер счёта клиента"
      ,case when trunc(DH.PDATE)=DH.DD then DH.PDATE 
            else DH.DD
       end as "Дата и время операции"     
      ,DH.NOMDOC as "Номер платежного документа"
      ,case when PL.ID>0 and VP.DDDELT<0 then (DH.DD+VP.DDDELT)
            else DH.DD
       end as "Дата платежного документа"
      ,dbo.S_GETVAR('Org_BIC',DH.DD) as "БИК/SWIFT КО плательщика"
      ,dbo.S_GETVAR('Short_Org_Name',DH.DD) as "Наименование КО плательщика"
      ,dbo.S_GETVAR('Org_CorrAcc',DH.DD) as "Корсчет КО плательщика"
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ABBIC
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BBBIC
            when nvl(PL.TYPEINOUT,0) = 0 then substr(dbo.S_GETVAR('Org_BIC',DH.DD),1,10)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD52_BIC)                                                /* входящий и УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=4) 
                 then ''                                                                           /* входящий и банк плательщика по имени */
            when (PL.TYPEINOUT = 2) and (SW.MT = 910) and (SW.FLD56_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD56_BIC)                                                /* входящий, банк плательщика может быть в 56 поле */
            when (PL.TYPEINOUT = 2) and (SW.MT = 910) and (SW.FLD56_OPT=4) 
                 then ''                                                                           /* входящий и банк плательщика по имени */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) 
                 then dbo.S_SW_LT_BIC(SW.REPLT)                                                    /* входящий, банк отправителя */
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300 and PL.BBBIC is NULL) 
                 then COALESCE(PL.BIK_F82,PL.SWBIK_F82,PL.BIK_F84,PL.SWBIK_F84)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD57_BIC)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=4) 
                 then ''
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD58_BIC)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=4) 
                 then ''
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) 
                 then dbo.S_SW_LT_BIC(SW.REPLT) /* исходящий */
            else PL.BBBIC
       end as "БИК/SWIFT КО получателя" 
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ABNAME
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BBNAME
            when nvl(PL.TYPEINOUT,0) = 0 then substr(dbo.S_GETVAR('Short_Org_Name',DH.DD),1,200)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=4) 
                 then SW.FLD52_D
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=1) 
                 then COALESCE(SW.FLD52_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC))                                /* УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT=4) 
                  then SW.FLD56_D
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT=1) 
                  then COALESCE(SW.FLD56_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD56_BIC))                                /* УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) 
                  then dbo.S_PODFT_NAME_SWIFT(SW.REPLT)                                                             /* входящий, банк отправителя */
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300 and PL.BBNAME is NULL) 
                  then COALESCE(PL.BNAME_F82,PL.BNAME_F84)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=1) 
                  then COALESCE(SW.FLD57_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD57_BIC))
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=4) 
                  then SW.FLD57_D
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=1) 
                  then COALESCE(SW.FLD58_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD58_BIC))
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=4) 
                  then SW.FLD58_D
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) 
                  then dbo.S_SW_LT_BIC(SW.REPLT) /* исходящий */
            ELSE PL.BBNAME 
       end as "Наименование КО получателя"         
      ,case when nvl(PL.TYPEINOUT,0) in (0,3) then substr(dbo.S_GETVAR('Org_CorrAcc',DH.DD),1,20)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT is not NULL) 
                 then SW.FLD52_IDENT
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT is not NULL) 
                 then SW.FLD56_IDENT
            when (PL.TYPEINOUT = 1) and (SW.MT in (103,1031,202,910)) and (SW.FLD57_OPT is not NULL) 
                 then SW.FLD57_IDENT
            when (PL.TYPEINOUT = 1) and (SW.MT in (103,1031,202)) and (SW.FLD58_OPT is not NULL) 
                 then SW.FLD58_IDENT
            else PL.BBKLS
       end as "Корсчет КО получателя"
      ,DH.SUMVD as "Сумма по Дт счета вал.счета" 
      ,0.0 as "Сумма по Кр счета вал.счета" 
      ,DH.SUMRUB as "Сумма по Дт счета НЭ" 
      ,0.0 as "Сумма по Кр счета НЭ"
      ,substr(dbo.GS_COM_ORG_AV('CODE_INN',O.ID,:D$DEnd),1,12) as "ИНН плательщика"
      ,substr(COALESCE(dbo.GS_COM_ORG_AV('NAME_SHORT',O.ID,:D$DEnd),dbo.GS_COM_ORG_AV('FULLNAME',O.ID,:D$DEnd),O.NAME),1,200) AS "Наименование плательщика"
      ,LS.N20 as "Счёт плательщика"  
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.AINN
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BINN
            when nvl(PL.TYPEINOUT,0) not in (0,3) then PL.BINN
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 2 then substr(dbo.GS_COM_ORG_AV('CODE_INN',O2.ID,DH.DD),1,14)
            when PL.ID is NULL and DH.TOPDOCOID is not NULL and exists (select 1 from DEF_DOC where DEF_DOC.OID=DH.TOPDOCOID and DEF_DOC.MODULE_OID=1000011) /* кассовый док-т */
                 then NULL
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 1 then substr(dbo.S_GETVAR('Org_INN',DH.DD),1,14)
       end as "ИНН получателя"
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ANAME
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BNAME
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 2 then O2.NAME
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 1 then substr(dbo.S_GETVAR('Short_Org_Name',DH.DD),1,200)
            when SW.MT=103 and PL.TYPEINOUT=1 then /* исходящий swift 103 */
                 CASE when SW.FLD59_OPT=1000 then SW.FLD59_NAME_ADDR  
                      when SW.FLD59_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD59_BEI)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT) 
                 end     
            when SW.MT=202 and PL.TYPEINOUT=1 then /* исходящий swift 202 */
                 CASE WHEN SW.FLD58_OPT = 4 then SW.FLD58_D 
                      WHEN SW.FLD58_OPT = 1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD58_BIC)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT)
                 end     
            when SW.MT in (103,910) and PL.TYPEINOUT=2 then /* входящий swift 103 910 */
                 CASE when SW.FLD50_OPT in (11,12) then SW.FLD50_NAME_ADDR  
                      when SW.FLD50_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD50_BEI)
                      when SW.MT=910 and SW.FLD50_OPT is NULL and SW.FLD52_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC)
                      when SW.MT=910 and SW.FLD50_OPT is NULL and SW.FLD52_OPT=4 then SW.FLD52_D
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT) 
                 end     
            when SW.MT=202 and PL.TYPEINOUT=2 then /* входящий swift 202 */
                 CASE WHEN SW.FLD52_OPT = 4 then SW.FLD52_D 
                      WHEN SW.FLD52_OPT = 1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT)
                 end     
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300) 
                 then COALESCE(PL.BNAME_F81,PL.BNAME)
            when PL.ID is NULL and DH.TOPDOCOID is not NULL and exists (select 1 from DEF_DOC where DEF_DOC.OID=DH.TOPDOCOID and DEF_DOC.MODULE_OID=1000011) /* кассовый док-т */
                 then dbo.S_KS_GET_DEAL_FIO(DH.TOPDOCOID,DH.TOPDOCID)
            else PL.BNAME
       end as "Наименование получателя"
      ,case WHEN SW.MT=103 AND (PL.TYPEINOUT = 1) THEN SW.FLD59_IDENT                              /* исход MT103 */
            WHEN SW.MT=103 AND (PL.TYPEINOUT = 2) and (SW.FLD50_OPT=12) THEN SW.FLD50_F             /* вход MT103*/          
            WHEN SW.MT=103 AND (PL.TYPEINOUT = 2) THEN SW.FLD50_IDENT                              /* вход MT103*/          
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) and (FLD50_OPT is not NULL) THEN SW.FLD50_IDENT  /* вход MT103*/          
            WHEN SW.MT=202 AND (PL.TYPEINOUT = 1) THEN SW.FLD58_IDENT                              /* исход MT202 */
            WHEN SW.MT=202 AND (PL.TYPEINOUT = 2) THEN SW.FLD52_IDENT                              /* вход MT202*/         
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) and (FLD52_OPT is not NULL) THEN SW.FLD52_IDENT  /* вход MT202*/          
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) THEN NULL                                       /* вход MT910*/          
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300) then COALESCE(PL.BNLS_F81,PL.BNLSVNESH)
            when nvl(PL.TYPEINOUT,0) in (1,2) and LS.ID not in (select LSID from RKO_SVPLT)
            then PL.BNLSVNESH
            else LS_K.N20 
       end as "Счёт получателя"
      ,DH.REASON as "Назначение платежа"  

   from RKO_LS LS
   left join COM_ORG2 O on LS.ORGID = O.ID
   left join (select DH.*, DH.NSD as NS, DH.NSK as NS_K
                from RKO_LS LS
                join RKO_DH DH on LS.ID = DH.NSD and DH.DD >= :D$DBeg and DH.DD <= :D$DEnd and DH.SUMVD>0 and nvl(DH.TP,0) not in (7,9,11) and DH.PLAN_FACT=2
             ) DH on DH.NS = LS.ID
   left join RKO_VP VP on VP.ID = DH.DOCID
   left join RKO_PLDOC PL on PL.ID = VP.ID
   left join SW_PLDOC SW on SW.ID = VP.ID
   left join RKO_LS LS_K on LS_K.ID = DH.NS_K
   left join COM_ORG2 O2 on O2.ID = LS_K.ORGID
   left join RKO_TYPLS A on A.ID = LS.TYPLS
   left join COM_CURRENCIES C on C.ID = LS.VALUTID

   where LS.LSTYPE=2 /* клиентский */
     and LS.ORGID = :V$ORGID
     and nvl(LS.ISKL,0) <> 1
     and LS.S_S <> -1
     and LS.DOPN <= :D$DEnd
     and nvl(LS.DCLS,to_date('01.01.4000','DD.MM.YYYY')) >= :D$DBeg
     and DH.ID>0
  
  
union all

/* проводки по Кр счёта */
select substr(dbo.GS_COM_ORG_AV('CODE_INN',O.ID,:D$DEnd),1,12) as "ИНН клиента"
      ,LS.N20 as "Номер счёта клиента"
      ,case when trunc(DH.PDATE)=DH.DD then DH.PDATE 
            else DH.DD
       end as "Дата и время операции"     
      ,DH.NOMDOC as "Номер платежного документа"
      ,case when PL.ID>0 and VP.DDDELT<0 then (DH.DD+VP.DDDELT)
            else DH.DD
       end as "Дата платежного документа"
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ABBIC
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BBBIC
            when nvl(PL.TYPEINOUT,0) = 0 then substr(dbo.S_GETVAR('Org_BIC',DH.DD),1,10)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD52_BIC)                                                /* входящий и УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=4) 
                 then ''                                                                           /* входящий и банк плательщика по имени */
            when (PL.TYPEINOUT = 2) and (SW.MT = 910) and (SW.FLD56_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD56_BIC)                                                /* входящий, банк плательщика может быть в 56 поле */
            when (PL.TYPEINOUT = 2) and (SW.MT = 910) and (SW.FLD56_OPT=4) 
                 then ''                                                                           /* входящий и банк плательщика по имени */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) 
                 then dbo.S_SW_LT_BIC(SW.REPLT)                                                    /* входящий, банк отправителя */
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300 and PL.BBBIC is NULL) 
                 then COALESCE(PL.BIK_F82,PL.SWBIK_F82,PL.BIK_F84,PL.SWBIK_F84)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD57_BIC)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=4) 
                 then ''
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=1) 
                 then dbo.S_SW_LT_BIC(SW.FLD58_BIC)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=4) 
                 then ''
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) 
                 then dbo.S_SW_LT_BIC(SW.REPLT) /* исходящий */
            else PL.BBBIC
       end as "БИК/SWIFT КО плательщика" 
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ABNAME
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BBNAME
            when nvl(PL.TYPEINOUT,0) = 0 then substr(dbo.S_GETVAR('Short_Org_Name',DH.DD),1,200)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=4) 
                 then SW.FLD52_D
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT=1) 
                 then COALESCE(SW.FLD52_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC))                                /* УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT=4) 
                  then SW.FLD56_D
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT=1) 
                  then COALESCE(SW.FLD56_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD56_BIC))                                /* УКАЗАН банк плательщика */
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) 
                  then dbo.S_PODFT_NAME_SWIFT(SW.REPLT)                                                             /* входящий, банк отправителя */
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300 and PL.BBNAME is NULL) 
                  then COALESCE(PL.BNAME_F82,PL.BNAME_F84)
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=1) 
                  then COALESCE(SW.FLD57_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD57_BIC))
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD57_OPT=4) 
                  then SW.FLD57_D
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=1) 
                  then COALESCE(SW.FLD58_BIC_N, dbo.S_PODFT_NAME_SWIFT(SW.FLD58_BIC))
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) and (SW.FLD58_OPT=4) 
                  then SW.FLD58_D
            when (PL.TYPEINOUT = 1) and (SW.MT in (103, 1031, 202)) 
                  then dbo.S_SW_LT_BIC(SW.REPLT) /* исходящий */
            ELSE PL.BBNAME 
       end as "Наименование КО плательщика"         
      ,case when nvl(PL.TYPEINOUT,0) in (0,3) then substr(dbo.S_GETVAR('Org_CorrAcc',DH.DD),1,20)
            when (PL.TYPEINOUT = 2) and (SW.MT in (103,1031,202,910)) and (SW.FLD52_OPT is not NULL) 
                 then SW.FLD52_IDENT
            when (PL.TYPEINOUT = 2) and (SW.MT in (910)) and (SW.FLD56_OPT is not NULL) 
                 then SW.FLD56_IDENT
            when (PL.TYPEINOUT = 1) and (SW.MT in (103,1031,202,910)) and (SW.FLD57_OPT is not NULL) 
                 then SW.FLD57_IDENT
            when (PL.TYPEINOUT = 1) and (SW.MT in (103,1031,202)) and (SW.FLD58_OPT is not NULL) 
                 then SW.FLD58_IDENT
            else PL.BBKLS
       end as "Корсчет КО плательщика"
      ,dbo.S_GETVAR('Org_BIC',DH.DD) as "БИК/SWIFT КО получателя"
      ,dbo.S_GETVAR('Short_Org_Name',DH.DD) as "Наименование КО получателя"
      ,dbo.S_GETVAR('Org_CorrAcc',DH.DD) as "Корсчет КО получателя"
      ,0.0 as "Сумма по Дт счета вал.счета" 
      ,DH.SUMVK as "Сумма по Кр счета вал.счета" 
      ,0.0 as "Сумма по Дт счета НЭ" 
      ,DH.SUMRUB as "Сумма по Кр счета НЭ"
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.AINN
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BINN
            when nvl(PL.TYPEINOUT,0) not in (0,3) then PL.BINN
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 2 then substr(dbo.GS_COM_ORG_AV('CODE_INN',O2.ID,DH.DD),1,14)
            when PL.ID is NULL and DH.TOPDOCOID is not NULL and exists (select 1 from DEF_DOC where DEF_DOC.OID=DH.TOPDOCOID and DEF_DOC.MODULE_OID=1000011) /* кассовый док-т */
                 then NULL
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 1 then substr(dbo.S_GETVAR('Org_INN',DH.DD),1,14)
       end as "ИНН плательщика"
      ,case when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.BNLS then PL.ANAME
            when nvl(PL.TYPEINOUT,0) = 3 and LS.ID = PL.ANLS then PL.BNAME
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 2 then O2.NAME
            when nvl(PL.TYPEINOUT,0) = 0 and LS_K.LSTYPE = 1 then substr(dbo.S_GETVAR('Short_Org_Name',DH.DD),1,200)
            when SW.MT=103 and PL.TYPEINOUT=1 then /* исходящий swift 103 */
                 CASE when SW.FLD59_OPT=1000 then SW.FLD59_NAME_ADDR  
                      when SW.FLD59_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD59_BEI)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT) 
                 end     
            when SW.MT=202 and PL.TYPEINOUT=1 then /* исходящий swift 202 */
                 CASE WHEN SW.FLD58_OPT = 4 then SW.FLD58_D 
                      WHEN SW.FLD58_OPT = 1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD58_BIC)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT)
                 end     
            when SW.MT in (103,910) and PL.TYPEINOUT=2 then /* входящий swift 103 910 */
                 CASE when SW.FLD50_OPT in (11,12) then SW.FLD50_NAME_ADDR  
                      when SW.FLD50_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD50_BEI)
                      when SW.MT=910 and SW.FLD50_OPT is NULL and SW.FLD52_OPT=1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC)
                      when SW.MT=910 and SW.FLD50_OPT is NULL and SW.FLD52_OPT=4 then SW.FLD52_D
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT) 
                 end     
            when SW.MT=202 and PL.TYPEINOUT=2 then /* входящий swift 202 */
                 CASE WHEN SW.FLD52_OPT = 4 then SW.FLD52_D 
                      WHEN SW.FLD52_OPT = 1 then dbo.S_PODFT_NAME_SWIFT(SW.FLD52_BIC)
                      else dbo.S_PODFT_NAME_SWIFT(SW.REPLT)
                 end     
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300) 
                 then COALESCE(PL.BNAME_F81,PL.BNAME)
            when PL.ID is NULL and DH.TOPDOCOID is not NULL and exists (select 1 from DEF_DOC where DEF_DOC.OID=DH.TOPDOCOID and DEF_DOC.MODULE_OID=1000011) /* кассовый док-т */
                 then dbo.S_KS_GET_DEAL_FIO(DH.TOPDOCOID,DH.TOPDOCID)
            else PL.BNAME
       end as "Наименование плательщика"
      ,case WHEN SW.MT=103 AND (PL.TYPEINOUT = 1) THEN SW.FLD59_IDENT                              /* исход MT103 */
            WHEN SW.MT=103 AND (PL.TYPEINOUT = 2) and (SW.FLD50_OPT=12) THEN SW.FLD50_F             /* вход MT103*/          
            WHEN SW.MT=103 AND (PL.TYPEINOUT = 2) THEN SW.FLD50_IDENT                              /* вход MT103*/          
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) and (FLD50_OPT is not NULL) THEN SW.FLD50_IDENT  /* вход MT103*/          
            WHEN SW.MT=202 AND (PL.TYPEINOUT = 1) THEN SW.FLD58_IDENT                              /* исход MT202 */
            WHEN SW.MT=202 AND (PL.TYPEINOUT = 2) THEN SW.FLD52_IDENT                              /* вход MT202*/         
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) and (FLD52_OPT is not NULL) THEN SW.FLD52_IDENT  /* вход MT202*/          
            WHEN SW.MT=910 AND (PL.TYPEINOUT = 2) THEN NULL                                       /* вход MT910*/          
            when (PL.TYPEINOUT = 2 and PL.TYPEDOC=300) then COALESCE(PL.BNLS_F81,PL.BNLSVNESH)
            when nvl(PL.TYPEINOUT,0) in (1,2) and LS.ID not in (select LSID from RKO_SVPLT)
            then PL.BNLSVNESH
            else LS_K.N20 
       end as "Счёт плательщика"
      ,substr(dbo.GS_COM_ORG_AV('CODE_INN',O.ID,:D$DEnd),1,12) as "ИНН получателя"
      ,substr(COALESCE(dbo.GS_COM_ORG_AV('NAME_SHORT',O.ID,:D$DEnd),dbo.GS_COM_ORG_AV('FULLNAME',O.ID,:D$DEnd),O.NAME),1,200) AS "Наименование получателя"
      ,LS.N20 as "Счёт получателя"  
      ,DH.REASON as "Назначение платежа"  

   from RKO_LS LS
   left join COM_ORG2 O on LS.ORGID = O.ID
   left join (select DH.* ,DH.NSK as NS, DH.NSD as NS_K
                from RKO_LS LS
                join RKO_DH DH on LS.ID = DH.NSK and DH.DD >= :D$DBeg and DH.DD <= :D$DEnd and DH.SUMVK>0 and nvl(DH.TP,0) not in (7,9,11) and DH.PLAN_FACT=2
             ) DH on DH.NS = LS.ID

   left join RKO_VP VP on VP.ID = DH.DOCID
   left join RKO_PLDOC PL on PL.ID = VP.ID
   left join SW_PLDOC SW on SW.ID = VP.ID
   left join RKO_LS LS_K on LS_K.ID = DH.NS_K
   left join COM_ORG2 O2 on O2.ID = LS_K.ORGID
   left join RKO_TYPLS A on A.ID = LS.TYPLS
   left join COM_CURRENCIES C on C.ID = LS.VALUTID

   where LS.LSTYPE=2 /* клиентский */
     and LS.ORGID = :V$ORGID
     and nvl(LS.ISKL,0) <> 1
     and LS.S_S <> -1
     and LS.DOPN <= :D$DEnd
     and nvl(LS.DCLS,to_date('01.01.4000','DD.MM.YYYY')) >= :D$DBeg
     and DH.ID>0

