CREATE PROCEDURE [dbo].[AddTaminPrescription]
    @PrescriptionId BIGINT,
    @WarehouseCode VARCHAR(3),
    @TrackingCode INT
AS
BEGIN
    DECLARE @SupportTransactionCode SMALLINT = 0;
    DECLARE @SupportTransactionName NVARCHAR(MAX);
    IF EXISTS
    (
        SELECT 1
        FROM dbo.GeneralSetting
        WHERE Parameter = 'TAMIN_Settings_ActiveSaveSupport'
              AND Value = 'True'
    )
    BEGIN

        SELECT @SupportTransactionCode = CAST(ISNULL(Value, 0) AS SMALLINT)
        FROM dbo.GeneralSetting
        WHERE Parameter = 'TAMIN_Settings_TransactionCodeSupport';

        SELECT @SupportTransactionName = [Name]
        FROM dbo.Over_Under
        WHERE AutoId = @SupportTransactionCode;

    END;
    DECLARE @SupplementaryExists BIT = 0;
    DECLARE @CtrMojodi CHAR(1);
    DECLARE @MedicalGradeCode VARCHAR(3);
    DECLARE @MedicalGradeName NVARCHAR(45);
    DECLARE @MedicalGradeStatus NVARCHAR(10);
    DECLARE @Id UNIQUEIDENTIFIER;
    DECLARE @InsurerCode VARCHAR(5);
    DECLARE @RowIndex INT;
    DECLARE @GoodsCode VARCHAR(15);
    DECLARE @DeliverCount INT;
    DECLARE @DeliverCountCache INT;
    DECLARE @Differ MONEY;
    DECLARE @Status TINYINT;
    DECLARE @InsurerPercent REAL;
    DECLARE @SalePrice MONEY;
    DECLARE @DrugPrice MONEY;
    DECLARE @Instruction NVARCHAR(MAX);
    DECLARE @IsLock TINYINT;
    DECLARE @PurchasePrice MONEY;
    DECLARE @SupportAmount MONEY = 0;
    DECLARE @TotalSupportAmount MONEY = 0;
    DECLARE @Mobile VARCHAR(15);
    DECLARE @IsBulk BIT = 0;
    DECLARE @NationalCode VARCHAR(12);


    DECLARE @CacheData AS TABLE
    (
        Id UNIQUEIDENTIFIER NOT NULL,
        InsurerCode VARCHAR(3),
        InsurerName NVARCHAR(50),
        InsuredName NVARCHAR(50),
        InsuredLastName NVARCHAR(50),
        BirthDate VARCHAR(10),
        Mobile VARCHAR(15),
        NationalCode VARCHAR(12),
        MedicalId INT,
        DoctorName NVARCHAR(100),
        DoctorLastName NVARCHAR(100),
        MedicalGradeCode VARCHAR(3),
        MedicalGradeName NVARCHAR(45),
        MedicalGradeStatus NVARCHAR(10),
        AccountValidTo VARCHAR(10),
        VisitDate VARCHAR(10),
        Description VARCHAR(10),
        IsPaper SMALLINT,
        TurnNo SMALLINT
    );
    BEGIN TRY
        BEGIN TRAN;
        SELECT TOP (1)
            @Id = Id,
            @InsurerCode = PrescriptionTypeCode
        FROM dbo.TaminPrescription
        WHERE TrackingCode = @TrackingCode
        ORDER BY CreatedOn DESC;

        IF EXISTS
        (
            SELECT 1
            FROM dbo.EPMPrescriptionTransaction
            WHERE PrescriptionId = @Id
        )
            SET @SupplementaryExists = 1;


        SET @InsurerCode =
        (
            SELECT Value
            FROM dbo.GeneralSetting
            WHERE RIGHT(Parameter, 4) = @InsurerCode
        );

        DELETE FROM @CacheData;
        INSERT INTO @CacheData
        (
            Id,
            InsurerCode,
            InsurerName,
            InsuredName,
            InsuredLastName,
            BirthDate,
            Mobile,
            NationalCode,
            MedicalId,
            DoctorName,
            DoctorLastName,
            AccountValidTo,
            VisitDate,
            Description,
            IsPaper,
            TurnNo
        )
        SELECT P.Id,
               CASE
                   WHEN ISNULL(@InsurerCode, '') = '' THEN
                       P.InsurerCode
                   ELSE
                       @InsurerCode
               END InsurerCode,
               I.Sa_Name InsurerName,
               P.InsuredName,
               P.InsuredLastName,
               dbo.MiladiToShamsi(P.BirthDate) BirthDate,
               P.Mobile,
               P.NationalCode,
               P.Medicalid,
               P.DoctorName,
               P.DoctorLastName,
               dbo.MiladiToShamsi(GETDATE()) AccountValidTo,
               dbo.MiladiToShamsi(P.PrescriptionDate) VisitDate,
               'Success' Description,
               P.IsPaper,
               ISNULL(TurnNo, 0) TurnNo
        FROM dbo.TaminPrescription P WITH (NOLOCK)
            LEFT OUTER JOIN dbo.Sahmiyeh I WITH (NOLOCK)
                ON I.Sazman_Code = (CASE
                                        WHEN ISNULL(@InsurerCode, '') = '' THEN
                                            InsurerCode
                                        ELSE
                                            @InsurerCode
                                    END
                                   )
        WHERE P.Id = @Id AND IsDeleted = 0;
        -------------------------------------------------- Check Mobile Value
        SELECT @Mobile = Mobile,
               @NationalCode = NationalCode,
               @InsurerCode = InsurerCode
        FROM @CacheData;
        IF (
               ISNULL(@Mobile, '0') = '0'
               OR (
                      ISNULL(@Mobile, '0') <> '0'
                      AND CHARINDEX('*', @Mobile) > 0
                  )
           )
            IF EXISTS
            (
                SELECT TOP 1
                    Mobile
                FROM dbo.Notebimeh
                WHERE Sazman_Code = @InsurerCode
                      AND Meli_Card_No = @NationalCode
            )
                SELECT TOP 1
                    @Mobile = Mobile
                FROM dbo.Notebimeh
                WHERE Sazman_Code = @InsurerCode
                      AND Meli_Card_No = @NationalCode;
            ELSE
                SET @Mobile = '';
        ----------------------------------------------------------------
        IF
        (
            SELECT COUNT(0) FROM @CacheData
        ) = 0
        BEGIN
            ROLLBACK TRAN;
            SELECT NEWID() Id,
                   '' InsurerCode,
                   '' InsurerName,
                   '' InsuredName,
                   '' InsuredLastName,
                   '' BirthDate,
                   '' Mobile,
                   '' NationalCode,
                   0 Medicalid,
                   '' DoctorName,
                   '' DoctorLastName,
                   '' MedicalGradeCode,
                   '' MedicalGradeName,
                   '' MedicalGradeStatus,
                   '' AccountValidTo,
                   '' VisitDate,
                   '-1' Description,
                   0 IsPaper,
                   0 TurnNo,
                   CAST(0 AS MONEY) TotalSupportAmount,
                   0 SupplementaryTransactionId,
                   CAST(0 AS MONEY) SupplementaryTotalAmount,
                   CAST(0 AS MONEY) SupportiveInsuranceTotalAmount,
                   CAST(0 AS MONEY) IncurableTotalAmount,
                   '' RASAClaimNumber;
            RETURN;
        END;
        ----------------------------------- Check Grade Value
        SELECT @MedicalGradeCode = D.Grade_Code,
               @MedicalGradeName = S.Grade_Name,
               @MedicalGradeStatus = CASE
                                         WHEN S.Status = 1 THEN
                                             N'عمومي'
                                         WHEN S.Status = 2 THEN
                                             N'متخصص'
                                         WHEN S.Status = 3 THEN
                                             N'فوق تخصص'
                                         WHEN S.Status = 4 THEN
                                             N'ماما'
                                         WHEN S.Status = 5 THEN
                                             N'دندانپزشک'
                                         WHEN S.Status = 6 THEN
                                             N'اتباع'
                                     END
        FROM dbo.Doctor D
            INNER JOIN dbo.Grade S
                ON S.Grade_Code = D.Grade_Code
        WHERE D.Nezam_No =
        (
            SELECT TOP 1 CAST(MedicalId AS VARCHAR(10))FROM @CacheData
        );
        UPDATE @CacheData
        SET Mobile = @Mobile,
            MedicalGradeCode = @MedicalGradeCode,
            MedicalGradeName = @MedicalGradeName,
            MedicalGradeStatus = @MedicalGradeStatus;
        ------------------------------------------------------------
        DECLARE CursorTaminPrescription CURSOR FOR
        SELECT D.RowIndex,
               D.IsBulk,
               GoodsCode,
               D.DeliverCount,
               (D.InsurerAmount + ROUND(D.InsuredAmount, 0)) / D.DeliverCount SalePrice,
               D.DifferenceAmount,
               CASE
                   WHEN (Franshiz = 0)
                        OR (
                               Franshiz > 0
                               AND D.InsurerCeilingAmount = 0
                           ) THEN
                       0
                   ELSE
                       ROUND(100 - D.InsurerAmount * 100 / CAST((D.InsurerAmount + D.InsuredAmount) AS FLOAT), 6)
               END AS Franshiz,
               CASE
                   WHEN D.Franshiz = 100 THEN
                       4
                   WHEN (D.Franshiz = 0)
                        OR (
                               D.Franshiz > 0
                               AND D.InsurerCeilingAmount = 0
                           ) THEN
                       0
                   WHEN D.DifferenceAmount > 0 THEN
                       6
                   WHEN D.DifferenceAmount = 0
                        AND Franshiz > 0 THEN
                       5
               END AS Status,
               D.InsurerCeilingAmount,
               D.Instruction,
               W.Price_Kharid,
               ISNULL(D.SupportAmount, 0) SupportAmount
        FROM dbo.TaminPrescriptionDetail D WITH (NOLOCK)
            INNER JOIN dbo.Anbar W WITH (NOLOCK)
                ON W.K_Code = D.GoodsCode
                   AND W.A_Code = @WarehouseCode
        WHERE D.PrescriptionId = @Id
              AND ISNULL(D.GoodsCode, '') <> ''
              AND ISNULL(D.DeliverCount, 0) <> 0
        ORDER BY D.RowIndex;
        /*-------------------------------------------------*/
        OPEN CursorTaminPrescription;
        FETCH NEXT FROM CursorTaminPrescription
        INTO @RowIndex,
             @IsBulk,
             @GoodsCode,
             @DeliverCount,
             @SalePrice,
             @Differ,
             @InsurerPercent,
             @Status,
             @DrugPrice,
             @Instruction,
             @PurchasePrice,
             @SupportAmount;
        WHILE @@FETCH_STATUS = 0
        BEGIN
            EXEC dbo.ChkCtrMojodi @K_Code = @GoodsCode,
                                  @Sender = @WarehouseCode,
                                  @TmpCtrMojodi = @CtrMojodi OUTPUT;
            SET @IsLock = 1;
            IF @Status IN ( 0, 1, 3 )
                SET @IsLock = 0;
            IF @Status = 0
                SET @InsurerPercent = 0;
            INSERT INTO dbo.TmpDrugHavaleh
            (
                Id_Havaleh,
                Radif,
                K_Code,
                Sender,
                Reciver,
                K_Qty1,
                Price_Forosh,
                CtrMojodi,
                Serial_Flag,
                Make_Flag,
                Hamrah_Flag,
                Tot_Differ,
                BimarPercent,
                Status,
                TypeOfMedicine,
                Price_Forosh_O,
                Price_Sazman_O,
                Main_Price,
                Use_Name,
                Price_Kharid,
                Link_Record
            )
            VALUES
            (   @PrescriptionId,
                @RowIndex,
                @GoodsCode,
                @WarehouseCode,
                '10000',
                @DeliverCount,
                @SalePrice,
                @CtrMojodi,
                1,
                0,
                0,
                @Differ,
                @InsurerPercent,
                @Status,
                @IsLock,
                @DrugPrice,
                @DrugPrice,
                @DrugPrice,
                @Instruction,
                @PurchasePrice,
                CASE
                    WHEN @IsBulk = 1 THEN
                        1
                    ELSE
                        NULL
                END
            );
            ------------------------------  Add Supportive amount --------------------------------------			 
            IF (@SupportAmount > 0 AND @SupplementaryExists = 0)
            BEGIN
                SET @TotalSupportAmount = @TotalSupportAmount + @SupportAmount;
                INSERT INTO dbo.Tmp_Over_Under_Row_Drug
                (
                    PrescriptionId,
                    Code_Over_Under,
                    Code_Act,
                    Kind_Act,
                    Price,
                    [Percent],
                    [Type],
                    [Name]
                )
                VALUES
                (SCOPE_IDENTITY(),
                 @SupportTransactionCode,
                 10 ,
                 10 ,
                 @SupportAmount,
                 (@SupportAmount * 100) / (@SalePrice * @DeliverCount),
                 '0',
                 @SupportTransactionName
                );
            END;
            -----------------------------------------
            FETCH NEXT FROM CursorTaminPrescription
            INTO @RowIndex,
                 @IsBulk,
                 @GoodsCode,
                 @DeliverCount,
                 @SalePrice,
                 @Differ,
                 @InsurerPercent,
                 @Status,
                 @DrugPrice,
                 @Instruction,
                 @PurchasePrice,
                 @SupportAmount;
        END;
        CLOSE CursorTaminPrescription;
        DEALLOCATE CursorTaminPrescription;
        --------------------------(Insert Along)----------------------------------
        SET @DeliverCountCache = 0;
        SET @DeliverCount = 0;
        SET @SalePrice = 0;
        SET @CtrMojodi = '0';
        DECLARE @TobedoneInsert BIT;
        DECLARE @GoodsCodeSubset VARCHAR(20);
        DECLARE @QtySubset REAL;
        DECLARE @EditQty_Flag BIT;
        DECLARE @Medical CHAR(1);
        DECLARE @GoodsAlong AS TABLE
        (
            GoodsCode VARCHAR(20),
            Qty REAL,
            IsAlong BIT NULL
        );
        INSERT INTO @GoodsAlong
        (
            GoodsCode,
            Qty,
            IsAlong
        )
        SELECT G.K_Code,
               T.K_Qty1,
               G.Hamrah_Flag
        FROM dbo.TmpDrugHavaleh T
            INNER JOIN dbo.KalaId G
                ON G.K_Code = T.K_Code
        WHERE T.Id_Havaleh = @PrescriptionId;

        DECLARE CursorIsAlong CURSOR FOR
        SELECT GoodsCode,
               Qty
        FROM @GoodsAlong
        WHERE IsAlong = 1;
        OPEN CursorIsAlong;
        FETCH NEXT FROM CursorIsAlong
        INTO @GoodsCode,
             @DeliverCount;
        WHILE @@FETCH_STATUS = 0
        BEGIN
            DECLARE CursorSubset CURSOR FOR
            SELECT A.K_Code_H,
                   A.K_Qty1,
                   W.Price_Forosh,
                   A.EditQty_Flag,
                   ISNULL(   CASE
                                 WHEN Darou_Flag = 1 THEN
                                     1
                                 WHEN Darou_Flag IN ( 0, 5 ) THEN
                                     0
                                 WHEN Darou_Flag IN ( 6 ) THEN
                                 (
                                     SELECT CASE
                                                WHEN Price_Sazman.Status = 1 THEN
                                                    1
                                                ELSE
                                                    0
                                            END
                                     FROM dbo.Price_Sazman WITH (NOLOCK)
                                     WHERE W.K_Code = K_Code
                                           AND Sazman_Code = @InsurerCode
                                 )
                             END,
                             0
                         ) Darou_Flag,
                   W.Price_Kharid
            FROM dbo.Hamrah_Kala A
                INNER JOIN dbo.Anbar W WITH (NOLOCK)
                    ON W.K_Code = A.K_Code_H
                INNER JOIN dbo.KalaId G WITH (NOLOCK)
                    ON G.K_Code = W.K_Code
            WHERE A.K_Code = @GoodsCode
                  AND W.A_Code = @WarehouseCode
                  AND W.Active_Flag = 0
                  AND ISNULL(A.K_Qty1, 0) > 0;
            OPEN CursorSubset;
            FETCH NEXT FROM CursorSubset
            INTO @GoodsCodeSubset,
                 @QtySubset,
                 @SalePrice,
                 @EditQty_Flag,
                 @Medical,
                 @PurchasePrice;
            WHILE @@FETCH_STATUS = 0
            BEGIN
                SET @TobedoneInsert = 1;
                IF EXISTS
                (
                    SELECT GoodsCode
                    FROM @GoodsAlong
                    WHERE GoodsCode = @GoodsCodeSubset
                )
                BEGIN
                    IF @QtySubset = 0
                        SET @TobedoneInsert = 0;
                    ELSE
                    BEGIN
                        SET @DeliverCountCache
                            = CASE
                                  WHEN @EditQty_Flag = 1 THEN
                        (@QtySubset * @DeliverCount)
                        - ISNULL(
                          (
                              SELECT SUM(Qty) FROM @GoodsAlong WHERE GoodsCode = @GoodsCodeSubset
                          ),
                          0
                                )
                                  ELSE
                        (@QtySubset) - ISNULL(
                                       (
                                           SELECT SUM(Qty) FROM @GoodsAlong WHERE GoodsCode = @GoodsCodeSubset
                                       ),
                                       0
                                             )
                              END;
                        IF @DeliverCountCache <= 0
                            SET @TobedoneInsert = 0;
                    END;
                END;
                ELSE
                BEGIN
                    IF @QtySubset = 0
                    BEGIN
                        SET @TobedoneInsert = 1;
                        SET @DeliverCountCache = @QtySubset;
                    END;
                    ELSE
                    BEGIN
                        SET @DeliverCountCache
                            = CASE
                                  WHEN @EditQty_Flag = 1 THEN
                        (@QtySubset * @DeliverCount)
                        - ISNULL(
                          (
                              SELECT SUM(Qty) FROM @GoodsAlong WHERE GoodsCode = @GoodsCodeSubset
                          ),
                          0
                                )
                                  ELSE
                        (@QtySubset) - ISNULL(
                                       (
                                           SELECT SUM(Qty) FROM @GoodsAlong WHERE GoodsCode = @GoodsCodeSubset
                                       ),
                                       0
                                             )
                              END;
                        IF @DeliverCountCache < 0
                            SET @TobedoneInsert = 0;
                    END;
                END;
                IF @TobedoneInsert = 1
                BEGIN
                    IF NOT EXISTS
                    (
                        SELECT K_Code
                        FROM dbo.TmpDrugHavaleh
                        WHERE Id_Havaleh = @PrescriptionId
                              AND K_Code = @GoodsCodeSubset
                              AND Status = @Medical
                    )
                    BEGIN
                        INSERT INTO dbo.TmpDrugHavaleh
                        (
                            Id_Havaleh,
                            Radif,
                            K_Code,
                            Sender,
                            Reciver,
                            K_Qty1,
                            Price_Forosh,
                            CtrMojodi,
                            Serial_Flag,
                            Make_Flag,
                            Hamrah_Flag,
                            Tot_Differ,
                            BimarPercent,
                            Status,
                            TypeOfMedicine,
                            Price_Forosh_O,
                            Main_Price,
                            Link_Record,
                            Price_Kharid
                        )
                        VALUES
                        (@PrescriptionId,
                         @RowIndex + 1,
                         @GoodsCodeSubset,
                         @WarehouseCode,
                         '10000',
                         @DeliverCountCache,
                         @SalePrice,
                         @CtrMojodi,
                         1  ,
                         0  ,
                         0  ,
                         0  ,
                         0  ,
                         @Medical,
                         0  ,
                         @SalePrice,
                         @SalePrice,
                         1  ,
                         @PurchasePrice
                        );
                    END;
                    ELSE
                    BEGIN
                        UPDATE dbo.TmpDrugHavaleh
                        SET K_Qty1 = K_Qty1 + @DeliverCountCache
                        WHERE Id_Havaleh = @PrescriptionId
                              AND K_Code = @GoodsCodeSubset
                              AND Status = @Medical;
                    END;
                    SET @RowIndex = @RowIndex + 1;
                END;
                FETCH NEXT FROM CursorSubset
                INTO @GoodsCodeSubset,
                     @QtySubset,
                     @SalePrice,
                     @EditQty_Flag,
                     @Medical,
                     @PurchasePrice;
            END;
            CLOSE CursorSubset;
            DEALLOCATE CursorSubset;
            FETCH NEXT FROM CursorIsAlong
            INTO @GoodsCode,
                 @DeliverCount;
        END;
        CLOSE CursorIsAlong;
        DEALLOCATE CursorIsAlong;

        -------------------------------------- Check Total Support Amount -----------------------------        
        DECLARE @EPMTransaction TABLE
        (
            Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
            SupplementaryTransactionId SMALLINT NULL
                DEFAULT (0),
            SupplementaryTotalAmount DECIMAL(16, 0) NULL
                DEFAULT (0),
            SupportiveInsuranceTotalAmount DECIMAL(16, 0) NULL
                DEFAULT (0),
            IncurableTotalAmount DECIMAL(16, 0) NULL
                DEFAULT (0),
            RASAClaimNumber VARCHAR(15) NULL
        );

        IF (@SupplementaryExists = 1)
        BEGIN
            SET @TotalSupportAmount = 0;
            INSERT INTO @EPMTransaction
            (
                Id,
                SupplementaryTransactionId,
                SupplementaryTotalAmount,
                SupportiveInsuranceTotalAmount,
                IncurableTotalAmount,
                RASAClaimNumber
            )
            EXEC dbo.EPMMakePrescriptionDetailTransaction @PrescriptionKind = 0,
                                                          @PrescriptionTempId = @PrescriptionId,
                                                          @PrescriptionId = @Id;
        END;
        ELSE IF (@TotalSupportAmount > 0)
        BEGIN

            INSERT INTO dbo.Tmp_Over_Under_Factor_Drug
            (
                Id_Havaleh,
                Name,
                Code_Over_Under,
                Type,
                Price,
                Act_Code,
                Guy_Code,
                Max_Price,
                Max_percent,
                ShowPrice,
                IsDuty
            )
            VALUES
            (@PrescriptionId,
             @SupportTransactionName,
             @SupportTransactionCode,
             '0',
             @TotalSupportAmount,
             -1 ,
             -3 ,
             0  ,
             0  ,
             0  ,
             0
            );

        END;
        ------------------------------------------------------------------------------------------------ 	 
        COMMIT TRAN;
        SELECT TOP 1
            H.Id,
            H.InsurerCode,
            H.InsurerName,
            H.InsuredName,
            H.InsuredLastName,
            H.BirthDate,
            H.Mobile,
            H.NationalCode,
            H.MedicalId,
            H.DoctorName,
            H.DoctorLastName,
            H.MedicalGradeCode,
            H.MedicalGradeName,
            H.MedicalGradeStatus,
            H.AccountValidTo,
            H.VisitDate,
            'Success' Description,
            H.IsPaper,
            CAST(ISNULL(H.TurnNo, 0) AS INT) TurnNo,
            @TotalSupportAmount TotalSupportAmount,
            ISNULL(Tr.SupplementaryTransactionId, 0) SupplementaryTransactionId,
            ISNULL(Tr.SupplementaryTotalAmount, 0) SupplementaryTotalAmount,
            ISNULL(Tr.SupportiveInsuranceTotalAmount, 0) SupportiveInsuranceTotalAmount,
            ISNULL(Tr.IncurableTotalAmount, 0) IncurableTotalAmount,
            Tr.RASAClaimNumber
        FROM @CacheData H
            OUTER APPLY
        (
            SELECT SupplementaryTotalAmount,
                   SupplementaryTransactionId,
                   SupportiveInsuranceTotalAmount,
                   IncurableTotalAmount,
                   RASAClaimNumber
            FROM @EPMTransaction
        ) AS Tr;


    END TRY
    BEGIN CATCH
        IF CURSOR_STATUS('global', 'CursorTaminPrescription') = 1
        BEGIN
            CLOSE CursorTaminPrescription;
            DEALLOCATE CursorTaminPrescription;
        END;
        DECLARE @ErrorMessage NVARCHAR(MAX),
                @ErrorSeverity INT,
                @ErrorState INT;
        SELECT @ErrorMessage = ERROR_MESSAGE() + N' Line ' + CAST(ERROR_LINE() AS NVARCHAR(5)),
               @ErrorSeverity = ERROR_SEVERITY(),
               @ErrorState = ERROR_STATE();
        ROLLBACK TRANSACTION;
        RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
        SELECT NEWID() Id,
               '' InsurerCode,
               '' InsurerName,
               '' InsuredName,
               '' InsuredLastName,
               '' BirthDate,
               '' Mobile,
               '' NationalCode,
               0 Medicalid,
               '' DoctorName,
               '' DoctorLastName,
               '',
               '' MedicalGradeCode,
               '' MedicalGradeName,
               '' MedicalGradeStatus,
               '' AccountValidTo,
               '' VisitDate,
               '-2' Description,
               0 IsPaper,
               0 TurnNo,
               CAST(0 AS MONEY) TotalSupportAmount,
               0 SupplementaryTransactionId,
               CAST(0 AS MONEY) SupplementaryTotalAmount,
               CAST(0 AS MONEY) SupportiveInsuranceTotalAmount,
               CAST(0 AS MONEY) IncurableTotalAmount,
               '' RASAClaimNumber;
    END CATCH;
END;