<?php 
include 'dheader.php'; 
?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<form action="" method="get">
<div class="manage_buttons hidden-print">
	<div class="row">
	<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				
                <span class="input-group-addon bg">Name</span>
                <select name="name" class=" form-control" id="search-select">
				  <option value="">Supplier Name</option>
				  <?php
					$listNames = $db->query("SELECT * FROM supplier_setup");
					while($rowNames = $listNames->fetchArray()){
						$selected = "";
						if (isset($_GET["name"])) {
						 	$cus_id = preg_replace('/[^0-9]/', "", strtolower($_GET["name"]));
						 	if ($cus_id == $rowNames['id']) {
						 		$selected = "selected";
						 	}
						}    
				?>
					<option value="s<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['supplier_name']; ?></option>
				<?php } ?>
				</select>
            </div>
		</div>

		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				<span class="input-group-addon bg">Item</span>
                <input type="text" name="item" list="itemList" value="<?php echo $_GET['item']; ?>" class=" form-control" id="search-select">
				  <datalist id="itemList">
				  	<option value="All" selected="">
				  <?php
					$listNames = $db->query("SELECT * FROM itemsetup");
					while($rowNames = $listNames->fetchArray()){    
				   ?>
					<option value="<?php echo $rowNames['item_name']; ?>">
				<?php } ?>
				<?php
					$kitNames = $db->query("SELECT * FROM kitsetup");
					while($rowNames = $kitNames->fetchArray()){    
				   ?>
					<option value="<?php echo $rowNames['kit_name']; ?>">
				<?php } ?>	
				  </datalist>
            </div>
		</div>

		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange">
                <span class="input-group-addon bg">
                   From					                       	</span>
                <input type="text" class="form-control start_date datepicker" value="<?php echo $_GET['sdate']; ?>" name="sdate" id="sdate">
            </div>
		</div>
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange1">
                <span class="input-group-addon bg">
                   To			                                </span>
                <input type="text" class="form-control end_date datepicker" value="<?php echo $_GET['edate']; ?>" name="edate" id="edate">
            </div>	
		</div>
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange1">
		<button type="submit" title="Submit" class="btn btn-success">Submit</button>
			</div>
		</div>	
	</div>
</div>

</form>
	<div class="container-fluid">
		<div class="row manage-table">

	
			<div class="panel panel-piluku">
				<div class="panel-heading">
				<h3 class="panel-title">
				<?php
				if (isset($_GET['sdate'])) {
				
					$sdate = $_GET['sdate'];
					$scus_date = explode('/', $sdate);
					$sget_sdate = $scus_date[1].'/'.$scus_date[0].'/'.$scus_date[2];

					// End date
	             	$edate = $_GET['edate'];
					$ecus_date = explode('/', $edate);
					$get_edate = $ecus_date[1].'/'.$ecus_date[0].'/'.$ecus_date[2];
				}
				?>
					Reports - Supplier Item Report	<?php echo @$sget_sdate; ?>-<?php echo @$get_edate; ?>		
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Date</th>
								<th>Name</th>
								<th>User</th>
								<th>Item</th>
								<th>What ?</th>
								<th>QTY</th>
								<th>Rate</th>
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
						?>
						<?php
							// Start date
							$sdate = $_GET['sdate'];
							$scus_date = explode('/', $sdate);
							$get_sdate = $scus_date[2].'/'.$scus_date[0].'/'.$scus_date[1];

							// End date
			             	$edate = $_GET['edate'];
							$ecus_date = explode('/', $edate);
							$get_edate = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];

							// Sales and Return
		            		$sql = "SELECT * FROM customerinvoice ";
		            		$sql .= "WHERE php_date BETWEEN '".$get_sdate."' AND '".$get_edate."' ";
		            		$sql .= "AND name = '".$_GET['name']."' AND payment_type != 'Opening Balance' ";
		            		if($_GET['item'] == 'All'){
		            			// Show all Item
		            		}else{
		            			$sql .= "AND item_name = '".$_GET['item']."' ";
							}

							$tqty = 0;
							$tprice = 0;
		            		$run = $db->query($sql);
		            		while($row = $run->fetchArray()){ 
		            			$tqty += $row['qty'];
								$tprice += $row['sell'];
			            ?>    
			            	<tr>
			            		<td><?php echo $row['show_date']; ?></td>
			            		<td><?php echo $row['name_word']; ?></td>
			            		<td><?php echo $row['user_name']; ?></td>
			            		<?php
			            		$unit = $db->query("SELECT * FROM units WHERE id = '".$row['unit']."'");
			            		$funit = $unit->fetchArray();
			            		?>	
			            		<td><?php echo $row['item_name'] . " " . $funit['unit']; ?></td>
			            		<td><?php echo $row['sale_type']; ?></td>
			            		<td><?php echo $row['qty']; ?></td>
			            		<td><?php echo $row['sell']; ?></td>
			            	</tr>
			            <?php } } ?>
			            	<tr>
								<td colspan="5" align="center" style="color: green;">Total Items : <?php echo $tqty; ?> & Price : <?php echo $tprice; ?></td>
							</tr>
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

			
<?php include 'dfooter.php'; ?>