<?php
	include("includes/dbconfig.php");
	$pagetitle = "Customers";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Make Claim</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default box -->
					<?php
						if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] == 4 || $_SESSION["connectedusergroup"] == 1)
						{
					?>
					<div class="row">
						<div class="col-md-12">
							<div id="servererrormessage">
							 
							</div>
						</div>
					</div>

					<div class="row">
						<div class="col-md-12">
							<div style="">
							</div>
						</div>
					</div>
				
					<div class="row">
						<div class="col-md-12" id ='Loadhere' >
							<div class="box box-primary" id="customerdetails" style="">
								<div class="box-body">
									
															
									
									<div>
										<div id="">
										<!-- <form method="post" action="customerregister.php"> -->
										<form method="post" action="makeclaims.php">
											<table class="table table-hover"  id="">
												
												<tbody>
													<tr>
														<td><b>Customer</b></td><td>
														
														<?php
														if(isset($_GET['customer_id'])){
															$customer_id = $_GET['customer_id'];
														}elseif(isset($_POST['customer_id'])){
															$customer_id = $_POST['customer_id'];
														}else{
															$customer_id = null;
														}
														$customers = DB::query("SELECT id, name, phone, email, claim FROM customers where id = '{$customer_id}'");
														
														foreach ($customers as $customer)
														{
													?>
														<?php 
															$id = $customer['id']; 

														 
						 
						 $items = DB::query("SELECT   bill, claim, bill, receipt_mode FROM cash_receipt where customer_id = '".$id."'");
																				
						 $total_bills = 0;
						 $total_claims = 0;
						 $previous_claims = 0;
						 
						 foreach ($items as $item)
						 {
							$total_claims +=   ($item['claim']/100)*($item['bill']);
							  
							 
							 
						 }

						 
						 
						 //get previous claims
						 $claims = DB::query("SELECT   claim from customer_claims where customer_id = '".$id."'");
						 
						 
						 foreach ($claims as $claim)
						 {
							 $previous_claims +=   $claim['claim'];
							 
						 }

						 $points_balance =  $total_claims - $previous_claims;

				 

															$name = $customer['name']; 
															echo $customer['name'];
														}
														?>
														 
														<input type="hidden" name="customer_id" id="customer_id" value="<?php echo $customer_id; ?>" />
													<?php
														#}
													?>
														
														</td>
													</tr>
													<tr>
														<td><b>Claim</b></td><td><input type="text" name="customer_claim" placeholder="Item Name, Code or Barcode" class="form-control" /> </td>
													</tr>
													<tr>
														<td></td><td><input type="submit" name="submit" value="Search" /></td>
													</tr>
												</tbody>
											</table>
										</form>
										</div>
									</div>
									
									<div>
									<?php
													if(isset($_POST['customer_claim'])){
														$item_name = $_POST['customer_claim'];
														$customer_id = trim($_POST['customer_id']);
														$products = DB::query("SELECT id, item_name, item_code, bar_code, description, default_sell_price FROM stock_item where item_name like '%{$item_name}%'OR   item_code = '{$item_name}' OR bar_code = '{$item_name}'  ");
														
														$products2 = DB::query("SELECT id, item_name, item_code, bar_code, description, default_sell_price FROM stock_item where item_code = '{$item_name}'");
														
														$products3 = DB::query("SELECT id, item_name, item_code, bar_code, description, default_sell_price FROM stock_item where bar_code = '{$item_name}'");
														$i = 1;
														 
										?>
<h3>Search Results</h3>
<table class="table table-hover"  id="">
	<tr>
		<td><b>#</b></td>
		<td><b>Name</b></td>
		<td><b>Code</b></td>
		<td><b>Barcode</b></td>
		<td><b>Price</b></td>
		<td></td>
	</tr>
	<?php
	foreach ($products as $product){
		if(( $points_balance >=  $product['default_sell_price'] ) and $product['default_sell_price']  > 0 ){
	?>
<tr>
		<td><?php echo $i; ?></td>
		<td><?php echo $product['item_name']; ?></td>
		<td><?php echo $product['item_code']; ?></td>
		<td><?php echo $product['bar_code']; ?></td>
		<td><?php echo $product['default_sell_price']; ?></td>
		<td>
			<input type="hidden" value="<?php echo  $customer_id.'  : '.$product['id'].'  : '.$product['default_sell_price'].'  : '. $product['item_name']; ?>" id='<?php echo $product['id'];?>' />
			<button  type="button" class="btn btn-mini btn-info" value="<?php echo $product['id']; ?>" onclick="return post_claim(<?php  echo $product['id'];?>)"> Process Claim </button>
			 
		 </tr>
<?php	
		
		
														$i++;
		}
														}
	?>
	<?php
	foreach ($products2 as $product){
		if(( $points_balance >=  $product['default_sell_price'] ) and $product['default_sell_price']  > 0 ){
	?>
<tr>
		<td><?php echo $i; ?></td>
		<td><?php echo $product['item_name']; ?></td>
		<td><?php echo $product['item_code']; ?></td>
		<td><?php echo $product['bar_code']; ?></td>
		<td><?php echo $product['default_sell_price']; ?></td>
		<td>
			<input type="hidden" value="<?php echo  $customer_id.'  : '.$product['id'].'  : '.$product['default_sell_price'].'  : '. $product['item_name']; ?>" id='<?php echo $product['id'];?>' />
			<button  type="button" class="btn btn-mini btn-info" value="<?php echo $product['id']; ?>" onclick="return post_claim(<?php  echo $product['id'];?>)"> Process Claim </button>
			 
		 </tr>
<?php	
														$i++;
		}
														}
	?>
	<?php
	foreach ($products3 as $product){
		if(( $points_balance >=  $product['default_sell_price'] ) and $product['default_sell_price']  > 0 ){
	?>
<tr>
		<td><?php echo $i; ?></td>
		<td><?php echo $product['item_name']; ?></td>
		<td><?php echo $product['item_code']; ?></td>
		<td><?php echo $product['bar_code']; ?></td>
		<td><?php echo $product['default_sell_price']; ?></td>
		<td>
			<input type="hidden" value="<?php echo  $customer_id.'  : '.$product['id'].'  : '.$product['default_sell_price'].'  : '. $product['item_name']; ?>" id='<?php echo $product['id'];?>' />
			<button  type="button" class="btn btn-mini btn-info" value="<?php echo $product['id']; ?>" onclick="return post_claim(<?php  echo $product['id'];?>)"> Process Claim </button>
			 
		 </tr>
<?php	
														$i++;
		}
														}
	?>
</table>
<?php										
														
													}
														
														?>
									
</div>			
								</div><!-- /.box-body -->
							</div><!-- /.box -->
						</div>
						
						
					</div>
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="glyphicon glyphicon-ban-circle"></i>
						Limited Access
					</div>
					<?php
						}
					?>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			<footer class="main-footer text-center">
				<strong>Copyright &copy; <?php echo date("Y"); ?> BSMPOS.</strong> All rights reserved.
			</footer>
		</div><!-- ./wrapper -->
		
		
		
		<!-- jQuery 2.1.3 -->
		<script src="plugins/jQuery/jQuery-2.1.3.min.js"></script>
		<!-- Bootstrap 3.3.2 JS -->
		<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
		<!-- SlimScroll -->
		<script src="plugins/slimScroll/jquery.slimScroll.min.js" type="text/javascript"></script>
		<!-- FastClick -->
		<script src='plugins/fastclick/fastclick.min.js'></script>
		<!-- DATA TABES SCRIPT -->
		<script src="plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
		<script src="plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
		<!-- AdminLTE App -->
		<script src="dist/js/app.min.js" type="text/javascript"></script>
		
		<script src="plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/cashsalesregister.js" type="text/javascript"></script>
		

		<script>
			function post_claim(claim){
				var data1 = $('#'+claim).val();
			 

				var request = $.ajax({ url: 'register_customer_claims.php', type: "POST",  data: { searching:  data1 } ,  dataType: "html"  });


				request.done(function(reply ) {   $('#Loadhere').html(reply);      });

				 

				return false;
			}
		</script>
	</body>
</html>