<?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>All Claims</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">
							<div class="card card-primary" id="customerclaims" style="">
								<div class="card-body">
									
									 <table id="example2" class="table table-bordered table-hover">
												<thead>
													<tr>
														<th style="display:none;"></th>
														<th>#</th>
														<th>Customer Name</th>
														<th class="text-right">Claim</th>
														<th class="">Product Name</th>
														<th class="">Product Code</th>
														<th class="">Price</th>
														<th>Date</th>
													</tr>
												</thead>
												<tbody>
													 
													<form class="form" method="get" action="allclaims.php">
													
													<div class="row">
														<div class="col-lg-4">
															From<br />
															<input type="text" class="form-control" name="start_date" placeholder="YYYY-MM-DD" id="start_date" >
														</div>
														<div class="col-lg-4">
															To<br />
															<input type="text" class="form-control" name="end_date" placeholder="YYYY-MM-DD" id="end_date" >
														</div>
														<div class="col-lg-4">
															&nbsp;<br />
															<input type="submit" class="form-control btn-info" value="Search" />
														</div>
													</div>
														  
													</form>
													
													<?php
													$customer_id = null;
													$customer_name = "UNKNOWN";
													
													/* $customers = DB::query("SELECT id, name FROM customers");
	foreach ($customers as $customer)
	{
		$customer_name = $customer['name'];
		$customer_id = $customer['id'];
	} */
													
													
													if(isset($_GET['start_date'])){
															$start_date = $_GET['start_date'];
													}else{
														/* $today = date('Y-m-d');
														$start_date = date('Y-m-d', strtotime($today. ' - 1 days')); */

														$start_date = date('Y-m-d');
													}
													
													if(isset($_GET['end_date'])){
															$end_date = $_GET['end_date'];
													}else{
														/* $today = date('Y-m-d');
														$end_date = date('Y-m-d', strtotime($today. ' + 1 days')); */

														$end_date = date('Y-m-d');
													}
														$items = DB::query("SELECT id, customer_id, claim, date, product_id FROM customer_claims where date between '{$start_date}' and '{$end_date}'");
														
														$total_claims = 0;
														
														$product_name = "UNKNOWN";
																$bar_code = "UNKNOWN";
																$price = "UNKNOWN";
														
														foreach ($items as $item)
														{
															$customer_id = $item['customer_id'];
															
															$customers = DB::query("SELECT id, name FROM customers where id = '{$customer_id}'");
															foreach ($customers as $customer)
															{
																$customer_name = $customer['name'];
																$customer_id = $customer['id'];
															}
															
															//get product info
															$product_info = DB::query("SELECT id, item_name, bar_code, default_sell_price from stock_item where id = '".$item['product_id']."'");
															
															
															foreach ($product_info as $prdct)
															{
																$product_name = $prdct['item_name'];
																$bar_code = $prdct['bar_code'];
																$price = $prdct['default_sell_price'];
															}
													?>
													<tr>
														<td style="display:none;"></td>
														<td><?php echo $customer_id; ?></td>
														<td>
															<?php echo $customer_name; ?>
														</td>
														<td class="text-right"><?php $total_claims = $total_claims + $item['claim']; echo $item['claim']; ?></td>
														
														<td><?php echo $product_name; ?></td>
														<td><?php echo $bar_code; ?></td>
														<td><?php echo $price; ?></td>
														
														<td><?php echo $item['date']; ?></td>
													</tr>
													<?php
														}
													?>
													<tr style="background-color: #ccc;">
														<td style="display:none;"></td>
														<td></td>
														<td></td>
														<td class="text-right"><b><?php echo $total_claims; ?></b></td>
														<td></td>
														<td></td>
														<td></td>
														<td></td>
													</tr>
												</tbody>
											</table>
										 
								</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 -->

			 
		</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>
		
				<!-- bootstrap datepicker -->
		<script src="<?php echo $base_url; ?>plugins/datepicker/bootstrap-datepicker.js"></script>

		<script type="text/javascript">
		//Date picker
    $('#start_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
	
	$('#end_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
		</script>
	</body>
</html>