<?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>Customers</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<?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">
							<span style="padding: 10px; margin: 3px; background-color: #3C8DBC;" >
							<a href="createcustomer.php" style="color: #fff;"><i class="fa fa-plus"></i> New Customer</a>
							</span>
							
							<span style="padding: 10px; margin: 3px; background-color: #3C8DBC;" >
							<a href="searchforcustomer.php" style="color: #fff;"><i class="fa fa-plus"></i> Search for Customer</a>
							</span>
							
							<!-- <span style="padding: 10px; margin: 3px; background-color: #3C8DBC;" >
							<a href="makeclaim.php" style="color: #fff;"><i class="fa fa-plus"></i> Make Claim</a>
							</span> -->
							
							<!-- <span style="padding: 10px; margin: 3px; background-color: #3C8DBC;" >
							<a href="customerpreviousclaims.php" style="color: #fff;"><i class="fa fa-plus"></i> Previous Claims</a>
							</span> -->
						</div>
					</div>
					<div>&nbsp;</div>
					<?php
						if(isset($_GET['product_id'])){
							
							$product_id = trim($_GET['product_id']);
							$product = trim($_GET['product']);
						 
							$customer_id = trim($_GET['customer_id']);
                                    $customer_claim = trim($_GET['product_price']);
									 $customer_claim_date = date('Y-m-d');
									 
									 //get points of customer
									 
									 
									 $items = DB::query("SELECT id, customer_name, value_date, bill, claim FROM cash_receipt where customer_id = '{$customer_id}'");
														
									$total_bills = 0;
									$total_claims = 0;
									
									foreach ($items as $item)
									{
										  $total_claims = $total_claims + ($item['claim']/100)*($item['bill']);
									}
									
									//get previous claims
									$claims = DB::query("SELECT id, customer_id, claim from customer_claims where customer_id = '{$customer_id}'");
									$previous_claims = 0;
									
									
									foreach ($claims as $claim)
									{
										  $previous_claims = $previous_claims + $claim['claim'];
									}

									 
                                    DB::$error_handler = false;
		                            DB::$throw_exception_on_error = true;
									
									if($customer_claim <= ($total_claims - $previous_claims)){
										try
										{
										   
											$trxnConnextn = new MeekroDB();
											$trxnConnextn->startTransaction();
											
											$trxnConnextn->insert
											('customer_claims', 
												array
												(
													'customer_id' => $customer_id,
													'claim' => $customer_claim,
													'date' => $customer_claim_date,
													'product_id' => $product_id,
													'personel' => $_SESSION['connecteduserid'],
													'shop_id' => $_SESSION["connecteduserbranch"]
												)
											);


											$insertedrecord = $trxnConnextn->insertId();
											$trxnConnextn->commit();
											
											///////////////////////
	  
											$pricedata = DB::query('SELECT quantity FROM item_stock_level WHERE id ='.$product.''); 
											if(sizeof($pricedata) >0){
												 
													$quantity =  $pricedata[0]['quantity'] - 1 ;
													 
													DB::query('UPDATE item_stock_level SET quantity = '.$quantity.' WHERE id  = '.$product.'');
											}
												 
											
											/////////////////////
											
									?>
									<div class="row">
										<div class="col-md-12">
											<p style="padding: 5px; background-color: green; color: #fff;">Customer claim successesfully saved.</p>
										</div>
									</div>
									<div>&nbsp;</div>
									<?php
										}
										catch(MeekroDBException $e)
										{
											$err = $err.$e->getMessage()."<br>";
											$err = $err.$e->getQuery()."<br>";
											$trxnConnextn->rollback();
											$errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
											$server_response[] = array('issaved' => 0, 'message' => $errormessage);
											DB::$error_handler = 'meekrodb_error_handler';
											DB::$throw_exception_on_error = false;
											echo json_encode($server_response);
											exit();
										}	
									}else{
									?>
									<div class="row">
										<div class="col-md-12">
											<p style="padding: 5px; background-color: red; color: #fff;">You are trying to make a claim (<?php echo $customer_claim.'>>>'.$customer_claim; ?>) which is more than the points your have (<?php echo ($total_claims - $previous_claims); ?>).</p>
										</div>
									</div>
									<div>&nbsp;</div>
									<?php	
									}
		
                                    
					?>
						
					<?php
						}
					?>
					
					<div class="row">
						<div class="col-md-12">
							<div class="card card-primary" id="customerdetails" style="">
								<div class="card-body">
									
									<div>
										<div id="cartitemslist">
											 
										</div>
									</div>
								</div><!-- /.card-body -->
							</div><!-- /.card -->
						</div>
						
						
					</div>
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="fa fa-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 -->
		
		
		
	<?php include ('includes/general_footer.php')?>
		
		<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 () {
    //Initialize Select2 Elements
    $('.select2').select2();
 
	function test(){
		alert();
	}
	$('#change_display').hide();

	 
 
  })
  
  // AJAX call for autocomplete 
$(document).ready(function(){
	$("#search-card").css("background","#ffe6e6");
	
	$("#search-card").keyup(function(){
		 
		$.ajax({
		type: "POST",
		url: "get_customers.php",
		data:'keyword='+$(this).val(),
		beforeSend: function(){
			 
			$("#clientname").css("background","#FF0");
		},
		success: function(data){ 
			$("#clientname").html(data); 
			 
			$("#clientname").css("background","#ffcccc");
			$("#search-card").css("background","#ffffff");
		}
		});
	});
});
//To select country name
 
</script>
	</body>
</html>