CREATE PROCEDURE [dbo].[sp_SIOGetPrescriptionByID]   @PrescriptionId NUMERIC
 AS 
DECLARE @MedicalNo AS INT ;
SELECT @MedicalNo = CAST(Value AS INT)
FROM dbo.GeneralSetting
WHERE Parameter = 'HIO_NezamNo';

SELECT Id_Havaleh PrescriptionId,       
	   Tarikh_Noskheh VisitDate ,
       CAST(1 AS bit) VisitDateSpecified,
	   Tarikh_Paziresh AS ServiceDate , 
	   Tarikh_Paziresh ServiceDate ,
       Note_Code  MemberNN,
       Page_No  PaperNumber,
       CAST(DrugH.Nezam_No AS INT) RequestedByPhyscianCode,
       @MedicalNo  ServicedByPhyscianCode,
       CAST(TotPriceBimar + TotPriceSazman AS INT ) TotalAmount,
       CAST(TotPriceBimar AS INT) PayedAmount,
       CAST(TotPriceSazman AS INT) InsuranceAmount,
	   Tarikh_Noskheh  FamilyDoctorDate,	   
       CAST(1 AS bit) FamilyDoctorDateSpecified,
       CAST(1 AS bit) FamilyDoctorCodeSpecified,
       CAST(DrugH.FamilyDoctorCode AS INT) FamilyDoctorCode,
       CAST(LEFT(REPLACE(dbo.MiladiToShamsi(CONVERT(VARCHAR(10), Tarikh_Noskheh, 111)), '/', ''), 6) AS INT ) RecieptCycleYearAndMonth,
       CAST(1 AS bit) PaperNumberSpecified,
       CAST(1 AS bit) PayedAmountSpecified,
       CAST(1 AS bit) RecieptCycleYearAndMonthSpecified,
       CAST(1 AS bit) RequestedByPhyscianCodeSpecified,
       CAST(1 AS bit) ServiceDateSpecified,
       CAST(1 AS bit) ServicedByPhyscianCodeSpecified,
       CAST(1 AS bit) TotalAmountSpecified,
       CAST(1 AS bit) InsuranceAmountSpecified
FROM DrugH
WHERE Id_Havaleh = @PrescriptionId
---------------------------------------------------------------------------
SELECT Id_Havaleh HeaderPrescriptionId,
       dbo.KalaId.Meli_Code NationalNumber,
       CAST(K_Qty1 AS INT) NumberOfRequest,
       CAST(K_Qty1 AS INT) NumberOfDeliveredRequest,
       CAST(Tot_forosh   AS INT) MinistrationPayment,
       CAST( (Tot_forosh * ( 100 - BimarPercent) / 100) AS INT)  MinistrationInsurance, 
       CAST( (Tot_forosh * BimarPercent) / 100  AS INT  ) PatientPayment,       
       CAST(1 AS bit)  MinistrationInsuranceSpecified,
       CAST(1 AS bit)  MinistrationPaymentSpecified,
       CAST(1 AS bit)  NumberOfDeliveredRequestSpecified,
       CAST(1 AS bit)  NumberOfRequestSpecified,
       CAST(1 AS bit)  patientPaymentSpecified
FROM DrugHavaleh DH
    INNER JOIN KalaId
        ON KalaId.K_Code = DH.K_Code
WHERE Id_Havaleh = @PrescriptionId
AND DH.Status IN ( 5 , 9 , 8 , 6 , 4 , 2 ) 