<?php
	 
	$pagetitle = "Shop Stock";
	require ('my_functions.php');
	 require('includes/applicationtop.php');
	 require('includes/applicationmenu.php');
	 
	
	$err= "";
	$stocklistfound = FALSE;
	
	
	
	
	if(isset($_POST['getitembtn']))
	{
		if($_POST['shop']=="")
		{
			$err = $err."Shop Selection Required!<br>";
		}
		else
		{	
			if(trim($_POST['itemsearchtext'])=="")
			{
				$err = $err."Item Name, Code Or Barcode Required!<br>";
			}
			else
			{
				if($_SESSION["connectedusergroup"] == 3 && ($_POST['shop'] != $_SESSION["connecteduserbranch"]))
				{
					$err=$err."You Are Not Authorised To Access Stock Information From Other Shops!<br>";
				}
				$search_string =  (string) trim($_POST['itemsearchtext']);
				$shop = $_POST['shop']; 
				     $pricedata = DB::query("SELECT DISTINCT(id), item_name, category,  item_name, bar_code, item_code FROM  stock_item  WHERE  item_name  LIKE '%".$search_string."%' OR bar_code  = '".$search_string."' OR item_code  = '".$search_string."'    ORDER BY  item_name  ASC    "  );
		  
				  if(DB::count() == 0)
				{
					$err=$err."No Item Found!<br>";
				}
				else
				{
					$stocklistfound = TRUE;
				}  
				
			 
			}
		}
	}

