<?php
include("../include/session.php");
if(!$session->logged_in) {
	header("Location: ../main.php");
}
else {
	include("../storeHeader.php");
//	include("../include/db.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) );	 
	
		 
		 	
		$displayQuery = "SELECT nam_itemhistory.productId, SUM(nam_itemhistory.quantity) AS totalReceived, nam_products.productId, nam_products.productName FROM nam_itemhistory, products WHERE nam_itemhistory.date >= '$start' AND nam_itemhistory.date <= '$end'  AND nam_products.productId = nam_itemhistory.productId AND nam_products.category = '$cat' AND nam_itemhistory.details LIKE 'delivery%' GROUP BY nam_products.productName, nam_itemhistory.productId, nam_products.productName ORDER BY nam_products.productName ASC";
		
		
		$displayResult = execsql($displayQuery);
		$i = 0;
		$num = mysql_num_rows($displayResult);
		
		if ($num==0) {
		  $message="No records to show"; 
		   }
		else  {
		  $message="End of list";
		  }
		  		
		  
		?> 

<link
	href="calendar.css" rel="stylesheet" type="text/css" />
<script
	language="javascript" src="../include/calendar.js"></script>
    <script type="text/javascript" src="../css/tablesort.js"></script>
	<style type="text/css">
<!--
.style2 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: medium;
	color: #000000;
}
-->
    </style>
	<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
    </script>
<div class="headerbox2">Stock Received</div>
    <br />
    <p class="main"><font size="3"><b> <?php echo $cat?></b></font></p><br />
<br />
<p class="main">
  <label class="std">other lists:</label>
  &nbsp;&nbsp;
  <input name="button6" type="button" class="linkbutton" id="button6" onclick="MM_goToURL('parent','stockReceived.php?date=0&amp;cat=Tiles and Accessories&amp;productId=');return document.MM_returnValue" value="Tiles &amp; Acc." />
  <input name="button" type="button" class="linkbutton" id="button" onclick="MM_goToURL('parent','stockReceived.php?date=0&amp;cat=Direct Materials&amp;productId=');return document.MM_returnValue" value="Direct Materials" />
  <input name="button3" type="button" class="linkbutton" id="button3" onclick="MM_goToURL('parent','stockReceived.php?date=0&amp;cat=Store Item&amp;productId=');return document.MM_returnValue" value="Store Items" />
</p>
<br /><br />
    <div id="coloredContainer">
      <form action="stockDelivered.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="Select" />
          <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 id="tableList" class="sortable-onload-0 rowstyle-alt no-arrow " >
	<thead>
		<tr>
			<th width="176" class="sortable-textc"><div align="left">Product</div></th>
            <th width="121" class="sortable-text"><div align="left">Supplier</div></th>
          <!--			<th class="sortable-text">Product Name</th>-->
			<th width="63" class="sortable-numeric">Qty</th>
			<th width="123" class="sortable-numeric">Unit Price</th>
		    <th width="108" class="sortable-numericy">Total </th>
		</tr>
	</thead>
	<tbody>
	<?php
    $b9=0;
	while($i < $num){
		$b1 = mysql_result($displayResult, $i, "productId");
		  $productQuery = "SELECT productName, suppliersName, price FROM nam_products WHERE productId = $b1 " ;
		  $productResult = execsql($productQuery);
		  $b1a = mysql_result($productResult, 0, "productname");
		  $b1b = mysql_result($productResult, 0, "suppliersName");
		  $b1c = mysql_result($productResult, 0, "price");
		$b3 = mysql_result($displayResult, $i, "totalReceived");
		
		//$b8 = mysql_result($displayResult, $i, "reference");
		
		$b7=$b3*$b1c;
		$b9 += $b7;
		?>
		<tr >
			<td ><a href="stockMovement.php?monthdisplay=0&cat=Tiles and Accessories&productIdSelect=<?php echo $b1a ?>" onmouseover="ddrivetip('<?php echo $b1."<br>(".$b1a.")<br>".$cat."<br><b>Click for item history</b>" ?>','lightyellow', 150)" onmouseout="hideddrivetip()";><?php echo $b1a?></a></td>

			<td align="left"><a href="stockMovement.php?monthdisplay=0&cat=Tiles and Accessories&productIdSelect=<?php echo $b1b ?>" onmouseover="ddrivetip('<?php echo $b1b ?>','lightyellow', 150)" onmouseout="hideddrivetip()";><?php echo $b1b?>  </a></td>
			<td align="right"><?php echo $b3?></td>
			<td align="right"><?php echo $b1c?></td>
		    <td align="right"><?php $b7=$b3*$b1c;  echo number_format($b7, 2, '.', ',')?> </td>
		</tr>
        
		<?php
		$i++;
		  
	}
	
	?>
	</tbody>
	<tfoot>
	    <tr >
          <td colspan="4"><div align="right">total:&nbsp;&nbsp;&nbsp;</div></td>
          <td><div align="right"><?php echo number_format($b9, 2, '.', ',')?></div></td>
        </tr> 
  </tfoot>
</table>
		
	<p><b><font color='#FF0000'>
    <?php  echo $message ?>
        </font></b></p>
	<p>
	  <?php
	include("../footer.php");
}
//

?>
	</p>
	