<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 
include ("../navigation/header.php");

$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {  
	
$cust_id = $_GET['cust_id'];	

include ("../navigation/store_sidebar.php");

$table_name = $_SESSION['db_prefix']."_customer";
    $position_query = "SELECT cust_id, comp_name, name_2 FROM $table_name ORDER BY comp_name ASC";
	$arrId3 = array ();
	$arrName3 = array ();
	$position_result = execsql($position_query);
	if ($position_result!="") {
	$position_num = mysql_num_rows($position_result);
	$k = 0;
	while ($k < $position_num) {
		$arrId3[$k] = mysql_result($position_result, $k, "cust_id");
		$arrName3[$k] = mysql_result($position_result, $k, "comp_name").', '.mysql_result($position_result, $k, "name_2");
		$k++;
	}
	}

if ($cust_id!=0) {

$table_name2 = $_SESSION['db_prefix']."_customer";
$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
$customer_result = mySQL_query($customer_query);
$customer_num = mysql_num_rows($customer_result);	
$account_type = mysql_result($customer_result, 0, "account_type");
				if ($account_type=="corporate") { 
					$cust_name = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
}
else {
	$cust_id = 0;
	$cust_name = 'All';
	$cust_query_line = '>';
}
	
		
?>
<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">

<!--jscript-->
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

<?php
include('../include/period_select_date.php');

if ($cust_id!=0) {
$table_name2 = $_SESSION['db_prefix']."_order_lines";
$order_query = "SELECT store_item_id, order_no, order_date, cust_id, quantity, price FROM $table_name2 WHERE cust_id = '".$cust_id."' AND order_date >= '".$start."' AND order_date <= '".$end."'  ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);	
}
else {
$table_name2 = $_SESSION['db_prefix']."_order_lines";
$order_query = "SELECT store_item_id, order_no, order_date, cust_id, quantity, price FROM $table_name2 WHERE order_date >= '".$start."' AND order_date <= '".$end."'  ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);	
}

if ($cust_id!=0) {
		$table_name = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT status, account_type, comp_name, name_2, title, bag_box, box_no, postal_place, phone, cell_1  FROM $table_name WHERE cust_id = '".$cust_id."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);
		
		$type = mysql_result($customer_result, 0, "account_type");
			$b2a = mysql_result($customer_result, 0, "comp_name");
 			$b2b = mysql_result($customer_result, 0, "bag_box");
			$b2c = mysql_result($customer_result, 0, "box_no");
			$b2d = mysql_result($customer_result, 0, "postal_place");
			$b2e = mysql_result($customer_result, 0, "phone");
			$b2f = mysql_result($customer_result, 0, "cell_1");
			$b2g = mysql_result($customer_result, 0, "status");
			$b2h="";
			
			
$customerTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Company:</td><td width=175>".$b2a."</td></tr><tr><td>Address:</td><td>".$b2b."&nbsp;".$b2c."<br>".$b2d."</td></tr><tr><td>Phone:</td><td>".$b2e."</td></tr><tr><td>Contact:</td><td>".$b2f."</td></tr></table><br><b>Click for details</b>";		
		
		
}
else { 
		$table_name = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT status, account_type, comp_name, name_2, title, bag_box, box_no, postal_place, phone, cell_1  FROM $table_name ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);
		
		$b2a = 'All';
$customerTT ="Showing sales for all customers";
			
			
}				
				


?>

 
       
</head>

<body>

<div class="main_frame">
 
<div class="page_head_frame">
    <label class="label_page_head">Customer sales </label>
</div>

<div class="page_middle_frame">

<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "420px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "asc" ]],
					"aoColumns": [
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 7},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 8},
								{"iDataSort": 9},
								{"bVisible": false},
								{"bVisible": false},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
       
        
 
         <?php
$page='customer_ordered_list.php';
$reference_name='cust_id';
$reference= $cust_id;
$reference_name_2='';
$reference_2='';
include('../include/period_select_block_2.php');
?>       </form>

<div><br>
<div class="page_inside_frame">
<form name="form1" id="form1"  action="" method="get">
Select customer:&nbsp;&nbsp;
<select name="comp_name_select" class="select300"  onchange="window.location.href= this.form.comp_name_select.options[this.form.comp_name_select.selectedIndex].value">
           <?php echo "<option value=customer_ordered_list.php?date=3&start_date=".$start."&end_date=".$end."&cust_id=0&show=0&item_type=>All</option>"; ?>
           <option value="<?php echo $cust_id ?>" selected><?php echo $cust_name ?></option>
            <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=customer_ordered_list.php?date=3&show=0&item_type=All&start_date=".$start."&end_date=".$end."&cust_id=".$arrId3[$k].">".$arrName3[$k]."</option>";
			
	}
?>
</select>
</form>
</div>
<br><br>
</div>
<div class="page_inside_frame">
<label class="label_std"><font size="+1">Sales to <a href="account_details.php?cust_id=<?php echo $cust_id ?>" onMouseover="ddrivetip('<?php echo $customerTT ?>', 250)"; onMouseout="hideddrivetip()"><?php echo $b2a ?></a></font></label>
          <table width="680" id="scroll_table" class="data_display" >
            <thead>
            <tr>
              <th width="168" >Customer</th>
              <th width="168" >Item</th>
              <th width="106" >Order date</th>
              <th width="86" >Order no</th>
              <th width="66" >Quantity</th>
              <th width="76" >Price</th>
              <th >Total<br /></th>
              
              <th width="26">&nbsp;</th>
              <th width="25">&nbsp;</th>
              <th width="23">&nbsp;</th>
            </tr>
            </thead>
            <?php 
