<!---
This file is Copyright © 1998-2009 by Home Care Business Services. This work may not be reproduced, in whole or in part, 
using any medium, including, but not limited to, electronic transmission, CD-ROM or published in print, without the express 
permission of Home Care Business Services.

$Revision: 1.10 $
$Id: unsigned_orders.cfm,v 1.10 2009/11/19 14:19:33 melissa Exp $
$Date: 2009/11/19 14:19:33 $
--->
<!--- Page specific code, such as queries and variable settings should go here --->
<script type="text/javascript">
	$(document).ready(function () {
		$("#DataTableunsent").dataTable({
			"sPaginationType": "full_numbers",
			//"bJQueryUI": true,
			"aaSorting": [[1,'ASC']]
		}); 
	});

</script>
<cfparam name=url.type default="">
<!--- Query matches getUnsignedOrders from cron_dashboard_emp_count.cfm --->
<cfquery datasource="#Application.DataSrc#" name="Visits">
	SELECT pPatients.Patient_ID, pPatients.Status, pPatients.Pt_Last AS PatientLast, 
		pPatients.Pt_First AS PatientFirst, 
		pCMS4857.Order_ID, pCMS4857.Form_Type, 
		pCMS4857.Date_Sent, pCMS4857.Certification_From, 
		pCMS4857.Certification_To, pCMS4857.MD_Digital_Signature, 
		pCMS4857.Date_Create, DATEDIFF(NOW(), pCMS4857.Date_Sent) AS age,
		pCMS4857.Order_description, pCMS4857.Form_Status,
		pCMS4857.Date_Received,
		pPhysicians.Phys_ID, pPhysicians.Phys_Last, pPhysicians.Phys_First,
		pEmployee.Emp_Last, pEmployee.Emp_First,
		COALESCE(assessment_assign.Emp_ID, 2) AS AssignedEmpID,
		latest_assmt.Assmt_ID, latest_assmt.F26
	FROM (
		SELECT 
			pAssessments.Admit_ID,
			pAssessments.Assmt_ID,
			pAssessments.Patient_ID,
			pAssessments.F26
		FROM #Request.prefix_db_agency#.pAssessments
		JOIN #Request.prefix_db_agency#.pAdmit ON pAdmit.Admit_ID = pAssessments.Admit_ID
			AND pAdmit.Status = 0
		WHERE pAssessments.Status = 0
			AND (LEFT(pAssessments.F26, 1) = '1' OR LEFT(pAssessments.F26, 1) = '4')
			<!--- Only latest admission per patient --->
			AND pAdmit.Admit_ID = (
				SELECT Admit_ID
				FROM #Request.prefix_db_agency#.pAdmit
				WHERE Patient_ID = pAssessments.Patient_ID
					AND Status = 0
				ORDER BY M0030_START_CARE_DT DESC
				LIMIT 1
			)
			<!--- Only latest SOC/Recert assessment for that admission --->
			AND pAssessments.Assmt_ID  IN (
				SELECT ass.Assmt_ID
				FROM #Request.prefix_db_agency#.pAssessments ass
				WHERE ass.Admit_ID = pAssessments.Admit_ID
					AND ass.Status = 0
					AND (LEFT(ass.F26, 1) = '1' OR LEFT(ass.F26, 1) = '4')
				ORDER BY ass.F178 DESC, ass.Assmt_ID DESC
			)
			<!--- Exclude discharged admissions (has discharge assessment 7/8/9)
			AND NOT EXISTS (
				SELECT 1
				FROM #Request.prefix_db_agency#.pAssessments discharge_assmt
				WHERE discharge_assmt.Admit_ID = pAssessments.Admit_ID
					AND discharge_assmt.Status = 0
					AND (LEFT(discharge_assmt.F26, 1) = '7' OR LEFT(discharge_assmt.F26, 1) = '8' OR LEFT(discharge_assmt.F26, 1) = '9')
					AND discharge_assmt.F25 = 1
			) --->
	) AS latest_assmt
	JOIN #Request.prefix_db_agency#.pAdmit pAdmit ON pAdmit.Admit_ID = latest_assmt.Admit_ID
		AND pAdmit.Status = 0
	JOIN #Request.prefix_db_agency#.pPatients pPatients ON pPatients.Patient_ID = latest_assmt.Patient_ID
		AND pPatients.Status < 2
	JOIN #Request.prefix_db_agency#.pCMS4857 ON pCMS4857.Assmt_ID = latest_assmt.Assmt_ID
		AND pCMS4857.Status = 0
		AND pCMS4857.Date_Sent IS NOT NULL
		AND pCMS4857.Date_Received IS NULL
	LEFT OUTER JOIN #Request.prefix_db_agency#.assessment_assign ON assessment_assign.Assmt_ID = latest_assmt.Assmt_ID
	LEFT OUTER JOIN #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID = assessment_assign.Emp_ID
		AND pEmployee.Status = 0
	LEFT JOIN #Request.prefix_db_agency#.employee_auth ON pEmployee.Emp_ID = employee_auth.eaID
	LEFT OUTER JOIN #Request.prefix_db_lookup#.pPhysicians ON pAdmit.Render_Phys_ID = pPhysicians.Phys_ID
	WHERE pPatients.Loc_ID = '#session.agencyid#'
		AND (pCMS4857.Form_Status IS NULL OR pCMS4857.Form_Status NOT IN ('Signed & Received via Fax','Signed & Received', 'E-Signed and Received'))
		<cfif session.earole contains 'Field' AND session.eaRole does not contain 'Project Manager' AND session.eaRole does not contain 'Quality'>
			AND COALESCE(assessment_assign.Emp_ID, 2) = <cfqueryparam value="#session.employeeid#" cfsqltype="CF_SQL_INTEGER">
		</cfif>
		GROUP BY pCMS4857.Order_ID
	ORDER BY pPatients.Pt_Last ASC, pCMS4857.Certification_To ASC
	
