CREATE PROC [dbo].[PISGetPyramidPrescription] @PrescriptionId NUMERIC
AS
DECLARE @MedicationTerminology TABLE
(
    [Code] [VARCHAR](20) NOT NULL,
    [Alias] [VARCHAR](10) NULL,
    [Term] [NVARCHAR](MAX) NULL,
    [PersianMeaning] [NVARCHAR](MAX) NULL,
    [TerminologyID] [NVARCHAR](150) NULL,
    [Type] [TINYINT] NOT NULL
);


INSERT INTO @MedicationTerminology
SELECT [Code],
       [Alias],
       [Term],
       [PersianMeaning],
       [TerminologyID],
       [Type]
FROM MedicationTerminology
WHERE Type IN ( 1, 2, 3, 4, 5 );


SELECT P.Id_Havaleh PrescriptionId,
       N.Meli_Card_No NationalCode,
       N.Name Firstname,
       N.Family Lastname,
       P.Resive_Name FullName,
       SP.CompositionUID,
       SP.PatientUID,
       CAST(LEFT(dbo.MiladiToShamsi(N.BirthDate), 4) AS INT) BirthYear,
       N.Note_Code InsuredNumber,
       N.Tel Tel,
       N.Mobile,
       N.Mess Address,
       P.Tarikh AdmissionDateTime,
       P.TotPriceOTC TechnicalCost,
       P.Tarikh_Paziresh DispenseDate,
       PatientCost = CAST(CASE
                              WHEN TotPriceFaniSazman > 0 THEN
                                  P.TotPriceBimar + P.TotPriceFaniBimar
                              ELSE
                                  P.TotPriceBimar
                          END AS MONEY),
       TotalCost = CAST(CASE
                            WHEN TotPriceFaniSazman > 0 THEN
                                P.TotPriceBimar + P.TotPriceSazman + P.TotPriceFaniBimar + P.TotPriceFaniSazman
                            ELSE
                                P.TotPriceBimar + P.TotPriceSazman + P.TotPriceFaniSazman
                        END AS MONEY),
       P.Nezam_No PrescriberCode,
       d.Name PrescriberName,
       d.Family PrescriberLastName,
       RIGHT(PISP.HID, LEN(PISP.HID) - 2) HID,
       MTIPISP.Term InsuranceTerm,
       PH.Name PrescribingPhysiciansFirstName,
       PH.Family PrescribingPhysiciansLastName,
       PH.MedicalCode PrescribingPhysiciansId,
       PT.Name PharmacyTechniciansFirstName,
       PT.Family PharmacyTechniciansLastName,
       PT.NationalCode PharmacyTechniciansNationalCode,
       MTIB.Code InsuranceBoxCode,
       InsuranceCode = CASE
                           WHEN MTIB.Code IS NULL
                                OR MTIB.Code = '' THEN
                               MTI.Code
                           ELSE
                               MTIB.Term
                       END
FROM dbo.DrugH P WITH (NOLOCK)
    JOIN dbo.Notebimeh N
        ON N.Note_Code = P.Note_Code
           AND N.Sazman_Code = P.Sazman_Code
    JOIN dbo.Doctor d
        ON d.Nezam_No = P.Nezam_No
    LEFT JOIN dbo.PISReceivedPrescription PISP
        ON PISP.Id = P.Id
    LEFT JOIN dbo.PISSenderPrescription SP
        ON SP.Id = PISP.PreviousSenderId
    LEFT JOIN @MedicationTerminology MTIPISP
        ON MTIPISP.Code = PISP.InsurerCode
           AND MTIPISP.Type = 3
    LEFT JOIN @MedicationTerminology MTIB
        ON MTIB.Type = 4
           AND P.Sazman_Code = MTIB.Alias
    LEFT JOIN @MedicationTerminology MTI
        ON MTI.Alias = P.Sazman_Code
           AND MTI.Type = 3
    LEFT JOIN dbo.DefinePharmacyTechnicalAndPharmacist PH
        ON PH.UserName = P.CheckU
           AND PH.[Status] = 0
    LEFT JOIN dbo.DefinePharmacyTechnicalAndPharmacist PT
        ON PT.UserName = P.[User_id]
           AND PT.[Status] = 1
WHERE Id_Havaleh = @PrescriptionId;

SELECT PD.Id_Havaleh PrescriptionId,
       PD.AutoId Id,
       CASE
           WHEN GoodsSerial.BatchNumber IS NOT NULL
                AND GoodsSerial.BatchNumber != '' THEN
               GoodsSerial.BatchNumber
           ELSE
               CAST(PD.AutoId AS VARCHAR(10))
       END RowNumber,
       ISNULL(PD.Link_Like, 0) ParentId,
       G.K_Code Code,
       G.Barcode IRC,
       G.ErxCode Erx,
       G.Meli_Code Generic,
       PD.K_Qty1 TotalNumber,
       G.Name2 Name,
       G.Name1 FaName,
       PatientCost = CAST(CASE
                              WHEN PD.Status = 4
                                   OR
                                   (
                                       PD.Status = 8
                                       AND PD.BimarPercent = 0
                                   ) THEN
                                  0
                              ELSE
                                  ROUND(PD.Tot_forosh * PD.BimarPercent / 100, 0)
                          END AS MONEY),
       InsuranceCost = CAST(CASE
                                WHEN PD.Status = 4
                                     OR
                                     (
                                         PD.Status = 8
                                         AND PD.BimarPercent = 0
                                     ) THEN
                                    PD.Tot_forosh
                                ELSE
                                    PD.Tot_forosh - CASE
                                                        WHEN PD.Status = 4
                                                             OR
                                                             (
                                                                 PD.Status = 8
                                                                 AND PD.BimarPercent = 0
                                                             ) THEN
                                                            0
                                                        ELSE
                                                            ROUND(PD.Tot_forosh * PD.BimarPercent / 100, 0)
                                                    END
                            END AS MONEY),
       MTF.Term FrequencyTerm,
       MTF.Code FrequencyCode,
       MTR.Term RouteTerm,
       MTR.Code RouteCode,
       MTS.Term ShapeTerm,
       MTS.Code ShapeCode,
       PD.Note [Description]
FROM dbo.DrugHavaleh PD WITH (NOLOCK)
    JOIN dbo.KalaId G
        ON G.K_Code = PD.K_Code
    LEFT JOIN @MedicationTerminology MTF
        ON MTF.Code = PD.FrequencyCode
           AND MTF.Type = 1
    LEFT JOIN @MedicationTerminology MTR
        ON MTR.Code = PD.RouteCode
           AND MTR.Type = 2
    LEFT JOIN @MedicationTerminology MTS
        ON MTS.Code = G.ShapeCode
           AND MTS.Type = 5
    LEFT JOIN
    (
        SELECT K_Code GoodsCode,
               Serial BatchNumber,
               A_Code WarehouseCode
        FROM Serial
    ) AS GoodsSerial
        ON GoodsSerial.GoodsCode = PD.K_Code
           AND WarehouseCode = CAST(PD.Sender AS VARCHAR(3))
WHERE Id_Havaleh = @PrescriptionId;