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 @Price_Consumer_New MONEY
		DECLARE @Price_Finish_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 ,
				@Price_Consumer_New = i.Price_Consumer,
				@Price_Finish_New = i.Price_Finish,
                @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 ,
					  Price_Consumer,
					  Price_Finish,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
					  @Price_Consumer_New,
					  @Price_Finish_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 ,
					  Price_Consumer,
					  Price_Finish,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
					  @Price_Consumer_New,
					  @Price_Finish_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 ,
					  Price_Consumer,
					  Price_Finish,
                      ChangePrice, User_Id
                    )
            VALUES  ( @Anbar_Code ,
                      @Kala_Code ,
                      @Price_Kharid_New ,
                      @Price_Omdeh_New ,
                      @Price_Forosh_New ,
					  @Price_Consumer_New,
					  @Price_Finish_New,
                      '9', @UserId
                    )

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

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


    
END
