<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

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

if ($permission_num==1) {
	include ("../navigation/store_sidebar.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 = 'stores'  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 = 'stores' 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 = 'stores' AND generated >= '".$start."' AND no_view IS NULL 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" />


</head>

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

<div class="page_middle_frame" >	
  <form action="stores_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="" /><input name="delete" type="hidden" id="delete" 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;">
<!--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": [[ 4, "desc" ]],
					"aoColumns": [
								{"bSortable": false},
								{"iDataSort": 4},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bVisible": false},
								{"bSortable": false}
								 ]
								 
				} );
			} );
		</script>
<table width="683" id="scroll_table" class="data_display">
  <thead>
  <tr>
    <th><a href="stores_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="stores_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");
	$d1 =  mysql_result($alert_result, $i, "detail_1");
	$d2 =  mysql_result($alert_result, $i, "detail_2");
	$d3 =  mysql_result($alert_result, $i, "detail_3");
	$d6 =  mysql_result($alert_result, $i, "read_done");
	
	
	$alert='';
	$message='';
	switch ($b2) {
    case 35:
        $alert = 'Minimum stock';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."'  ";
		$name_result = mySQL_query($name_query);
		$item_name = mysql_result($name_result, 0, "item_name");
				
		$message = 'Store item <a href=store_item_display.php?store_item_id='.$d1.'><b>'.$item_name.'</b></a> in <b>'.$d3.'</b> is below minimum stock level.<br>Stock quantity is now: <b>'.$d2.'</b>';
        break;
	
		
	}
	?>
  
  
  <tr>
    <td width="50" align="left"><?php if ($d6=='yes') { ?> <img src="../images/round_green.png" width="12" height="12" /> <?php } else {?><a href="stores_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="stores_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'] = "store/store_main.php";	
    include ("../navigation/store_sidebar.php");?>
	<body onLoad="accessdenied(); return false">
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>