<?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']);
	
	$product = mysql_real_escape_string($_GET['product']);
	$caption_message = "";
	if ($product=="double") {$caption_message = "Orders with DOUBLE ROMAN products" ; }
	if ($product=="slate") {$caption_message = "Orders with SLATE products" ; }
	if ($product=="venetian") {$caption_message = "Orders with VENETIAN products" ; }
	if ($product=="acc") {$caption_message = "Orders with Accessories" ; }
	
	$cust_no = mysql_real_escape_string($_GET['search_select']);	
	
	$start = mysql_real_escape_string($_GET['date1']);
    $end = mysql_real_escape_string($_GET['date2']);
	
			
	$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) );	
	
    $suppliersQuery = "SELECT CompanyName,  CustNo FROM customer WHERE active=" . 1 ." ORDER BY CompanyName ASC";
	$suppliersResult = execsql($suppliersQuery);
	$suppliersNum = mysql_num_rows($suppliersResult);
	$w = 0;
	$arrId = array ();
	$arrName = array ();
	while ($w < $suppliersNum) {
		$arrId[$w] = mysql_result($suppliersResult, $w, "CustNo");
		$arrName[$w] = mysql_result($suppliersResult, $w, "CompanyName");
		$w++;
	}
    
   if ($cust_no!="0") {
    $customerQuery = "SELECT CompanyName,  CustNo FROM customer WHERE CustNo=$cust_no";
	$customerResult = execsql($customerQuery);
	$customerNum = mysql_num_rows($customerResult);
	$customer_name = mysql_result($customerResult, 0, "CompanyName");
  }
  else {
	  $customer_name= "all";
  }
	?>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../css/tablesort.js"></script>
<table id="tableMain" width="595">
  <!--begin of tableMain-->
  <tr>
    <td ><div class="headerbox2"> Product sales</div><br /></td>
    <!--center of tableMain-->
  </tr>
  <tr>
    <td><div id="coloredContainer"><font size="3"><b><?php echo $caption_message. " for ".$customer_name ?></b></font><br />
<b>Period from:&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($start )) ?>&nbsp;&nbsp;&nbsp; to&nbsp;&nbsp;&nbsp; <?php echo date("d-m-Y", strtotime($end )) ?></b></div>
  <br />
