CREATE TRIGGER [dbo].[TU_HamrahDrug]
ON [dbo].[TmpDrugHavaleh]
FOR UPDATE
AS
DECLARE @Qty1 REAL;
DECLARE @Qty2 REAL;
DECLARE @Hamrah_Flag BIT;
DECLARE @Make_Flag BIT;
DECLARE @Autoid NUMERIC(9);
DECLARE @K_Code VARCHAR(20);
DECLARE @HKqty REAL;
DECLARE @MainKCode VARCHAR(20);
DECLARE @EditQty Flag;
DECLARE @KQty REAL;
DECLARE @Sender VARCHAR(5);
DECLARE @Price MONEY;
DECLARE @Price_Forosh MONEY;
DECLARE @Sazman_Code VARCHAR(3);
DECLARE @Darou_Flag CHAR(1);
DECLARE @SumDiffer MONEY;

IF UPDATE(K_Qty1)
BEGIN
    SELECT @Qty1 = K_Qty1,
           @Hamrah_Flag = Hamrah_Flag,
           @Autoid = AutoId,
           @MainKCode = K_Code
    FROM inserted;
    SELECT @Qty2 = K_Qty1,
           @Sender = Sender
    FROM deleted;

    SELECT @Sazman_Code = TmpDrugH.Sazman_Code
    FROM TmpDrugH
        INNER JOIN Inserted
            ON TmpDrugH.Id_Havaleh = Inserted.Id_Havaleh
    GROUP BY Sazman_Code;


    IF (@Qty1 != @Qty2)
    BEGIN
        IF (@Hamrah_Flag = 1)
        BEGIN
            DECLARE TmpDrug CURSOR FOR
            SELECT AutoId,
                   K_Code
            FROM TmpDrugHavaleh
            WHERE Link_Record = @Autoid;
            OPEN TmpDrug;
            FETCH NEXT FROM TmpDrug
            INTO @Autoid,
                 @K_Code;
            WHILE @@FETCH_STATUS = 0
            BEGIN
                --------------------------------------------------------------------------------
                SELECT @Price_Forosh = Price_Forosh,
                       @Darou_Flag = Darou_Flag,
                       @Price = Price
                FROM
                (
                    SELECT KalaId.K_Code,
                           Anbar.Price_Forosh,
                           CASE
                               WHEN KalaId.Darou_Flag <> '6' THEN
                                   KalaId.Darou_Flag
                               WHEN KalaId.Darou_Flag = '6' THEN
                               (
                                   SELECT ISNULL(Price_Sazman.Status, 0)
                                   FROM Price_Sazman
                                   WHERE Sazman_Code = @Sazman_Code
                                         AND Price_Sazman.K_Code = KalaId.K_Code
                               )
                           END AS Darou_Flag,
                           CASE
                               WHEN KalaId.Darou_Flag <> '6' THEN
                                   0
                               WHEN KalaId.Darou_Flag = '6' THEN
                               (
                                   SELECT ISNULL(Price_Sazman.Price, 0)
                                   FROM Price_Sazman
                                   WHERE Sazman_Code = @Sazman_Code
                                         AND Price_Sazman.K_Code = KalaId.K_Code
                                         AND Status IN ( '4', '6' )
                               )
                           END AS Price,
                           KalaId.Meli_Code
                    FROM KalaId
                        INNER JOIN Anbar
                            ON KalaId.K_Code = Anbar.K_Code
                    WHERE (Anbar.Active_Flag = '0')
                          AND (Anbar.A_Code = @Sender)
                ) PublicTable
                WHERE K_Code = @K_Code;

                --------------------------------------------------------------------------------
                SELECT @HKqty = K_Qty1,
                       @EditQty = EditQty_Flag
                FROM Hamrah_Kala
                WHERE K_Code = @MainKCode
                      AND K_Code_H = @K_Code;
                IF @EditQty = 1
                    SET @KQty = @Qty1 * @HKqty;
                ELSE
                    SET @KQty = @HKqty;

                SET @SumDiffer = 0;
                IF @Darou_Flag IN ( '4', '6' )
                    IF (@Price <> 0)
                       AND (@Price < @Price_Forosh)
                        SET @SumDiffer = (@KQty * @Price_Forosh) - (@KQty * @Price);


                UPDATE dbo.TmpDrugHavaleh
                SET K_Qty1 = @KQty,
                    Tot_Differ = @SumDiffer
                WHERE AutoId = @Autoid;
                FETCH NEXT FROM TmpDrug
                INTO @Autoid,
                     @K_Code;
            END;
            CLOSE TmpDrug;
            DEALLOCATE TmpDrug;
        END;
    END;
END;