?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Stock Ledger</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"] == 3 || $_SESSION["connectedusergroup"] == 2)
							{
						?>
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							<?php
								if($err!="")
								{
							?>
							<div class="alert alert-danger">
								<i class="fa fa-ban-circle"></i><br>
								<?php 
									echo $err; 
								?>
							</div>
							<?php
								}
							?>
							<div class="row">
								<div class="col-sm-2">
								</div>
								<div class="col-sm-8">
									 		
										<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
											<div class="form-group col-sm-6">
												<label for="shop">Select Shop</label>
												<select class="form-control" name="shop" id="shop" data-placeholder="Choose One">
													<option value="0">Choose One</option>
													<?php
														$results = DB::query("SELECT id, full_name FROM shop");
														
														foreach ($results as $row)
														{
													?>
													<option <?php if(isset($_POST['shop']) && $row['id']==$_POST['shop']) echo "selected=\"selected\""; ?> value="<?php echo $row['id']; ?>"><?php echo $row['full_name']; ?></option>
													<?php
														}
													?>
												</select>
											</div><!-- form-group -->
											
											<div class="form-group col-sm-6">
												<label for="itemsearchtext">Item</label>
												<div class="input-group">
													<input type="text" value="<?php echo isset($_POST['itemsearchtext']) ? $_POST['itemsearchtext'] : "" ?>" name="itemsearchtext" id="itemsearchtext" class="form-control" placeholder="Enter Item Name, Code Or Barcode" />
													<span class="input-group-btn">
														<button class="btn btn-primary btn-flat" type="submit" name="getitembtn" id="getitembtn">Go!</button>
													</span>
												</div>
											</div><!-- form-group -->
										</form>
									 
								</div>
								<div class="col-sm-2">
								</div>
							</div>
							
							<?php
								if($stocklistfound && $err=="")
								{
							?>
							<h3 class="card-title text-center" >Search Result Set Items List</h3>
							<div class="card-body table-responsive no-padding">
							<h6 class="card-title text-center" id ='deleted'> </h6>
								<div class="card-body table-responsive" id ='item' >
									<table id="stockitemstable" class="table table-striped">
										<thead>
											<tr> 
												<th>Name</th>
												<th>Category</th>
												<th>Code</th>
												<th>Barcode</th>
												 
												<th class="text-right">Stock Qty</th>
												 
												<th class="text-right">Items Transfered</th>
												<th class="text-right">Balance</th>
												<th class="text-right">Views</th>
												 
											</tr>
										</thead>
										<tfoot>
											<tr>
												 
												<th>Name</th>
												<th>Category</th>
												<th>Code</th>
												<th>Barcode</th>
												 
												<th class="text-right">Stock Qty</th>
												 
												<th class="text-right">Items Transfered</th>
												<th class="text-right">Balance</th>
												<th class="text-right">Views</th>
												 
											</tr>
										</tfoot>
										<tbody>
											<?php
											 
											 
												 foreach($pricedata as $row)
												{
												 
												$no_sold_special  = $no_sold  =0;
												 
												 $item = 0;
												 $item_id = DB::query("SELECT  id from stock_item WHERE item_code   = ".$row['item_code']."  ");
												foreach($item_id as $rowi)
												{
													 $item_price_id = DB::query("SELECT  id from item_price WHERE shop  = ".$shop." AND item   = ".$rowi['id']."  ");
													 foreach($item_price_id as $rowid)
												        {
															  $item = $rowi['id'];
														 
															 $item_id = DB::query("SELECT  SUM(quantity) as qty FROM cash_sale_item WHERE price_id   = ".$rowid['id']."  ");
															 
															  $no_sold  = $item_id[0]['qty'];
															  
															 
															  $special_sales = DB::query('SELECT  SUM(quantity) as qty FROM special_sale_item WHERE  is_cancelled ="0" AND price_id   = '.$rowid['id'].'  AND shop = '.$shop.' ');
															   
															   
															   $no_sold_special  = $special_sales[0]['qty'];
															   
															 
															 
														 
															
															
															
														}   
												}   

                                             $shop = $_POST['shop'];
											 $it_qty = 0;
											 if($item > 0){
											      $it_qty = No_sold($item , $shop);
												  // $it_qty =  Current_stock($item , $shop);
											 }
											 
											$balance =  ($it_qty - $no_sold -  $no_sold_special);
											if($balance < 0){
												$balance = 0;
											}
											

											 
												
												
												 

												?>
											  <tr>
												 
												<td style="vertical-align:middle;"><?php  echo $row['item_name']; ?></td>
												<td style="vertical-align:middle;"><?php echo Item_category( $row['category']); ?></td>
												<td style="vertical-align:middle;"><?php  echo $row['item_code']; ?></td>
												<td style="vertical-align:middle;"><?php  echo $row['bar_code']; ?></td>
												<?php  if( $it_qty  < 1) {  $it_qty  = 0;  }?>
												 
												 
												<td style="vertical-align:middle;" class="text-right"><?php  if( $no_sold > 1){echo ( $no_sold); } else echo 0; ?></td>
												<td style="vertical-align:middle;" class="text-right"><?php   echo Sum_received($item , $shop);
												  ?></td>
											 
												
                                               <td style="vertical-align:middle;" class="text-right"><?php echo $balance;?></td>
											   
											   <td style="vertical-align:middle;" class="text-right"> 
											   <a href="#"  onclick ='More_details(<?php echo $item; ?> , <?php echo $shop; ?>)'>
											   Details </a> 
											   
											   </td>
												
												 
											</tr>  
											<?php
												 }
											?>
										</tbody>
									</table>
									
									 
									 
									 
								</div>
							</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>
 
    function More_details(info , shop){
		 
		 
	 $("#item").empty();
         $.ajax({
                type: 'POST',
                url: 'item_ledger_details.php',
                data: {category: info, shop: shop},
				success: function(html){ 
				 $("#item").append(html);
				},
                dataType: 'html'
         });   
    }
	
	 function Recal_items(item , shop , id){
		 
		 $("#deleted").empty();
         $.ajax({
                type: 'POST',
                url: 'Recall_items.php',
                data: {  item: item, id: id,   shop: shop},
				success: function(html){ 
				 $("#deleted").append(html);
				 More_details(item , shop);
				},
                dataType: 'html'
         }); 
		  	 
	   
    }
	
	 function complete_recall( ){
		 if($('#quantity').val() < 1){
			 alert('Please enter a value greater or equal to zero');
			 return false;
		 }
		 
		  
         $.ajax({
                type: 'POST',
                url: 'Recall_items.php',
                data: {  item_id: $('#item_id').val(), specific_id: $('#specific_id').val(),   shop_id: $('#shop_id').val(), quantity:$('#quantity').val()},
				success: function(html){ 
				 $("#flash").html(html);
				  More_details($('#item_id').val() , $('#shop_id').val());
				},
                dataType: 'html'
         }); 
		  	 
	   
    }
	//
	
	
	 
</script>

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