<!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");
include('../include/period_select_date.php');

$product_item_id = $_GET['product_item_id'];
$select = $_GET['select'];

$table_name = $_SESSION['db_prefix']."_product_store";
$product_name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$product_name_result = $con->query($product_name_query);
$row = mysqli_fetch_assoc($product_name_result);
$product_item_name = $row['item_name'];

if ($select=='All') {
$table_name2 = $_SESSION['db_prefix']."_deliver_product_lines";
$order_deliver_query = "SELECT delivery_no, deliver_date, cust_id, quantity, batch_code, price FROM $table_name2 WHERE  product_item_id = '".$product_item_id."' AND deliver_date BETWEEN '".$start."' AND '".$end."'  ORDER BY deliver_date DESC ";
$order_deliver_result = $con->query($order_deliver_query);
$order_deliver_num = mysqli_num_rows($order_deliver_result);

$table_name2 = $_SESSION['db_prefix']."_cash_deliver_product_lines";
$cash_deliver_query = "SELECT cash_sale_no, sale_date, product_item_id, quantity, batch_code, price FROM $table_name2 WHERE  product_item_id = '".$product_item_id."' AND sale_date BETWEEN '".$start."' AND '".$end."'  ORDER BY sale_date DESC ";
$cash_deliver_result = $con->query($cash_deliver_query);
$cash_deliver_num = mysqli_num_rows($cash_deliver_result);
}

if ($select=='Order') {
$table_name2 = $_SESSION['db_prefix']."_deliver_product_lines";
$order_deliver_query = "SELECT delivery_no, deliver_date, cust_id, quantity, batch_code, price FROM $table_name2 WHERE  product_item_id = '".$product_item_id."' AND deliver_date BETWEEN '".$start."' AND '".$end."'  ORDER BY deliver_date DESC ";
$order_deliver_result = $con->query($order_deliver_query);
$order_deliver_num = mysqli_num_rows($order_deliver_result);

$cash_deliver_num = 0;
}

if ($select=='Cash') {
$table_name2 = $_SESSION['db_prefix']."_cash_deliver_product_lines";
$cash_deliver_query = "SELECT cash_sale_no, sale_date, product_item_id, quantity, batch_code, price FROM $table_name2 WHERE  product_item_id = '".$product_item_id."' AND sale_date BETWEEN '".$start."' AND '".$end."'  ORDER BY sale_date DESC ";
$cash_deliver_result = $con->query($cash_deliver_query);
$cash_deliver_num = mysqli_num_rows($cash_deliver_result);

$order_deliver_num = 0;
}


$table_name = $_SESSION['db_prefix']."_product_store";
$position_query = "SELECT product_item_id, item_name FROM $table_name WHERE subCategory = 'Sales product' ORDER BY item_name ASC";
$arrId3 = array ();
$arrName3 = array ();
$position_result = $con->query($position_query);
$b = 0;
while($row = $position_result->fetch_assoc()) {
		$arrId3[$b] = $row["product_item_id"];
		$arrName3[$b] = $row["item_name"];
		$b++;
}


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

       
</head>

<body>

<div class="main_frame">
 
  <div class="page_head_frame">
    <label class="label_page_head"> </label>
    <label class="label_page_head">Per product deliveries</label>&nbsp;&nbsp;<?php echo $product_item_name ?>
  </div>

<div class="page_middle_frame"> 

<div>
<?php
$page='delivery_products_detailed.php';
$reference_name='product_item_id';
$reference=$product_item_id;
$reference_name_2='select';
$reference_2=$select;
include('../include/period_select_block_2.php');
?> 
</div>

<div style="margin-top:125px;">
<form action="delivery_products_detailed.php" method="get" name="form2">
  <?php echo $product_item_name ?>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Select another product here: 
    <select name="product_select" class="select300"  onchange="window.location.href= this.form.product_select.options[this.form.product_select.selectedIndex].value">
      <option value="  " selected="selected"> </option>
      <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=delivery_products_detailed.php?date=3&start_date=".$start."&end_date=".$end."&product_item_id=".$arrId3[$k]."&select=".$select.">".$arrName3[$k]."</option>";
	}
	?>
    </select>
 <br>
