<?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['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
							{
								if($_POST['shop'] == "x")
								{
									$salesdata = DB::queryFirstRow("SELECT 'All Shops' AS full_name, COUNT(cash_receipt.id) AS trxncount, COALESCE(SUM(cash_receipt.bill), 0) AS totalsales FROM cash_receipt WHERE cash_receipt.value_date>=%s AND cash_receipt.value_date<=%s AND cash_receipt.is_cancelled = 0", date("Y-m-d", strtotime(trim($_POST['from']))), date("Y-m-d", strtotime(trim($_POST['to']))));
									
									if(DB::count() == 0)
										$err = $err."No Sales Information Found!<br>";
								}
								else
								{
									$salesdata = DB::queryFirstRow("SELECT shop.full_name, COUNT(cash_receipt.id) AS trxncount, COALESCE(SUM(cash_receipt.bill), 0) AS totalsales FROM cash_receipt INNER JOIN shop ON cash_receipt.shop = shop.id WHERE cash_receipt.shop=%i AND cash_receipt.value_date>=%s AND cash_receipt.value_date<=%s AND cash_receipt.is_cancelled = 0 GROUP BY cash_receipt.shop", $_POST['shop'], date("Y-m-d", strtotime(trim($_POST['from']))), date("Y-m-d", strtotime(trim($_POST['to']))));
									
									if(DB::count() == 0)
										$err = $err."No Sales Information Found!<br>";
								}
							}
						}
					//}
				 
			}
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h6>Shop Sales Summary</h6>
					<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">
                <h6 class="card-title">Sales Summary</h6>
              </div>
              <div class="card-body">
			  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
									
                <div class="row">
                  <div class="col-4">
                    <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="col-4">
                    <label for="from">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>
			  </div>
              <!-- /.card-body -->
            </div>
							
							 
								 
								
								<div class="col-md-12" style="margin-top: 25px"></div>
								<div>
								<?php
									if($err=="" && isset($_POST['getreceiptsbtn']))
									{
								?>
								 
								
								
								 
								<?php 
								
								$shop =  $_POST['shop'];
								 
								  
								 $start = date("Y-m-d", strtotime($_POST['from']));
								  $end  = date("Y-m-d", strtotime($_POST['to']));
								 
								 
								
								?>
								
								
								
								 <h4> Total sales between <?php echo $start; ?> -  <?php echo $end; ?></h4>
								 <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>
								  
								  
								  
								  
								  
								   <table class="table table-striped   table-hover" id="ziana">
												<thead>
												
												<tr class="info">
												<th>Shop </th>   <th>Items</th> <th>Total Items</th>  
												</tr>
												</thead>
								 
								<?php
								
								  
								
								  sales_summary($shop , $start, $end);
									 
								?>
								
								</table>
								
								 <h4> Total Losses between <?php echo $start; ?> -  <?php echo $end; ?></h4>
								 <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="$('#loss_summary').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px">  Download XLS</a>
								   
								  </button>
								  
								  
								  
								  
								  
								  <table class="table table-striped  table-hover" id="loss_summary">
												<thead>
												
												<tr class="info">
												<th>Shop </th> <th> Type </th>   <th>Items</th> <th>Total Items</th>  
												</tr>
												</thead>
								 
								<?php
								
								  
								
								  Loss_summary($shop , $start, $end);
									 
								?>
								
								</table>
								
								
								
									<div class="col-md-12" style="margin-top: 25px"></div>
								<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="$('#summary').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px">  Download XLS</a>
								   
								  </button>
								
								  
								      <h4> Summary of sales between <?php echo $start; ?> -  <?php echo $end; ?></h4>
								 
												 <table class="table table-striped  table-hover" id="summary">
												<thead>
												
												<tr class="info"> 
												<th>Item </th> <th>Bar code </th> <th>Shop </th>  <th>Unit Cost</th><th>Unit SP   Set </th><th>Qty Sold</th> <th>@Sold  </th>  	<th>Total CP </th>	  	<th>Total SP </th>
												</tr>
												</thead>
								 
								<?php
								 
								 
								
								 sale_details($shop , $start, $end);
								 
								?>
								
								</table>
								
								
								 <h4> Faulty / Damages Reported </h4>
								 
								 <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="$('#Loss').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px">  Download XLS</a>
								   
								  </button>
												 <table class="table table-striped  table-hover" id="Loss">
												<thead>
												
												<tr class="info">
												<th>Item </th> <th>Bar code </th> <th>Shop </th><th>Qty </th>  <th>Unit Cost</th><th>Unit SP   Set </th> <th>@Loss  </th>  		<th>Total Loss </th> <th>Loss Type</th> 
												</tr>
												</thead>
								 
								<?php
								 
								 
								
								  Defect_sale_details($shop , $start, $end);
									 
								?>
								
								</table>
								</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>

		<!-- jQuery 2.1.3 -->
	 
		 
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/store.js" type="text/javascript"></script>
	</body>
</html>