

select t1."Oid" Oid,
       t2."CustomersFrom" CustomersFrom,
       t1."PartnerType" PartnerType,
       t1."Customer" Customers,
       t1."ConnectInfo" ConnectInfo,
       t3."DateExecution" DateExecution_Account,
       t3."CurrencyName" CurrencyName_Account,
       round(cast(t4."AmountDEV_Account" as numeric),2) AmountDEV_Account,
       round(cast(t4."AmountHUF_Account" as numeric),2) AmountHUF_Account,
       round(cast(t4."BallanceDEV" as numeric),2) BallanceDEV_Account,
       case when round(cast(t4."AmountDEV_Account" as numeric),2)<>0 then round(cast(round(cast(t4."AmountHUF_Account" as numeric),2)/round(cast(t4."AmountDEV_Account" as numeric),2) as numeric),2) else 0 end CurrencyRate_Account,
       t1."LiquidAssets" LiquidAssets,
       t2."DateExecution" DateExecution,
       round(cast(t1."InAmountDEV" as numeric),2) InAmountDEV,
       round(cast(t1."OutAmountDEV" as numeric),2) OutAmountDEV,
       round(cast(t1."InAmountDEV2" as numeric),2) InAmountDEV2,
       round(cast(t1."OutAmountDEV2" as numeric),2) OutAmountDEV2,
       case when "InAmountHUF">0 then
                  case when t1."InAmountDEV" <>0 then round(cast(t1."InAmountHUF" as numeric),2)/round(cast(t1."InAmountDEV" as numeric),2) 
	          else 0 
                  end
             else
                  case when t1."OutAmountDEV" <>0 then round(cast(t1."OutAmountHUF" as numeric),2)/round(cast(t1."OutAmountDEV" as numeric),2) 
                  else 0
                  end
       end CurrencyRate,
       round(cast(t1."InAmountHUF" as numeric),2) InAmountHUF,
       round(cast(t1."OutAmountHUF" as numeric),2) OutAmountHUF,
       case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."InAmountDEV" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 else round(cast(t1."InAmountDEV2" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 end InAmountHUF_Account,
       case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."OutAmountDEV" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2) 
                                                 else round(cast(t1."OutAmountDEV2" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2)
                                                 end OutAmountHUF_Account,  
       case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."InAmountDEV" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 else round(cast(t1."InAmountDEV2" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 end - round(cast(t1."InAmountHUF" as numeric),2)  InAmountHUF_Diff,
       case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."OutAmountDEV" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2) 
                                                 else round(cast(t1."OutAmountDEV2" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2)
                                                 end - round(cast(t1."OutAmountHUF" as numeric),2) OutAmountHUF_Diff,
       t1."GLRowsRateDiff" GLRowsRateDiff,
       case when t1."PartnerType" = 0 Then
            case when case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."InAmountDEV" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 else round(cast(t1."InAmountDEV2" as numeric),2)* round(cast(t3."CurrencyRate" as numeric),2)
                                                 end - round(cast(t1."InAmountHUF" as numeric),2) > 0 Then -1 
                 else 1
            end
            when t1."PartnerType" = 1 Then
            case when  case when t3."CurrencyName"=t5."CurrencyName" then round(cast(t1."OutAmountDEV" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2) 
                                                 else round(cast(t1."OutAmountDEV2" as numeric),2)*round(cast(t3."CurrencyRate" as numeric),2)
                                                 end - round(cast(t1."OutAmountHUF" as numeric),2) > 0 Then 1
                 else -1
            end
            else 0
            end RateDiffState
from "GLRows" t1 
     left join "GLHeader" t2 on t1."GLHeader"=t2."Oid" 
     join "vv_PartnerConnectionInfoAccount" t3 on t2."CustomersFrom"=t3."CustomersFrom" 
          and t1."PartnerType"=t3."PartnerType" and t1."Customer"=t3."Customers" and t1."ConnectInfo"=t3."ConnectInfo" 
     left join "PCIGroup" t4 on t2."CustomersFrom"=t4."CustomersFrom" 
          and t1."PartnerType"=t4."PartnerType" and t1."Customer"=t4."Customers" and t1."ConnectInfo"=t4."ConnectInfo" 
     left join "LiquidAssets" t5 on t1."LiquidAssets"=t5."Oid"
     left join "CurrencyName" t6 on t5."CurrencyName"=t6."Oid"
     left join "CurrencyName" t7 on t3."CurrencyName"=t7."Oid"
where t2."GCRecord" is null and
      t2."GLDocumentType" in (2,3,4) and
      t6."ShortName" <> 'HUF' and coalesce(t7."ShortName",'HUF')<>'HUF' and
      ((t1."PartnerType"=0 and t1."InAmountHUF">0) or (t1."PartnerType"=1 and t1."OutAmountHUF">0))