<br>
    <label>
      <input type="radio" name="order_cash_radio_group" value="All" id="order_cash_radio_group_0" <?php if ($select=='All') { ?> checked <?php } ?> onClick="window.location.href='delivery_products_detailed.php?select=All&product_item_id=<?php echo $product_item_id ?>&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>';">All</label>
    &nbsp;&nbsp;
    <label>
      <input type="radio" name="order_cash_radio_group" value="Order" id="order_cash_radio_group_1" <?php if ($select=='Order') { ?> checked <?php } ?> onClick="window.location.href='delivery_products_detailed.php?select=Order&product_item_id=<?php echo $product_item_id ?>&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>';">Order</label>
    &nbsp;&nbsp;
    <label>
      <input type="radio" name="order_cash_radio_group" value="Cash" id="order_cash_radio_group_2 "<?php if ($select=='Cash') { ?> checked <?php } ?>" onClick="window.location.href='delivery_products_detailed.php?select=Cash&product_item_id=<?php echo $product_item_id ?>&date=3&start_date=<?php echo $start ?>&end_date=<?php echo $end ?>';">Cash</label>
    
</form>   
</div> 

<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": "400px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 9, "desc" ], [ 1, "asc" ]],
					"aoColumns": [
								{"iDataSort": 9},
								{"bSortable": true},
								{"iDataSort": 8},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false},
								{"bVisible": false}
							 ]
				} );
			} );
		</script>
    
<table width="700" id="scroll_table" class="data_display" >
        <thead>
            <tr>
              <th align="center" >Delivery date</th>
              <th align="center" >Order /cash</th>
              <th >Del note<br />
              no</th>
              <th >Delivered to</th>
              <th >Quantity</th>
              <th >Batch code</th>
              <th width="46">Unit Price</th>
              <th width="59">Total</th>
              <th width="23">&nbsp;</th>
              <th width="23">&nbsp;</th>
          </tr>
      </thead>
            <?php 
$a7tot=0;			
if ($order_deliver_num > 0) {
$price=0;
while($row = $order_deliver_result->fetch_assoc()) {
		$a1 = $row["delivery_no"];
		$a2 = $row["deliver_date"];
		$a3 = $row["cust_id"];
		$a4 = $row["quantity"];
		$a5 = $row["batch_code"];
		$a6 = $row["price"];
		$a7 = $a4 * $a6;
		$a7tot = $a7tot + $a7;
		
		$a10 = 'order';
		   
	     $table_name3 = $_SESSION['db_prefix']."_customer";
         $customer_query = "SELECT comp_name,  bag_box, box_no, postal_place, phone, contact_1 FROM $table_name3 WHERE cust_id = '".$a3."' ";
		 $customer_result = $_result = $con->query($customer_query);
		 $customer_num = mysqli_num_rows($customer_result);
		 if ($customer_num!=0) {
			   	$row = mysqli_fetch_assoc($customer_result);
				$a3a = $row["comp_name"];
				$a3b = $row["bag_box"];
		 		$a3c = $row["box_no"];
		 		$a3d = $row["postal_place"];
		 		$a3e = $row["phone"];
		 		$a3f = $row["contact_1"];
		}
	
	$table_name = $_SESSION['db_prefix']."_deliver_product_lines";
	$line_query = "SELECT  product_item_id, quantity  FROM $table_name WHERE delivery_no = '".$a1."' ";
	$line_result = $con->query($line_query);
	$line_num = mysqli_num_rows($line_result);
		
	$total=0;
	$line_text="";
			
	while($row = $line_result->fetch_assoc()) {
		$product_item_id = $row["product_item_id"];
		$quantity = $row["quantity"];
				
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$product_item_name = $row['item_name'];
					  
	   	$line_text = $line_text."<tr><td>".$product_item_name."</td><td>".$quantity."</td></tr>";
	}
		
	
	
	$customerTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Company:</td><td width=175>".$a3a."</td></tr><tr><td>Address:</td><td>".$a3b."&nbsp;".$a3c."<br>".$a3d."</td></tr><tr><td>Phone:</td><td>".$a3e."</td></tr><tr><td>Contact:</td><td>".$a3f."</td></tr></table><br><b>Click for details</b>";
	
	$deliverTT ="<table id=table_tool_tip width=250 ><tr><td>Item</td><td>Quantity</td></tr>".$line_text."<tr><td>&nbsp;</td><td>&nbsp;</td></tr></table><br><b>Click for details</b>";
	
	
	
	?>
            <tr>
              <td width="51"align="left"  style="padding-left:0px;"><?php echo date("d-m-Y", strtotime($a2))?></td>               
              <td width="51"align="left"  style="padding-left:0px;"><?php echo $a10 ?></td>
              <td width="85" align="left"><span style="padding-left:0px;"><a href="deliver_product_display.php?delivery_no=<?php echo $a1 ?> " onMouseOver="ddrivetip('<?php echo $deliverTT ?>', 250)"; onMouseOut="hideddrivetip()"><?php echo $a1 ?></a></span></td>
              <td width="189" align="left"><a href="" onmouseover="ddrivetip('<?php echo $customerTT ?>', 250)"; onmouseout="hideddrivetip()"><?php echo $a3a ?></a></td>
              <td width="66" align="left"><?php echo $a4 ?></td>
              <td width="116" align="left"><?php echo $a5 ?></td>
              <td align="right"><?php echo number_format($a6, 2, '.', ',') ?></td>
              <td align="right"><?php echo number_format($a7, 2, '.', ',') ?></td>
              <td align="left"><?php echo $a1 ?></td>
              <td align="left"><?php echo $a2 ?></td>
            </tr>
            <?php
}
}
?>
            <?php 

