<?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['expanse']=="")
			{
				$err = $err."Shop Selection Required!<br>";
			}
			else
			{
				if($_SESSION["connectedusergroup"] == 3 && $_POST['expanse'] != $_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
		{
			$err = $err."Limited Access!<br>";
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
			 

				<!-- 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">
                <h6 class="card-title">Expense report</h6>
              </div>
              <div class="card-body">
			  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
									
                <div class="row">
                  <div class="col-4">
                  <label for="expanse">Select category</label>
											<select  class="form-control" name="expanse" required  id="expanse" data-placeholder="Choose One">
												<option value="x">All Types</option>
												 
				  
				  <?php
										$shopsalesdata = DB::query("SELECT *from expanse_types ");
										
									 
										
										foreach($shopsalesdata as $row)
										{
										 
										 
											echo ' <option value='.$row['eid'].'>'.$row['expanse_type'].'</option>';
									?>
									
										 
									<?php
										}
									?>
									 
                        </select>
                  </div>
                  <div class="col-4">
                    	<label for="from"> Between Start Date</label>
											
											 
											
											<input type="text" value="<?php echo isset($_POST['from']) ?  $_POST['from'] : "" ?>" name="from" id="from"  data-date-format="dd-mm-yyyy"  class="form-control"   />
										
                  </div>
                  <div class="col-4">
                    <label for="to">End Date</label>
											<div class="input-group">
											
											 
											
												<input type="text" 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>
                </div>
				</form>
				</div>
              </div>
              <!-- /.card-body -->
            </div>
							
							<div class="card-body table-responsive no-padding">
								 
								
								<div class="col-md-12" style="margin-top: 25px"></div>
								<div>
								<?php
									if($err=="" && isset($_POST['getreceiptsbtn']))
									{
								?>
								  
								
								<div class="col-md-12" style="margin-top: 25px"></div>
								<?php 
								
								$expanse =  $_POST['expanse'];
								 
								  
								 $start = date("Y-m-d", strtotime($_POST['from']));
								  $end  = date("Y-m-d", strtotime($_POST['to']));
								 
								 
								
								?>
								
								 <h6> Expanses  between <?php echo $start; ?> -  <?php echo $end; ?></h6>
								 <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
									 <span class="fa fa-th-list"></span> <a href="#" onclick="$('#ziana').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px">  Download XLS</a>
								   
								  </button>
								  
								  
								  			  <a href='newexpanse.php'> Add New</a>
			   <table class="table">
			    <thead>
                    <tr>
                    
                      <th>Amount</th>
					  <th>Type</th>
                      <th>Date</th>
					   <th>Branch</th>
					  <th>Notes</th>
					  <th>Captured By</th>
					 
                       
                    </tr>
                  </thead>
			  <?php
			                       

								   //
								    $money = 0;
									$shop_label = '';
									$shopsalesdata = array();
									if($expanse  > 0){
										$shop_label =  ' and expanses.expanse='.$expanse .'   ';
										
										 
										$shopsalesdata = DB::query("SELECT exid, examount, expanse_type, exnotes, full_name, exdate, first_name , last_name FROM `expanses` 
										join shop on shop.id = exbranch left JOIN expanse_types on expanse_types.eid=expanse JOIN personnel_details on personnel_details.id=captured_by 
										WHERE expanses.exdate BETWEEN CAST('".$start."' AS DATE) AND CAST('".$end."' AS DATE) ");
										
										
									}else  {
									   
			  
										$shopsalesdata = DB::query("SELECT exid, examount, expanse_type, exnotes, full_name, exdate, first_name , last_name FROM `expanses`  join shop on shop.id = exbranch left JOIN expanse_types on expanse_types.eid=expanse JOIN personnel_details on personnel_details.id=captured_by 	limit 10");
										
									 
									  }
									 
										
										foreach($shopsalesdata as $row)//
										{
										 
										 
											echo ' <tr > 
											<td>'.$row['examount'].'</td>
											<td>'.$row['expanse_type'].'</td>
											<td>'.$row['exdate'].'</td>
											<td>'.$row['full_name'].'</td>
											 <td>'.$row['exnotes'].'</td>
											
											<td>'.$row['first_name'].' , '.$row['last_name'].'</td> 
											</tr>';
									 
										}
			  ?>
			  </table>
								 
								<?php
								
								  
								
							  //sales_summary_details($shop , $start, $end);
									 
								?>
								
							 
									 
								   
								
								
								
								</div>
							</div>
						</div><!-- /.card-body -->
						<?php
							}
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->
 
		</div><!-- ./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="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/store.js" type="text/javascript"></script>
	</body>
</html>