<?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');
$argsYear = date("Y");
if(isset($_REQUEST['line'])){
	$argsLine = $_REQUEST['line'];	
}
if(isset($_REQUEST['group'])){
	$argsGroup = $_REQUEST['group'];
}
if(isset($_REQUEST['sku'])){
	$argsSKU = $_REQUEST['sku'];
}
if(isset($_REQUEST['year'])){
	$argsYear = $_REQUEST['year'];
}


?>
 
 <!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 Sales Report <?= $argsYear . "-" . $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 Sales Report</h3>
		<span></span>
		<br>
		<form class="hidden-print">
			<input type="hidden" name="p" value="itemorderhistory">
			<input type="hidden" name="a" value="admin.soldreport">
			<span>
					<label for="fdate">Year:</label>
					<input type="text" style="width: 150px;" class="form-control" name="year" value="<?= $argsYear ?>">				
					<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>Line<br>
					Group</th>
	  				<th>SKU<br>
					Description</th>
					<th>Jan</th>
					<th>Feb</th>
					<th>Mar</th>
					<th>Apr</th>
					<th>May</th>
					<th>Jun</th>
					<th>Jul</th>
					<th>Aug</th>
					<th>Sep</th>
					<th>Oct</th>
					<th>Nov</th>
					
					<th>Dec</th>
					<th>Total</th>
					<th>Year</th>
					
					
					
	  			</tr>
	  		</thead>
			
			<tbody>
	    <?php
			
			
			
			if($argsSKU . $argsLine . $argsGroup != ""){
				if($argsLine == ""){
				$argsLine = "%";
				}
				if($argsGroup == ""){
					$argsGroup = "%";
				}
				if($argsSKU == ""){
					$argsSKU = "%";
				}
				$query = "SELECT * FROM SoldByMonth WHERE `Line` LIKE '" . $argsLine . "' AND `Group` LIKE '" . $argsGroup . "' AND REPLACE(`SKU`,'-','') LIKE REPLACE('" . $argsSKU . "', '-','') AND `Year` LIKE '" . $argsYear . "';";
				
				$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['Line'] . "<br>" . $row['Group'] ?>
			</td>
			<td>
				<?= $row['SKU'] . "<br>" . $row['Description'] ?>
			</td>
			<td>
				<?= $row['January'] ?>
			</td>
			<td>
				<?= $row['February'] ?>
			</td>
			<td>
				<?= $row['March'] ?>
			</td>
			<td>
				<?= $row['April'] ?>
			</td>
			<td>
				<?= $row['May'] ?>
			</td>
			<td>
				<?= $row['June'] ?>
			</td>
			<td>
				<?= $row['July'] ?>
			</td>
			<td>
				<?= $row['August'] ?>
			</td>
			<td>
				<?= $row['September'] ?>
			</td>
			<td>
				<?= $row['October'] ?>
			</td>
			<td>
				<?= $row['November'] ?>
			</td>
			<td>
				<?= $row['December'] ?>
			</td>
			<td>
				<?= $row['Shipped'] ?>
			</td>
			<td>
				<?= $row['Year'] ?>
			</td>
			
		</tr>
		<?php
				}
			}
		?>
		
			</tbody>
			
	     </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>