<!DOCTYPE html>
<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('../include/session.php');
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']."_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' ";
		execsql($read_query);
}

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

if ($delete=='all') {
	  	$table_name = $_SESSION['db_prefix']."_alert_message";
		$read_query = "UPDATE $table_name SET no_view = 'yes' WHERE user_name =  '".$_SESSION['username']."' AND department = 'stores' ";
		execsql($read_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" />

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



</head>

<body>
<?php
include ("../navigation/header.php");
$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>
<div class="main_frame" style="height:auto;">
<div class="page_head_frame" > <label class="label_page_head">Stores Alerts</label>
</div>
        <br />
<div class="page_middle_frame">
<?php
$page='stores_alerts_list.php?set=&delete=';
$reference_name='set';
$reference='';
include('../include/period_select_block.php');
?>
<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>
<div  class="page_inside_frame" style="height:460px;">
<table width="683" id="scroll_table" class="data_display">
  <thead>
  <tr>
    <th><a href="../store/stores_alerts_list.php?set=all&amp;date=1&amp;date1=<?php echo $start?>&amp;date2=<?php echo $end?>&amp;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="66">Date</th>
    <th width="106">Alert</th>
    <th width="389">Message</th>
    <th width="23">&nbsp;</th>
    <th width="36"><a href="../store/stores_alerts_list.php?delete=all&amp;date=1&amp;date1=<?php echo $start?>&amp;date2=<?php echo $end?>&amp;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");
	$b6 =  mysql_result($alert_result, $i, "read_done");
	
	
	$alert='';
	$message='';
	switch ($b2) {
    case 16:
        $alert = 'Purchase order to authorise';
		
		$table_name = $_SESSION['db_prefix']."_purchase";
		$purchase_query = "SELECT purchase_id, supplier_id, value, ordered_by, purchase_date FROM $table_name WHERE purchase_order = '".$d1."'  ";
		$purchase_result = mySQL_query($purchase_query);
		$purchase_id = mysql_result($purchase_result, 0, "purchase_id");
		$supplier_id = mysql_result($purchase_result, 0, "supplier_id");
		$value = mysql_result($purchase_result, 0, "value");
		$ordered_by = mysql_result($purchase_result, 0, "ordered_by");
		$purchase_date = mysql_result($purchase_result, 0, "purchase_date");
		
				
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$account_name_query = "SELECT account_name FROM $table_name WHERE supplier_id= '".$supplier_id."' ";
		$account_name_result = mySQL_query($account_name_query);
		$account_name = mysql_result($account_name_result, 0, "account_name");
				
		$message = 'Purchase order <a href=../store/purchase_order.php?purchase_id='.$purchase_id.'&message=><b>'.$d1.'</b></a> for <a href=../store/supplier_details.php?supplier_id='.$supplier_id.'><b>'.$account_name.'</b></a>. ordered on<b> '.date("d-m-Y", strtotime($purchase_date)).'</b> by <b>'.$ordered_by.'</b> is ready for authorisation (made by: <b>'.$d2.'</b>)';
        break;
		
	case 25:
        $alert = 'Purchase order authorised';
		
		$table_name = $_SESSION['db_prefix']."_purchase";
		$purchase_query = "SELECT purchase_id, purchase_order, supplier_id, value, ordered_by, purchase_date FROM $table_name WHERE purchase_id = '".$d1."'  ";
		$purchase_result = mySQL_query($purchase_query);
		$purchase_id = mysql_result($purchase_result, 0, "purchase_id");
		$purchase_order = mysql_result($purchase_result, 0, "purchase_order");
		$supplier_id = mysql_result($purchase_result, 0, "supplier_id");
		$value = mysql_result($purchase_result, 0, "value");
		$ordered_by = mysql_result($purchase_result, 0, "ordered_by");
		$purchase_date = mysql_result($purchase_result, 0, "purchase_date");
		
				
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$account_name_query = "SELECT account_name FROM $table_name WHERE supplier_id= '".$supplier_id."' ";
		$account_name_result = mySQL_query($account_name_query);
		$account_name = mysql_result($account_name_result, 0, "account_name");
				
		$message = 'Purchase order <a href=../store/purchase_order.php?purchase_id='.$purchase_id.'&message=><b>'.$purchase_order.'</b></a> for <a href=../store/supplier_details.php?supplier_id='.$supplier_id.'><b>'.$account_name.'</b></a> ordered on <b> '.date("d-m-Y", strtotime($purchase_date)).'</b> by <b>'.$ordered_by.'</b> is authorised by <b>'.$d2.'</b>';
        break;
		
	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/store_status_list.php?status=min><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="35" align="left"><?php if ($b6=='yes') { ?> <img src="../images/round_green.png" width="12" height="12" /> <?php } else {?><a href="../store/stores_alerts_list.php?set=yes&amp;message_id=<?php echo $b0 ?>&amp;date=1&amp;date1=<?php echo $start?>&amp;date2=<?php echo $end?>&amp;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/stores_alerts_list.php?delete=yes&amp;message_id=<?php echo $b0 ?>&amp;date=1&amp;date1=<?php echo $start?>&amp;date2=<?php echo $end?>&amp;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>
</div>
</body>
<?php } else {
$_SESSION['denied_return'] = "store/store_main.php";	
    include ("../navigation/admin_sidebar.php");?>
	<body onLoad="accessdenied(); return false">
<?php }
?>
<?php include ('../navigation/side_bar.php'); ?>
</html>