<?php
include("../include/session.php");
if( !$session->isAdmin()){
	//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']);
	
		
	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) );	
	
    $cust_no = mysql_real_escape_string($_GET['search_select']);	
    
    $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">
  <form action="ordersTotal.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(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="date" type="hidden" id="date" value="1" />
        <br />
      </label>
 Select customer:
  
  <select name="search_select" id="search_select" >
    <option value="<?php echo $cust_no ?>" selected="selected"><?php echo $customer_name ?></option>
    <option value="0">All</option>
    <?php
    	for ($i = 0; $i < count($arrId); $i++) {
		echo "<option value=" . "\"" . $arrId[$i] . "\"" . ">" . $arrName[$i] . "</option>";
		$_SESSION['CustomerName'] = $arrName[$i];
		//foreach($arrId as $key=>$value){
			//echo "<option value=\"delivery.php?productId=".$arrId[$i]."&cat=$cat&formEnabled2=1&date=$date" . "\"" . ">" . $arrName[$i] . "</option>";	
	    }
     ?>
  </select>
  </span>
  <input name="button" type="submit" class="smalllinkbuttonblue" id="button" value="Go" />
  </form></div><br />


      <div id="coloredContainer"><font size="3"><b>Sales for <?php echo $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 ($cust_no=='0') {
     	$doubleQuery = "SELECT products.productName, orders.productId,  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 productName";
		//echo $doubleQuery;
  }
  else {
	    $doubleQuery = "SELECT products.productName, orders.productId,  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 productName";
  }
				//echo $doubleQuery;
	$doubleResult = execsql($doubleQuery);
	$doubleNum = mysql_num_rows($doubleResult);
	
	
 if ($cust_no=='0') {
	$slateQuery = "SELECT products.productName, orders.productId,  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 productName";
 }
 else {
	$slateQuery = "SELECT products.productName, orders.productId,  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 productName";
 }
				//echo $slateQuery;
	$slateResult = execsql($slateQuery);
	$slateNum = mysql_num_rows($slateResult);
	
if ($cust_no=='0') {
	$venetianQuery = "SELECT products.productName, orders.productId,  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 productName";
 }
 else {
	$venetianQuery = "SELECT products.productName, orders.productId,  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 productName";
 }
				//echo $venetianQuery;
	$venetianResult = execsql($venetianQuery);
	$venetianNum = mysql_num_rows($venetianResult);	
	
	
if ($cust_no=='0') {	
	$accQuery = "SELECT products.productName, orders.productId,  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 productName";
}else {
	$accQuery = "SELECT products.productName, orders.productId,  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 productName";
}
				//echo $accQuery;
	$accResult = execsql($accQuery);
	$accNum = mysql_num_rows($accResult);
	
	?>
	
    <table width="576" class="sortable-onload-0 rowstyle-alt no-arrow " id="tableList"  >
                
          
   <?php 
   $i=0;
   $b0=0;
   while ($i<$doubleNum) {
	   $b0 = $b0 + mysql_result($doubleResult, $i, "summed_quantity_double");
	 $i++;
   } 
   $j=0;
   $b1=0;
   while ($j<$slateNum) {
	   $b1 = $b1 + mysql_result($slateResult, $j, "summed_quantity_slate");
	 $j++;
   }  
   $m=0;
   $b3=0;
   while ($m<$venetianNum) {
	   $b3 = $b3 + mysql_result($venetianResult, $m, "summed_quantity_venetian");
	 $m++;
   }  
   $k=0;
   $b2=0;
   while ($k<$accNum) {
	   $b2 = $b2 + mysql_result($accResult, $k, "summed_quantity_acc");
	 $k++;
   }  
	   ?>
   <tr >
     <th align="left">Product group</th>
     <th align="left">Sales /period</th>
     <th align="right">&nbsp;</th>
     <th align="right">&nbsp;</th>
   </tr>
   <tr onclick="document.location='products_per_order.php?product=double&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>' "  style="cursor:pointer">
    <td width="200">DOUBLE ROMAN</td>
    <td width="98" align="right"><a href="products_per_order.php?product=double&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>"><?php echo $b0 ?></a>      </t></td>
    <td width="68" align="right">&nbsp;</td>
    <td width="68" align="right">&nbsp;</td>
  </tr>
  <tr onclick="document.location='products_per_order.php?product=slate&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>' "  style="cursor:pointer">
    <td>SLATE</td>
    <td align="right"><a href="products_per_order.php?product=slate&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>"><?php echo $b1 ?></a></td>
    <td align="right">&nbsp;</td>
    <td align="right">&nbsp;</td>
  </tr>
  <tr onclick="document.location='products_per_order.php?product=venetian&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>' "  style="cursor:pointer">
    <td>VENETIAN</td>
    <td align="right"><a href="products_per_order.php?product=venetian&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>"><?php echo $b3 ?></a></td>
    <td align="right">&nbsp;</td>
    <td align="right">&nbsp;</td>
  </tr>
  <tr onclick="document.location='products_per_order.php?product=acc&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>' "  style="cursor:pointer">
    <td>Accessories</td>
    <td align="right"><a href="products_per_order.php?product=acc&date=1&date1=<?php echo $start ?>&date2=<?php echo $end?>&search_select=<?php echo $cust_no ?>"><?php echo $b2 ?></a></td>
    <td align="right">&nbsp;</td>
    <td align="right">&nbsp;</td>
  </tr>
    
      </table>
    <br />

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

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

?>