<?php
	include("my_functions.php");
	$pagetitle = "Shop Sales Summary";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$err= "";
	
	$startdatevalid = false;
	$from = DATE('Y-m-d'); 
	$to = DATE('Y-m-d');
	$shop = $_SESSION["connecteduserbranch"];
	
	if(isset($_POST['getreceiptsbtn']))
	{
		if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] < 5)
		{
			
			if($_POST['shop']=="")
			{
				$err = $err."Shop Selection Required!<br>";
			}
			else if($_SESSION["connectedusergroup"] == 3)
			{
				if($_SESSION["connectedusergroup"] == 3 && $_POST['shop'] != $_SESSION["connecteduserbranch"])
					$err = $err."You Are Not Authorised To Access date From Other Shops!<br>";
			}
			else
			{
				if($_SESSION["connectedusergroup"] == 4 && $_POST['shop'] != $_SESSION["connecteduserbranch"])
					$err = $err."You Are Not Authorised To Access date From Other Shops!<br>";
			}
			
			
			
			if( $_POST['from']=="")
			{
				$err = $err."Start Date Required!<br>";
			}
			 
			
			if(trim($_POST['to'])=="")
			{
				$err = $err."End Date Required!<br>";
			}
			else
			{
				 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
							{
								$to = $_POST['to'];
								$from = $_POST['from'];
								$shop = intval($_POST['shop']);
								 
							}
			}
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				 

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] < 5)
							{
						?>
						<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">Sales breakdown</h6>
              </div>
              <div class="card-body">
			  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
									
                <div class="row d-print-none">
                  <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");
													//salesbreakdown.php
													
													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="col-4">
                   	<label for="from">Start Date</label>
											<input type="date" value="<?php echo isset($_POST['from']) ? $_POST['from'] : "" ?>" name="from" id="from" class="form-control"  />
										
                  </div>
									  <div class="col-5">
										<label for="to">End Date</label>
											<div class="input-group">
												<input type="date" value="<?php echo isset($_POST['to']) ? $_POST['to'] : "" ?>" name="to" id="to" 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 class="card-body table-responsive  "> <br /><br /><br />
								 
								
								<div class="col-md-12" style="margin-top: 25px"></div>
								<?php
									if($err==""  )
									{ 
								 $pdata = payment_types($from, $to , $shop);
								 
								 if(!empty($pdata)){
								     
								     echo '<h3 lign="center"> GENERAL REPORT FROM ('.$from.'- '.$to.')</h3>';
								
								?>
								
								 
								<table class="table table-striped table-bordered" id="receiptstable">
								
								<thead>
										<tr>
											<th>Mode</th>
											<th  >Sales</th>
											<th  >Deposits</th>
											<th  >Workshop / Repairs</th>
											<th  >Top Ups</th>
											<th  >Invoice Top Ups</th>
											<th  >Total</th>
										</tr>
									</thead>
								
								
								 
								<?php
								 
								
								$from=  date('Y-m-d', strtotime($from. ' - 1 days'));
								$to  =  date('Y-m-d', strtotime($to. ' + 1 days'));
								
								$shop= $_SESSION["connecteduserbranch"];
								
								 if($_SESSION["connectedusergroup"]==4){
									$shop = $_SESSION["connecteduserbranch"];
								}
								echo $pdata ;
								
								
								
								?>
								</table>
								
								<?php
									}
									}
								
								
								
									 
								?>
								 
								 
							     </div>
							   </div>
						  <!-- /.card-body -->
						</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')?>
		<!-- InputMask -->
		<script src="plugins/input-mask/jquery.inputmask.js" type="text/javascript"></script>
		<script src="plugins/input-mask/jquery.inputmask.date.extensions.js" type="text/javascript"></script>
		<script src="plugins/input-mask/jquery.inputmask.extensions.js" type="text/javascript"></script>
		<!-- AdminLTE App -->
	 
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script>
			$(function() 
			{
				//Datemask2 mm/dd/yyyy
				$("#datemask2").inputmask("mm/dd/yyyy", {"placeholder": "mm/dd/yyyy"});
				//Money Euro
				$("[data-mask]").inputmask();
			});
		</script>
	</body>
</html>