

   SELECT '01-Account' As "MainType",
          t2."Oid" As "Oid",
          t2."CustomersFrom" As "CustomersFrom",
          t1."Customer" As "Customers",
          t1."PartnerType" As "PartnerType",
          t1."ConnectInfo" As "ConnectInfo",
          t2."CurrencyName" As "CurrencyName",
          max(t3."AmountHUFBrutto") As "DebitTotalHUF",
          0 As "CreditTotalHUF",
          max(t3."AmountDEVBrutto") As "DebitTotalDEV",
          0 As "CreditTotalDEV",
          max(t3."AmountHUFBrutto") As "BallanceHUF",
          max(t3."AmountDEVBrutto") As "BallanceDEV",
          '' As "ShortName",
          '' As "Description",
          t2."DateExecution" As "DateExecution",
          t2."DateCreated" As "DateCreated",
          t2."DatePayment" As "DatePayment",
          CURRENT_DATE-Cast(t2."DatePayment" AS Date) As "DelayedDay",
          CASE WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)<=0 THEN 0
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>0 AND CURRENT_DATE-Cast(t2."DatePayment" AS date)<=15 then 1
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>15 and CURRENT_DATE-Cast(t2."DatePayment" AS date)<=30 then 2
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>30 and CURRENT_DATE-Cast(t2."DatePayment" AS date)<=60 then 3
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>60 and CURRENT_DATE-Cast(t2."DatePayment" AS date)<=90 then 4
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>90 and CURRENT_DATE-Cast(t2."DatePayment" AS date)<=180 then 5
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>180 and CURRENT_DATE-Cast(t2."DatePayment" AS date)<=360 then 6
               WHEN CURRENT_DATE-Cast(t2."DatePayment" AS date)>360 then 7                                                          
           END As "DelayedRange",
           t2."CurrencyName" As "CurrencyName_Account",
           SUM(t1."AmountHUF") As "AmountHUF_Account",
           SUM(t1."AmountDEV") As "AmountDEV_Account",
           t2."DateExecution" As "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"=false 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' As "MainType",
          t1."Oid" As "Oid",
          t2."CustomersFrom" As "CustomersFrom",
          t1."Customer" As "Customers",
          t1."PartnerType" As "PartnerType",
          t1."ConnectInfo" As "ConnectInfo",
          t3."CurrencyName" As "CurrencyName",
          0 As "DebitTotalHUF",
          case WHEN t1."PartnerType"=0 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end AS "CreditTotalHUF",
          0 AS "DebitTotalDEV",
          case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end AS "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 AS "BallanceHUF",
          (case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."InAmountDEV")
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."OutAmountDEV")*-1   
                WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."OutAmountDEV")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."InAmountDEV")*-1
                WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."InAmountDEV2")
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV2")<>0 and
                     coalesce(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
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."OutAmountDEV2")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF'then sum(t1."InAmountDEV2")*-1                               
          else 0     
          end)*-1 AS "BallanceDEV",
          max(t3."ShortName"||', '||coalesce(t2."DocumentNumber",'')) AS "ShortName",
          '' AS "Description",
          t2."DateExecution" AS "DateExecution",
          t2."DateExecution" AS "DateCreated",
          t4."DatePayment" AS "DatePayment",
          0 AS "DelayedDay",
          0 AS "DelayedRange",
          t4."CurrencyName" AS "CurrencyName_Account",
          0 AS "AmountHUF_Account",
          0 AS "AmountDEV_Account", 
          max(t4."DateExecution") AS "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' As "MainType",
          t1."Oid" As "Oid",
          t2."CustomersFrom" As "CustomersFrom",
          t1."Customer" As "Customers",
          t1."PartnerType" As "PartnerType",
          t1."ConnectInfo" As "ConnectInfo",
          t3."CurrencyName" As "CurrencyName",
          0 As "DebitTotalHUF",
          CASE WHEN t1."PartnerType"=0 AND SUM(t1."InAmountHUF")<>0 AND coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" THEN SUM(t1."AmountHUF")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end As "CreditTotalHUF",
          0 As "DebitTotalDEV",
          case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end As "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 As "BallanceHUF",
          (case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and 
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1   
                WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1
                WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."InAmountDEV2")
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV2")<>0 and
                     coalesce(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
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."OutAmountDEV2")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF'then sum(t1."InAmountDEV2")*-1                               
          end)*-1 As "BallanceDEV",
          MAX(t3."ShortName"||', '||coalesce(t2."DocumentNumber",'')) As "ShortName",
          '' As "Description",
          t2."DateExecution" As "DateExecution",
          t2."DateExecution" As "DateCreated",
          t4."DatePayment" As "DatePayment",
          0 As "DelayedDay",
          0 As "DelayedRange",
          t4."CurrencyName" As "CurrencyName_Account",
          0 As "AmountHUF_Account",
          0 As "AmountDEV_Account", 
          max(t4."DateExecution") As "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' As "MainType",
          t1."Oid" As "Oid",
          t2."CustomersFrom" As "CustomersFrom",
          t1."Customer" As "Customers",
          t1."PartnerType" As "PartnerType",
          t1."ConnectInfo" As "ConnectInfo",
          t2."CurrencyName" As "CurrencyName",
          0 As "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 As "CreditTotalHUF",
          0 As "DebitTotalDEV",
          0 As "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 As "BallanceHUF",
          0 As "BallanceDEV",
          max(t7."ShortName"||', '||coalesce(t8."DocumentNumber",'')||', '||coalesce(t2."DocumentNumber",''))As "ShortName",
          '' As "Description",
          t2."DateExecution" As "DateExecution",
          t2."DateExecution" As "DateCreated",
          null As "DatePayment",
          0 As "DelayedDay",
          0 As "DelayedRange",
          t4."CurrencyName" As "CurrencyName_Account",
          0 As "AmountHUF_Account",
          0 As "AmountDEV_Account", 
          max(t4."DateExecution") As "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" As "Oid",
          t2."CustomersFrom" As "CustomersFrom",
          t1."Customer" As "Customers",
          t1."PartnerType" As "PartnerType",
          t1."ConnectInfo" As "ConnectInfo",
          t3."CurrencyName" As "CurrencyName",
          0 As "DebitTotalHUF",
          case WHEN t1."PartnerType"=0 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountHUF")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountHUF")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end As "CreditTotalHUF",
          0 As "DebitTotalDEV",
          case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1
               WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")
               WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")*-1   
               WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."OutAmountDEV2")
               WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" then sum(t1."InAmountDEV2")*-1                                 
          end As "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 As "BallanceHUF",
          (case WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1   
                WHEN t1."PartnerType"=1 and sum(t1."OutAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")=t3."CurrencyName" then sum(t1."AmountDEV")*-1
                WHEN t1."PartnerType"=0 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."InAmountDEV2") 
                WHEN t1."PartnerType"=0 and sum(t1."OutAmountDEV2")<>0 and
                     coalesce(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
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF' then sum(t1."OutAmountDEV2")
                WHEN t1."PartnerType"=1 and sum(t1."InAmountDEV2")<>0 and
                     coalesce(t4."CurrencyName",t3."CurrencyName")<>t3."CurrencyName" and t4."Currency"<>'HUF'then sum(t1."InAmountDEV2")*-1                               
          else 0     
          end)*-1 As "BallanceDEV",
          max(t3."ShortName"||', '||t2."DocumentNumber") As "ShortName",
          '' As "Description",
          t2."DateExecution" As "DateExecution",
          t2."DateExecution" As "DateCreated",
          t2."DateExecution" As "DatePayment",
          0 As "DelayedDay",
          0 As "DelayedRange",
          t4."CurrencyName" As "CurrencyName_Account",
          0 As "AmountHUF_Account",
          0 As "AmountDEV_Account",
          max(t4."DateExecution") As "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' AS "MainType",
          t1."Oid" AS "Oid",
          t1."CustomersFrom" AS "CustomersFrom",
          t1."Customers" AS "Customers",
          0 AS "PartnerType",
          t1."ConnectInfo" AS "ConnectInfo",
          t1."CurrencyName" AS "CurrencyName",
          t1."TotalBruttoHUF" AS "DebitTotalHUF",
          0 AS "CreditTotalHUF",
          t1."TotalBruttoDEV" AS "DebitTotalDEV",
          0 AS "CreditTotalDEV",
          t1."TotalBruttoHUF" AS "BallanceHUF",
          t1."TotalBruttoDEV" AS "BallanceDEV",
          '' AS "ShortName",
          t1."Description" AS "Description",
          t1."DateExecution" AS "DateExecution",
          t1."DateCreated" AS "DateCreated",
          t1."DatePayment" AS "DatePayment",
          CURRENT_DATE-Cast(t1."DatePayment" AS date) AS "DelayedDay",
          case WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)<=0 then 0
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>0 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=15 then 1
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>15 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=30 then 2
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>30 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=60 then 3
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>60 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=90 then 4
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>90 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=180 then 5
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>180 and CURRENT_DATE-Cast(t1."DatePayment" AS date)<=360 then 6
               WHEN CURRENT_DATE-Cast(t1."DatePayment" AS date)>360 then 7                                                          
          end AS "DelayedRange",
          t1."CurrencyName" AS "CurrencyName_Account",
          t1."TotalBruttoHUF" AS "AmountHUF_Account",
          t1."TotalBruttoDEV" AS "AmountDEV_Account",
          t1."DateExecution" AS "DateExecution_Account"
   from "InvoiceHeader" t1
   where t1."GLAccounts" is null and
         t1."IsEditable"=false and
         t1."GCRecord" is null

   union all 
   
   select '01-Account' AS "MainType",
          t1."Oid" AS "Oid",
          t1."CustomersFrom" AS "CustomersFrom",
          t1."Customers" AS "Customers",
          1 AS "PartnerType",
          t1."DocumentNumber" AS "ConnectInfo",
          t1."F_CurrencyName" AS "CurrencyName",
          t1."F_AmountBruttoHUF" AS "DebitTotalHUF",
          0 AS "CreditTotalHUF",
          t1."F_AmountBruttoDEV" AS "DebitTotalDEV",
          0 AS "CreditTotalDEV",
          t1."F_AmountBruttoHUF" AS "BallanceHUF",
          t1."F_AmountBruttoDEV" AS "BallanceDEV",
          '' AS "ShortName",
          '' AS "Description",
          t1."F_DateExecution" AS "DateExecution",
          t1."F_DateCreated" AS "DateCreated",
          t1."F_DatePayment" AS "DatePayment",
          CURRENT_DATE-Cast(t1."F_DatePayment" AS date) AS "DelayedDay",
          case WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=0 then 0
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>0 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=15 then 1
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>15 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=30 then 2
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>30 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=60 then 3
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>60 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=90 then 4
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>90 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=180 then 5
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>180 and CURRENT_DATE-Cast(t1."F_DatePayment" AS date)<=360 then 6
               WHEN CURRENT_DATE-Cast(t1."F_DatePayment" AS date)>360 then 7                                                     
          end As "DelayedRange",
          t1."F_CurrencyName" AS "CurrencyName_Account",
          t1."F_AmountBruttoHUF" AS "AmountHUF_Account",
          t1."F_AmountBruttoDEV" AS "AmountDEV_Account",
          t1."F_DateExecution" AS "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"=false and
         t1."IsStorno"=false and
         t2."RegistryMainType"=0 and
         t1."RegistryAcceptState"=0