<!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");

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

require_once('../include/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	
	if ($date==1) {
	$start = mysql_real_escape_string($_GET['date1']);
    $end = mysql_real_escape_string($_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)));
	
	
	
$table_name2 = $_SESSION['db_prefix']."_tracking_store";
$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>
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<body>
<?php include ('../include/tooltip.php'); ?>

<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">Stores tracking</label></td>
            <td width="287">&nbsp;</td>
          </tr>
        </table>
  </div>
        <br />
<br />

<div class="page_middle_frame">	
  <form action="tracking_stores.php" method="get" name="dateform" class="today" id="dateform">
    <table width="665"  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" /></td>
        </tr>
      <tr>
        <td width="12" height="25" valign="middle">&nbsp;&nbsp; </td>
        <td width="37" valign="middle"><label class="label_std">  from:</label> </td>
        <td width="259" 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="26" valign="middle"><label class="label_std">to:</label></td>
        <td width="236" 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="67" valign="middle"><input name="button5" type="submit" class="linkbutton"  id="button5" value="Go" /></td>
        </tr>
    </table>
  </form>
  </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");
	$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 10:
        $action = 'New store item';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a>';
        break;
    case 11:
        $action = 'Receive stock';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$sup_name_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$d2."' ";
		$sup_name_result = mySQL_query($sup_name_query);
		$sup_name = mysql_result($sup_name_result, 0, "account_name");
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> from <a href=../store/supplier_details.php?supplier_id='.$d2.'>'.$sup_name.'</a> on invoice: <a href=../store/receive_note.php?receive_id='.$d3.'>'.$d4.'</a>';
        break;
    case 12:
        $action = 'Receive timber';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$sup_name_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$d2."' ";
		$sup_name_result = mySQL_query($sup_name_query);
		$sup_name = mysql_result($sup_name_result, 0, "account_name");
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> from <a href=../store/supplier_details.php?supplier_id='.$d2.'>'.$sup_name.'</a> on invoice: <a href=../store/receive_note.php?receive_id='.$d3.'>'.$d4.'</a>';
        break;
	case 13:
        $action = 'Receive Mitek';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$sup_name_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$d2."' ";
		$sup_name_result = mySQL_query($sup_name_query);
		$sup_name = mysql_result($sup_name_result, 0, "account_name");
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> from <a href=../store/supplier_details.php?supplier_id='.$d2.'>'.$sup_name.'</a> on invoice: <a href=../store/receive_note.php?receive_id='.$d3.'>'.$d4.'</a>';
        break;
	case 14:
        $action = 'Supply direct materials';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$item_unit = mysql_result($item_result, 0, "unit");
		
		
		$table_name = $_SESSION['db_prefix']."_captured_jobs";
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT $table_name.cust_id, comp_name FROM $table_name, $table_name2 WHERE job_no= '".$d4."' AND $table_name.cust_id=$table_name2.cust_id ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_id = mysql_result($cust_name_result, 0, "cust_id");
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$details = $d2.' '.$item_unit.' <a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> for <a href=../production/captured_job.php?job_no='.$d4.'>'.$d4.'</a> of: <a href=../sales/customer_details.php?cust_id='.$cust_id.'>'.$cust_name.'</a>';
        break;
	case 15:
        $action = 'Deliver job (trusses)';
				
		$table_name = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$d2."' ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$table_name = $_SESSION['db_prefix']."_supply_customer";
		$supply_query = "SELECT supply_id FROM $table_name WHERE delivery_note= '".$d3."' ";
		$supply_result = mySQL_query($supply_query);
		$supply_num = mysql_num_rows($supply_result);
		if ($supply_num!=0) {
		$supply_id = mysql_result($supply_result, 0, "supply_id");
		}
		else {
			$supply_id = '';
		}
		
		$details = 'Truss '.$d1.'</a> for <a href=../production/captured_job.php?job_no='.$d4.'>'.$d4.'</a> of: <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> del no:	<a href=../store/customer_supplied.php?del_note='.$d3.'&supply_id='.$supply_id.'&pr_option=copy>'.$d3.'</a>';
        break;
	case 16:
        $action = 'Deliver job (store items)';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$d2."' ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$table_name = $_SESSION['db_prefix']."_supply_customer";
		$supply_query = "SELECT supply_id FROM $table_name WHERE delivery_note= '".$d3."' ";
		$supply_result = mySQL_query($supply_query);
		$supply_num = mysql_num_rows($supply_result);
		if ($supply_num!=0) {
		$supply_id = mysql_result($supply_result, 0, "supply_id");
		}
		else {
			$supply_id = '';
		}
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> for <a href=../production/captured_job.php?job_no='.$d4.'>'.$d4.'</a> of: <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> del no:	<a href=../store/customer_supplied.php?del_note='.$d3.'&supply_id='.$supply_id.'&pr_option=copy>'.$d3.'</a>';
		
        break;
	case 17:
        $action = 'Cash sale';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		//echo '<br>*****'.$d3;
		if ($d3!='') {
		$table_name = $_SESSION['db_prefix']."_supply_customer";
		$supply_query = "SELECT supply_id, cust_id FROM $table_name WHERE delivery_note= '".$d3."' ";
		//echo '<br>*****'.$supply_query;
		$supply_result = mySQL_query($supply_query);
		$supply_num = mysql_num_rows($supply_result);
		$supply_id = mysql_result($supply_result, 0, "supply_id");
		$cust_id = mysql_result($supply_result, 0, "cust_id");
		//echo '<br>*****cust id:'.$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");
		}
		else {
			$cust_name = '';
		}
		
		if ($d4==0) {
			$job_link = 'cash to';
		}
		else {
			$job_link = 'cash on <a href=../production/captured_job.php?job_no='.$d4.'>'.$d4.'</a> of'; 
		}
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> for '.$job_link.' <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> del no:	<a href=../store/customer_supplied.php?del_note='.$d3.'&supply_id='.$supply_id.'&pr_option=copy>'.$d3.'</a>';
				
        break;
	case 18:
        $action = 'Supply department';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> to '.$d2;
		
        break;
	case 19:
        $action = 'Supply employee';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_employee";
		$emp_name_query = "SELECT emp_first_name, emp_last_name FROM $table_name WHERE emp_id= '".$d2."' ";
		$emp_name_result = mySQL_query($emp_name_query);
		$emp_name = mysql_result($emp_name_result, 0, "emp_first_name").' '.mysql_result($emp_name_result, 0, "emp_last_name");
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> to <a href=../hr_module/employee-details.php?emp_id='.$d2.'>'.$emp_name;
		
        break;
	case 20:
        $action = 'New supplier';
		
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$sup_name_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$d2."' ";
		$sup_name_result = mySQL_query($sup_name_query);
		$sup_name = mysql_result($sup_name_result, 0, "account_name");
		$details = '<a href=../store/supplier_details.php?supplier_id='.$d2.'>'.$sup_name.'</a>';
		
		
        break;
	case 21:
        $action = 'Stock count';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a>';
		break;
	case 22:
        $action = 'Cost price adjustment';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_num = mysql_num_rows($item_result);
		if ($item_num!=0)  {
		       $item_name = mysql_result($item_result, 0, "item_name");
			   $details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> old:'.number_format($d3, 2, '.', ',').' new: '.number_format($d4, 2, '.', ',');
		}
		else {
			$item_name='something';
			if ($d3=='') {$d3=0;}
			$details = $d1.' old:'.number_format($d3, 2, '.', ',').' new: '.number_format($d4, 2, '.', ',');
	}
	    break;
	case 23:
        $action = 'Edit supplier';
		$table_name = $_SESSION['db_prefix']."_suppliers";
		$name_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$d1."' ";
		$name_result = mySQL_query($name_query);
		$name = mysql_result($name_result, 0, "account_name");
		$details = '<b>'.$d4.'</b> for <b><a href=../store/supplier_details.php?supplier_id='.$d1.'>'.$name.'</a></b> changed from <b>'.$d2.'</b> to <b>'.$d3.'</b>';
		break;
		
	case 24:
        $action = 'Edit store item';
				
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		
		$details = '<b>'.$d4.'</b> for <b><a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a></b> changed from <b>'.$d2.'</b> to <b>'.$d3.'</b>';
		break;
	case 25:
        $action = 'Stock adjustment';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'><b>'.$item_name.'</b></a>, old value: <b>'.$d2.'</b> counted value: <b>'.$d3.'</b>';
		break;		
			
	case 26:
        $action = 'Sales price adjustment';
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_num = mysql_num_rows($item_result);
		if ($item_num!=0)  {
		       $item_name = mysql_result($item_result, 0, "item_name");
			   $details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> old:'.number_format($d3, 2, '.', ',').' new: '.number_format($d4, 2, '.', ',');
		}
		else {
			$item_name='something';
			if ($d3=='') {$d3=0;}
			$details = $d1.' old:'.number_format($d3, 2, '.', ',').' new: '.number_format($d4, 2, '.', ',');
	}
	    break;	
	
	case 27:
        $action = 'Cash sale delivery';
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d1."' ";
		$item_result = mySQL_query($item_query);
		$item_name = mysql_result($item_result, 0, "item_name");
		
		$table_name = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$d2."' ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$table_name = $_SESSION['db_prefix']."_supply_customer";
		$supply_query = "SELECT supply_id FROM $table_name WHERE delivery_note= '".$d3."' ";
		$supply_result = mySQL_query($supply_query);
		$supply_num = mysql_num_rows($supply_result);
		if ($supply_num!=0) {
		$supply_id = mysql_result($supply_result, 0, "supply_id");
		}
		else {
			$supply_id = '';
		}
		
		$details = '<a href=../store/store_item_display.php?store_item_id='.$d1.'>'.$item_name.'</a> for <a href=../sales/quotation.php?quote_no='.$d4.'>'.$d4.'</a> of: <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> del no:	<a href=../store/customer_supplied.php?del_note='.$d3.'&supply_id='.$supply_id.'&pr_option=copy>'.$d3.'</a>';
		
        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>