<?php
	include("my_functions.php");
	$pagetitle = "Shop Sales Summary";
	 require('includes/applicationtop.php');
	 require('includes/applicationmenu.php');
	
	$err= "";
	
	$startdatevalid = false;
	
	if(isset($_POST['getreceiptsbtn']))
	{
		if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 3)
		{
			  if($_POST['cat'] < 1)
			{
				$err = $err."At least one category is required!<br>";
			}
			
			if($_POST['shop']=="")
			{
				$err = $err."Shop Selection Required!<br>";
			}
			else
			{
				if($_SESSION["connectedusergroup"] == 3 && $_POST['shop'] != $_SESSION["connecteduserbranch"])
					$err = $err."You Are Not Authorised To Access Receipts From Other Shops!<br>";
			}
			
			if(trim($_POST['from'])=="")
			{
				$err = $err."Start Date Required!<br>";
			}
			else
			{
				 
				 
					if(date("Y-m-d", strtotime(trim($_POST['from']))) > date("Y-m-d"))
					{
						$err = $err."Future Start Date Not Supported.<br>";
					}
					else
					{
						$startdatevalid = true;
					}
				 
			}
			
			if(trim($_POST['to'])=="")
			{
				$err = $err."End Date Required!<br>";
			}
			else
			{
				 
					/*if(date("Y-m-d", strtotime(trim($_POST['to']))) > date("Y-m-d"))
					{
						//$err = $err."Future End Date Not Supported.<br>";
					}
					else
					{ */
						if($startdatevalid)
						{
							if(date("Y-m-d", strtotime(trim($_POST['from']))) > date("Y-m-d", strtotime(trim($_POST['to']))))
							{
								$err = $err."Start Date Can Not Be Greater Than The End Date.<br>";
							}
							else
							{
								 
							}
						}
					//}
				 
			}
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 3)
							{
						?>
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							
							<?php
								if($err!="")
								{
							?>
							<div class="alert alert-danger alert-dismissable">
								<i class="fa fa-ban"></i>
								<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><br>
								<?php 
									echo $err; 
								?>
							</div>
							<?php
								}
							?>
							
							<div class="card card-info">
              <div class="card-header">
			  <h4>Sales report by category</h4>
                
              </div>
              <div class="card-body">
			  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
									
                <div class="row">
                  <div class="col-3">
                   <label for="shop">Category</label>
											<select  class="form-control" name="cat" id="cat" data-placeholder="Choose One" required>
												<option value="0">Choose one</option>
												<?php
													$shopsdata = DB::query("SELECT id, category_name , category_description FROM item_category ");
													
													foreach ($shopsdata as $shoprow)
													{
												?>
												<option <?php if(isset($_POST['cat']) && $shoprow['id']==$_POST['cat']) echo "selected=\"selected\""; ?> value="<?php echo $shoprow['id']; ?>"><?php echo $shoprow['category_name']. " :  ". $shoprow['category_description']; ?></option> 
												<?php
													}
												?>
											</select>
										</div><!-- form-group -->
                 
                  
                  <div class="col-3">
                   
											
				<label for="shop">Select Shop</label>
											<select  class="form-control" name="shop" id="shop" data-placeholder="Choose One">
												<option value="x">All Shops</option>
												<?php
													$shopsdata = DB::query("SELECT id, full_name FROM shop ");
													
													foreach ($shopsdata as $shoprow)
													{
												?>
												<option <?php if(isset($_POST['shop']) && $shoprow['id']==$_POST['shop']) echo "selected=\"selected\""; ?> value="<?php echo $shoprow['id']; ?>"><?php echo $shoprow['full_name']; ?></option>
												<?php
													}
												?>
											</select> </div>
											
											<div class="form-group col-3">
											<label for="from">Start Date</label>
											
											 
											
											<input type="text" required  value="<?php echo isset($_POST['from']) ?  $_POST['from'] : "" ?>" name="from" id="from"  data-date-format="dd-mm-yyyy"  class="form-control"   />
										</div><!-- form-group -->
				  
				  <div class="form-group col-3">
											<label for="to">End Date</label>
											<div class="input-group">
											
											 
											
												<input type="text" required  value="<?php echo isset($_POST['to']) ?  $_POST['to'] : "" ?>" name="to" id="to"  data-date-format="dd-mm-yyyy"  class="form-control"   />
												<span class="input-group-btn">
													<button class="btn btn-primary btn-flat" type="submit" name="getreceiptsbtn" id="getreceiptsbtn">Go!</button>
												</span>
											</div>
										</div><!-- form-group -->
                </div>
				</form>
              </div>
              
			
							
							 
								<?php
									if($err=="" && isset($_POST['getreceiptsbtn']))
									{
								?>
								 
								
								
								 
								<?php 
								
								$shop =  $_POST['shop'];
								$cat =  $_POST['cat'];
								 
								  
								 $start = date("Y-m-d", strtotime($_POST['from']));
								  $end  = date("Y-m-d", strtotime($_POST['to']));
								 
								 
								
								?>
								
								
								
								 
								 
								  
								  
								  
								  
								  
								
								
								
								<p>
								<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
									 <span class="glyphicon glyphicon-th-list"></span> <a href="#" onclick="$('#summary').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px">  Download XLS</a>
								   
								  </button>
								  </p>
								
								  
								      <h6> Summary of sales for <b> <?php  echo Category_name($cat );?> </b>between <?php echo $start; ?> -  <?php echo $end; ?></h6>
								 
												 <table class="table table-striped  table-hover" id="summary">
												<thead>
												
												<tr class="info"> 
												 <th>Item </th> <th>Bar code </th> <th>Shop </th><th>Qty Sold</th>  <th>Unit Cost</th><th>Unit SP   Set </th> <th>@Sold  </th>  <th>Total CP </th> 	  	<th>Total SP </th>
												</tr>
												</thead>
								 
								<?php
								  
								 Category_sale_details($shop, $start , $end , $cat)
								 
								?>
								
								</table>
								
								 
								</div>
							</div>
						</div><!-- /.card-body -->
						<!-- /.card-body -->
            </div>
			
						<?php
							}
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="glyphicon glyphicon-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->
 
		
		<div class="modal" id="waitdialogmodal"  data-backdrop="static">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                  <div class="modal-body">
					<div class="text-center">
						<img src="<?php echo $base_url . $ajax_loading_image; ?>" />
					</div>
				  </div>
              </div>
            </div>
        </div>

		 
	
	<?php include ('includes/general_footer.php')?>
	 
<script src="js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="tableExport.js"></script>
<script type="text/javascript" src="jquery.base64.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript" src="jspdf/libs/sprintf.js"></script>
<script type="text/javascript" src="jspdf/jspdf.js"></script>
<script type="text/javascript" src="jspdf/libs/base64.js"></script> 
 <link href="css/datepicker.css" rel="stylesheet">
 
<script>
 
		$(function(){
			 
			$('#from').datepicker();
			$('#to').datepicker();
			 
			 
		});
	</script>

		 
		<script src="dist/js/app.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/store.js" type="text/javascript"></script>
	 	</body>
</html>