<?php
	/* (C) DairyWindow 2012-2017 */
	
	require "inc/connect.php";
	
	require "inc/get_units.php";
	require "inc/cypherstuff.php"; //functions for converting dates to cyphers, and also expirys
	require "inc/texttonumber.php";
	require "inc/numbertotext.php";
	
	$title = "Yesterday";
	require "inc/head.php";
	$menu="yesterday";
	require "inc/menu.php";
	
	if($debug) showdebug();
	
	if(isset($_POST['datefrom']) && $_POST['datefrom']!="" && $_POST['datefrom']!=null) $datefrom = $_POST['datefrom'];
	else $datefrom = date('Y-m-d',strtotime('1 month ago'));
	
	if(isset($_POST['dateto']) && $_POST['dateto']!="" && $_POST['dateto']!=null) $dateto = $_POST['dateto'];
	else $dateto = date('Y-m-d');
	
	echo "<div id='topDIV'><h1 class='text-center'>".$title."<span class='visible-lg-inline'> and today</span></h1><h2 class='text-center visible-lg-block'>Click on a row to view</h2></div><div id='loading' ><img src='images/loading-small.gif' alt='Please wait. Loading...' width='111' height='156' style='margin-top:150px;' class='center-block'><br><div class='text-center'><img src='images/carregando.gif' width='16' height='16'>&nbsp;Please wait while loading...</div></div><div class='container' style='display:none;' id='main' >";
	
	$sql="
		Select
		  dw_cypher.cypherID,
		  dw_cypher.cypherText,
		  dw_cypher.cypherOrderID,
		  dw_spec.specText,
		  dw_unit.unitText,
		  dw_location.locationText,
		  dw_group.groupText,
		  dw_test.testText,
		  dw_result.resultFriendly,
		  dw_user.userFirst,
		  dw_user.userLast,
		  dw_result.resultModDate
		From
		  dw_result Left Join
		  dw_unit
			On dw_result.resultUnit = dw_unit.unitID Left Join
		  dw_test
			On dw_result.resultTest = dw_test.testID Left Join
		  dw_location
			On dw_unit.unitLocation = dw_location.locationID Left Join
		  dw_cypher
			On dw_unit.unitCypher = dw_cypher.cypherID Left Join
		  dw_spec
			On dw_cypher.cypherSpec = dw_spec.specID Left Join
		  dw_user
			On dw_result.resultModUser = dw_user.userID Left Join
		  dw_group
			On dw_group.groupID = dw_test.testGroup
		Where
		  dw_cypher.cypherSite = ? And
		  dw_result.resultModDate >= ?
		Order By
		  dw_cypher.cypherManuDate Desc,
		  dw_spec.specOrder,
		  dw_unit.unitText,
		  dw_group.groupOrder,
		  dw_test.testOrder,
		  dw_result.resultModDate
	";
	if(!$resultquery = mysqli_prepared_query($link, $sql, "is", array($_SESSION['userSite'], date('Y-m-d',strtotime('yesterday'))), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
	echo "<table class='table table-striped table-bordered table-hover sorttable'><thead><tr>
		<th>Cypher</th>
		<th>Product</th>
		<th>Sample</th>
		<th>Location</th>
		<th>Group</th>
		<th>Test</th>
		<th>Result</th>
		<th>Entered by</th>
		<th>Date</th>
	</tr></thead><tbody>";
	if(mysqli_prepared_num_rows($resultquery)>0){
		foreach($resultquery as $resultarray){
			echo "<tr onclick=\"window.location='results.php?r=".$resultarray['cypherID']."';\" style='cursor:pointer;' >
				<td class='nobr'>".$resultarray['cypherText'].' '.$resultarray['cypherOrderID']."</td>
				<td class='nobr'>".$resultarray['specText']."</td>
				<td class='nobr'>".$resultarray['unitText']."</td>
				<td class='nobr'>".$resultarray['locationText']."</td>
				<td class='nobr'>".$resultarray['groupText']."</td>
				<td class='nobr'>".$resultarray['testText']."</td>
				<td class='nobr'>".$resultarray['resultFriendly']."</td>
				<td class='nobr'>".$resultarray['userFirst']." ".$resultarray['userLast']."</td>
				<td class='nobr'>".date('d/m/y h:m:s',strtotime($resultarray['resultModDate']))."</td>
			</tr>";
		}
	}
	echo "</tbody></table>";
	require "inc/footer.php"; ?>