<!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_sales";
$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": [[ 5, "desc" ]],
					"aoColumns": [
								{"iDataSort": 5},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
</head>

<body>
<div class="main_frame" style="height:auto;">
<div  class="page_head_frame"> <label class="label_page_head">Sales tracking</label></td>
</div>
        <br />
<br />
<br />
<br />

<div class="page_middle_frame" >	
  <form action="tracking_sales.php" method="get" name="dateform" class="today" id="dateform">
    <table width="686"  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="43" valign="middle"><label class="label_std">  from:</label> </td>
        <td width="244" valign="middle"><?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2014, 2090);
	 	$myCalendar->writeScript();?></td>
        <td width="31" valign="middle"><label class="label_std">to:</label></td>
        <td width="297" valign="middle"><?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2014, 2090);
		$myCalendar->writeScript();?></td>
        <td width="71" 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="322">details</th>
    <th width="51">&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 40:
        $action = 'Request quotation';
		
		$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_num = mysql_num_rows($cust_name_result);
		if ($cust_name_num!=0) {
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		}
		else {
			$cust_name='';
		}
		
		$details = '<a href=../sales/quotation_request.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a>';
        break;
    case 41:
        $action = 'Capture quotation';
		$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_num = mysql_num_rows($cust_name_result);
		if ($cust_name_num!=0) {
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		}
		else {
			$cust_name='';
		}
		
		$details = '<a href=../sales/quotation.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a>';
        break;
    case 42:
        $action = 'Note entry quotation';
		$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_num = mysql_num_rows($cust_name_result);
		if ($cust_name_num!=0) {
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		}
		else {
			$cust_name='';
		}
		
		$details = '<a href=../sales/quotation.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> '.$d3;
        break;
	case 43:
        $action = 'Accept quotation';
		$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");
		
		$details = '<a href=../sales/quotation.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> '.$d3;
        break;
	case 44:
        $action = 'Edit quotation';
		$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");
		
		$details = '<a href=../sales/quotation.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a> changed <b>'.$d3. '</b> to <b>'.$d4.'</b>';
        break;
	case 45:
        $action = 'Edit customer';
		$table_name = $_SESSION['db_prefix']."_customer";
		$cust_name_query = "SELECT comp_name FROM $table_name WHERE cust_id= '".$d1."' ";
		$cust_name_result = mySQL_query($cust_name_query);
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		
		$details = $d4.' for <a href=../sales/customer_details.php?cust_id='.$d1.'><b>'.$cust_name.'</b></a> changed from <b>'.$d2.'</b> to <b>'.$d3.'</b>' ;
        break;	
	case 47:
        $action = 'Edit cash sale quotation';
		$table_name = $_SESSION['db_prefix']."_quotations";
		$quote_query = "SELECT  cust_id  FROM $table_name WHERE quote_no = '".$d1."' ";
		$quote_result = mySQL_query($quote_query);
		$cust_id = mysql_result($quote_result, 0, "cust_id");
				
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d2."'  ";
		$name_result = mySQL_query($name_query);
		$d2a = mysql_result($name_result, 0, "item_name");	
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d3."'  ";
		$name_result = mySQL_query($name_query);
		$d3a = mysql_result($name_result, 0, "item_name");	
		
		$details = '<b>'.$d4.'</b> on cash sale quote <a href=../sales/quotation.php?quote_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> changed from <b>'.$d2a. '</b> to <b>'.$d3a.'</b>';
        break;
		
	case 48:
        $action = 'Edit cash sale quotation';
		$table_name = $_SESSION['db_prefix']."_quotations";
		$quote_query = "SELECT  cust_id  FROM $table_name WHERE quote_no = '".$d1."' ";
		$quote_result = mySQL_query($quote_query);
		$cust_id = mysql_result($quote_result, 0, "cust_id");
				
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
		
				
		$details = '<b>'.$d4.'</b> on cash sale quotation <a href=../sales/quotation.php?quote_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> changed from <b>'.$d2. '</b> to <b>'.$d3.'</b>';
        break;		
		
		case 49:
        $action = 'Edit final quotation';
		$table_name = $_SESSION['db_prefix']."_quotations";
		$quote_query = "SELECT  cust_id  FROM $table_name WHERE quote_no = '".$d1."' ";
		$quote_result = mySQL_query($quote_query);
		$cust_id = mysql_result($quote_result, 0, "cust_id");
				
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
		
				
		$details = '<b>'.$d4.'</b> on cash sale quoatation <a href=../sales/quotation.php?quote_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> changed from <b>'.$d2. '</b> to <b>'.$d3.'</b>';
        break;	
		
	case 50:
        $action = 'Add product to quotation';
		$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
		$quote_query = "SELECT  cust_id  FROM $table_name WHERE job_no = '".$d1."' ";
		$quote_result = mySQL_query($quote_query);
		$cust_id = mysql_result($quote_result, 0, "cust_id");
				
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
		
				
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$d2."'  ";
		$name_result = mySQL_query($name_query);
		$d2a = mysql_result($name_result, 0, "item_name");	
		
		$details = '<b>'.$d4.' '.$d2a.'</b> were added to cash sale quoatation <a href=../sales/quotation.php?quote_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> ';
        break;	
		
		case 51:
        $action = 'Delete product from quotation';
		$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
		$quote_query = "SELECT  cust_id  FROM $table_name WHERE job_no = '".$d1."' ";
		$quote_result = mySQL_query($quote_query);
		$cust_id = mysql_result($quote_result, 0, "cust_id");
				
		$table_name2 = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
				
		$details = '<b>'.$d4.'</b> deleted from cash sale quotation <a href=../sales/quotation.php?quote_no='.$d1.'><b>'.$d1.'</b></a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'><b>'.$cust_name.'</b></a> ';
        break;	
		
		case 52:
        $action = 'Cash sale quotation';
		$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_num = mysql_num_rows($cust_name_result);
		if ($cust_name_num!=0) {
		$cust_name = mysql_result($cust_name_result, 0, "comp_name");
		}
		else {
			$cust_name='';
		}
		
		$details = '<a href=../sales/quotation.php?quote_no='.$d1.'>Q'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$d2.'>'.$cust_name.'</a>';
        break;
		
		case 54:
        $action = 'Cash sale authorised';
		    $table_name = $_SESSION['db_prefix']."_captured_cash_sale";
			$job_query = "SELECT cust_id, project_no, project_place FROM $table_name WHERE job_no = '".$d1."' ";
			$job_result = mySQL_query($job_query);
			$job_num = mysql_num_rows($job_result);
							
			$cust_id = mysql_result($job_result, 0, "cust_id");
			$project_no = mysql_result($job_result, 0, "project_no");
			$project_place = mysql_result($job_result, 0, "project_place");
				
			$table_name2 = $_SESSION['db_prefix']."_customer";
			$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
			$customer_result = mySQL_query($customer_query);
			$customer_num = mysql_num_rows($customer_result);	
			$a1 = mysql_result($customer_result, 0, "account_type");
			if ($a1=="corporate") { 
				$name = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}
			
		$details = '<a href=../production/authorisation.php?job_no='.$d1.'>J'.$d1.'</a> for <a href=../sales/customer_details.php?cust_id='.$cust_id.'>'.$name.'</a> '.$project_no.' '.$project_place;
        break;	
		
		case 55:
        $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;
			
		
}
	
	?>
  
  
  <tr>
    <td width="67" align="left"><?php echo date("d-m-Y", strtotime($b1 )) ?></td>
    <td width="57" align="left"><?php echo date("H:i", strtotime($b1 ))  ?></td>
    <td width="64" align="left"><?php echo $b2 ?></td>
    <td width="94" 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>