TR_BANKACCOUNT_VALIDCURRENCY

Definition

Copy


CREATE trigger dbo.TR_BANKACCOUNT_VALIDCURRENCY on BANKACCOUNT for INSERT, UPDATE not for replication as
begin
  set nocount on;

  if dbo.UFN_INSTALLEDPRODUCTS_PRODUCTIS('97D98E59-5B0B-446F-BF48-DE8709F50AFE') = 1
  begin
    --validate that the currency is in the currency set of the account system

    declare @DEFAULTCURRENCY uniqueidentifier;
    select @DEFAULTCURRENCY = ID from dbo.CURRENCY where ISORGANIZATIONCURRENCY = 1;

    if (select count(ID) from inserted) > 0 and
       (not exists(select I.ID from INSERTED I
        inner join dbo.PDACCOUNTSYSTEM S on S.ID = I.PDACCOUNTSYSTEMID
        inner join dbo.CURRENCYSETTRANSACTIONCURRENCY C on C.CURRENCYSETID = S.CURRENCYSETID
        where C.CURRENCYID = I.TRANSACTIONCURRENCYID or I.TRANSACTIONCURRENCYID is null or I.TRANSACTIONCURRENCYID = @DEFAULTCURRENCY))
    BEGIN
      RAISERROR('ERR_BANKACCOUNT_TRANSACTIONCURRENCYID_NOTINCURRENCYSET',  16, 1)
          ROLLBACK TRANSACTION
    END

    --Do not allow to change transaction currency once transactions have been added to bank account

    if exists(select I.ID from inserted I
        inner join deleted D on D.ID = I.ID
        inner join dbo.BANKACCOUNTTRANSACTION BAT on I.ID = BAT.BANKACCOUNTID
        where ((not D.TRANSACTIONCURRENCYID is null) and I.TRANSACTIONCURRENCYID <> D.TRANSACTIONCURRENCYID) or I.TRANSACTIONCURRENCYID is null)
    BEGIN
      RAISERROR('ERR_BANKACCOUNT_TRANSACTIONCURRENCYIDCANNOTBECHANGED',  16, 1)
          ROLLBACK TRANSACTION
    END
  end

end