<!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) {

include ("../navigation/store_sidebar.php");
		
?>
<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="../css/tabs.css" />
<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> 



<script type="text/javascript" src="../jscript/jquery.jqplot.js"></script>
<script type="text/javascript" src="../jscript/jqplot.pieRenderer.js"></script>
<script type="text/javascript" src="../jscript/jqplot.donutRenderer.js"></script>
<link rel="stylesheet" type="text/css" href="../css/jquery.jqplot.css" />

<?php
$show = $_GET['show'];
$cust_id = $_GET['cust_id'];
$item_type = $_GET['item_type'];
$list_item_type = $_GET['item_type'];
$cust_query_line = '=';

include('../include/period_select_date.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 ();
	$k=0;
	$position_result = $con->query($position_query);
	while($row = $position_result->fetch_assoc()) {
		$arrId3[$k] = $row["cust_id"];
		$arrName3[$k] = $row["comp_name"];
		$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 = $con->query($customer_query);
$row = mysqli_fetch_assoc($customer_result);
	$account_type = $row["account_type"];
	

				if ($account_type=="corporate") { 
					$cust_name = $row["comp_name"];
				} 
				else {
					$cust_name = $row["title"]." ".$row["name_2"]." ".$row["comp_name"];
				}
}
else {
	$cust_id = 0;
	$cust_name = 'All';
	$cust_query_line = '>';
}

//calculation tiles per customer



$table_name = $_SESSION['db_prefix']."_product_store";
$product_query = "SELECT product_item_id, item_name  FROM $table_name WHERE subCategory = 'Sales product' ";
$product_result = $con->query($product_query);


//if ($cust_id==0) {
//$table_name1 = $_SESSION['db_prefix']."_product_store";
//$table_name2 = $_SESSION['db_prefix']."_deliver_product_lines";		 	
//$product_display_query = "
//SELECT $table_name1.product_item_id, $table_name1.item_name, SUM($table_name2.quantity) AS summed_quantity 
//FROM $table_name1, $table_name2 
//WHERE $table_name1.product_item_id >0
//AND $table_name1.product_item_id = $table_name2.product_item_id
//AND ($table_name2.deliver_date >= '".$start."' AND $table_name2.deliver_date <= '".$end."' )
//GROUP BY $table_name2.product_item_id
//ORDER BY $table_name1.item_name ASC
//";
//
//}
//else {
//$table_name1 = $_SESSION['db_prefix']."_product_store";
//$table_name2 = $_SESSION['db_prefix']."_deliver_product_lines";	
//$product_display_query = "
//SELECT $table_name1.product_item_id, $table_name1.item_name, SUM($table_name2.quantity) AS summed_quantity 
//FROM $table_name1, $table_name2 
//WHERE $table_name1.product_item_id = $table_name2.product_item_id
//AND ($table_name2.deliver_date >= '".$start."' AND $table_name2.deliver_date <= '".$end."' )
//GROUP BY $table_name2.product_item_id
//ORDER BY $table_name1.item_name ASC
//";
//}
//$product_display_result = $con->query($product_display_query);


$table_name1 = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_cash_deliver_product_lines";	
$cash_total_product_display_query = "
SELECT  SUM($table_name2.quantity) AS cash_total_qty 
FROM $table_name2 
WHERE  ($table_name2.sale_date >= '".$start."' AND $table_name2.sale_date <= '".$end."' )
";
$cash_total_product_display_result = $con->query($cash_total_product_display_query);
$row = mysqli_fetch_assoc($cash_total_product_display_result);
$cash_total_qty = $row["cash_total_qty"];

$table_name1 = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_deliver_product_lines";	
$total_product_display_query = "
SELECT  SUM($table_name2.quantity) AS total_qty 
FROM $table_name2 
WHERE  ($table_name2.deliver_date >= '".$start."' AND $table_name2.deliver_date <= '".$end."' )
";
$total_product_display_result = $con->query($total_product_display_query);
$row = mysqli_fetch_assoc($total_product_display_result);
$total_qty = $row["total_qty"];

$all_total_qty = $total_qty + $cash_total_qty;


if ($cust_id>0) {
$table_name = $_SESSION['db_prefix']."_customer";
$cust_name_query = "SELECT  comp_name FROM $table_name WHERE cust_id = '".$cust_id."'  ";
$cust_name_result = $con->query($cust_name_query);
$row = mysqli_fetch_assoc($cust_name_result);
$cust_name = $row['comp_name'];
}
else {
$cust_name = '';
}
?>

  
</head>

<body>

<div class="main_frame">
 
  <div class="page_head_frame">
    <label class="label_page_head">Product deliveries</label></div>
<div class="page_middle_frame">

<?php
$page='delivery_products.php';
$reference_name='show';
$reference=$show;
$reference_name_2='cust_id';
$reference_2=$cust_id;
$reference_name_3='item_type';
$reference_3=$item_type;
include('../include/period_select_block_small.php');
?>
<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">
  <option value="<?php echo $cust_id ?>"><?php echo $cust_name ?></option>
  			<?php echo "<option value=delivery_products.php?date=3&start_date=".$start."&end_date=".$end."&cust_id=-1&show=0&item_type=>Cash sale</option>"; ?>
            <?php echo "<option value=delivery_products.php?date=3&start_date=".$start."&end_date=".$end."&cust_id=0&show=0&item_type=>All</option>"; ?>
            <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=delivery_products.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 />

<form name="form2" id="form2"  action="" method="get">
<div class="page_inside_frame">
<div style="float:left; min-height:210px;">
Customer deliveries on total deliveries for <?php if ($cust_id=='-1') { echo "Cash sales"; } else { echo $cust_name; } ?> :<br>
<br>
  <table width="650" id="table_details_display"  >
	<thead>
		<tr>
		  <th colspan="3" align="left">Total products delivered this period :&nbsp;&nbsp;<b><?php echo $all_total_qty ?></b>&nbsp;&nbsp;(<?php echo $cash_total_qty ?> cash sales)</th>
		  <th colspan="2" align="center">
          <?php if ($cust_id=='-1') { echo "Cash sales"; } else { ?><a href="account_details.php?cust_id=<?php echo $cust_id ?>" onMouseover="ddrivetip('Click for account details', 150)"; onMouseout="hideddrivetip()"><?php echo $cust_name ?></a><?php } ?></th>
		  </tr>
		<tr>
		  <th width="115" align="left">Products</th>
          <th width="66" align="center">Total Deliveries<br>
            Quantity</th>
          <th width="66" align="center">% of<br>
Total Deliveries</th>
          
          <th width="69" align="center">Company<br> 
          Deliveries
</th>
          <th width="69" align="center">Company %<br>
            of Total Deliveries</th>
          </tr>
	</thead>
	
  <?php
   	$i=0;
	$b2_tot=0;
	$b3=0;
	$b4=0;
	$b5=0;
	while($row = $product_result->fetch_assoc()) {
		$b0 = $row["product_item_id"];
		$b1 = $row["item_name"];
		
		$b2 = 0;
		
		
		$table_name = $_SESSION['db_prefix']."_deliver_product_lines";
		$del_query = "SELECT SUM(quantity) AS del_quantity FROM $table_name WHERE  (deliver_date >= '".$start."' AND deliver_date <= '".$end."' ) AND product_item_id = '".$b0."' ";
		$del_result = $con->query($del_query);
		$row = mysqli_fetch_assoc($del_result);
		$b2 = $row['del_quantity'];
		
		$table_name = $_SESSION['db_prefix']."_cash_deliver_product_lines";
		$cash_del_query = "SELECT SUM(quantity) AS sale_quantity FROM $table_name WHERE  (sale_date >= '".$start."' AND sale_date <= '".$end."' ) AND product_item_id = '".$b0."' ";
		$cash_del_result = $con->query($cash_del_query);
		$row = mysqli_fetch_assoc($cash_del_result);
		$sale_quantity = $row['sale_quantity'];
		
		
		$b2 = $b2 + $sale_quantity;
		
		$b2_tot = $b2_tot + $b2;
		
		$b3 = ($b2/$all_total_qty) * 100;

		$b4 = '';
		$b5 = '';
		

		if ($cust_id>0) {
		$table_name = $_SESSION['db_prefix']."_deliver_product_lines";
		$total_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name WHERE product_item_id = '".$b0."' AND cust_id = '".$cust_id."' AND ($table_name2.deliver_date >= '".$start."' AND $table_name2.deliver_date <= '".$end."' ) ";
		$total_result = $con->query($total_query);
		$row = mysqli_fetch_assoc($total_result);
		$b4 = $row["summed_quantity"];
		$b5 = ($b4/$all_total_qty) * 100;
		}
				
		if ($cust_id==-1) {
		$table_name = $_SESSION['db_prefix']."_cash_deliver_product_lines";
		$total_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name WHERE product_item_id = '".$b0."' AND (sale_date >= '".$start."' AND sale_date <= '".$end."' ) ";
		$total_result = $con->query($total_query);
		$row = mysqli_fetch_assoc($total_result);
		$b4 = $row["summed_quantity"];
		$b5 = ($b4/$all_total_qty) * 100;
		}
					
		?>
        
       	<tr >
		  <td ><a href="delivery_products_detailed.php?product_item_id=<?php echo $b0 ?>&date=&select=All" onMouseover="ddrivetip('Click for detailed deliveries', 150)"; onMouseout="hideddrivetip()"><?php echo $b1 ?></a></td>
          <td align="right" ><?php echo $b2; ?></td>
          <td align="right" ><?php echo number_format($b3, 2, '.', ',') ?></td>
          
          <td align="center" ><?php if ($cust_id!=0) { echo $b4; } ?></td>
          <td align="center" ><?php if ($b5!=0) {echo number_format($b5, 2, '.', ',');} ?></td>
          </tr>
		<?php
		$i++;
}
?>

</table>
</div>
<div style="float:right">
<!--Customer sales distribution-->
<div id="pie1" style="height:200px;width:300px; "></div>
<script>
$(document).ready(function(){
    var plot1 = $.jqplot('pie1', [[
	<?php
	$k=0;
	while ($k<$tile_type_num) { ?>
	['<?php echo ${"tile_type".$k} ?>',<?php echo ${"tile_quantity".$k} ?>],
	<?php 
	$k++;
	}
	?>
	]], {
        gridPadding: {top:0, bottom:38, left:0, right:0},
        seriesDefaults:{
            renderer:$.jqplot.PieRenderer, 
            trendline:{ show:true }, 
            rendererOptions: { padding: 8, showDataLabels: true }
        },
        legend:{
            show:true, 
            placement: 'inside', 
            rendererOptions: {
                numberRows: 1
            }, 
            location:'s',
            marginTop: '15px'
        }       
    });
});
</script>
</div>

<br />
</div>

</form>

<?php if ($show==1) { ?>
<div class="page_inside_frame">
<table width="479" id="table_details_display">
  <tr>
    <td colspan="4">Delivery summary for <?php echo $list_item_type ?> delivered to <?php echo $cust_name ?>  </td>
    </tr> 
  <tr>
    <td width="90">Delivery no</td>
    <td width="81">Date</td>
    <td width="231">Customer</td>
    <td width="57">Quantity</td>
  </tr>
  <?php
  $i=0;
  $tot_quantity=0;
  while ($i<$delivery_summary_num) {
	  $delivery_no = mysql_result($delivery_summary_result, $i, "delivery_no");
	  $delivery_reference = mysql_result($delivery_summary_result, $i, "delivery_reference");
	  $delivery_date = mysql_result($delivery_summary_result, $i, "delivery_date");
	  $quantity = mysql_result($delivery_summary_result, $i, "quantity");
	  $cust_id = mysql_result($delivery_summary_result, $i, "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");
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
	  
	  ?>
  <tr>
    <td><a href="delivery.php?delivery_reference=<?php echo $delivery_reference ?>" onMouseover="ddrivetip('Click for delivery details.', 120)"; onMouseout="hideddrivetip()"><?php echo $delivery_no ?></a></td>
    <td><?php echo date("d-m-Y", strtotime($deliver_date)) ?></td>
    <td><a href="account_details.php?cust_id=<?php echo $cust_id ?>" onMouseover="ddrivetip('Click for customer details', 150)"; onMouseout="hideddrivetip()"><?php echo $cust_name ?></a></td>
    <td align="right"><?php echo number_format($quantity, 0, '.', ',') ?><?php $tot_quantity = $tot_quantity + $quantity ?></td>
  </tr>
  <?php
  $i++;
  }
  ?>
  <tr>
    <td colspan="3" align="right">Total:&nbsp;&nbsp;</td>
    <td align="right"><?php echo number_format($tot_quantity, 0, '.', ',') ?></td>
  </tr>
</table>
</div>
<?php } ?>
 </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 }
?>

