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

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

if ($permission_num==1) {

include ("../navigation/sales_sidebar.php");
	
		
?>
<!--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');
$cust_id = $_GET['cust_id'];


$table_name2 = $_SESSION['db_prefix']."_tile_order_lines";
$order_query = "SELECT store_item_id, order_no, order_date, cust_id, quantity, price, purchase_price, branch FROM $table_name2 WHERE cust_id = '".$cust_id."' AND order_date BETWEEN '".$start."' AND '".date('Y-m-d', strtotime(date("Y-m-d", strtotime($end))." +1 day"))."'  ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);	

if ($order_num!=0) {
$cust_id = mysql_result($order_result, 0, "cust_id");
			$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);
			if ($customer_num!=0) {
			$type = mysql_result($customer_result, 0, "account_type");
			if ($type=="corporate") { 
				$a2a = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$a2a = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".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="";
			}
			else {
			$a2a='';
			$a2b = '';
			$a2c = '';
			$a2d = '';
			$a2e = '';
			$a2f = '';
			$a2g = '';
			$a2h="";
			}
				
				
$customerTT ="<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>";
}
else {
	$a2a='';
	$a2b = '';
	$a2c = '';
	$a2d = '';
	$a2e = '';
	$a2f = '';
	$a2g = '';
	$a2h="";
	$customerTT='';
}
?>

 
       
</head>

<body>

<div class="main_frame">
 
<div class="page_head_frame">
    <label class="label_page_head">Customer sales reports</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": [[ 2, "asc" ]],
					"aoColumns": [
								
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 9},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 10},
								{"iDataSort": 11},
								{"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 style="margin-top:120px;">

          <label class="label_std"><font size="+2">Sales to <a href="customer_details.php?cust_id=<?php echo $cust_id ?>" onMouseover="ddrivetip('<?php echo $customerTT ?>', 250)"; onMouseout="hideddrivetip()"><?php echo $a2a ?></a></font></label> 
          </div>
          <table width="445" id="scroll_table" class="data_display" >
            <thead>
            <tr>
              <th >Item</th>
              <th >Branch</th>
              <th >Order date</th>
              <th >Order no</th>
              <th >Quantity</th>
              <th >Purchase<br />price</th>
              <th >Sales<br />price</th>
              <th >Total<br />purchase</th>
              <th >Total<br />sales</th>
              
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&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, "branch");
	$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 = mysql_result($order_result, $i, "purchase_price");
	$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']."_tile_order_lines";
	$line_query = "SELECT  store_item_id, quantity, purchase_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, "purchase_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/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 $a2 ?></td>
              <td align="left"><?php echo date("d-m-Y", strtotime($a3)) ?></td>
              <td align="left"><span style="padding-left:0px;"><a href="tile_order_display.php?order_no=<?php echo $a1 ?>" onMouseOver="ddrivetip('<?php echo $orderTT ?>', 450)"; onMouseOut="hideddrivetip()"><?php echo $a1 ?></a></span></td>
              <td align="right"><?php echo $a6 ?></td>
              <td align="right"><?php echo $a9 ?></td>
              <td align="right"><?php echo $a10 ?></td>
              <td align="right"><?php echo number_format($a9_sub, 2, '.', ',') ?></td>
              <td width="80" align="right"><?php echo number_format($a10_sub, 2, '.', ',') ?></td>
              
              <td align="left"><?php echo $a3 ?></td>
              <td align="left"><?php echo $a9_sub ?></td>
              <td align="left"><?php echo $a10_sub ?></td>
            </tr>
            <?php
  $i++;
}
?>
            <tfoot>
              <tr>
                <td colspan="7" align="right" valign="bottom">Total:</td>
                <td align="right" valign="bottom"><?php echo number_format($a9_total, 2, '.', ',') ?></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> 

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

