USE [SISTEST]
GO

/****** Object:  View [dbo].[vv_WaitforRateDiff]    Script Date: 05/18/2012 09:20:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE view [dbo].[vv_WaitforRateDiff]
as
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(t4.AmountDEV_Account,2) AmountDEV_Account,
       round(t4.AmountHUF_Account,2) AmountHUF_Account,
       round(t4.BallanceDEV,2) BallanceDEV_Account,
       case when round(t4.AmountDEV_Account,2)<>0 then round(round(t4.AmountHUF_Account,2)/round(t4.AmountDEV_Account,2),2) else 0 end CurrencyRate_Account,
       t1.LiquidAssets LiquidAssets,
       t2.DateExecution DateExecution,
       round(t1.InAmountDEV,2) InAmountDEV,
       round(t1.OutAmountDEV,2) OutAmountDEV,
       round(t1.InAmountDEV2,2) InAmountDEV2,
       round(t1.OutAmountDEV2,2) OutAmountDEV2,
       case when InAmountHUF>0 then
                  case when t1.InAmountDEV <>0 then round(t1.InAmountHUF,2)/round(t1.InAmountDEV,2) 
	          else 0 
                  end
             else
                  case when t1.OutAmountDEV <>0 then round(t1.OutAmountHUF,2)/round(t1.OutAmountDEV,2) 
                  else 0
                  end
       end CurrencyRate,
       round(t1.InAmountHUF,2) InAmountHUF,
       round(t1.OutAmountHUF,2) OutAmountHUF,
       case when t3.CurrencyName=t5.CurrencyName then round(t1.InAmountDEV,2)* round(t3.CurrencyRate,2)
                                                 else round(t1.InAmountDEV2,2)* round(t3.CurrencyRate,2)
                                                 end InAmountHUF_Account,
       case when t3.CurrencyName=t5.CurrencyName then round(t1.OutAmountDEV,2)*round(t3.CurrencyRate,2) 
                                                 else round(t1.OutAmountDEV2,2)*round(t3.CurrencyRate,2)
                                                 end OutAmountHUF_Account,  
       case when t3.CurrencyName=t5.CurrencyName then round(t1.InAmountDEV,2)* round(t3.CurrencyRate,2)
                                                 else round(t1.InAmountDEV2,2)* round(t3.CurrencyRate,2)
                                                 end - round(t1.InAmountHUF,2)  InAmountHUF_Diff,
       case when t3.CurrencyName=t5.CurrencyName then round(t1.OutAmountDEV,2)*round(t3.CurrencyRate,2) 
                                                 else round(t1.OutAmountDEV2,2)*round(t3.CurrencyRate,2)
                                                 end - round(t1.OutAmountHUF,2) OutAmountHUF_Diff,
       t1.GLRowsRatediff GLRowsRatediff,
       case when t1.PartnerType = 0 Then
            case when case when t3.CurrencyName=t5.CurrencyName then round(t1.InAmountDEV,2)* round(t3.CurrencyRate,2)
                                                 else round(t1.InAmountDEV2,2)* round(t3.CurrencyRate,2)
                                                 end - round(t1.InAmountHUF,2) > 0 Then -1 
                 else 1
            end
            when t1.PartnerType = 1 Then
            case when  case when t3.CurrencyName=t5.CurrencyName then round(t1.OutAmountDEV,2)*round(t3.CurrencyRate,2) 
                                                 else round(t1.OutAmountDEV2,2)*round(t3.CurrencyRate,2)
                                                 end - round(t1.OutAmountHUF,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 isnull(t7.ShortName,'HUF')<>'HUF' and
      ((t1.PartnerType=0 and t1.InAmountHUF>0) or (t1.PartnerType=1 and t1.OutAmountHUF>0))





GO

