<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<?php
include ("../navigation/header.php");
$page_name = "HR-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/hr_sidebar.php");
?>
<?php

require_once('../include/tc_calendar.php');
	$date = $_GET['date'];
	
	if ($date==1) {
	$start = $_GET['date1'];
    $end = $_GET['date2'];
	}
	else {
	$start = date("Y-m-01");
	$end = date("Y-m-31");
	}
		
	if ($date!=1) {
	   $ds = "1"; }
	   else {
		$ds = date("d", strtotime($start) );
	   }
	$ms = date("m", strtotime($start) );
	$ys = date("Y", strtotime($start) );
	
	
	$de = date("d", strtotime($end) );
	$me = date("m", strtotime($end) );
	$ye = date("Y", strtotime($end) );	 
	
	//$endsearch = date('Y-m-d', strtotime('+1 day', strtotime($end)));

$set = $_GET['set'];
$delete = $_GET['delete'];

if ($set=='yes') {
	  $message_id = $_GET['message_id'];
		$table_name = $_SESSION['db_prefix']."_alert_message";
		$read_query = "UPDATE $table_name SET read_done = 'yes' WHERE message_id = '".$message_id."' ";
		execsql($read_query);
}

if ($set=='all') {
	  	$table_name = $_SESSION['db_prefix']."_alert_message";
		$read_query = "UPDATE $table_name SET read_done = 'yes' WHERE user_name =  '".$_SESSION['username']."' AND department = 'hr'  generated BETWEEN '".$start."' AND '".$end."' ";
		execsql($read_query);
}

if ($delete=='yes') {
	  $message_id = $_GET['message_id'];
		$table_name = $_SESSION['db_prefix']."_alert_message";
		$delete_query = "DELETE FROM $table_name WHERE message_id = '".$message_id."'   "; 
		execsql($delete_query);
}

if ($delete=='all') {
	  $message_id = $_GET['message_id'];
		$table_name = $_SESSION['db_prefix']."_alert_message";
		$delete_query = "DELETE FROM $table_name WHERE user_name =  '".$_SESSION['username']."' AND department = 'hr' AND   generated BETWEEN '".$start."' AND '".$end."' ";
		execsql($delete_query);
}
	
	
$table_name2 = $_SESSION['db_prefix']."_alert_message";
$alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$_SESSION['username']."' AND department = 'hr' AND generated >= '".$start."' AND generated <= '".$end."' ORDER BY message_id DESC ";
$alert_result = mySQL_query($alert_query);
$alert_num = mysql_num_rows($alert_result);


?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<script language="javascript" src="../jscript/calendar.js"></script>
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>

<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 3, "desc" ]],
					"aoColumns": [
								{"bSortable": false},
								{"iDataSort": 3},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bVisible": false},
								{"bSortable": false}
								 ]
								 
				} );
			} );
		</script>
</head>

<body>
<div class="main_frame" style="height:auto;">
<div class="page_middle_frame" > <label class="label_page_head">HR Alerts</label></div>
        <br />
<br />
<br />
<br />

<div class="page_middle_frame" >	
  <form action="alerts_list.php" method="get" name="dateform" class="today" id="dateform">
    <table width="686"  id='table_calendar'>
      <tr>
        <td height="20" colspan="3" align="left" valign="middle"><label class="label_std">Select time period to display</label></td>
        <td valign="middle">&nbsp;</td>
        <td colspan="2" valign="middle"><input name="date" type="hidden" id="date" value="1" /><input name="set" type="hidden" id="set" value="" /></td>
        </tr>
      <tr>
        <td width="12" height="25" valign="middle">&nbsp;&nbsp; </td>
        <td width="43" valign="middle"><label class="label_std">  from:</label> </td>
        <td width="244" valign="middle"><?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2010, 2030);
	 	$myCalendar->writeScript();?></td>
        <td width="31" valign="middle"><label class="label_std">to:</label></td>
        <td width="297" valign="middle"><?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2010, 2030);
		$myCalendar->writeScript();?></td>
        <td width="71" valign="middle"><input name="button5" type="submit" class="linkbutton"  id="button5" value="Go" /></td>
        </tr>
    </table>
  </form>
