<?php
	include("my_functions.php");
	 
	$pagetitle = "Store Quantities By Category";
    require('includes/applicationtop.php');
	 require('includes/applicationmenu.php');
	
	$err= "";
	
	$itemscount = 0;
	$seles_details = '';
	
	 
	
	function Item_name($item){
		$name = 'Select one';
		$categorydata = DB::query("SELECT item_name FROM stock_item where id =%i" , $item);
			if(DB::count() != 0){										
		       foreach ($categorydata as $category)
	              {
			 $name =  $category['item_name'];
			  
		          }
			}
			return $name;
	}
	
	 
	
	function PriceId($item , $shop){
		$id = 0;
		$count =0;
		$categorydata = DB::query("SELECT id FROM item_price where item=%i AND shop =%i"  , $item , $shop);
			if(DB::count() != 0){										
		       foreach ($categorydata as $category)
	              {
					  if($count >0){
						  exit('More data foiund');
					  }
			          $id =  $category['id'];
					 
					  $count +=1;
					
			  
		          }
			}
			return $id;
	}
	
	//Wild Bill Hickok
	//Billy the Kid (1859-1881)
	//John Wesley Hardin (1853-1895)
	//Wyatt Earp (1848-1929)
	
	 
	
	function Item_sold_with_id($price_id , $shop){
		$qty = 0;
		$categorydata = DB::query("SELECT sum(quantity) as qty  FROM cash_sale_item where price_id=%i  " , $price_id , $shop);
			if(DB::count() != 0){										
		       foreach ($categorydata as $category)
	              {
			 $qty =  $category['qty'];
			  
		          }
			}
			return $qty;
	}
	
	function Format_dates($source ){
		 
    return  date("Y-m-d", strtotime($source)); 
       
    
	}
	
	 
	
		 
	
	
	  
	
		function No_disposed($price_id, $start, $end , $shop , $type){
			$epecific_price_id_sales = 0;
			 $stockitemsdata = DB::query('SELECT sum(quantity) as qty  FROM special_sale_item WHERE bounce_type = '.$type.' AND  is_cancelled = 0 AND  shop = '.$shop.'  AND price_id ='.$price_id.' and created_date >= STR_TO_DATE("'.$start.'", "%Y-%m-%d") AND created_date < STR_TO_DATE("'.$end.'", "%Y-%m-%d") + INTERVAL 1 DAY  ORDER BY id'); 
		
			 
		     if(DB::count() != 0){
				foreach ($stockitemsdata  as $category)
	              {
			       $epecific_price_id_sales =  $category['qty'];  
			  
		          } 
				
			 }
			 return $epecific_price_id_sales;
	}
	
	
		  
	
 
	
	
	
	if(isset($_POST['category']))
	{
		 $start = strtotime($_POST['start']);
         $end = strtotime($_POST['end']);
		if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 2)
		{
			if($end < $start){
				$err=$err."End date should atleast be greater than start date!<br>";
				
			}
			 
			//if(DB::count() == 0)
				//$err=$err."No Stock List For Selected Category 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">
					<h1>Stock movement report</h1>
					 
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 2)
							{
						?>
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							
							<div class="card card-danger">
              <div class="card-header">
                <h3 class="card-title">Different Width</h3>
              </div>
              <div class="card-body">
			  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
										 						
									
                <div class="row">
                  <div class="col-2">
                    <label for="category">Start DATE</label>
											<input class="form-control col-sm-12"  required value='<?php  if(isset($_POST['start'])){ echo $_POST['start']; }?>' type='date' name ='start' id ='start' />
										 
                  </div>
                  <div class="col-2">
                     <label for="category">End DATE</label>
											<input class="form-group col-sm-12" required value='<?php  if(isset($_POST['end'])){ echo $_POST['end']; }?>'   type='date' name ='end' id ='end' />
									 
                  </div>
                  <div class="col-2">
                   <label for="category">Stock Item Category</label>
											<select   class="form-control category" name="category" required id="category"   data-placeholder="Choose One">
											
											 <option value="0">All</option>
												<?php
													$categorydata = DB::query("SELECT id, category_name FROM item_category ORDER BY category_name");
													
													foreach ($categorydata as $category)
													{
												?>
												<option <?php if(isset($_POST['category']) && $category['id']==$_POST['category']) echo "selected=\"selected\""; ?> value="<?php echo $category['id']; ?>"><?php echo $category['category_name']; ?></option>
												<?php
													}
												?>
											</select>
                  </div>
				  <div class="col-2">
                    <label for="category">Item</label> <br />
											<select   class="form-control  " name="item"  id="item"   data-placeholder="Choose One">
											<option value="<?php  if(isset($_POST['item'])){ echo $_POST['item']; } else echo 0;?>"><?php  if(isset($_POST['item'])){ echo Item_name($_POST['item']); }else {?>No item selected <?php }  ?></option>
											 <?php if(isset($_POST['item'])){ ?>
											 <option value="0">Select All</option>
											 
											 <?php
											 $categorydata = DB::query("SELECT item_name, id FROM stock_item where category =%i" , $_POST['category']);
												if(DB::count() != 0){
													$name ='';
													
												   foreach ($categorydata as $category)
													  {
														 
														echo  '<option value="'.$category['id'].'">'.$category['item_name'].'</option>';
												  
													  }
												}
											 
											 }?>
											</select>
                  </div>
				  <div class="col-2">
                    <label for="category">Shop</label> <br />
											<select   class="form-control " name="shop"   id="shop"   data-placeholder="Choose One">
											 
											<option value="<?php  if(isset($_POST['shop'])){ echo $_POST['shop']; } else echo 0;?>"><?php  if(isset($_POST['shop'])){ echo Shop_name($_POST['shop']); }else {?>No item selected <?php }  ?></option>
											 <?php ?>
											  <option value="0">Select All</option>
										 
											 <?php
											 
											 $categorydata = DB::query("SELECT full_name , id FROM shop  " );
													if(DB::count() != 0){										
													   foreach ($categorydata as $category)
														  {
															  echo ' <option value="'.$category['id'].'">'.$category['full_name'].'</option>';
													 
													  
														  }
													}
											 ?>
											</select>
                  </div>
				  
				  <div class="form-group col-1">
											<label for="category"> </label> <br />
											<button type='submit' class='btn btn-info'> Submit </button>
										 
										</div>
                </div>
				</form>	
              </div>
              <!-- /.card-body -->
            </div>
							
							<div class="card-body table-responsive no-padding">
								 
								
								<div class="col-md-12" style="margin-top: 25px"></div>
								<?php
									if($err=="" && isset($_POST['category']))
									{
								?>
								<div class="col-sm-12">
									<div class="input-group-btn">
										<button onclick="gendawano('General_item_ledger', 'categoryfocus', <?php echo $_POST['category']; ?>)" class="btn btn-sm btn-primary pull-right"><i class="fa fa-print"></i> Print</button>
									</div>
								</div>
								
								<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="$('#employees').tableExport({type:'excel',escape:'false'});"> <img src="images/xls.png" width="24px"> XLS</a>
								   
								  </button>
																
								<div class="card-body table-responsive"  id="employees">
								<h4 class="card-title text-center">  <b>Stock Movement report <?php  echo Format_dates($_POST['start']);?> to <?php  echo Format_dates($_POST['end']);?>  </b> 
												

								</h4>
									<table id="stockitemstable" class="table table-striped"  >
										<thead>
											 <tr class="success" > 
											 <th>Item  </th><th>Bar Code </th><th> From</th> <th> Destination</th>  
												   <th>Date </th><th>  Notes</th> <th> Status</th>
												   <th> Personel </th> <th> Quantity</th>
												    <th>   
													<a href="Recelling_items_from_shop.php"    >
											         Recall  </a> 
													
													</th>
												   
												   </tr> 
										</thead>
										 
											
										<?php
									 
										 
									 
										 $item_details =  Item_movements( $_POST['shop'],   $_POST['item'] , Format_dates($_POST['start']), Format_dates($_POST['end']) );
										 if( $item_details ){
											 echo  $item_details[0];
											 
										 }
										 
							 
										

										?>
										
										 <tr class="success" > 
											 <TD>Total </TD><TD>  </TD><TD>  </TD> <TD>  </TD>  
												   <TD> </TD><TD>   </TD> <TD>  </TD>
												   <TD>   </TD> <TD> <?php   echo  $item_details[1];?></TD>
												    <TD>   
													<a href="Recelling_items_from_shop.php"    >
											         Recall  </a> 
													
													</TD>
												   
												   </tr> 
										
									 
										<tbody>
											 
										</tbody>
									</table>
								</div>
								<?php
									}
									
									if($err != "")
									{
								?>
								<div class="alert alert-danger">
									<i class="fa fa-ban-circle"></i><br>
									<?php 
										echo $err; 
									?>
								</div>
								<?php
									}
								?>
							</div>
							
						</div><!-- /.card-body -->
						<div class="card-footer">
							
						</div><!-- /.card-footer-->
						<?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 -->

			<footer class="main-footer text-center">
				<strong>Copyright &copy; <?php echo date("Y"); ?> BSMPOS.</strong> All rights reserved.
			</footer>
		</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>
		
		<div class="modal" id="mediummodal">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-body">					
						<div class="row" id="mediummodaldetails">
					
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="modal" id="largemodal">
			<div class="modal-dialog modal-lg">
				<div class="modal-content">
					<div class="modal-body">					
						<div class="row" id="largemodaldetails">
							
						</div>
					</div>
				</div>
			</div>
		</div>
		
	
		<?php include ('includes/general_footer.php')?> 
<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>
 
<script>
$(document).ready(function() {
    $('select.category').change(function(){
		 $('#item') .attr("readonly", false);  
		$("#item").empty();
         $.ajax({
                type: 'POST',
                url: 'select_items_from_category.php',
                data: {category: $('select.category').val() },
				success: function(html){ 
				 $("#item").append(html);
				},
                dataType: 'html'
         }); 
    });
});
</script>

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