CREATE PROC [dbo].[Cash_CalcTransactionMultiplePrescription]
    @CashUserId SMALLINT,
    @TransactionCode SMALLINT
AS
DECLARE @TotalPrice MONEY = 0;

SELECT @TotalPrice = SUM(   CASE
                                WHEN Kind_Flag = 1 THEN
                                    TotPriceforosh
                                ELSE
                                    -1 * TotPriceforosh
                            END
                        )
FROM dbo.TmpDrugH
WHERE kind_Save IN ( 2, 3 )
      AND MultipleFactorUserId = @CashUserId;

IF @TotalPrice > 0
BEGIN
    ----------------------------------------------------------------------------------
    DELETE FROM dbo.Tmp_Over_Under_Factor_Cash
    WHERE Id_Havaleh IN (
                            SELECT Id_Havaleh FROM dbo.TmpDrugH WHERE MultipleFactorUserId = @CashUserId
                        );
    --------------------------------------------------------------------------------
    DECLARE @FactorId NUMERIC(18, 0) = 0,
            @CalcSum MONEY = @TotalPrice,
            @SalesAmount MONEY = 0,
            @CardAmount MONEY = 0,
            @CardPercent REAL = 0,
            @tmp MONEY = 0;
    DECLARE curCashCalc CURSOR FOR
    SELECT Id_Havaleh,
           TotPriceforosh
    FROM TmpDrugH
    WHERE Kind_Flag = 1
          AND kind_Save IN ( 2, 3 )
          AND MultipleFactorUserId = @CashUserId
    ORDER BY TmpDrugH.TotPriceforosh;
    OPEN curCashCalc;
    FETCH NEXT FROM curCashCalc
    INTO @FactorId,
         @SalesAmount;
    WHILE @@FETCH_STATUS = 0
    BEGIN
        ----------------------------
        IF @CalcSum - @SalesAmount >= 0
        BEGIN
            SET @CalcSum = @CalcSum - @SalesAmount;
            SET @CardAmount = @SalesAmount;
            SET @CardPercent = 100;
            INSERT INTO dbo.Tmp_Over_Under_Factor_Cash
            (
                Id_Havaleh,
                Act_Code,
                ShowPrice,
                Max_Price,
                Max_Percent,
                Code_Over_Under,
                Type,
                Price,
                [Percent],
                IsDuty,
                Guy_Code
            )
            SELECT @FactorId,
                   -1,
                   0,
                   0,
                   0,
                   @TransactionCode,
                   0,
                   @CardAmount,
                   @CardPercent,
                   0,
                   -4;

        END;
        ELSE IF @CalcSum - @SalesAmount < 0
        BEGIN
            SET @CardAmount = @CalcSum;
            SET @CardPercent = 100 * @CalcSum / @SalesAmount;
            INSERT INTO dbo.Tmp_Over_Under_Factor_Cash
            (
                Id_Havaleh,
                Act_Code,
                ShowPrice,
                Max_Price,
                Max_Percent,
                Code_Over_Under,
                Type,
                Price,
                [Percent],
                IsDuty,
                Guy_Code
            )
            SELECT @FactorId,
                   -1,
                   0,
                   0,
                   0,
                   @TransactionCode,
                   0,
                   @CardAmount,
                   @CardPercent,
                   0,
                   -4;

            SET @CalcSum = 0;
        END;

        FETCH NEXT FROM curCashCalc
        INTO @FactorId,
             @SalesAmount;
    END;
    CLOSE curCashCalc;
    DEALLOCATE curCashCalc;
--------------------------------------------------------------------------------
END;
ELSE
    SELECT @TotalPrice;
