TR_BANKACCOUNTTRANSACTION_INSERTUPDATE_CURRENCY

Definition

Copy


                    CREATE trigger [dbo].[TR_BANKACCOUNTTRANSACTION_INSERTUPDATE_CURRENCY] on [dbo].[BANKACCOUNTTRANSACTION] 
                    after insert, update 
                    not for replication
                    as
                    begin
                        set nocount on;

                        declare @ORGANIZATIONCURRENCYID uniqueidentifier = dbo.UFN_CURRENCY_GETORGANIZATIONCURRENCY(); 
                        declare @ORGANIZATIONAMOUNTORIGINCODE tinyint;

            select @ORGANIZATIONAMOUNTORIGINCODE = @ORGANIZATIONAMOUNTORIGINCODE  from dbo.MULTICURRENCYCONFIGURATION;

            -- For multicurrency you need a corporate rate to populate the organization amount.  If a 

            -- corporate rate does not exist then your organization amount will be zero which is data corruption

            -- so we will not allow you to enter a transaction without a corporate rate.

              if exists
              (select * from INSERTED as I
               inner join dbo.BANKACCOUNT as BA on I.BANKACCOUNTID = BA.ID
               where 
                 ((@ORGANIZATIONAMOUNTORIGINCODE = 1 and BA.TRANSACTIONCURRENCYID <> @ORGANIZATIONCURRENCYID)
                  or
                  (@ORGANIZATIONAMOUNTORIGINCODE = 0 and I.BASECURRENCYID <> @ORGANIZATIONCURRENCYID))
                 and
                 I.ORGANIZATIONEXCHANGERATEID is null)
                  begin
                      raiserror('ERR_CORPORATERATE_RATE_REQUIRED', 16, 1)
                      rollback transaction
                  end

                        -- if we try to save an amount without explicitly setting an organization amount and an exchange rate, 

                        -- copy the amount to the organization and transaction amounts.

                        if update(AMOUNT)
                        begin
                            update
                                dbo.BANKACCOUNTTRANSACTION
                            set
                                BANKACCOUNTTRANSACTION.ORGANIZATIONAMOUNT = BANKACCOUNTTRANSACTION.AMOUNT,
                                BANKACCOUNTTRANSACTION.TRANSACTIONAMOUNT = BANKACCOUNTTRANSACTION.AMOUNT,
                                BANKACCOUNTTRANSACTION.BASECURRENCYID = @ORGANIZATIONCURRENCYID,
                                BANKACCOUNTTRANSACTION.CHANGEDBYID = BANKACCOUNTTRANSACTION.CHANGEDBYID,
                                BANKACCOUNTTRANSACTION.DATECHANGED = BANKACCOUNTTRANSACTION.DATECHANGED
                            from
                                inserted
                            inner join
                                dbo.BANKACCOUNTTRANSACTION on inserted.ID = BANKACCOUNTTRANSACTION.ID
                inner join dbo.BANKACCOUNT as BA on BANKACCOUNTTRANSACTION.BANKACCOUNTID = BA.ID
                            where
                                BANKACCOUNTTRANSACTION.ORGANIZATIONEXCHANGERATEID is null
                                and (BANKACCOUNTTRANSACTION.BASECURRENCYID is null or BANKACCOUNTTRANSACTION.BASECURRENCYID = @ORGANIZATIONCURRENCYID)
                                and BANKACCOUNTTRANSACTION.BASEEXCHANGERATEID is null
                and (BA.TRANSACTIONCURRENCYID is null or BA.TRANSACTIONCURRENCYID = @ORGANIZATIONCURRENCYID)
                and
                                (
                                    BANKACCOUNTTRANSACTION.ORGANIZATIONAMOUNT <> BANKACCOUNTTRANSACTION.AMOUNT
                                    or BANKACCOUNTTRANSACTION.TRANSACTIONAMOUNT <> BANKACCOUNTTRANSACTION.AMOUNT
                                    or BANKACCOUNTTRANSACTION.AMOUNT = 0
                                )
                        end
                    end