TR_MKTEXPENSE_IU_ORGANIZATIONCOST

Definition

Copy


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

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

  -- copy the amount to the organization amount.

  if update(COST)
    begin
      declare @ORGANIZATIONCURRENCYID uniqueidentifier = dbo.[UFN_CURRENCY_GETORGANIZATIONCURRENCY](); 

      update dbo.[MKTEXPENSE] set
        [MKTEXPENSE].[ORGANIZATIONCOST] = [MKTEXPENSE].[COST],
        [MKTEXPENSE].[BASECURRENCYID] = @ORGANIZATIONCURRENCYID,
        [MKTEXPENSE].[CHANGEDBYID] = [MKTEXPENSE].[CHANGEDBYID],
        [MKTEXPENSE].[DATECHANGED] = [MKTEXPENSE].[DATECHANGED]
      from inserted
      inner join dbo.[MKTEXPENSE] on inserted.ID = [MKTEXPENSE].[ID]
      where [MKTEXPENSE].[CURRENCYEXCHANGERATEID] is null
      and ([MKTEXPENSE].[BASECURRENCYID] is null or [MKTEXPENSE].[BASECURRENCYID] = @ORGANIZATIONCURRENCYID)
      and ([MKTEXPENSE].[ORGANIZATIONCOST] <> [MKTEXPENSE].[COST] or [MKTEXPENSE].[COST] = 0);
    end
end