TR_MKTMATERIAL_IU_ORGANIZATIONCOST

Definition

Copy


CREATE trigger [dbo].[TR_MKTMATERIAL_IU_ORGANIZATIONCOST] on [dbo].[MKTMATERIAL] 
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.[MKTMATERIAL] set
        [MKTMATERIAL].[ORGANIZATIONCOST] = [MKTMATERIAL].[COST],
        [MKTMATERIAL].[BASECURRENCYID] = @ORGANIZATIONCURRENCYID,
        [MKTMATERIAL].[CHANGEDBYID] = [MKTMATERIAL].[CHANGEDBYID],
        [MKTMATERIAL].[DATECHANGED] = [MKTMATERIAL].[DATECHANGED]
      from inserted
      inner join dbo.[MKTMATERIAL] on inserted.ID = [MKTMATERIAL].[ID]
      where [MKTMATERIAL].[CURRENCYEXCHANGERATEID] is null
      and ([MKTMATERIAL].[BASECURRENCYID] is null or [MKTMATERIAL].[BASECURRENCYID] = @ORGANIZATIONCURRENCYID)
      and ([MKTMATERIAL].[ORGANIZATIONCOST] <> [MKTMATERIAL].[COST] or [MKTMATERIAL].[COST] = 0);
    end
end