if ($cash_deliver_num>0) {
$price=0;
while($row = $cash_deliver_result->fetch_assoc()) {
		$c1 = $row["cash_sale_no"];
		$c2 = $row["sale_date"];
		$c4 = $row["quantity"];
		$c5 = $row["batch_code"];
		$c6 = $row["price"];
		$c7 = $c4 * $c6;
		$a7tot = $a7tot + $c7;
		
		$c10 = 'cash';
		   
	     $table_name3 = $_SESSION['db_prefix']."_cash_deliver_product";
         $customer_query = "SELECT cust_name,  cust_address, cust_place, cust_phone FROM $table_name3 WHERE cash_sale_no = '".$c1."' ";
		 $customer_result = $_result = $con->query($customer_query);
		 $customer_num = mysqli_num_rows($customer_result);
		 if ($customer_num!=0) {
			   	$row = mysqli_fetch_assoc($customer_result);
				$c3a = $row["cust_name"];
				$c3b = $row["cust_address"];
		 		$c3c = $row["cust_place"];
		 		$c3d = $row["cust_phone"];
		}
	
	$table_name = $_SESSION['db_prefix']."_cash_deliver_product_lines";
	$line_query = "SELECT  product_item_id, quantity  FROM $table_name WHERE cash_sale_no = '".$c1."' ";
	$line_result = $con->query($line_query);
	$line_num = mysqli_num_rows($line_result);
		
	$total=0;
	$line_text="";
			
	while($row = $line_result->fetch_assoc()) {
		$product_item_id = $row["product_item_id"];
		$quantity = $row["quantity"];
				
		$table_name = $_SESSION['db_prefix']."_product_store";
		$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
		$name_result = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$product_item_name = $row['item_name'];
					  
	   	$line_text = $line_text."<tr><td>".$product_item_name."</td><td>".$quantity."</td></tr>";
	}
		
	
	
	$customerTT ="<table id=table_tool_tip width=250 ><tr><td  width=75>Customer:</td><td width=175>".$c3a."</td></tr><tr><td>Address:</td><td>".$c3b."<br>".$c3c."</td></tr><tr><td>Phone:</td><td>".$c3d."</td></tr></table><br><b>Click for details</b>";
	
	$deliverTT ="<table id=table_tool_tip width=250 ><tr><td>Item</td><td>Quantity</td></tr>".$line_text."<tr><td>&nbsp;</td><td>&nbsp;</td></tr></table><br><b>Click for details</b>";
	
	
	
	?>
            <tr>
              <td align="left"  style="padding-left:0px;"><?php echo date("d-m-Y", strtotime($c2))?></td>
              <td align="left"  style="padding-left:0px;"><?php echo $c10 ?></td>
              <td align="left"><span style="padding-left:0px;"><a href="cash_deliver_product_display.php?cash_sale_no=<?php echo $c1 ?> " onMouseOver="ddrivetip('<?php echo $deliverTT ?>', 250)"; onMouseOut="hideddrivetip()"><?php echo $c1 ?></a></span></td>
              <td align="left"><a href="" onmouseover="ddrivetip('<?php echo $customerTT ?>', 250)"; onmouseout="hideddrivetip()"><?php echo $c3a ?></a></td>
              <td align="left"><?php echo $c4 ?></td>
              <td align="left"><?php echo $c5 ?></td>
              <td align="right"><?php echo number_format($c6, 2, '.', ',') ?></td>
              <td align="right"><?php echo number_format($c7, 2, '.', ',') ?></td>
              <td align="left"><?php echo $c1 ?></td>
              <td align="left"><?php echo $c2 ?></td>
            </tr>
    <?php
	}
}
	?>  
            
            <tfoot>
            <tr>
              <td colspan="7" align="right"  style="padding-left:0px;">All total</td>
              <td align="right"><?php echo number_format($a7tot, 2, '.', ',') ?></td>
              <td align="left">&nbsp;</td>
              <td align="left">&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 }
?>

