<!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 = "AD-tracking";
include ("../navigation/access.php");
include('../include/period_select_date.php');	

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

	
	
$table_name2 = $_SESSION['db_prefix']."_tracking_orders";
$tracking_query = "SELECT * FROM $table_name2 WHERE entry_date >= '".$start."' AND entry_date <= '".$end."' ORDER BY action_id DESC ";
$tracking_result = mySQL_query($tracking_query);
$tracking_num = mysql_num_rows($tracking_result);


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

<!--jscript-->
<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": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 5},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
</head>

<body>


<div class="main_frame">
<div class="page_head_frame">
        
        <table width="825" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="523"><label class="label_page_head">Orders tracking</label></td>
            <td width="287">&nbsp;</td>
          </tr>
        </table>
  </div>
        <br />
<br />

<div class="page_middle_frame">	
<?php
$page='tracking_orders.php';
$reference_name='';
$reference='';
$reference_name_2='';
$reference_2='';
$reference_name_3='';
$reference_3='';
include('../include/period_select_block_small.php');
?>
</div>


<div class="page_middle_frame">
<table width="683" id="scroll_table" class="data_display"  >
  <thead>
  <tr>
    <th>date</th>
    <th >time</th>
    <th>user</th>
    <th>action</th>
    <th width="371" >details</th>
    <th width="55" >&nbsp;</th>
    </tr>
    </thead>
<?php 

$i=0;

while ($i<$tracking_num) {
    $b0  =  mysql_result($tracking_result, $i, "action_id");
	$b1 =  mysql_result($tracking_result, $i, "entry_date");
	$b2 =  mysql_result($tracking_result, $i, "user_name");
	$b3 =  mysql_result($tracking_result, $i, "action");
	$d1 = mysql_result($tracking_result, $i, "details_1");
	$d2 = mysql_result($tracking_result, $i, "details_2");
	$d3 = mysql_result($tracking_result, $i, "details_3");
	$d4 = mysql_result($tracking_result, $i, "details_4");
	
	
	
	$action='';
	$details='';
	switch ($b3) {
    case 40:
        $action = 'New account';
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_no = '".$d2."' ";
		$name_result = mySQL_query($name_query);
		$comp_name = mysql_result($name_result, 0, "comp_name");
		$details = 'New account for: <a href=../store/account_details.php?cust_no='.$d2.'><b>'.$comp_name.'</b></a>';
        break;
		
	case 41:
        $action = 'Edit account';
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_no = '".$d1."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		$details = '<b>'.$d4.'</b> for <b><a href=../store/account_details.php?cust_no='.$d1.'>'.$name.'</a></b> changed from <b>'.$d2.'</b> to <b>'.$d3.'</b>';
		break;	
		
	case 42:
        $action = 'New order';
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$d2."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		$details = 'New order <b><a href=../store/order.php?order_reference='.$d3.'>'.$d1.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$d2.'>'.$name.'</a></b> ';
		break;
	
	case 43:
        $action = 'Edit order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT  cust_id, order_no FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$order_no = mysql_result($customer_result, 0, "order_no");
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		
		if ($d4=='Delivery date') {
			  if ($d2=='2000-01-01') {
			         $d2='t.b.a.';
		}
	}
		
		$details = '<b>'.$d4.'</b> on order <b><a href=../store/order.php?order_reference='.$d1.'>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> changed from <b>'.$d2.'</b> to <b>'.$d3.'</b>.';
		break;	
		
	case 44:
        $action = 'Edit order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT  cust_id FROM $table_name WHERE order_no = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		$details = '<b>'.$d4.'</b> on order <b><a href=../store/order.php?order_no='.$d1.'>'.$d1.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> deleted.';
		break;	
		
	case 45:
        $action = 'Edit order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT  cust_id, order_no FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$order_no = mysql_result($customer_result, 0, "order_no");
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		
		$details = '<b>'.$d4.'</b> on order <b><a href=../store/order.php?order_reference='.$d1.'>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> replaced with a qty of <b>'.$d2.'</b>.';
		break;	
		
	case 46:
        $action = 'Edit order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT  cust_id, order_no FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$order_no = mysql_result($customer_result, 0, "order_no");
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		
		$details = '<b>'.$d3.'</b> on order <a href=../store/order.php?order_reference='.$d1.'><b>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> added with a qty of <b>'.$d4.'</b>.';
		break;
		
	case 47:
        $action = 'Edit delivery date';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT  cust_id, order_no FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		$order_no = mysql_result($customer_result, 0, "order_no");
				
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		
		$details = 'Delivery date on order <a href=../store/order.php?order_reference='.$d1.'><b>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> changed from <b>'.date("d-m-Y", strtotime($d2)).'</b> to <b>'.date("d-m-Y", strtotime($d3)).'</b>.';
		break;	
		
	case 48:
        $action = 'Cancel order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT order_no,  cust_id FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$order_no = mysql_result($customer_result, 0, "order_no");
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		$details = 'Order <b><a href=../store/order.php?order_reference='.$d1.'>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> was cancelled.';
		break;	
		
	case 49:
        $action = 'Reverse cancel order';
		
		$table_name = $_SESSION['db_prefix']."_orders";
		$customer_query = "SELECT order_no,  cust_id FROM $table_name WHERE order_reference = '".$d1."'  ";
		$customer_result = mySQL_query($customer_query);
		$order_no = mysql_result($customer_result, 0, "order_no");
		$cust_id = mysql_result($customer_result, 0, "cust_id");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "comp_name");
		$details = 'Cancellation of order <b><a href=../store/order.php?order_reference='.$d1.'>'.$order_no.'</b></a> for <b><a href=../store/account_details.php?cust_id='.$cust_id.'>'.$name.'</a></b> was reversed.';
		break;				
    
}
	
	?>
  
  
  <tr>
    <td width="54" align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td width="38" align="left"><?php echo date("H:i", strtotime($b1 ))  ?></td>
    <td width="50" align="left"><?php echo $b2 ?></td>
    <td width="87" align="left"><?php echo $action ?></td>
    <td align="left"><?php echo $details ?></td>
    <td align="left"><?php echo $b1 ?></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>