<?php
   if ($product=="double") {
	   if ($cust_no=='0') {
     		$doubleQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_double   FROM  products, orders WHERE products.productName LIKE '%DOUBLE%' AND products.productId = orders.productId AND orders.date >= '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
	   }
	   else {
		  $doubleQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_double   FROM  products, orders WHERE products.productName LIKE '%DOUBLE%' AND products.productId = orders.productId AND orders.CustNo = '".$cust_no."' AND orders.date > '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
	   }
		//echo $doubleQuery;
	$doubleResult = execsql($doubleQuery);
	$doubleNum = mysql_num_rows($doubleResult);
   }
   
   if ($product=="slate") {
	   if ($cust_no=='0') {
     		$slateQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_slate   FROM  products, orders WHERE products.productName LIKE '%SLATE%' AND products.productId = orders.productId AND orders.date >= '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
	   }
	   else {
		 $slateQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_slate   FROM  products, orders WHERE products.productName LIKE '%SLATE%' AND products.productId = orders.productId AND orders.CustNo = '".$cust_no."' AND orders.date > '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";  
	   }
			
		//echo $slateQuery;
	$slateResult = execsql($slateQuery);
	$slateNum = mysql_num_rows($slateResult);
   }
   
   if ($product=="venetian") {
	   if ($cust_no=='0') {
     		$venetianQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_venetian   FROM  products, orders WHERE products.productName LIKE '%VENETIAN%' AND products.productId = orders.productId AND orders.date >= '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
	   }
	   else {
		 $venetianQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_venetian   FROM  products, orders WHERE products.productName LIKE '%VENETIAN%' AND products.productId = orders.productId AND orders.CustNo = '".$cust_no."' AND orders.date > '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";  
	   }
			
	//	echo $venetianQuery;
	$venetianResult = execsql($venetianQuery);
	$venetianNum = mysql_num_rows($venetianResult);
	//echo '<br>=============='.$venetianNum;
   }
   
   if ($product=="acc") {
	      if ($cust_no=='0') {
     		$accQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_acc   FROM  products, orders WHERE products.productName NOT LIKE '%SLATE%' AND products.productName NOT LIKE '%VENETIAN%' AND products.productName NOT LIKE '%DOUBLE%' AND products.productId = orders.productId AND products.category='Tiles and Accessories' AND orders.date >= '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
		  }
		  else {
			$accQuery = "SELECT products.productName, orders.productId, orders.CustNo, orders.orderNo,  SUM(orders.quantity) AS summed_quantity_acc   FROM  products, orders WHERE products.productName NOT LIKE '%SLATE%' AND products.productName NOT LIKE '%VENETIAN%'  AND products.productName NOT LIKE '%DOUBLE%' AND products.productId = orders.productId AND products.category='Tiles and Accessories' AND orders.CustNo = '".$cust_no."' AND orders.date >= '".$start."' AND orders.date <= '".$end."'  GROUP BY orderNo";
		  }
				//echo $accQuery;
	$accResult = execsql($accQuery);
	$accNum = mysql_num_rows($accResult);
   }
	?>
  <table width="576" class="sortable-onload-3r rowstyle-alt no-arrow " id="tableList"  >
  <tr>
      <th class="sortable-numeric">Order No</th>
      <th class="sortable-text"> Customer</th>
      <th class="sortable-numerict"> Qty</th>
      <th class="sortable-date-dmy">Date</th>
    </tr>
    <?php 
   if ($product=="double") {
   $i=0;
   while ($i<$doubleNum) {
	   $b0 =  mysql_result($doubleResult, $i, "summed_quantity_double");
	   $b1 = mysql_result($doubleResult, $i, "orders.orderNo");
	   $b2a = mysql_result($doubleResult, $i, "orders.CustNo");
	   $nameQuery = "SELECT companyName FROM customer WHERE CustNo = ".$b2a;
	   $nameResult = execsql($nameQuery);
	   $b2 = mysql_result($nameResult, 0, "companyName");
	   
	   $orderQuery = "SELECT orders.quantity, products.productName, orders.reserved, orders.complete, orders.delCol, orders.date FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b1 ";
	$orderResult = execsql($orderQuery);
	
	$j = 0;
	$o_num = mysql_num_rows($orderResult);
	$toolTipText = "";
	
	while ($j < $o_num) {
	    $o0 = mysql_result($orderResult, $j, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName"); 
		$o2 = mysql_result($orderResult, $j, "orders.date"); 
		
	$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
	
	$j++;
		}
   
	
	   ?>
    
    <tr>
      <td width="115"><a href="displayOrder.php?OrderNoText=<?php echo $b1?>&edit=0" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";>
        <?php echo $b1?>
      </a></td>
      <td width="259" align="left"><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"></a><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"></a><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"><?php echo $b2 ?></a></td>
      <td width="90" align="right"><?php echo $b0 ?></td>
      <td width="92" align="left"><?php echo date("d-m-Y ", strtotime($o2 )) ?></td>
      </tr>
    <?php
  $i++;
}
   }?>
    
  <?php
if ($product=="acc") {
   $i=0;
   while ($i<$accNum) {
	   $b0 =  mysql_result($accResult, $i, "summed_quantity_acc");
	   $b1 = mysql_result($accResult, $i, "orders.orderNo");
	   $b2a = mysql_result($accResult, $i, "orders.CustNo");
	   $nameQuery = "SELECT companyName FROM customer WHERE CustNo = ".$b2a;
	   $nameResult = execsql($nameQuery);
	   $b2 = mysql_result($nameResult, 0, "companyName");
	   
	   $orderQuery = "SELECT orders.quantity, products.productName, orders.reserved, orders.complete, orders.delCol, orders.date FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b1 ";
	$orderResult = execsql($orderQuery);
	
	$j = 0;
	$o_num = mysql_num_rows($orderResult);
	$toolTipText = "";
	
	while ($j < $o_num) {
	    $o0 = mysql_result($orderResult, $j, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName"); 
		$o2 = mysql_result($orderResult, $j, "orders.date"); 
		
	$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
	
	$j++;
		}
   
	
	   ?>
    <tr>
      <td width="115"><a href="displayOrder.php?OrderNoText=<?php echo $b1?>&edit=0" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";>
        <?php echo $b1?>
      </a></td>
      <td width="259" align="left"><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"><?php echo $b2 ?></a></td>
      <td width="90" align="right"><?php echo $b0 ?></td>
      <td width="92" align="left"><?php echo date("d-m-Y", strtotime($o2 )) ?></td>
      </tr>
    <?php
  $i++;
}
   }?>  
    
    <?php
if ($product=="slate") {
   $i=0;
   while ($i<$slateNum) {
	   $b0 =  mysql_result($slateResult, $i, "summed_quantity_slate");
	   $b1 = mysql_result($slateResult, $i, "orders.orderNo");
	   $b2a = mysql_result($slateResult, $i, "orders.CustNo");
	   $nameQuery = "SELECT companyName FROM customer WHERE CustNo = ".$b2a;
	   $nameResult = execsql($nameQuery);
	   $b2 = mysql_result($nameResult, 0, "companyName");
	   
	   $orderQuery = "SELECT orders.quantity, products.productName, orders.reserved, orders.complete, orders.delCol, orders.date FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b1 ";
	$orderResult = execsql($orderQuery);
	
	$j = 0;
	$o_num = mysql_num_rows($orderResult);
	$toolTipText = "";
	
	while ($j < $o_num) {
	    $o0 = mysql_result($orderResult, $j, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName");
		$o2 = mysql_result($orderResult, $j, "orders.date"); 
		
	$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
	
	$j++;
		}
	   ?>
 
       

    <tr>
      <td width="115"><a href="displayOrder.php?OrderNoText=<?php echo $b1?>&edit=0" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";>
        <?php echo $b1?>
      </a></td>
      <td width="259" align="left"><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"><?php echo $b2 ?></a></td>
      <td width="90" align="right"><?php echo $b0 ?></td>
      <td width="92" align="left"><?php echo date("d-m-Y", strtotime($o2 )) ?></td>
      </tr>
    <?php
  $i++;
}
   }?>  
    

 <?php
if ($product=="venetian") {
   $i=0;
   while ($i<$venetianNum) {
	   $b0 =  mysql_result($venetianResult, $i, "summed_quantity_venetian");
	   $b1 = mysql_result($venetianResult, $i, "orders.orderNo");
	   $b2a = mysql_result($venetianResult, $i, "orders.CustNo");
	   $nameQuery = "SELECT companyName FROM customer WHERE CustNo = ".$b2a;
	   $nameResult = execsql($nameQuery);
	   $b2 = mysql_result($nameResult, 0, "companyName");
	   
	   $orderQuery = "SELECT orders.quantity, products.productName, orders.reserved, orders.complete, orders.delCol, orders.date FROM orders, products WHERE (orders.productId = products.productID)  AND orders.orderNo = $b1 ";
	$orderResult = execsql($orderQuery);
	
	$j = 0;
	$o_num = mysql_num_rows($orderResult);
	$toolTipText = "";
	
	while ($j < $o_num) {
	    $o0 = mysql_result($orderResult, $j, "orders.quantity");
		$o1 = mysql_result($orderResult, $j, "products.productName");
		$o2 = mysql_result($orderResult, $j, "orders.date"); 
		
	$toolTipText =  $toolTipText . $o0." ".$o1."<br>";
	
	$j++;
		}
	   ?>
 
       

    <tr>
      <td width="115"><a href="displayOrder.php?OrderNoText=<?php echo $b1?>&edit=0" onmouseover="ddrivetip('<?php echo "<b>on order: </b><br>" . $toolTipText ?>','', 300)" onmouseout="hideddrivetip()";>
        <?php echo $b1?>
      </a></td>
      <td width="259" align="left"><a href="ordersCustomer.php?search_select=<?php echo $b2a ?>&date=0"><?php echo $b2 ?></a></td>
      <td width="90" align="right"><?php echo $b0 ?></td>
      <td width="92" align="left"><?php echo date("d-m-Y", strtotime($o2 )) ?></td>
      </tr>
    <?php
  $i++;
}
   }?>  

   
  </table>
    <br />

 
	
         
      <br />
      
 
          
      <br /></td>
  </tr>
</table>
<!--end of tableMain-->

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

?>