<?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) );	
	
   
    $customerQuery = "SELECT CompanyName,  CustNo FROM nam_customer WHERE CustNo=$search";
	$customerResult = execsql($customerQuery);
	$customerNum = mysql_num_rows($customerResult);
	$customer_name = mysql_result($customerResult, 0, "CompanyName");
  
   
	?>
<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 sale per customer</div>
      <br />
      <div id="coloredContainer"><font size="3"><b><?php echo "Product sales 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 />
    <br /></td>
    <!--center of tableMain-->
  </tr>
  <tr>
    <td><div id="coloredContainer">
  <br />
  
  <?php
  
     $idQuery = "SELECT  CustNo FROM  nam_customer WHERE active=1  ORDER BY CompanyName ";
	 $idResult = execsql($idQuery);
	 $idNum = mysql_num_rows($idResult);
     $x=0;
	 $alltot=0;
	 while ($x<$idNum) {
		 $a = 'CustNo';	
		 ${$a.$x} = mysql_result($idResult, $x, "CustNo");
		
		 $x++;
	 }
   
				$z=0;
				
				while ($z<$idNum) {
				
					$custNoSearch = ${'CustNo'.$z};
			 
			  if ($search!="0") {  
					$custNoSearch = $search;
					$idNum=1;
				}
				
				$selectQuery = "SELECT productId, client, SUM(quantity) AS summedQuantity, price FROM nam_orders  WHERE CustNo ='".$custNoSearch."' AND date > '".$start."' AND date <= '".$end."' GROUP BY productId, client ORDER BY client";
				
	$selectResult = execsql($selectQuery);
	$selectNum = mysql_num_rows($selectResult);
	
	//$b0 = mysql_result($selectResult, $i, "productId");
	
			
			
			
			$nameQuery = "SELECT companyName FROM nam_customer WHERE CustNo = ".$custNoSearch;
			$nameResult = execsql($nameQuery);
	        $b4 = mysql_result($nameResult, 0, "companyName");
	
				?>
      <table width="576" class="sortable-onload-0 rowstyle-alt no-arrow " id='tableList' >
        <thead>
          <tr>
            <th colspan="4" align="center" class="sortable-text"><font size="4"><b><font size="4"><b><?php echo $b4?></b></font></b></font></th>
          </tr>
        </thead>
        <tbody>
          <?php
  $i=0;
  $subtot=0;
  
  if ($selectNum==0) {
	?>  
  <tr>
    <td width="200"><font color="red">No orders for this period.</font></td>
    <td width="98"</td>
    <td width="68"</td>
    <td width="68"</td>
  </tr>
  <?php }
  else {
  
  while ($i < $selectNum) {
	  
	  		 
			$b1 = mysql_result($selectResult, $i, "productId");
			
			$productQuery = "SELECT productName FROM nam_products WHERE productId = ".$b1;
			$productResult = execsql($productQuery);
	        $b5 = mysql_result($productResult, 0, "productName");
			$b2 = mysql_result($selectResult, $i, "summedQuantity"); 
			$b3 = mysql_result($selectResult, $i, "price");
			$b6 = $b2*$b3;
			$subtot = $subtot + $b6;
			$alltot = $alltot + $b6;
  
  ?>
  <tr>
    <td width="200"><?php echo $b5 ?></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($b6 , 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++;
				}
				
				?>
      
 <table width="575" border="0" cellspacing="0" cellpadding="0" id="tableList">
        <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 />
    <p>&nbsp; </p></td>
  </tr>
</table>
<!--end of tableMain-->
<p>
  <?php
	}
	include("../footer.php");

?>
</p>
</p>
