CREATE TRIGGER [dbo].[TI_History_Price] ON [dbo].[Anbar]
    AFTER INSERT
AS
    BEGIN
        DECLARE @Price_Kharid_New MONEY
        DECLARE @Price_Omdeh_New MONEY
        DECLARE @Price_Forosh_New MONEY

        DECLARE @Anbar_Code VARCHAR(5)
        DECLARE @Kala_Code VARCHAR(15)
        DECLARE @UserId SMALLINT

        SELECT  @Price_Kharid_New = Price_Kharid ,
                @Price_Omdeh_New = Price_Omdeh ,
                @Price_Forosh_New = Price_Forosh ,
                @UserId = i.User_Id ,
                @Anbar_Code = A_Code ,
                @Kala_Code = K_Code
        FROM    Inserted i
        WHERE   A_Code = i.A_Code
                AND K_Code = i.K_Code

        IF ( ISNULL(@Price_Kharid_New, 0) <> 0 )
            INSERT  INTO dbo.History_Price
                    ( A_Code ,
                      K_Code ,
                      Price_Kharid ,
                      Price_Omdeh ,
                      Price_Forosh ,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
                      '3', @UserId
                    )

        IF ( ISNULL(@Price_Forosh_New, 0) <> 0 )
            INSERT  INTO dbo.History_Price
                    ( A_Code ,
                      K_Code ,
                      Price_Kharid ,
                      Price_Omdeh ,
                      Price_Forosh ,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
                      '5', @UserId
                    )

        IF ( ISNULL(@Price_Omdeh_New, 0) <> 0 )
            INSERT  INTO dbo.History_Price
                    ( A_Code ,
                      K_Code ,
                      Price_Kharid ,
                      Price_Omdeh ,
                      Price_Forosh ,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
                      '9', @UserId
                    )
    
    END
