<?php
	
	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 = "Report";
	require "inc/head.php";
	$menu = "reportfp";
	require "inc/menu.php";
	
	if($debug) showdebug();
	
	if(isset($_REQUEST['datefrom']) && $_REQUEST['datefrom']!="" && $_REQUEST['datefrom']!=null) $datefrom = clean($_REQUEST['datefrom']);
	else $datefrom = date('Y-m-d',strtotime('1 month ago'));
	
	if(isset($_REQUEST['dateto']) && $_REQUEST['dateto']!="" && $_REQUEST['dateto']!=null) $dateto = clean($_REQUEST['dateto']);
	else $dateto = date('Y-m-d');
?>
<div id='topDIV'>
	<h1 class='text-center'><?php echo $title; ?></h1>
</div>
<div class='col-md-3' role='complementary' >
	<form method='post' style='position:fixed'>
		<div class='form-group'>
			<label for='datefrom'>Date from</label>
			<input type='date' class='form-control' name='datefrom' placeholder='Date from' value='<?php echo $datefrom; ?>'>
		</div>
		<div class='form-group'>
			<label for='dateto'>Date to</label>
			<input type='date' class='form-control' name='dateto' placeholder='Date to' value='<?php echo $dateto; ?>'>
		</div>
		<div class='form-group'>
			<label for='s'>Product</label>
			<?php
				$sql="
					Select
						dw_spec.specID,
						dw_spec.specText,
						dw_spec.specShortDesc
					From
						dw_spec
					Where
						dw_spec.specSite = ?
					Order By
						dw_spec.specStyle,
						dw_spec.specOrder,
						dw_spec.specText
				";
				if(!$specquery = mysqli_prepared_query($link, $sql, "i", array($_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			?>
			<select name='s' id='s' class='form-control'>
				<option value='0'>All products</option>
				<?php
					foreach($specquery as $specarray){
						echo "<option value='".$specarray['specID']."'";
						if(isset($_REQUEST['s']) && $_REQUEST['s']!="" && $_REQUEST['s']!=null && $_REQUEST['s']==$specarray['specID']) echo " selected";
						if($specarray['specText']==$specarray['specShortDesc']) echo ">".$specarray['specText']."</option>";
						else echo ">".$specarray['specText']." - ".$specarray['specShortDesc']."</option>";
					}
				?>
			</select>
		</div>
		<button type='submit' class='btn btn-default btn-primary'>Refresh</button>
	</form>
</div>
<div class='col-md-9' 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 id='percent'>
		<div class='progress'><div class='progress-bar progress-bar-striped active' role='progressbar' aria-valuenow='0' aria-valuemin='0' aria-valuemax='100' style='width: 0%;'>0%</div></div>
	</div>
</div>
<div class='col-md-9' role='main' id='main' style='display:none;'>
	<?php
		$where = "";
		if(isset($_REQUEST['s']) && $_REQUEST['s']!="" && $_REQUEST['s']!=null && $_REQUEST['s']!='0' && $_REQUEST['s']!=0){
			$where .= " AND specID=".clean($_REQUEST['s']);
		}
		$sql="
			Select
				dw_cypher.cypherID,
				dw_cypher.cypherText,
				dw_cypher.cypherOrderID,
				dw_spec.specText
			From
				dw_spec Right Join
				dw_cypher
					On dw_spec.specID = dw_cypher.cypherSpec
			Where
				dw_cypher.cypherManuDate >= ? And
				dw_cypher.cypherManuDate <= ? And
				dw_cypher.cypherSite = ?".$where."
			Order By
				dw_cypher.cypherManuDate
		";
		if(!$cypherquery = mysqli_prepared_query($link, $sql, "ssi", array($datefrom, $dateto, $_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
		if(mysqli_prepared_num_rows($cypherquery)>0){
			$counter=0;
			foreach($cypherquery as $cypherarray){
				
				$cypherID     = $cypherarray['cypherID'];
				$cypherText   = $cypherarray['cypherText'];
				$cypherOrderID = $cypherarray['cypherOrderID'];
				$specText     = $cypherarray['specText'];
				
				$sql="
					Select
						dw_test.testID,
						dw_test.testText,
						dw_test.testFormat,
						dw_test.testRound,
						dw_test.testUnits
					From
						dw_spec Right Join
						(dw_cypher Right Join
						(dw_group Right Join
						(dw_unit Right Join
						(dw_test Right Join
						dw_result
							On dw_test.testID = dw_result.resultTest)
							On dw_unit.unitID = dw_result.resultUnit)
							On dw_group.groupID = dw_test.testGroup)
							On dw_cypher.cypherID = dw_unit.unitCypher)
							On dw_spec.specID = dw_cypher.cypherSpec
					Where
						dw_cypher.cypherID = ? And
						dw_test.testSite = ? And
						dw_test.testActive = 1
					Group By
						dw_test.testID
					Order By
						dw_group.groupOrder,
						dw_group.groupText,
						dw_test.testOrder,
						dw_test.testText
				";
				if(!$testquery = mysqli_prepared_query($link, $sql, "ii", array($cypherID, $_SESSION['userSite']), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
				if(mysqli_prepared_num_rows($testquery)>0){
					echo "<div class='container'><table class='table table-striped table-bordered'><tr><th><a href='results.php?r=".$cypherID."'>".$cypherText." ".$cypherOrderID." ".$specText."</a></th><th>Average</th><th>Units</th>";
					foreach($testquery as $testarray){
						$testID    = $testarray['testID'    ];
						$testFormat = $testarray['testFormat'];
						$testRound = $testarray['testRound' ];
						$testUnits = $testarray['testUnits' ];
						$testText  = $testarray['testText'  ];
						
						$sql="
							# get results for each test
							Select
								dw_result.resultFriendly,
								max(dw_result.resultModDate) # this is to get rid of retests
							From
								dw_cypher Right Join(
									dw_unit Right Join(
										dw_test Right Join
											dw_result
										On dw_test.testID = dw_result.resultTest
									)
									On dw_unit.unitID = dw_result.resultUnit
								)
								On dw_cypher.cypherID = dw_unit.unitCypher
							Where
								dw_cypher.cypherID = ? And
								dw_test.testID = ? And
								dw_unit.unitActive = 1
							Group By
								dw_unit.unitID,
								dw_result.resultFriendly
						";
						if(!$resultquery = mysqli_prepared_query($link, $sql, "ii", array($cypherID, $testID), __FILE__, __LINE__)) reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
						$sum = 0;
						$count = 0;
						foreach($resultquery as $resultarray){
							$sum += texttonumber($resultarray['resultFriendly'], $testFormat);
							$count++;
						}
						if(mysqli_prepared_num_rows($resultquery)>0){
							echo "<tr><td>".$testText;
							if($debug) echo " [".$testID."]";
							echo "</td><td title='".$count." results'>".str_replace('<',"&lt;",numbertotext(($sum/$count), $testFormat, $testRound))."</td><td>".$testUnits."</td></tr>";
						}
					}
					echo "</tr></table></div>";
				}
			}
		}
	?>
</div>
<script>
	dropdown("#s");
</script>
<?php require "inc/footer.php"; ?>