$i=0;

$a9_total=0;
$a10_total=0;
$price=0;

while ($i<$order_num) {
	
	$a1 = mysql_result($order_result, $i, "order_no");
	$a2 = mysql_result($order_result, $i, "cust_id");
	
	$table_name = $_SESSION['db_prefix']."_customer";
		$customer_query = "SELECT status, account_type, comp_name, bag_box, box_no, postal_place, phone, cell_1  FROM $table_name WHERE cust_id = '".$a2."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);
		
			$a2a = mysql_result($customer_result, 0, "comp_name");
 			$a2b = mysql_result($customer_result, 0, "bag_box");
			$a2c = mysql_result($customer_result, 0, "box_no");
			$a2d = mysql_result($customer_result, 0, "postal_place");
			$a2e = mysql_result($customer_result, 0, "phone");
			$a2f = mysql_result($customer_result, 0, "cell_1");
			$a2g = mysql_result($customer_result, 0, "status");
			$a2h="";
			
			$customer2TT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Company:</td><td width=175>".$a2a."</td></tr><tr><td>Address:</td><td>".$a2b."&nbsp;".$a2c."<br>".$a2d."</td></tr><tr><td>Phone:</td><td>".$a2e."</td></tr><tr><td>Contact:</td><td>".$a2f."</td></tr></table><br><b>Click for details</b>";	
			
	$a3 = mysql_result($order_result, $i, "order_date");
	$a4 = mysql_result($order_result, $i, "store_item_id");
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$a4."'  ";
	$name_result = mySQL_query($name_query);
	$a4a = mysql_result($name_result, 0, "item_name");
	
	
	$a6 = mysql_result($order_result, $i, "quantity");
	
	$a9 = '';
	$a10 = mysql_result($order_result, $i, "price");
	
	$a9_sub = $a9 * $a6;
	$a9_total = $a9_total + $a9_sub;
	$a10_sub = $a10 * $a6;
	$a10_total = $a10_total + $a10_sub;
	
	
	$table_name = $_SESSION['db_prefix']."_order_lines";
	$line_query = "SELECT  store_item_id, quantity, price  FROM $table_name WHERE order_no = '".$a1."' ";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
	
	
	$l=0;
	$total=0;
	$line_text="";
	$products=mysql_result($line_result, 0, "store_item_id");
		
	while($l<$line_num) {
		$store_item_id = mysql_result($line_result, $l, "store_item_id");
		$quantity = mysql_result($line_result, $l, "quantity");
		$price = mysql_result($line_result, $l, "price");
		
		
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$store_item_name = mysql_result($name_result, 0, "item_name");
		$sub_total =  $quantity * $price;
		$total = $total + $sub_total;
			  
	   	$line_text = $line_text."<tr><td>".$store_item_name."</td><td>".$quantity."</td><td align=right>".number_format($price, 2, '.', ',')."</td><td align=right>".number_format($sub_total, 2, '.', ',')."</td></tr>";
	   $l++;
	}
	
	$orderTT ="<table id=table_tool_tip width=450 ><tr><td>Item</td><td>Quantity</td><td align=center>Price</td><td align=center>Total</td></tr>".$line_text."</table><br><b>Click for details</b>";
	
	
	?>
            <tr>
              <td align="left"  style="padding-left:0px;"><a href="../store/account_details.php?cust_id=<?php echo $a2 ?>" onMouseover="ddrivetip('<?php echo $customer2TT ?>', 250)"; onMouseout="hideddrivetip()"><?php echo $a2a ?></a></td>
              <td align="left"  style="padding-left:0px;"><a href="store_item_display.php?store_item_id=<?php echo $a4 ?>" onMouseover="ddrivetip('<b>Click for details</b>', 100)"; onMouseout="hideddrivetip()"><?php echo $a4a ?></a></td>
              <td align="left"><?php echo date("d-m-Y", strtotime($a3)) ?></td>
              <td align="left"><a href="../store/tile_order_display.php?order_no=<?php echo $a1 ?>" onMouseOver="ddrivetip('<?php echo $orderTT ?>', 450)"; onMouseOut="hideddrivetip()"><?php echo $a1 ?></a></td>
              <td align="right"><?php echo $a6 ?></td>
              <td align="right"><?php echo $a10 ?></td>
              <td width="86" align="right"><?php echo number_format($a10_sub, 2, '.', ',') ?></td>
              
              <td align="left"><?php echo $a3 ?></td>
              <td align="left"><?php echo $a10 ?></td>
              <td align="left"><?php echo $a10_sub ?></td>
            </tr>
            <?php
  $i++;
}
?>
            <tfoot>
              <tr>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">Total:</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom">&nbsp;</td>
                <td align="right" valign="bottom"><?php echo number_format($a10_total, 2, '.', ',') ?></td>
                <td >&nbsp;</td>
                <td >&nbsp;</td>
                <td >&nbsp;</td>
               
              </tr>
            </tfoot>
          </table>
</div>
</div>
</div> 

<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php 
    $_SESSION['denied_return'] = "store/store_main.php?date=";	?>
<body onLoad="accessdenied(); return false">
</body>
<?php }
?>