</div>  
<div  class="page_middle_frame" style="height:460px;">
<table width="683" id="scroll_table" class="data_display">
  <thead>
  <tr>
    <th><a href="hr_alerts_list.php?set=all&date=1&date1=<?php echo $start?>&date2=<?php echo $end?>&delete=" onMouseover="ddrivetip('Click to set all to read.', 110)"; onMouseout="hideddrivetip()"><img src="../images/round_red_green.png" width="18" height="12" /></a></th>
    <th width="75">date</th>
    <th width="90">alert</th>
    <th width="313">message</th>
    <th width="91">&nbsp;</th>
    <th width="36"><a href="hr_alerts_list.php?delete=all&date=1&date1=<?php echo $start?>&date2=<?php echo $end?>&set=" onMouseover="ddrivetip('Click to delete all alerts.', 120)"; onMouseout="hideddrivetip()"><img src="../images/delete_all.png" width="20" height="16" /></a></th>
    </tr>
   </thead>
<?php 

$i=0;

while ($i<$alert_num) {
    $b0  =  mysql_result($alert_result, $i, "message_id");
	$b1 =  mysql_result($alert_result, $i, "generated");
	$b2 =  mysql_result($alert_result, $i, "alert");
	$b3 =  mysql_result($alert_result, $i, "detail_1");
	$b4 =  mysql_result($alert_result, $i, "detail_2");
	$b5 =  mysql_result($alert_result, $i, "detail_3");
	$b6 =  mysql_result($alert_result, $i, "read_done");
	
	
	$alert='';
	$message='';
	switch ($b2) {
    case 10:
        $alert = 'New order';
		
		$table_name = $_SESSION['db_prefix']."_captured_jobs";
		$job_query = "SELECT cust_id, designer, value FROM $table_name WHERE job_no = '".$b3."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$designer = mysql_result($job_result, 0, "designer");
		$value = mysql_result($job_result, 0, "value");
				
		$table_name = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$cust_id."' ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$message = '<a href=../production/captured_job.php?job_no='.$b3.'>J'.$b3.'</a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'>'.$cust_name.'</a>. Value: P'.number_format($value, 2, '.', ',').', designer: '.$designer.', received by: '.$b4.'.';
        break;
	case 20:
        $alert = 'Employee late';
		
		$table_name = $_SESSION['db_prefix']."_employee";
		$name_query = "SELECT emp_first_name, emp_last_name FROM $table_name WHERE emp_id= '".$b3."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "emp_first_name").' '. mysql_result($name_result, 0, "emp_last_name");
				
		$message = '<a href=../hr_module/employee-details.php?emp_id='.$b3.'>'.$name.'</a> has been '.$b5.' times late in the past '.$b4.' days.';
        break;
	case 30:
        $alert = 'Minimum stock';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name, quantity, minimumStock FROM $table_name WHERE store_item_id= '".$b3."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$stock_level = mysql_result($item_result, 0, "quantity");
		$min_level = mysql_result($item_result, 0, "minimumStock");
		
		
		$message = '<a href=../store/store_item_display.php?store_item_id='.$b3.'>'.$item_name.'</a> is only '.$stock_level.' left in stock. Minimum required stock is: '.$min_level;
        break;
		
	}
	?>
  
  
  <tr>
    <td width="50" align="left"><?php if ($b6=='yes') { ?> <img src="../images/round_green.png" width="12" height="12" /> <?php } else {?><a href="hr_alerts_list.php?set=yes&message_id=<?php echo $b0 ?>&date=1&date1=<?php echo $start?>&date2=<?php echo $end?>&delete=" onMouseover="ddrivetip('Click to set to read.', 100)"; onMouseout="hideddrivetip()"><img src="../images/round_red.png" width="12" height="12" /></a><?php } ?></td>
    <td align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td align="left"><?php echo $alert ?></td>
    <td align="left"><?php echo $message ?></td>
    <td align="left"><?php echo $b1?></td>
    <td align="left"><a href="hr_alerts_list.php?delete=yes&message_id=<?php echo $b0 ?>&date=1&date1=<?php echo $start?>&date2=<?php echo $end?>&set=" onMouseover="ddrivetip('Click to delete alert.', 100)"; onMouseout="hideddrivetip()"><img src="../images/delete.png" width="16" height="16" /></a></td>
    </tr>
  <?php
  $i++;
}
?>

</table>
</div>

</div>
</body>
<?php } else {
$_SESSION['denied_return'] = "admin_module/user_list.php";	
    include ("../navigation/admin_sidebar.php");?>
	<body onLoad="accessdenied(); return false">
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>