<?php
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("../session.php");
}else{
	include_once("connect.php");
	include_once("session.php");
}
$argsLine = "";
$argsGroup = "";
$argsSKU = "";
$argsFDate = date('Y-m-d');
$argsTDate = date('Y-m-d');
if(isset($_REQUEST['line'])){
	$argsLine = $_REQUEST['line'];	
}
if(isset($_REQUEST['group'])){
	$argsGroup = $_REQUEST['group'];
}
if(isset($_REQUEST['sku'])){
	$argsSKU = $_REQUEST['sku'];
}
if(isset($_REQUEST['fdate'])){
	$argsFDate = $_REQUEST['fdate'];
}
if(isset($_REQUEST['tdate'])){
	$argsTDate = $_REQUEST['tdate'];
}


?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?> - Item Order History <?= $argsFDate . "-" . $argsTDate . " " . $argsLine . " "  . $argsGroup . " " . $argsSKU ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="../js/bootstrap.min.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
       	 <script src="../js/excellentexport.js"></script>
				 <script src="../js/functions.js"></script>
		<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
		<script src="//cdn.datatables.net/1.10.16/js/dataTables.bootstrap.js"></script>
		<script src="//cdn.datatables.net/plug-ins/1.10.16/api/sum().js"></script>
		<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/jszip-2.5.0/dt-1.10.16/b-1.4.2/b-html5-1.4.2/b-print-1.4.2/datatables.min.css"/>
 
		<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
		<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
		<script type="text/javascript" src="https://cdn.datatables.net/v/bs/jszip-2.5.0/dt-1.10.16/b-1.4.2/b-html5-1.4.2/b-print-1.4.2/datatables.min.js"></script>


           <script type="text/javascript">
			var table;
             $(document).ready(function(){
					table = $('#itemOrderHistoryTable').DataTable( {
						paging: false,
						//dom: 'Bfrtip',
						buttons: [
					       'copy', 'excel', 'pdf'
						],
						drawCallback: function (row, data, start, end, display) {
							var api = this.api();
							var intVal = function ( i ) {
								return typeof i === 'string' ?
									i.replace(/[\$,]/g, '')*1 :
									typeof i === 'number' ?
										i : 0;
							};
							var pageTotal, qtyTotal = 0;
							// Total over this page
							if (api.column(8).data().length){
								pageTotal = api
									.column( 8, { page: 'current'} )
									.data()
									.reduce( function (a, b) {
										return intVal(a) + intVal(b);
									} );
							}else{
								pageTotal = 0;
							}
							
							if (api.column(5).data().length){
								qtyTotal = api
									.column( 5, { page: 'current'} )
									.data()
									.reduce( function (a, b) {
										return intVal(a) + intVal(b);
									} );
							}else{
								qtyTotal = 0;
							}
					  
							$('#filteredTotalSum').text(" " + moneyFormat(pageTotal));
							$('#filteredTotalQtySum').text(" " + (qtyTotal));
							
						}
					});
				table.buttons().container()
				.appendTo( $('#exportButtons') );
			});
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
	<link href="../css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
	<link href="//cdn.datatables.net/1.10.16/css/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
		<script src="../js/fileinput.js" type="text/javascript"></script>
   </head>
   <body>
   
       <?php
		include_once("./navbar.php");						
		?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container" style="padding-top: 10px;">
	<div class="panel panel-default">
	  <div class="panel-heading form-inline">
		<h3 class="panel-title">Item Order History</h3>
		<span></span>
		<br>
		<form>
			<input type="hidden" name="p" value="itemorderhistory">
			<input type="hidden" name="a" value="admin.report">
			<span>
					<label for="fdate">From:</label>
					<input type="date" style="width: 150px;" class="form-control" name="fdate" value="<?= $argsFDate ?>">
					<label for="tdate">To:</label>
					<input type="date" style="width: 150px;" class="form-control" name="tdate" value="<?= $argsTDate ?>"><br>
					<input type="text" style="width: 100px;" class="form-control" name="line" placeholder="Line" value="<?= $argsLine ?>" />
					<input type="text" style="width: 100px;" class="form-control" name="group" placeholder="Group" value="<?= $argsGroup ?>" />
					<input type="text" class="form-control" name="sku" placeholder="SKU" value="<?= $argsSKU ?>" />
					<button class="btn btn-success" onclick="">Search</button>
			</span>
		</form>
		
		<span id="exportButtons" class="pull-right"></span>
		<br><br>
	  </div>
	  <div class="panel-body">
	  	<table class="table table-hover table-condensed" id="itemOrderHistoryTable">
	  		<thead>
	  			<tr>
					<th>Customer</th>
					<th>Order Number</th>
					<th>Date/Time</th>
	  				<th>Line<br>
					Group</th>
	  				<th>SKU<br>
					Description</th>
					<th>Order Qty</th>
					<th>Available Qty</th>
					<th>Price</th>
					<th>Extension</th>
					
	  			</tr>
	  		</thead>
			
			<tbody>
	    <?php
			
			
			
			if($argsSKU . $argsLine . $argsGroup != ""){
				if($argsLine == ""){
				$argsLine = "%";
				}
				if($argsGroup == ""){
					$argsGroup = "%";
				}
				if($argsSKU == ""){
					$argsSKU = "%";
				}
				$query = "SELECT * FROM ORDER_HEAD JOIN ORDER_DETAIL  USING(OrderID) WHERE `Line` LIKE '" . $argsLine . "' AND `Group` LIKE '" . $argsGroup . "' AND REPLACE(`SKU`,'-','') LIKE REPLACE('" . $argsSKU . "', '-','') AND OrderDate BETWEEN '" . $argsFDate . "' AND '" . $argsTDate . " 23:59:59';";
				$result = mysqli_query($con, $query);
				$totalRows = 0;
				$totalOrderedQty = 0;
				$totalRevenue = 0;
				
				while($row = mysqli_fetch_array($result)){
					$totalRows++;
					$totalOrderedQty += $row['OrderQty'];
					$totalRevenue += ($row['Price'] * $row['OrderQty']);
				
			
		?>
		
		<tr>
			<td><?= $row['AccountNumber'] ?></td>
			<td><?= $row['OrderID'] ?></td>
			<td><?= date("m/d/Y", strtotime($row['OrderDate'])) ?><br><?= date("h:i a", strtotime($row['OrderDate'])) ?></td>
			<td><?= $row['Line'] ?><br><?= $row['Group'] ?></td>
			<td><b><?= $row['SKU'] ?></b><br><?= $row['Description'] . " " . $row['Notes'] ?></td>
			<td><?= $row['OrderQty'] ?></td>
			<td><?= $row['AvailableQty'] ?></td>
			<td><?= moneyFormat($row['Price']) ?></td>
			<td><?= moneyFormat($row['Price'] * $row['OrderQty']) ?></td>
			
			
		</tr>
		<?php
				}
			}
		?>
		
			</tbody>
			<tfoot>
				<tr>
					
					<th></th>
					<th></th>
					<th></th>
					<th></th>
					<th style="text-align: right;">Filtered Order Qty Total:</th>
					<th id="filteredTotalQtySum"></th>
					<th  style="text-align: right;" colspan="2">Filtered Total:</th>
					
					<th id="filteredTotalSum"></th>
				</tr>
			</tfoot>
	     </table>
	  </div>
	  <div class="panel-footer" style="height: 100px;" >
		<span class="pull-left">
			<b>Total Rows: <?= $totalRows ?></b><br>
			<b>Total Ordered Qty: <?= $totalOrderedQty ?></b><br>
			<b>Total Orders Amount: <?= moneyFormat($totalRevenue) ?></b>
		</span>
		
		<span class="pull-right">
			<b></b><span ></span><br>
			<b></b><span ></span>
		</span>
	  </div>
	</div>
</div>



<script type="text/javascript">
	
</script>

   </body>
   
 </html>