 <!--- This code makes sure that the user has logged in, if not it will not allow them to access this page --->
<cflock scope="Session" type="ReadOnly" timeout="10" throwontimeout="no">
    <cfset MM_Username=Iif(IsDefined("Session.MM_Username"),"Session.MM_Username",DE("")) />
    <cfset MM_UserAuthorization=Iif(IsDefined("Session.MM_UserAuthorization"),"Session.MM_UserAuthorization",DE("")) />
</cflock>

<cfif MM_Username EQ "">
    <cfset MM_referer=CGI.SCRIPT_NAME />

    <cfif CGI.QUERY_STRING NEQ "">
        <cfset MM_referer=MM_referer & "?" & CGI.QUERY_STRING />
    </cfif>

    <cfset MM_failureURL="login_denied.cfm?accessdenied=" & URLEncodedFormat(MM_referer) />
    <cflocation url="#MM_failureURL#" addtoken="no">
</cfif>


<!--- Export data into excel  --->
 <cftry>

        <!--- Fetch data in the format in which excel will be downloaded and displayed --->          
        <cfquery  name="GETMEDS_COUNT" datasource="#Application.DataSrc#">
            SELECT *, meds_pathway_lookup.Notes  AS medspath_Notes 
                FROM #Request.prefix_db_lookup#.meds_pathway_lookup
              LEFT OUTER JOIN #Request.prefix_db_lookup#.Meds_New ON Meds_New.Trade_Num = meds_pathway_lookup.Trade_Num 
              LEFT OUTER JOIN #Request.prefix_db_agency#.pPatients ON pPatients.Patient_ID = meds_pathway_lookup.Patient_ID
                LEFT OUTER JOIN #Request.prefix_db_lookup#.Agency ON Agency.Agency_ID = meds_pathway_lookup.Agency_ID
                LEFT OUTER JOIN #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID = meds_pathway_lookup.Emp_ID
                LEFT OUTER JOIN #Request.prefix_db_lookup#.CW_Coding_Class_PDGM 
                ON CW_Coding_Class_PDGM.Pathway_ID = meds_pathway_lookup.Path_ID

              WHERE meds_pathway_lookup.Emp_ID IS NOT NULL 
               <!--- AND meds_pathway_lookup.Agency_ID IS NOT NULL --->
              AND (meds_pathway_lookup.Approved  = "Not paired"  OR meds_pathway_lookup.Approved  = "For Review")
              AND pPatients.status < 2
              AND meds_pathway_lookup.Agency_ID  = '#url.agency_id#'
              AND meds_pathway_lookup.Med_ID != 0
                Group by meds_pathway_lookup.ID
                ORDER BY meds_pathway_lookup.Generic_Name
        </cfquery>
            
    
        <!--- build even/odd lists for MOD alternating background color display and display --->
        <cfset REQUEST.Filepath = "/var/www/EMR_user/datafiles/">
        <cfset VARIABLES.filename = "Meds_Require_Report#DateFormat(now(),"yyyymmdd")##TimeFormat(now(),"HHmmss")#.csv">
        <cfset theFile = REQUEST.filepath & VARIABLES.filename />
        <cfif GETMEDS_COUNT.recordcount gt 0>
          <cfset str = """" & "Generic Name" & """"
                     & "," & """" & "Classification" & """"
                     & "," & """" & "Diagnosis" & """"
                     & "," & """" & "Agency Name" & """"
                     & "," & """" & "Patient" & """"
                     & "," & """" & "Employee Name" & """"
                     & "," & """" & "Date" & """"
                     & "," & """" & "Status" & """"                 
                     & chr(13) & chr(10)  />
          
            <cfoutput query="GETMEDS_COUNT">
                <cfquery  name="getallproblems" datasource="#Application.DataSrc#">
                   SELECT pProblem.* FROM  #Request.prefix_db_agency#.pProblem
                   JOIN #Request.prefix_db_agency#.pAssessments ON  pAssessments.Assmt_ID = pProblem.Assmt_ID 
                   WHERE pAssessments.status = 0 
                   AND pProblem.status = 0 
                   AND pProblem.M0_No like 'M102%'
                   AND pAssessments.Patient_ID = '#Patient_ID#'
                   GROUP BY pProblem.Problem_ID
                </cfquery>
                <cfquery  name="getproblems" dbtype="query">
                  SELECT DISTINCT Description FROM getallproblems
                  WHERE Path_ID = #Path_ID# 
                </cfquery>
                <cfquery  name="Recommproblems" dbtype="query">
                  SELECT DISTINCT Description FROM getallproblems
                  WHERE Problem_ID = #Recomm_problem_ID# 
                </cfquery>
                <cfset prob_descritpion = ValueList(getproblems.Description) />
                <cfset str = str  & """" & #Generic_Name# & """"
                        & "," & """" & #Classification# & """"
                        & "," & """" & #prob_descritpion# & """"
                        & "," & """" & #Agency_Name# & """"
                        & "," & """" & #Pt_Last# & #Pt_First# & """"
                        & "," & """" & #Emp_First# & """"
                        & "," & """" & #Date_Created# & """"
                        & "," & """" & #Approved# & """"
                        & chr(13)   & chr(10)
                 />
            </cfoutput>
            <!--- Format the excel spreadsheet for display --->
               <cffile action="write" file="#REQUEST.Filepath##VARIABLES.filename#" output="#str#">
                <cfheader name="Content-Disposition" value="attachment;filename=#VARIABLES.filename#">
                <cfcontent file="#theFile#" deletefile="true" type="application/msexcel" />
        </cfif>
        <cfcatch type="any">
            <cfoutput>#cfcatch.Detail#</cfoutput>
         </cfcatch>

</cftry>
 

 



 