USE [SISTEST]
GO

/****** Object:  View [dbo].[vv_PartnerConnectionInfoDetail]    Script Date: 05/18/2012 09:19:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO








CREATE view [dbo].[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(d,t2.DatePayment,getdate()) DelayedDay,
          case when DATEDIFF(d,t2.DatePayment,getdate())<=0 then 0 
               when DATEDIFF(d,t2.DatePayment,getdate())>0 and DATEDIFF(d,t2.DatePayment,getdate())<=15 then 1
               when DATEDIFF(d,t2.DatePayment,getdate())>15 and DATEDIFF(d,t2.DatePayment,getdate())<=30 then 2
               when DATEDIFF(d,t2.DatePayment,getdate())>30 and DATEDIFF(d,t2.DatePayment,getdate())<=60 then 3
               when DATEDIFF(d,t2.DatePayment,getdate())>60 and DATEDIFF(d,t2.DatePayment,getdate())<=90 then 4
               when DATEDIFF(d,t2.DatePayment,getdate())>90 and DATEDIFF(d,t2.DatePayment,getdate())<=180 then 5
               when DATEDIFF(d,t2.DatePayment,getdate())>180 and DATEDIFF(d,t2.DatePayment,getdate())<=360 then 6
               when DATEDIFF(d,t2.DatePayment,getdate())>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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(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 
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.InAmountDEV)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.OutAmountDEV)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.OutAmountDEV)
                when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.InAmountDEV)*-1
                when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                     isnull(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 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
                when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF'then sum(t1.InAmountDEV2)*-1                               
          else 0     
          end)*-1 BallanceDEV,
          max(t3.ShortName+', '+isnull(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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(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
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
                when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
                when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and
                     isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
                when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
                when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                     isnull(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 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
                when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                     isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF'then sum(t1.InAmountDEV2)*-1                               
           end)*-1 BallanceDEV,
          max(t3.ShortName+', '+isnull(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(isnull(t7.ShortName,'')+', '++isnull(t8.DocumentNumber,'')+', '+isnull(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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountHUF)*-1
               when t1.PartnerType=0 and sum(t1.InAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountHUF)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountHUF)<>0 and isnull(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 isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
               when t1.PartnerType=0 and sum(t1.InAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.InAmountDEV2)
               when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)*-1   
               when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName then sum(t1.OutAmountDEV2)
               when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and isnull(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 
                isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
           when t1.PartnerType=0 and sum(t1.OutAmountDEV)<>0 and 
                isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1   
           when t1.PartnerType=1 and sum(t1.OutAmountDEV)<>0 and 
                isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)
           when t1.PartnerType=1 and sum(t1.InAmountDEV)<>0 and 
                isnull(t4.CurrencyName,t3.CurrencyName)=t3.CurrencyName then sum(t1.AmountDEV)*-1
           when t1.PartnerType=0 and sum(t1.InAmountDEV2)<>0 and 
                isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.InAmountDEV2)
           when t1.PartnerType=0 and sum(t1.OutAmountDEV2)<>0 and 
                isnull(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 
                isnull(t4.CurrencyName,t3.CurrencyName)<>t3.CurrencyName and t4.Currency<>'HUF' then sum(t1.OutAmountDEV2)
           when t1.PartnerType=1 and sum(t1.InAmountDEV2)<>0 and 
                isnull(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(d,t1.DatePayment,getdate()) DelayedDay,
          case when DATEDIFF(d,t1.DatePayment,getdate())<=0 then 0
               when DATEDIFF(d,t1.DatePayment,getdate())>0 and DATEDIFF(d,t1.DatePayment,getdate())<=15 then 1
               when DATEDIFF(d,t1.DatePayment,getdate())>15 and DATEDIFF(d,t1.DatePayment,getdate())<=30 then 2
               when DATEDIFF(d,t1.DatePayment,getdate())>30 and DATEDIFF(d,t1.DatePayment,getdate())<=60 then 3
               when DATEDIFF(d,t1.DatePayment,getdate())>60 and DATEDIFF(d,t1.DatePayment,getdate())<=90 then 4
               when DATEDIFF(d,t1.DatePayment,getdate())>90 and DATEDIFF(d,t1.DatePayment,getdate())<=180 then 5
               when DATEDIFF(d,t1.DatePayment,getdate())>180 and DATEDIFF(d,t1.DatePayment,getdate())<=360 then 6
               when DATEDIFF(d,t1.DatePayment,getdate())>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(d,t1.F_DatePayment,getdate()) DelayedDay,
          case when DATEDIFF(d,t1.F_DatePayment,getdate())<=0 then 0
               when DATEDIFF(d,t1.F_DatePayment,getdate())>0 and DATEDIFF(d,t1.F_DatePayment,getdate())<=15 then 1
               when DATEDIFF(d,t1.F_DatePayment,getdate())>15 and DATEDIFF(d,t1.F_DatePayment,getdate())<=30 then 2
               when DATEDIFF(d,t1.F_DatePayment,getdate())>30 and DATEDIFF(d,t1.F_DatePayment,getdate())<=60 then 3
               when DATEDIFF(d,t1.F_DatePayment,getdate())>60 and DATEDIFF(d,t1.F_DatePayment,getdate())<=90 then 4
               when DATEDIFF(d,t1.F_DatePayment,getdate())>90 and DATEDIFF(d,t1.F_DatePayment,getdate())<=180 then 5
               when DATEDIFF(d,t1.F_DatePayment,getdate())>180 and DATEDIFF(d,t1.F_DatePayment,getdate())<=360 then 6
               when DATEDIFF(d,t1.F_DatePayment,getdate())>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








GO