</cfquery>
<style>
@media print{
a[href]:after
{
content:none
}
}
</style>
<div class="portlet box red">
		<div class="portlet-title">
			<div class="caption">
				<i class="fa fa-gear"></i>Unsigned Orders by physician
			</div>
			<div class="tools">
				<a title="" data-original-title="" href="javascript:;" class="collapse"></a>
			</div>
			<div class="actions">
				<input type="button" class="btn default btn-xs" name="Cancel" value="Back" onclick="window.history.back();">
			  <a class="btn default btn-xs" href="https://secure.myhomecarebiz.com/patientadmin_new/index.cfm?page=patient&cat=add_487_patient">New Order</a>
			</div>
		</div>
	<div class="portlet-body form">
         <br />
		<table class="table table-striped table-bordered table-hover" id="DataTableunsent"> 
			<thead>
		    	<tr> 
			      <th><b>Patient</b></th>
				    <th><b>Form Type</b></th>
			      <th><b>Age</b></th>	   
			      <th><b>Certifying Physician</b></th>
				    <th><b>Order Description</b></th>
				    <th><b>Action</b></th>
					</tr>
			</thead>
			<tbody>
				<cfset totalDisplayedCount = 0>
				<cfset unsignedOrdersCountByEmp = StructNew()>
				<cfoutput query="Visits" > 
					<cfset totalDisplayedCount = totalDisplayedCount + 1>
					<cfset empIDToUse = AssignedEmpID>
					<cfif NOT IsNumeric(empIDToUse) OR empIDToUse EQ 0 OR empIDToUse EQ "">
						<cfset empIDToUse = 2>
					</cfif>
					<cfif NOT StructKeyExists(unsignedOrdersCountByEmp, empIDToUse)>
						<cfset unsignedOrdersCountByEmp[empIDToUse] = 0>
					</cfif>
					<cfset unsignedOrdersCountByEmp[empIDToUse] = unsignedOrdersCountByEmp[empIDToUse] + 1>
			        <cfset pid  = encrypt(#patient_id#,"#application.enckey#","AES","Hex") />
			        <cfset afid  = encrypt(#Order_ID#,"#application.enckey#","AES","Hex") />
						<tr id="orderrow_#Order_ID#">
							<td>
							  <a href="../patientadmin_new/index.cfm?page=patient&ID=#pid#&cat=Patient&assid=">
							  #PatientLast#, #PatientFirst#</a></b>&nbsp;(<cfif status EQ 0 >Active<cfelse>Inactive</cfif>)
							  </a>
							  </b> 
							</td>
						   <td>
						     <cfif Form_Type IS "Form 487"> 
						      	<a href="/patientadmin_new/index.cfm?a=1&page=patient&cat=487_update&rpt=487_update&id=#pid#&afid=#afid#">
								  		<font color="##f69">		
								  			Form 487
										</font>
								   </a> 
						      <cfelse> 
									<a href="/patientadmin_new/index.cfm?a=1&page=patient&cat=485update&rpt=485update&id=#pid#&afid=#afid#">
										<font color="##66CC33">
											Form 485
										</font>
									</a> 
						      </cfif> 
						      <br />#DateFormat(Certification_From,"mm/dd/yyyy")# - #DateFormat(Certification_To,"mm/dd/yyyy")# 
						   </td>
						   <td>#age#</td>
						   <td><cfif Len(Trim(Phys_Last & Phys_First))>#Phys_Last#, #Phys_First#<cfelse>&mdash;</cfif></td>
							<td>
						      	<span><font color="##FF0000">#Form_Status# <cfif Date_Sent NEQ ''> on  #DateFormat(Date_Sent, "mm/dd/yyyy hh:mm:ss")#</cfif></font></span>
									<cfif LEN(Order_description) gt 70>
									<a href="/patientadmin_new/index.cfm?a=1&page=patient&cat=487_update&rpt=487_update&id=#pid#&afid=#afid#">
										<br />#LEFT(Order_description,70)#...
									</a>
									</cfif>
							</td>
							<td>
								<cfif Form_Type IS "Form 487"> 
											<a class="btn green btn-sm"  href="../patientadmin_new/index.cfm?page=patient&ID=#pid#&cat=487_print&afid=#afid#" target='p'>
											<i  class="fa fa-print"></i>	Print
											</a> 
						   	<cfelse>
						         <a class="btn green btn-sm" href="../patientadmin_new/index.cfm?page=patient&ID=#pid#&cat=PrintAssesmentForm&afid=#afid#&Original=Print"   target='p'>
												<i  class="fa fa-print"></i> Print
										</a>
						      </cfif>
						    	<cfif session.earole contains 'Project Manager' OR session.earole contains 'Quality'>
						    			<a  class="btn red btn-sm"  onClick="Deleteorder('#Order_ID#')" ><i  class="fa fa-trash-o"></i> Delete</a> 
						    	</cfif>
							</td>
						</tr>
				</cfoutput> 
			</tbody>
		</table>
 </div>
</div>  

<script>
	function Deleteorder(Order_ID)
	{
	    bootbox.confirm({
	    	message: "Are you sure want to delete this Order?",
		    buttons: {
		        confirm: {
		            label: 'Yes',
		            className: 'btn-success'
		        },
		        cancel: {
		            label: 'No',
		            className: 'btn-danger'
		        }
		    },
		    callback: function (result) {

		    	 if ( result == true)
		        {
		        	jQuery.ajax({
	                url: "/components/general/general.cfc",
	                type: "get",  	
	                data: {
	                method: "deleteorder",
	                Order_ID: Order_ID
	                }, 
	                success: function (data){
	                		if (data == "Success")
	                		{
								$('#orderrow_'+Order_ID).hide();
								//if ( Form_Type == 'Form 485')
								//{
									//document.getElementById("add485_button").style.display="";
								//}
							}
							else
							{
								bootbox.alert("Deletoperation failed");
							}				        	      
	                    
	                }, 
	                error: function (xhr, textStatus, errorThrown){ 
	                alert(errorThrown);
	                }
	                });

		        	


		        }	
		          
		    }
		});

	 
	}

</script>

<!--- Refresh dashboard counts using same pattern as incomplete_assessments.cfm --->
<cfif session.IsAgencyLogin EQ 0 AND session.earole contains 'Field' AND session.eaRole does not contain 'Project Manager' AND session.eaRole does not contain 'Quality'>
	<cfquery name="markDashCountForLoggedEmployee" datasource="#Application.DataSrc#">
		UPDATE #Request.prefix_db_agency#.Dash_emp_count SET status = 1
		WHERE status = 0 AND Header = <cfqueryparam value="Un-Signed Orders" cfsqltype="CF_SQL_VARCHAR">
		AND Emp_ID = <cfqueryparam value="#session.employeeid#" cfsqltype="CF_SQL_INTEGER">
	</cfquery>
	<cfquery name="insertDashEmpCount" datasource="#Application.DataSrc#">
		INSERT INTO #Request.prefix_db_agency#.Dash_emp_count (Daily_QA_Date, Emp_ID, Emp_Count, Header, Created_Date, status)
		VALUES (
			<cfqueryparam value="#DateFormat(Now(), 'yyyy-mm-dd')#" cfsqltype="CF_SQL_DATE">,
			<cfqueryparam value="#session.employeeid#" cfsqltype="CF_SQL_INTEGER">,
			<cfqueryparam value="#totalDisplayedCount#" cfsqltype="CF_SQL_INTEGER">,
			<cfqueryparam value="Un-Signed Orders" cfsqltype="CF_SQL_VARCHAR">,
			<cfqueryparam value="#Now()#" cfsqltype="CF_SQL_TIMESTAMP">,
			<cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
		)
	</cfquery>
<cfelse>
	<cfquery name="markDashCountForEmployee" datasource="#Application.DataSrc#">
		UPDATE #Request.prefix_db_agency#.Dash_emp_count SET status = 1
		WHERE status = 0 AND Header = <cfqueryparam value="Un-Signed Orders" cfsqltype="CF_SQL_VARCHAR">
	</cfquery>
	<cfloop collection="#unsignedOrdersCountByEmp#" item="empID">
		<cfif unsignedOrdersCountByEmp[empID] GT 0>
			<cfquery name="insertDashEmpCount" datasource="#Application.DataSrc#">
				INSERT INTO #Request.prefix_db_agency#.Dash_emp_count (Daily_QA_Date, Emp_ID, Emp_Count, Header, Created_Date, status)
				VALUES (
					<cfqueryparam value="#DateFormat(Now(), 'yyyy-mm-dd')#" cfsqltype="CF_SQL_DATE">,
					<cfqueryparam value="#empID#" cfsqltype="CF_SQL_INTEGER">,
					<cfqueryparam value="#unsignedOrdersCountByEmp[empID]#" cfsqltype="CF_SQL_INTEGER">,
					<cfqueryparam value="Un-Signed Orders" cfsqltype="CF_SQL_VARCHAR">,
					<cfqueryparam value="#Now()#" cfsqltype="CF_SQL_TIMESTAMP">,
					<cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
				)
			</cfquery>
		</cfif>
	</cfloop>
</cfif>

 