<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<link href="../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../css/tabs.css" />

<!--jscript-->
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 

<?php


include('../include/period_select_date.php');
$sub_category = $_GET['subCatSelect'];

if ($sub_category=='both') {
$table_name2 = $_SESSION['db_prefix']."_tile_order_lines";
$order_query = "SELECT store_item_id,  SUM(quantity) AS summed_quantity, price, purchase_price, branch FROM $table_name2 WHERE (sub_category = 'Tiles' OR sub_category = 'Accessories') AND order_date BETWEEN '".$start."' AND '".date('Y-m-d', strtotime(date("Y-m-d", strtotime($end))." +1 day"))."'  GROUP BY store_item_id ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);	
$table_caption = 'Sales Tiles & Accessories';
}
else {
$table_name2 = $_SESSION['db_prefix']."_tile_order_lines";
$order_query = "SELECT store_item_id,  SUM(quantity) AS summed_quantity, price, purchase_price, branch FROM $table_name2 WHERE  sub_category = '".$sub_category."' AND order_date BETWEEN '".$start."' AND '".date('Y-m-d', strtotime(date("Y-m-d", strtotime($end))." +1 day"))."'  GROUP BY store_item_id ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);	
$table_caption = 'Sales '.$sub_category;
}

?>

<div class="page_middle_frame" style="height:690px;"> 

<ul class="tabs">
      <li>
          <input type="radio"  name="tabs" id="tab1">
          <label class="tab" for="tab1"><a class="tabs" href="tile_sales_list.php?branch=Gaborone&date=&subCatSelect=<?php echo $sub_category ?>">Gaborone</a></label>
<div id="tab-content1" class="tab-content animated fadeIn" style="height:630px;"> 

</div>
 </li>
        <li>
          <input type="radio" name="tabs" id="tab2">
          <label  class="tab" for="tab2"><a class="tabs" href="tile_sales_list.php?branch=Francistown&date=&subCatSelect=<?php echo $sub_category ?>">Francistown</a></label>
<div id="tab-content2" class="tab-content animated fadeIn"> 

</div>
        </li>
        <li>
          <input type="radio" checked name="tabs" id="tab3">
          <label class="tab"for="tab3"><a class="tabs" href="tile_sales_list.php?branch=Combined&date=&subCatSelect=<?php echo $sub_category ?>">Combined</a></label>
           
 <div id="tab-content3" class="tab-content animated fadeIn">  

<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": [
								{"iDataSort": 7},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"iDataSort": 8},
								{"iDataSort": 9},
								{"bVisible": false},
								{"bVisible": false},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>
       
        
 
         <?php
$page='tile_sales_list.php';
$reference_name='branch';
$reference='Combined';
$reference_name_2='subCatSelect';
$reference_2= $sub_category;
include('../include/period_select_block_2.php');
?>       </form>
       
        
        
          <div style="margin-top:120px;">

          <label class="label_std"><font size="+2"><?php echo $table_caption ?></font></label> 
          </div>
          <table width="445" id="scroll_table" class="data_display" >
            <thead>
            <tr>
              <th >Item</th>
              <th >Branch</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;

$a3_total=0;
$a4_total=0;
$price=0;

while ($i<$order_num) {
	$a1 = mysql_result($order_result, $i, "store_item_id");
	$a2 = mysql_result($order_result, $i, "summed_quantity");
	$a3 = mysql_result($order_result, $i, "price");
	$a4 = mysql_result($order_result, $i, "purchase_price");
	$a5 = mysql_result($order_result, $i, "branch");
		
	$table_name = $_SESSION['db_prefix']."_store_items";
	$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$a1."'  ";
	$name_result = mySQL_query($name_query);
	$a1a = mysql_result($name_result, 0, "item_name");
	
	$a4_sub = $a4 * $a2;
	$a4_total = $a4_total + $a4_sub;
	$a3_sub = $a3 * $a2;
	$a3_total = $a3_total + $a3_sub;
	
	$table_name = $_SESSION['db_prefix']."_tile_order_lines";
	$order_no_query = "SELECT order_no, order_date, cust_id, branch FROM $table_name WHERE store_item_id = '".$a1."' AND order_date BETWEEN '".$start."' AND '".date('Y-m-d', strtotime(date("Y-m-d", strtotime($end))." +1 day"))."' ";
	$order_no_result = mySQL_query($order_no_query);
	$order_no_num = mysql_num_rows($order_no_result);
	$j = 0;
	$orderTT_lines = '';
	while ($j < $order_no_num) {
	  $order_no = mysql_result($order_no_result, $j, "order_no");
	  $branch = mysql_result($order_no_result, $j, "branch");
	  $order_date = mysql_result($order_no_result, $j, "order_date");
	  
	  $cust_id = mysql_result($order_no_result, $j, "cust_id");
	 			$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");
 				}
	  
	  $orderTT_lines = $orderTT_lines.'<tr><td>'.$order_no.'</td><td>'.$branch.'</td><td>'.date("d-m-Y", strtotime($order_date)).'</td><td>'.$cust_name.'</td><tr>';
	  $j++;
	}

    //$order_no = '';
	
	$orderTT_head = '<table id=table_tool_tip width=450 ><tr><td>Order no</td><td>Branch</td><td>Date</td><td>Customer</td></tr>';
	
	$orderTT_foot = '</table><br><b>Click for more details.</b>';
	$orderTT = $orderTT_head.$orderTT_lines.$orderTT_foot;
	
	//$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>";
//	

	
	?>
            <tr>
              <td align="left"  style="padding-left:0px;"><a href="tile_ordered_list.php?store_item_id=<?php echo $a1?>&date=" onMouseover="ddrivetip('<?php echo $orderTT ?>', 450)"; onMouseout="hideddrivetip()"> <?php echo $a1a ?>  </a></td>
              <td align="left"><?php echo $a5 ?></td>
              <td align="right"><?php echo $a2 ?></td>
              <td align="right"><?php echo $a4 ?></td>
              <td align="right"><?php echo $a3 ?></td>
              <td align="right"><?php echo number_format($a4_sub, 2, '.', ',') ?></td>
              <td width="80" align="right"><?php echo number_format($a3_sub, 2, '.', ',') ?></td>
              
              <td align="left"><?php echo $a1a ?></td>
              <td align="left"><?php echo $a4_sub ?></td>
              <td align="left"><?php echo $a3_sub ?></td>
            </tr>
            <?php
  $i++;
}
?>
            <tfoot>
              <tr>
                <td colspan="5" align="right" valign="bottom">Total:</td>
                <td align="right" valign="bottom"><?php echo number_format($a4_total, 2, '.', ',') ?></td>
                <td align="right" valign="bottom"><?php echo number_format($a3_total, 2, '.', ',') ?></td>
                <td >&nbsp;</td>
                <td >&nbsp;</td>
                <td >&nbsp;</td>
               
              </tr>
            </tfoot>
          </table>
 

          </div>
        </li>
</ul>
 
  </div>


 

