<?php
include("../include/session.php");
if( !$session->isAdmin() && !$session->isRadstoneReceptionUser() ){
	//header("Location: ../Store/storeMenu.php");?>
<script language="javascript" type="text/javascript">
        <!--
		  window.location="denied.php";
        -->
       </script>
<?php 
}else{
	include("../storeHeader.php");
	
	require_once('classes/tc_calendar.php');
	$date = mysql_real_escape_string($_GET['date']);
	$search = $_GET['search_select'];
		
	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) );	
	

    
   
	?>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../css/tablesort.js"></script>
<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>
<table id="tableMain" width="595">
  <!--begin of tableMain-->
  <tr>
    <td ><div class="headerbox2"> Product orders</div>
      <div id="coloredContainer">
        <input name="button3" type="button" class="linkbutton" id="button3" onclick="MM_goToURL('parent','ordersCustomer.php?date=0&search_select=0');return document.MM_returnValue" value="order/customer" />
        <input name="button4" type="button" class="linkbutton" id="button4" onclick="MM_goToURL('parent','ordersProducts.php?date=0&search_select=DOUBLE%');return document.MM_returnValue" value="order/products" />
        <input name="button5" type="button" class="linkbutton" id="button5" onclick="MM_goToURL('parent','deliveriesProducts.php?date=0&search_select=DOUBLE%');return document.MM_returnValue" value="delivery/customer" />
      </div>
      
      <br />
      <div id="coloredContainer">
      <form action="ordersProducts.php" method="get">
      <label class="std">Select product</label>
        <select name="search_select" id="search_select">
          <option value="DOUBLE%">DOUBLE ROMAN</option>
          <option value="RAKE%">RAKE VERGE</option>
          <option value="SLATE%">SLATE</option>
          <option value="TAPERED RI%">TAPERED RIDGE</option>
          <option value="TAPERED HI%">TAPERED HIP STARTER</option>
          <option value="V RI%">V RIDGE</option>
          <option value="V HI%">V HIP STARTER </option>
          <option value="MONO%">MONO RIDGE</option>
          <option selected="selected"> </option>
        </select>
        <input name="button2" type="submit" class="linkbutton" id="button2" value="Select" />
        <input name="date" type="hidden" id="date" value="0" />
      </form>
      </div>
    <br /></td>
    <!--center of tableMain-->
  </tr>
  <tr>
    <td><div id="coloredContainer">
  <form action="deliveriesProducts.php" method="get" name="dateform" class="today" id="dateform">
    <span class="style7">
    from:
      &nbsp;&nbsp;
      <?php
		$myCalendar = new tc_calendar("date1", true);
		$myCalendar->setIcon("../img/iconCalendar.gif");
		$myCalendar->setDate($ds, $ms, $ys);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2009, 2020);
	 	$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(2009, 2020);
		$myCalendar->writeScript();?>
      &nbsp;&nbsp;
      <label>
        <input name="button" type="submit" class="smalllinkbuttonblue" id="button" value="Go" />
        <input name="date" type="hidden" id="date" value="1" />
      </label>
      <input name="CustNo" type="hidden" id="CustNo" value="" /> 
      <input name="search_select" type="hidden" id="search_select" value="<?php echo $search ?>" />
  </form></div><br />
  
  <?php
     $idQuery = "SELECT productId FROM nam_products  WHERE productName LIKE '".$search."' ORDER BY productName ";
	 $idResult = execsql($idQuery);
	 $idNum = mysql_num_rows($idResult);
     $x=0;
	 $alltot=0;
	 
	 while ($x<$idNum) {
		 $a = 'productId';	
		 ${$a.$x} = mysql_result($idResult, $x, "productId");
		
		 $x++;
	 }
   
				$z=0;
				while ($z<$idNum) {
				
				$productIdSearch = ${'productId'.$z};
				
				$selectQuery = "SELECT productId, client, SUM(quantity) AS summedQuantity, price FROM nam_orders  WHERE productId ='".$productIdSearch."' AND date > '".$start."' AND date <= '".$end."' GROUP BY productId, client ";
	$selectResult = execsql($selectQuery);
	$selectNum = mysql_num_rows($selectResult);
	
	//$b0 = mysql_result($selectResult, $i, "productId");
	if ($selectNum!=0) {
			
			$productQuery = "SELECT productName FROM nam_products WHERE productId = ".$productIdSearch;
			$productResult = execsql($productQuery);
	        $b4 = mysql_result($productResult, 0, "productName");
	
				?>
      <table width="576" class="sortable-onload-0 rowstyle-alt no-arrow " id="tabledisplay_2"  >
        <thead>
          <tr>
            <th colspan="4" align="center" class="sortable-text"><font size="4"><b><?php echo $b4?></b></font></th>
           
          </tr>
        </thead>
        <tbody>
          <?php
  $i=0;
  $subtot=0;
  while ($i < $selectNum) {
			 
			$b1 = mysql_result($selectResult, $i, "client");
			$b2 = mysql_result($selectResult, $i, "summedQuantity"); 
			$b3 = mysql_result($selectResult, $i, "price");
			$b5 = $b2*$b3;
			$subtot = $subtot + $b5;
			$alltot = $alltot + $b5;
  
  ?>
  <tr>
    <td width="200"><?php echo $b1 ?></td>
    <td width="98" align="right"><?php echo $b2 ?></td>
    <td width="68" align="right"><?php echo number_format($b3 , 2, '.', ', ')?></td>
    <td width="68" align="right"><?php echo number_format($b5 , 2, '.', ', ')?></td>
  </tr>
  <?php
  $i++;
  }
  ?>
  <tfoot>
  <tr>
     <td width="200"> </td>
     <td width="98"  > </td>
     <td width="68" >total:</td>
     <td width="68" align="right"><?php echo number_format($subtot , 2, '.', ', ')?></td>
  
  </tr>
  </tfoot>
        </tbody>
      </table><br />

 <?php
	}
  $z++;
				}
				
				?>
          <br />      
      <table width="575" border="0" cellspacing="0" cellpadding="0" id="tabledisplay_2">
        <tr>
          <td width="166">&nbsp;</td>
          <td width="313">All total:</td>
          <td width="96" align="right"><?php echo number_format($alltot , 2, '.', ', ')?></td>
        </tr>
      </table>
      <br />
      
 
          
      <br /></td>
  </tr>
</table>
<!--end of tableMain-->

  <?php
	}
	include("../footer.php");

?>