<?php
include("../include/session.php");
if(!$session->logged_in) {
	header("Location: ../main.php");
}
else {
	include "../reportHeader.php";
//	include("../include/db.php");
	$id = mysql_real_escape_string($_GET['id']);
	$inputId = mysql_real_escape_string($_POST['empId']);

	$dMonth = date("m");
	$dYear = date("y");


	$query ="";
	if(strlen($id) > 1){
		$query = "SELECT * FROM nam_employees WHERE empId = '".$id."'";
	}

	else {
		$query = "SELECT * FROM nam_employees WHERE empId = '".$inputId."'";
	}

	$result = execsql($query);
	if(!$result) {
		die('Could not query: ' . mysql_error());
	}


	$vioName = mysql_result($result,0,"empId");
	$num = mysql_num_rows($result);



	$i = 0;
	while ($i < $num) {
		$f1=mysql_result($result,$i,"empFName");
		$f2=mysql_result($result,$i,"empLName");
		$f3=mysql_result($result,$i,"empContact");
		$f4=mysql_result($result,$i,"empAge");
		$f5=mysql_result($result,$i,"emergName");
		$f6=mysql_result($result,$i,"emergNo");
		$f7=mysql_result($result,$i,"emergRelation");
		//	$f8=mysql_result($result,$i,"education");
		//	$f9=mysql_result($result,$i,"isEmployed");
		//	if($f9 < 1) {
		//		$employed = "Employed";
		//	}
		//	else {
		//		$employed = "Fired";
		//	}
		$f10=mysql_result($result, $i, "empId");
		//    $f11=mysql_result($result, $i, "pob");
		$f12=mysql_result($result, $i, "empSex");
		$f13=mysql_result($result, $i, "empMStatus");
		$f14=mysql_result($result, $i, "empIdNo");
		$f15=mysql_result($result, $i, "empPass");
		$f17=mysql_result($result, $i, "empCountry");
		$f18=mysql_result($result, $i, "empPost");
		$f19=mysql_result($result, $i, "empRes");
		$f20=mysql_result($result, $i, "empAccNo");
		$f21=mysql_result($result, $i, "empBranch");
		$f22=mysql_result($result, $i, "empDepend");
		$f23=mysql_result($result, $i, "empPos");
		$f27=mysql_result($result, $i, "empSection");
		$f24=mysql_result($result, $i, "empDateEmp");
		$f25=mysql_result($result, $i, "empShoe");
		$f26=mysql_result($result, $i, "empOverall");
		$f28=mysql_result($result, $i, "empEmployDate");
		$f30=mysql_result($result, $i, "firstWarning");
		$f31=mysql_result($result, $i, "writtenWarning");
		$f32=mysql_result($result, $i, "finalWarning");
		$f33=mysql_result($result, $i, "empRate");
		$f34=mysql_result($result, $i, "empBank");



		$queryVio = "SELECT * FROM nam_minorviolations WHERE empId = '".$vioName."'";
		$resultVio = execsql($queryVio);
		$numVio = mysql_num_rows($resultVio);



		$queryLate = "SELECT * FROM nam_timekeeping WHERE empId = '".$vioName."' AND month = '".$dMonth."' AND year ='".$dYear."'";
		$resultLate = execsql($queryLate);
		$numLate = mysql_num_rows($resultLate);


		$queryOver = "SELECT * FROM nam_overtime WHERE empId = '".$vioName."'";
		$resultOver = execsql($queryOver);
		$numOver = mysql_num_rows($resultOver);

		$queryLoan = "SELECT * FROM nam_loan WHERE empId = '".$vioName."'";
		$resultLoan = execsql($queryLoan);
		$numLoan = mysql_num_rows($resultLoan);

		?>


THESE ARE THE RESULTS:

		<?php		if($numVio > 2) {
			echo "<br />this employee HAS TOO MANY VIOLATIONS";
		}
		else { echo "";}
		?>


		<?php

		$amountOver = 0.00;
		$v = 0;
		$rate = mysql_result($result, $y, "empRate");

		while($v < $numOver) {
			$m1 = mysql_result($resultOver, $v, "hours");
			$m2 = mysql_result($resultOver, $v, "bonus");
			$amountOver = ((($rate * $m1)/100)*$m2);
			$v++;
		}
		if($numLate < 1){
			$amountOver = $amountOver + 50;
			echo "<br />This employee has not been late this month and has been given a bonus of P50!<br />"."Overtime Pay: P". $amountOver ."<br />";
		}else{ echo "<br />Overtime Pay: P".$amountOver . "<br />";}



		$dd = date("d");
		$mm = date("m");
		$yy = date("Y");
		$c = 0;
		$sundays = 0;
		$saturdays = 0;
		while($c < $dd) {
			if(date("l", mktime(0,0,0,$mm, date("d")-$c, $yy)) == "Sunday") {
				$sundays++;
			}
			$c++;
		}
			
		while($c < $dd) {
			if(date("l", mktime(0,0,0,$mm, date("d")-$c, $yy)) == "Saturday") {
				$saturdays++;
			}
			$c++;
		}
		//$day = date("d");
		//$dayNow = date("d");
		$days = $dd - ($sundays + $saturdays);
		$amount = 0.00;
		$x =0;

		while($x < $numLate){
			$l1= mysql_result($resultLate, $x, "hours");
			$amount = $amount + $l1;
			$x++;
		}

		$amountLost = $amount * $rate;
		$fullDay = $rate * 9;
		$owed = (($days * $fullDay)-$amountLost);
		echo "<br />Amount owed this month(not incl. overtime) : P" .$owed . "<br />";
		echo "Amount owed this month in Total : P" . ($owed + $amountOver);


		?>
<br />
<br />
<table>
	<tr>
		<td id="row1">ID Number:</td>
		<td id="row1"><?php echo $f10; ?></td>

		<td>Employees First Name:</td>
		<td><?php echo $f1; ?></td>
	</tr>
	<tr>
		<td>Employees Last Name:</td>
		<td><?php echo $f2; ?></td>

		<td>Employees Contact Number:</td>
		<td><?php echo $f3; ?></td>
	</tr>

	<tr>
		<td>Employees Age:</td>
		<td><?php echo $f4; ?></td>

		<td>Employees National Id Number:</td>
		<td><?php echo $f14; ?></td>
	</tr>

	<tr>
		<td>Employees Place of Birth:</td>
		<td><?php echo $f11; ?></td>
		<td>Employees sex:</td>
		<td><?php echo $f12; ?></td>
	</tr>
	<tr>
		<td>Employees Martial Status:</td>
		<td><?php echo $f13; ?></td>
		<td>Passport:</td>
		<td><?php echo $f15; ?></td>
	</tr>
	<tr>
		<td>Number of Dependants:</td>
		<td><?php echo $f22; ?></td>
	</tr>

	<tr>
		<td>Country:</td>
		<td><?php echo $f17; ?></td>
		<td>Employees Postal Address:</td>
		<td><?php echo $f18; ?></td>
	</tr>
	<tr>
		<td>Residential Address:</td>
		<td><?php echo $f19; ?></td>
		<td colspan="2"></td>
	</tr>
	<tr></tr>
	<tr>
		<td>Bank Name:</td>
		<td><?php echo $f34;?></td>

		<td>Account number:</td>
		<td><?php echo $f20; ?></td>
	</tr>

	<tr>
		<td>Branch Number:</td>
		<td><?php echo $f21; ?></td>

	</tr>
	<tr></tr>

	<tr>
		<td>Position: <?php echo $f23; ?></td>
		<td>Section:</td>
		<td><?php echo $f27;?></td>
	</tr>
	<tr>
		<td>Employment Date:</td>
		<td><?php echo $f24; ?></td>
	</tr>
	<tr>
		<td>Emergency Contact:</td>
		<td><?php echo $f5; ?></td>

		<td>Emergency Contacts Relation to Employee:</td>
		<td><?php echo $f7; ?></td>
	</tr>
	<tr>
		<td>Emergency Contacts Number:</td>
		<td><?php echo $f6; ?></td>
		<td>Emergency Contacts Education:</td>
		<td><?php echo $f8; ?></td>
	</tr>
	<tr>
		<td>Employement :</td>
		<td><?php echo $employed; ?></td>
	</tr>
	<tr></tr>
	<tr>
		<td>shoe size:</td>
		<td><?php echo $f25; ?></td>
		<td>Overall Size:</td>
		<td><?php echo $f26; ?></td>
	</tr>
	<tr>
		<td>End of Employment Date:</td>
		<td><?php echo $f28; ?></td>
		<td>Rate:P</td>
		<td><?php echo $f33;?> /Hour</td>
	</tr>
	<tr>
		<td>HAS RECEIVED FIRST WARNING:</td>
		<td><?php echo $f30; ?></td>
		<td>HAS RECEIVED WRITTEN WARNING:</td>
		<td><?php echo $f31; ?></td>
	</tr>
	<tr>
		<td>HAS RECEIVED FINAL WARNING:</td>
		<td><?php echo $f32; ?></td>

	</tr></table>	<?php
		$i++;
	}
	?>
		<br />
		<br />

		Times Late This MONTH
		<br />
		<br />
		<table id="test1" cellpadding="0" cellspacing="0" border="0"
			class="rowstyle-alt colstyle-alt no-arrow">
			<thead>
				<tr>
					<th class="sortable-numeric">Late Coming ID</th>
					<th class="sortable-text">Permission</th>
					<th class="sortable-text">Details</th>
					<th class="sortable-date-ymd">Date</th>
				</tr>
			</thead>
			<tbody>


			<?php
			//$permission = "";
			$a = 0;
			if($numLate != 0) {

				while($a < $numLate) {
					$b1 = mysql_result($resultLate, $a, "timeId");
					$b2 = mysql_result($resultLate, $a, "date");
					$b3 = mysql_result($resultLate, $a, "permission");
					if($b3 < 1){
						$permission ="No";
					}
					else {
						$permission = "Yes";
					}
					$b4 = mysql_result($resultLate, $a, "details");
					?>
				<tr class="alt">
					<td><?php echo $b1?></td>
					<td><?php echo $permission?></td>
					<td><?php echo $b4?></td>
					<td><?php echo $b2?></td>

				</tr>


				<?php
				$a++;
				}
			}
			else{

				echo "this employee has not been late this month<br /><br />";

			}

			?>
			</tbody>
		</table>

		<br />
		<br />

		Items Currently Out on Loan
		<table id="test1" cellpadding="0" cellspacing="0" border="0"
			class="rowstyle-alt colstyle-alt no-arrow">
			<thead>
				<tr>
					<th class="sortable-numeric">Loan ID</th>
					<th class="sortable-numeric">Product Id</th>
					<th class="sortable-numeric">Employee Id</th>
					<th class="sortable-numeric">Quantity</th>
					<th class="sortable-date-ymd">Date Loaned</th>

				</tr>
			</thead>
			<tbody>

			<?php
			//$permission = "";
			$a = 0;
			if($numLoan != 0) {

				while($a < $numLoan) {
					$b1 = mysql_result($resultLoan, $a, "loanId");
					$b2 = mysql_result($resultLoan, $a, "productId");
					$b3 = mysql_result($resultLoan, $a, "empId");
					$b4 = mysql_result($resultLoan, $a, "quantity");
					$b5 = mysql_result($resultLoan, $a, "date")
					?>
				<tr class="alt">
					<td><?php echo $b1?></td>
					<td><a href="../Store/showProduct.php?productId=<?php echo $b2?>"><?php echo $b2?></a></td>
					<td><?php echo $b3?></td>
					<td><?php echo $b4?></td>
					<td><?php echo $b5?></td>

				</tr>


				<?php
				$a++;
				}
			}
			else{

				echo "this employee does not currently have any items out on loan!<br /><br />";

			}

			?>
			</tbody>
		</table>

		<br />
		<br />


		<?php
		$j = 0;
		if($numVio < 1) {

			echo "This employee has no minor violations";
		}
		else{
			while($j < $numVio){
				$g1 = mysql_result($resultVio, $j, "vioId");
				$g2 = mysql_result($resultVio, $j, "empId");
				$g3 = mysql_result($resultVio, $j, "vioType");
				$g4 = mysql_result($resultVio, $j, "vioDetails");
				echo "Violation Id: $g1 <br />Violation Type: $g3<br /> Violation Details: $g4<br /><br />";

				$j++;

			}
		}

		?>
		<?php
		include "../footer.html";
}
?>