DROP TABLE IF EXISTS vv_PartnerConnectionInfoDetail;
DROP VIEW IF EXISTS vv_PartnerConnectionInfoDetail;
CREATE view vv_PartnerConnectionInfoDetail
as
   select '01-Account' MainType,
          t2.Oid Oid,
          t2.CustomersFrom CustomersFrom,
          t1.Customer Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          t2.CurrencyName CurrencyName,
          max(t3.AmountHUFBrutto) DebitTotalHUF,
          0 CreditTotalHUF,
          max(t3.AmountDEVBrutto) DebitTotalDEV,
          0 CreditTotalDEV,
          max(t3.AmountHUFBrutto) BallanceHUF,
          max(t3.AmountDEVBrutto) BallanceDEV,
          '' ShortName,
          '' Description,
          t2.DateExecution DateExecution,
          t2.DateCreated DateCreated,
          t2.DatePayment DatePayment,
          DATEDIFF(t2.DatePayment,curdate()) DelayedDay,
          case when DATEDIFF(t2.DatePayment,curdate())<=0 then 0
               when DATEDIFF(t2.DatePayment,curdate())>0 and DATEDIFF(t2.DatePayment,curdate())<=15 then 1
               when DATEDIFF(t2.DatePayment,curdate())>15 and DATEDIFF(t2.DatePayment,curdate())<=30 then 2
               when DATEDIFF(t2.DatePayment,curdate())>30 and DATEDIFF(t2.DatePayment,curdate())<=60 then 3
               when DATEDIFF(t2.DatePayment,curdate())>60 and DATEDIFF(t2.DatePayment,curdate())<=90 then 4
               when DATEDIFF(t2.DatePayment,curdate())>90 and DATEDIFF(t2.DatePayment,curdate())<=180 then 5
               when DATEDIFF(t2.DatePayment,curdate())>180 and DATEDIFF(t2.DatePayment,curdate())<=360 then 6
               when DATEDIFF(t2.DatePayment,curdate())>360 then 7                                                          
           end DelayedRange,
           t2.CurrencyName CurrencyName_Account,
           sum(t1.AmountHUF) AmountHUF_Account,
           sum(t1.AmountDEV) AmountDEV_Account,
           t2.DateExecution DateExecution_Account                    
   from GLRows t1 join GLHeader t2 on
        t1.GLHeader=t2.Oid join GLAccounts t3 on
        t1.GLHeader=t3.Oid
   where t2.GCRecord is null and
         t2.GLDocumentType=1 and
         t2.IsEditable=0 and 
         t1.PartnerType in (0,1) and
         t1.GCRecord is null
   group by t2.Oid,t2.CustomersFrom,t1.Customer,t1.PartnerType,
            t1.ConnectInfo,t2.CurrencyName,t2.DateExecution,t2.DateCreated,t2.DatePayment                  
  
 union all
   
   select '02-Bank' MainType,
          t1.Oid Oid,
          t2.CustomersFrom CustomersFrom,
          t1.Customer Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          t3.CurrencyName CurrencyName,
          0 DebitTotalHUF,
          case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalHUF,
          0 DebitTotalDEV,
          case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalDEV,
          (case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)*-1
          end)*-1 BallanceHUF,
          (case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.InAmountDEV)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.OutAmountDEV)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.OutAmountDEV)
                when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.InAmountDEV)*-1
                when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
                when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF'then sum(t1.InAmountDEV2)*-1                               
          else 0     
          end)*-1 BallanceDEV,
          max(t3.ShortName+', '+ifnull(t2.DocumentNumber,'')) ShortName,
          '' Description,
          t2.DateExecution DateExecution,
          t2.DateExecution DateCreated,
          t4.DatePayment DatePayment,
          0 DelayedDay,
          '' DelayedRange,
          t4.CurrencyName CurrencyName_Account,
          0 AmountHUF_Account,
          0 AmountDEV_Account,
          max(t4.DateExecution) DateExecution_Account 
   from GLRows t1 
        join GLHeader t2 on t1.GLHeader=t2.Oid
        left join GLBank t5 on t2.Oid=t5.Oid 
        left join LiquidAssets t3 on t5.LiquidAssets_Main=t3.Oid
        left join vv_PartnerConnectionInfoAccount t4 on t2.CustomersFrom=t4.CustomersFrom
             and t1.Customer=t4.Customers and t1.PartnerType=t4.PartnerType and t1.ConnectInfo=t4.ConnectInfo 
   where t2.GCRecord is null and
         t2.GLDocumentType=2 and
         t1.PartnerType in (0,1) and
         t1.GCRecord is null
   group by t1.Oid,t2.CustomersFrom,t1.Customer,t1.PartnerType,
            t1.ConnectInfo,t3.CurrencyName,t4.Currency,t2.DateExecution,t4.DatePayment,t4.CurrencyName

   union all
   
   select '03-Cassa' MainType,
          t1.Oid Oid,
          t2.CustomersFrom CustomersFrom,
          t1.Customer Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          t3.CurrencyName CurrencyName,
          0 DebitTotalHUF,
          case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalHUF,
          0 DebitTotalDEV,
          case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalDEV,
          (case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)*-1
          end)*-1 BallanceHUF,
          (case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
                when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and
                     ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
                when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
                when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                     ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF'then sum(t1.InAmountDEV2)*-1                               
           end)*-1 BallanceDEV,
          max(t3.ShortName+', '+ifnull(t2.DocumentNumber,'')) ShortName,
          '' Description,
          t2.DateExecution DateExecution,
          t2.DateExecution DateCreated,
          t4.DatePayment DatePayment,
          0 DelayedDay,
          '' DelayedRange,
          t4.CurrencyName CurrencyName_Account,
          0 AmountHUF_Account,
          0 AmountDEV_Account,
          max(t4.DateExecution) DateExecution_Account 
   from GLRows t1
        join GLHeader t2 on t1.GLHeader=t2.Oid
        left join GLCassa t5 on t2.Oid=t5.Oid
        left join LiquidAssets t3 on t5.LiquidAssets_Cassa=t3.Oid
        left join vv_PartnerConnectionInfoAccount t4 on t2.CustomersFrom=t4.CustomersFrom and
             t1.Customer=t4.Customers and t1.PartnerType=t4.PartnerType and t1.ConnectInfo=t4.ConnectInfo
   where t2.GCRecord is null and
         t2.GLDocumentType=3 and
         t1.PartnerType in (0,1) and
         t1.GCRecord is null
   group by t1.Oid,t2.CustomersFrom,t1.Customer,t1.PartnerType,
            t1.ConnectInfo,t3.CurrencyName,t4.Currency,t2.DateExecution,t4.DatePayment,t4.CurrencyName

   union all
   
   select '05-Ratediff' MainType,
          t1.Oid Oid,
          t2.CustomersFrom CustomersFrom,
          t1.Customer Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          t2.CurrencyName CurrencyName,
          0 DebitTotalHUF,
          case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 then sum(t1.AmountHUF)*-1
          end CreditTotalHUF,
          0 DebitTotalDEV,
          0 CreditTotalDEV,
          (case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 then sum(t1.AmountHUF)
                when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 then sum(t1.AmountHUF)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 then sum(t1.AmountHUF)
                when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)*-1
          end)*-1 BallanceHUF,
          0 BallanceDEV,
          max(ifnull(t7.ShortName,'')+', '++ifnull(t8.DocumentNumber,'')+', '+ifnull(t2.DocumentNumber,'')) ShortName,
          '' Description,
          t2.DateExecution DateExecution,
          t2.DateExecution DateCreated,
          Null DatePayment,
          0 DelayedDay,
          '' DelayedRange,
          t4.CurrencyName CurrencyName_Account,
          0 AmountHUF_Account,
          0 AmountDEV_Account,
          max(t4.DateExecution) DateExecution_Account 
   from GLRows t1
        join GLHeader t2 on t1.GLHeader=t2.Oid
        left join LiquidAssets t3 on t1.LiquidAssets=t3.Oid
        left join vv_PartnerConnectionInfoAccount t4 on  t2.CustomersFrom=t4.CustomersFrom and
             t1.Customer=t4.Customers and t1.PartnerType=t4.PartnerType and t1.ConnectInfo=t4.ConnectInfo
        left join GLRows t6 on
             t1.GLRowsRateDiff=t6.Oid left join LiquidAssets t7 on
             t6.LiquidAssets=t7.oid left join GLHeader t8 on
             t6.GLHeader=t8.oid
   where t2.GCRecord is null and
         t2.GLDocumentType=5 and
         t1.PartnerType in (0,1) and
         t1.GCRecord is null
   group by t1.Oid,t2.CustomersFrom,t1.Customer,t1.PartnerType,
            t1.ConnectInfo,t2.CurrencyName,t2.DateExecution,t2.DatePayment,t4.CurrencyName         
  
 union all         
   
   select case when max(t2.GLDocumentType)=4 then '04-Compensation' 
               when max(t2.GLDocumentType)=0 then '06-Mixed' 
          end  MainType,
          t1.Oid Oid,
          t2.CustomersFrom CustomersFrom,
          t1.Customer Customers,
          t1.PartnerType PartnerType,
          t1.ConnectInfo ConnectInfo,
          t3.CurrencyName CurrencyName,
          0 DebitTotalHUF,
          case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalHUF,
          0 DebitTotalDEV,
          case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)*-1                                 
          end CreditTotalDEV,
          (case when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0  then sum(t1.AmountHUF)
                when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0  then sum(t1.AmountHUF)*-1
          end)*-1 BallanceHUF,
          (case when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
           when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
           when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
           when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
           when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
           when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)*-1   
           when t1.PartnerType=1 and sum(t1.OutAmountDEV2)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
           when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                ifnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF'then sum(t1.InAmountDEV2)*-1                               
           else 0     
          end)*-1 BallanceDEV,
          max(t3.ShortName+', '+t2.DocumentNumber) ShortName,
          '' Description,
          t2.DateExecution DateExecution,
          t2.DateExecution DateCreated,
          t2.DateExecution DatePayment,
          0 DelayedDay,
          '' DelayedRange,
          t4.CurrencyName CurrencyName_Account,
          0 AmountHUF_Account,
          0 AmountDEV_Account,
          max(t4.DateExecution) DateExecution_Account 
   from GLRows t1
        join GLHeader t2 on t1.GLHeader=t2.Oid
        left join GLCompensation t5 on t2.Oid=t5.Oid left
        join LiquidAssets t3 on t5.LiquidAssets_Main=t3.Oid
        left join vv_PartnerConnectionInfoAccount t4 on t2.CustomersFrom=t4.CustomersFrom and
             t1.Customer=t4.Customers and t1.PartnerType=t4.PartnerType and t1.ConnectInfo=t4.ConnectInfo
   where t2.GCRecord is null and
         t2.GLDocumentType in (4,0) and
         t1.PartnerType in (0,1) and
         t1.GCRecord is null
   group by t1.Oid,t2.CustomersFrom,t1.Customer,t1.PartnerType,
            t1.ConnectInfo,t3.CurrencyName,t4.Currency,t2.DateExecution,t4.DatePayment,t4.CurrencyName     
    
   union all         

   select '01-Account' MainType,
          t1.Oid Oid,
          t1.CustomersFrom CustomersFrom,
          t1.Customers Customers,
          0 PartnerType,
          t1.ConnectInfo ConnectInfo,
          t1.CurrencyName CurrencyName,
          t1.TotalBruttoHUF DebitTotalHUF,
          0 CreditTotalHUF,
          t1.TotalBruttoDEV DebitTotalDEV,
          0 CreditTotalDEV,
          t1.TotalBruttoHUF BallanceHUF,
          t1.TotalBruttoDEV BallanceDEV,
          '' ShortName,
          t1.Description Description,
          t1.DateExecution DateExecution,
          t1.DateCreated DateCreated,
          t1.DatePayment DatePayment,
          DATEDIFF(t1.DatePayment,curdate()) DelayedDay,
          case when DATEDIFF(t1.DatePayment,curdate())<=0 then 0
               when DATEDIFF(t1.DatePayment,curdate())>0 and DATEDIFF(t1.DatePayment,curdate())<=15 then 1
               when DATEDIFF(t1.DatePayment,curdate())>15 and DATEDIFF(t1.DatePayment,curdate())<=30 then 2
               when DATEDIFF(t1.DatePayment,curdate())>30 and DATEDIFF(t1.DatePayment,curdate())<=60 then 3
               when DATEDIFF(t1.DatePayment,curdate())>60 and DATEDIFF(t1.DatePayment,curdate())<=90 then 4
               when DATEDIFF(t1.DatePayment,curdate())>90 and DATEDIFF(t1.DatePayment,curdate())<=180 then 5
               when DATEDIFF(t1.DatePayment,curdate())>180 and DATEDIFF(t1.DatePayment,curdate())<=360 then 6
               when DATEDIFF(t1.DatePayment,curdate())>360 then 7                                                          
          end DelayedRange,
          t1.CurrencyName CurrencyName_Account,
          t1.TotalBruttoHUF AmountHUF_Account,
          t1.TotalBruttoDEV AmountDEV_Account,
          t1.DateExecution DateExecution_Account     
   from InvoiceHeader t1
   where t1.GLAccounts is null and
         t1.IsEditable=0 and
         t1.GCRecord is null

   union all 
   
   select '01-Account' MainType,
          t1.Oid Oid,
          t1.CustomersFrom CustomersFrom,
          t1.Customers Customers,
          1 PartnerType,
          t1.DocumentNumber ConnectInfo,
          t1.F_CurrencyName CurrencyName,
          t1.F_AmountBruttoHUF DebitTotalHUF,
          0 CreditTotalHUF,
          t1.F_AmountBruttoDEV DebitTotalDEV,
          0 CreditTotalDEV,
          t1.F_AmountBruttoHUF BallanceHUF,
          t1.F_AmountBruttoDEV BallanceDEV,
          '' ShortName,
          '' Description,
          t1.F_DateExecution DateExecution,
          t1.F_DateCreated DateCreated,
          t1.F_DatePayment DatePayment,
          DATEDIFF(t1.F_DatePayment,curdate()) DelayedDay,
          case when DATEDIFF(t1.F_DatePayment,curdate())<=0 then 0
               when DATEDIFF(t1.F_DatePayment,curdate())>0 and DATEDIFF(t1.F_DatePayment,curdate())<=15 then 1
               when DATEDIFF(t1.F_DatePayment,curdate())>15 and DATEDIFF(t1.F_DatePayment,curdate())<=30 then 2
               when DATEDIFF(t1.F_DatePayment,curdate())>30 and DATEDIFF(t1.F_DatePayment,curdate())<=60 then 3
               when DATEDIFF(t1.F_DatePayment,curdate())>60 and DATEDIFF(t1.F_DatePayment,curdate())<=90 then 4
               when DATEDIFF(t1.F_DatePayment,curdate())>90 and DATEDIFF(t1.F_DatePayment,curdate())<=180 then 5
               when DATEDIFF(t1.F_DatePayment,curdate())>180 and DATEDIFF(t1.F_DatePayment,curdate())<=360 then 6
               when DATEDIFF(t1.F_DatePayment,curdate())>360 then 7                                                     
          end DelayedRange,
          t1.F_CurrencyName CurrencyName_Account,
          t1.F_AmountBruttoHUF AmountHUF_Account,
          t1.F_AmountBruttoDEV AmountDEV_Account,
          t1.F_DateExecution DateExecution_Account           
   from RegistryHeader t1 
        left join RegistryType t2 on t1.RegistryType=t2.Oid
   where t1.GCRecord is null and
         t1.F_GLAccounts is null and
         t1.IsEditable=0 and
         t1.IsStorno=0 and
         t2.RegistryMainType=0 and
         t1.RegistryAcceptState=0