CREATE VIEW [dbo].[TSignPaymentWGWView]
AS
--Retro test on cases: CR223062, CR223806, CR223867, CR223907, CR223923, CR224948
--nb20210806: Created View 4 calc signed value of transfer amounts
SELECT DISTINCT T.TransactionNumber
, CASE --WHEN WG.Payment = 5 THEN -1 --CR225628 Discount like transfer (partially) does neither need speciel threatment after switch from pieces to debit change
    WHEN WG.Payment = 1 and W.Payment = 13 THEN -1 --TIPS CR215787; GiftCert Køb CR225054
    WHEN CASE 
      WHEN (W.Payment = 5 OR W.Type = 4) THEN 0
      ELSE T.Payment END = 0 THEN 1
    ELSE -1 END AS SignedValue --OBS: Payment based sign flip (Pieces flip have to be doned outside cause this view is used on content and container records)

--, T.Payment, W.Payment AS wPayment, W.Type AS wType, WG.Payment AS wgPayment
FROM dbo.Transactions T (NOLOCK) 
  JOIN Ware W WITH (NOLOCK) ON T.WareNumber = W.WareNumber
  JOIN WAREGROUPS WG WITH (NOLOCK) ON W.WareGroupNumber = WG.WareGroupNumber
  --LEFT JOIN TransactionTransactionXtab TX WITH (NOLOCK) ON TX.FK_TransactionNumber1 = T.TransactionNumber AND RelationType=2
GO
