<?php
include("../include/session.php");
if(!$session->logged_in) {
	header("Location: ../main.php");
}
else {
	include("../storeHeader.php");
	
	require_once('classes/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	$cat = mysql_real_escape_string($_GET['cat']);
	
	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-d");
	}
	
		
	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)));
	
	$q = "SELECT * FROM nam_stockloss WHERE category = '".$cat."' AND date >= '$start' AND date <= '$end'";
	$r = execsql($q);
	$n = mysql_num_rows($r);
	$i = 0;
	
	$start = strtotime($start);
	$end = strtotime($end);
	$q = "SELECT productId, productName FROM nam_products";
	$resultPro = execsql($q);
	$numPro = mysql_num_rows($resultPro);
	$j = 0;
	$proArray = array ();
	while ($j < $numPro) {
		$proArray[mysql_result($resultPro, $j, "productId")] = mysql_result($resultPro, $j, "productName");
		$j++;
	}


	?> 
    
<link rel="stylesheet" href="../css/dhtmlwindow.css" type="text/css" />
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<link rel="stylesheet" href="../css/modal.css" type="text/css" />
<script type="text/javascript" src="../css/modal.js"></script>


<script type="text/javascript" src="../css/tablesort.js"></script>
<style type="text/css">
//<!--
//.style7 {font-size: 9}
//-->
//</style>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<div class="headerbox2">Stock Loss</div><br /><br />
<div id="coloredContainer">
<form action="stockLoss.php" method="get" name="dateform" class="today" id="dateform">
        from:
        &nbsp;&nbsp;
        <?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
	 	$myCalendar->writeScript();?>
        &nbsp;&nbsp;to:&nbsp;&nbsp;
        <?php
		$myCalendar = new tc_calendar("date2", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($de, $me, $ye);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2000, 2030);
		$myCalendar->writeScript();?>
        &nbsp;&nbsp;
<label>
  <input name="button5" type="submit" class="smalllinkbuttonblue"  id="button5" value="Go" />
  <input name="date" type="hidden" id="date" value="1" />
</label>

<input name="cat" type="hidden" id="cat" value="<?php echo $cat ?>" />
</form>
</div><br />


<table width="515" class="sortable-onload-0r rowstyle-alt no-arrow " id="tableList">
    <thead>
      <tr>
        <th width="110"  class="sortable-date-dmy">Date</th>
        <th width="86" class="sortable-text">Amount</th>
        <th width="266" class="sortable-text">Product Name</th>
      </tr>
    </thead>
    <tbody>
    
    <?php
$amount=0;
	while ($i < $n) {
		$b1 = mysql_result($r, $i, "productId");
		  $productQuery = "SELECT productId, productName,category FROM nam_products WHERE productId='".$b1."' ";
	      $productResult = execsql($productQuery);
		  $b1a = mysql_result($productResult, 0, "productName");
		  $b1b = mysql_result($productResult, 0, "category");
		$b2 = mysql_result($r, $i, "amount");
		$b3 = mysql_result($r, $i, "date");

		$amount += $b2;
?>
     <tr class="alt">
        <td><?php echo date("d-m-Y", strtotime($b3 ))?></td>
        <td align="right"><?php echo $b2?></td>
        <td><a href="stockMovement.php?productIdSelect=<?php echo $b1 ?>&date=0" onmouseover="ddrivetip('<?php echo $b1a."<br>(".$b1.")<br>".$b1b."<br><b>Click for item history</b>"?>','lightyellow', 150)" onmouseout="hideddrivetip()">
              <?php echo $b1a?>
            </a></td>
       
        </tr> 
 

    <?php    $i++;
		
	}?>
	
    	</table>
<p align="right">
<br />
	
	<?php
	include("../footer.php");
}
//

?>