

   SELECT '01-Account' AS "MainType",
	   t2."Oid" AS "Oid",
	   t2."CustomersFrom" AS "CustomersFrom",
	   t1."Customer" AS "Customers",
	   t1."PartnerType" AS "PartnerType",
	   t2."DocumentNumber" AS "ConnectInfo",
           t2."CurrencyName" AS "CurrencyName",
           sum(t1."AmountHUF") AS "BallanceHuf",
	   sum(t1."AmountDEV") AS "BallanceDev",
           t3."ShortName" AS "Currency",
           t2."DatePayment" AS "DatePayment",
           t2."DateCreated" AS "DateCreated",
           t2."DateExecution" AS "DateExecution",
          t2."CurrencyRate" AS "CurrencyRate",
          t2."DocumentNumber" AS "DocumentNumber",
          max(t2."RegistryNumber") AS "RegistryNumber"
   from "GLRows" t1 
	join "GLHeader" t2 on t1."GLHeader"=t2."Oid" 
        left join "CurrencyName" t3 on t2."CurrencyName"=t3."Oid"
   where t2."GCRecord" is null and
         t2."GLDocumentType"=1 and
         coalesce(t2."IsEditable", false) = false and
         t1."PartnerType" in (0,1) and
         t1."GCRecord" is null
   group by t2."Oid",t2."CustomersFrom",t1."Customer",t1."PartnerType",
            t2."DocumentNumber",t2."CurrencyName",t3."ShortName",t2."DateExecution",t2."DatePayment",
            t2."DateCreated",t2."DateExecution",t2."CurrencyRate"
        
  union all         
   
   SELECT '01-Account' AS "MainType",
	  t1."Oid" AS "Oid",
	  t1."CustomersFrom" AS "CustomersFrom",
	  t1."Customers" AS "Customers",
	  0 AS "PartnerType",
	  t1."DocumentNumber" AS "ConnectInfo",
	  t1."CurrencyName" AS currencyname,
	  t1."TotalBruttoHUF" AS "BallanceHuf",
	  t1."TotalBruttoDEV" AS "BallanceDev",
          t3."ShortName" AS "Currency",
          t1."DatePayment" AS "DatePayment",
          t1."DateCreated" AS "DateCreated",
          t1."DateExecution" AS "DateExecution",
          t1."CurrencyRate" AS "CurrencyRate",
          t1."DocumentNumber" AS "DocumentNumber",
          t1."DocumentNumber" AS "RegistryNumber"
   from "InvoiceHeader" t1 
	left join "CurrencyName" t3 on t1."CurrencyName"=t3."Oid"
   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 "BallanceHuf",
	   t1."F_AmountBruttoDEV" AS "BallanceDev",
           t3."ShortName" AS "Currency",
           t1."F_DatePayment" AS "DatePayment",
           t1."F_DateCreated" AS "DateCreated",
           t1."F_DateExecution" AS "DateExecution",
          t1."F_CurrencyRate" AS "CurrencyRate",
          t1."DocumentNumber" AS "DocumentNumber",
          t1."RegistryNumber" AS "RegistryNumber"
   from "RegistryHeader" t1 
        left join "RegistryType" t2 on t1."RegistryType"=t2."Oid" 
        left join "CurrencyName" t3 on t1."F_CurrencyName"=t3."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
