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

<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 = 'orders' ";
		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 = 'orders' ";
		execsql($read_query);
}
	
	
$table_name2 = $_SESSION['db_prefix']."_alert_message";
$alert_query = "SELECT * FROM $table_name2 WHERE user_name = '".$_SESSION['username']."' AND department = 'orders' 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>
<?php
include ("../navigation/header.php");
$page_name = "Orders-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/orders_sidebar.php");
?>
<div class="main_frame" style="height:auto;">
<div class="page_head_frame" > <label class="label_page_head">Orders Alerts</label>
</div>
        <br />
<div class="page_middle_frame">
<?php
$page='orders_alerts_list.php?set=&delete=';
$reference_name='set';
$reference='';
include('../include/period_select_block.php');
?>
<!--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>
<div  class="page_inside_frame" style="height:460px;">
<table width="683" id="scroll_table" class="data_display">
  <thead>
  <tr>
    <th><a href="orders_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="76">Date</th>
    <th width="86">Alert</th>
    <th width="399">Message</th>
    <th width="23">&nbsp;</th>
    <th width="36"><a href="orders_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");
	$b6 =  mysql_result($alert_result, $i, "read_done");
	
	
	$alert='';
	$message='';
	switch ($b2) {
	case 12:
        $alert = 'Order value';
		
		$table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_card_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
						
		$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");
		
		$exceed = $d3 - $d2;
				
		$message = 'Order value on <a href=../orders/compiled_job.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a>. exceeds with <b> P'.number_format($exceed, 2, '.', ',').'</b>. Order value <b>'.$d2.'</b>,  job value: <b>'.$d3.'</b>.';
        break;
	case 13:
        $alert = 'Order qty exceeds';
		
		$table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_card_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
						
		$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 = 'Store items reserved on <a href=../orders/compiled_job.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a>. exceeds available quantities.';
        break;	
    			
    case 15:
        $alert = 'Job completed';
		
		$table_name = $_SESSION['db_prefix']."_inspection_report";
		$job_query = "SELECT cust_id, inspected_by, inspection_date, job_approved FROM $table_name WHERE job_card_no = '".$d1."'  ";
		
		
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$inspected_by = mysql_result($job_result, 0, "inspected_by");
		$inspection_date = mysql_result($job_result, 0, "inspection_date");
		$approved = mysql_result($job_result, 0, "job_approved");
				
		$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 = 'Inspection report for <a href=../orders/inspection_report.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a>. done on <b> '.date("d-m-Y", strtotime($inspection_date)).'</b> by <b>'.$inspected_by.'</b>. Job approved: <b>'.$approved.'</b>.';
        break;
	case 17:
        $alert = 'Job inspected';
		
		$table_name = $_SESSION['db_prefix']."_inspection_report";
		$job_query = "SELECT cust_id, inspected_by, inspection_date, job_approved FROM $table_name WHERE job_card_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$inspected_by = mysql_result($job_result, 0, "inspected_by");
		$inspection_date = mysql_result($job_result, 0, "inspection_date");
		$approved = mysql_result($job_result, 0, "job_approved");
				
		$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 = 'Job <a href=../orders/inspection_report.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> has been inspected on <b>'.date("d-m-Y", strtotime($inspection_date)).'</b> by <b>'.$inspected_by.'</b>. The job can be invoiced.';
        break;
	case 18:
        $alert = 'Order non stock item';
		
		$table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT cust_id FROM $table_name WHERE job_card_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
						
		$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 = 'On job <a href=../orders/inspection_report.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> the folowing non stock items are ordered: <b>'.$d2.'</b><br>These items need to be ordered. Refer to the special order list.';
        break;
	case 19:
        $alert = 'Job invoiced';
		
		$table_name = $_SESSION['db_prefix']."_invoices";
		$invoice_query = "SELECT cust_id, invoiced_by, invoice_date, invoice_amount FROM $table_name WHERE invoice_no = '".$d2."'  ";
		$invoice_result = mySQL_query($invoice_query);
		$cust_id = mysql_result($invoice_result, 0, "cust_id");
		$invoiced_by = mysql_result($invoice_result, 0, "invoiced_by");
		$invoice_date = mysql_result($invoice_result, 0, "invoice_date");
		$invoice_amount = mysql_result($invoice_result, 0, "invoice_amount");
				
		$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 = 'Job <a href=../orders/inspection_report.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> has been invoiced on <b>'.date("d-m-Y", strtotime($invoice_date)).'</b> by <b>'.$invoiced_by.'</b>.Invoice no: <b>'.$d2.'</b>, invoice amount: <b>P'.number_format($invoice_amount, 2, '.', ',').'</b>';
        break;		
		
	
	case 44:
        $alert = 'Job card comment';
		
		$table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT cust_id, project FROM $table_name WHERE job_card_no = '".$d1."'  ";
		$job_result = mySQL_query($job_query);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$project = mysql_result($job_result, 0, "project");
				
		$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 new comment on job card <a href=../orders/job_card_history.php?job_card_no='.$d1.'><b>'.$d1.'</b></a> on <b>'.$project.'</b> for  <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a>';
        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="orders_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="orders_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>
</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>