<!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 = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");


require_once('../include/tc_calendar.php');
	$date = $_GET['date'];
	
include('../include/period_select_date.php');	

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

if ($set=='yes') {
	  $message_id = $_GET['message_id'];
		$table_name = $_SESSION['db_prefix']."_alerts_store";
		$read_query = "UPDATE $table_name SET read_done = 'yes' WHERE message_id = '".$message_id."' ";
		$read_result = $con->query($read_query);
}

if ($set=='all') {
	  	$table_name = $_SESSION['db_prefix']."_alerts_store";
		$read_query = "UPDATE $table_name SET read_done = 'yes' WHERE user_name =  '".$_SESSION['username']."' AND department = 'store'  AND recording_date >= '".$start."' AND recording_date <= '".$end."' ";
		$read_result = $con->query($read_query);
}

if ($delete=='yes') {
	    $message_id = $_GET['message_id'];
		$table_name = $_SESSION['db_prefix']."_alerts_store";
		$delete_query = "DELETE FROM $table_name WHERE message_id = '".$message_id."'   "; 
		$delete_result = $con->query($delete_query);
}

if ($delete=='all') {
	 	$table_name = $_SESSION['db_prefix']."_alerts_store";
		$delete_query = "DELETE FROM $table_name WHERE user_name =  '".$_SESSION['username']."' AND department = 'store' ";
		$delete_result = $con->query($delete_query);
}
	
	
$table_name2 = $_SESSION['db_prefix']."_alerts_store";
$alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$_SESSION['username']."' AND department = 'store' AND recording_date >= '".$start."' AND recording_date <= '".$end."' AND no_view IS NULL ORDER BY message_id DESC ";
$alert_result = $con->query($alert_query);
$alert_num = mysqli_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 />

	
  <?php
$page='store_alerts_list.php';
$reference_name='set';
$reference=$set;
$reference_name_2='delete';
$reference_2=$delete;
include('../include/period_select_block.php');
?>
 
<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="store_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="store_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($row = $alert_result->fetch_assoc()) {
	$b0 = $row["message_id"];
	$b1 = $row["recording_date"];
	$b2 = $row["alert"];
	$d1 = $row["detail_1"];
	$d2 = $row["detail_2"];
	$d3 = $row["detail_3"];
	$b6 = $row["read_done"];
	
	
	$alert='';
	$message='';
	switch ($b2) {
		
	case 30:
        $alert = 'Min stock';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name, minimumStock FROM $table_name WHERE store_item_id = '".$d1."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row["item_name"];
		$min_stock = $row["minimumStock"];
								
		$message = 'General stock item <a href="store_item_display.php?store_item_id='.$d1.'"><b>'.$item.'</b></a> is with a stock quantity of '.$d3.' below the minimum required of '.$min_stock.'.';
        break;	
    		
	case 31:
        $alert = 'Min stock';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name, minimumStock FROM $table_name WHERE store_item_id = '".$d1."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row["item_name"];
		$min_stock = $row["minimumStock"];
								
		$message = 'Direct material <a href="store_item_display.php?store_item_id='.$d1.'"><b>'.$item.'</b></a> is with a stock quantity of '.$d3.' below the minimum required of '.$min_stock.'.';
        break;
        		
	case 32:
        $alert = 'Min stock';
		
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT item_name, minimumStock FROM $table_name WHERE product_item_id = '".$d1."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row["item_name"];
		$min_stock = $row["minimumStock"];
								
		$message = 'Sales product <a href="product_item_display.php?product_item_id='.$d1.'"><b>'.$item.'</b></a> is with a stock quantity of '.$d3.' below the minimum required of '.$min_stock.'.';
        break;
		
	case 33:
        $alert = 'Stock count';
								
		$message = 'Stock count for  <b>'.date("l, d-m-Y", strtotime($d1)).'</b> was at '.date("H:i", strtotime($d3)).' hrs '.$d2.'.';
        break;
		
	case 34:
        $alert = 'Stock count';
		$message = 'Stock count for <b>'.$d2.'</b> was at <b>'.date("l, d-m-Y H:i", strtotime($b1)).'</b> not done yet.';
        break;
		
	case 35:
        $alert = 'Order qty';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$ref_query = "SELECT order_reference, order_date FROM $table_name WHERE order_no = '".$d1."' ";
		$ref_result = $con->query($ref_query);
		$row = mysqli_fetch_assoc($ref_result);
		$order_reference = $row['order_reference'];
		$order_date = $row['order_date'];
		$tt_text = 'Click to view order '.$d1.'.';
		
		$message = 'An order for <b>'.$d3.'</b> was received from <b>'.$d2.'</b> on <b>'.date("d-m-Y", strtotime($order_date)).'</b>. Order no: <a href=order.php?order_reference='.$order_reference.' onMouseover=ddrivetip("'.$tt_text.'", 150); onMouseout=hideddrivetip()><b>'.$d1.'</b>.</a>';
        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="store_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="store_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 { ?>
<script> needToConfirm = false; </script>
<?php
$_SESSION['denied_return'] = "store/store_main.php?date=0";		
    include ("../navigation/store_sidebar.php");?>
	<body onLoad="accessdenied(); return false">
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>