<?php
require_once("../config/config.php");
@extract($_GET);
## selected location
if($locationcode!=""){
	$loc="asc_code='".$locationcode."'";
	$imeiloc="owner_code='".$locationcode."'";
}else{
	$locstr=getAccessLocation($_SESSION['userid'],$link1);
	$loc="asc_code in (".$locstr.")";
	$imeiloc="owner_code in (".$locstr.")";
}

?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <script src="../js/jquery.min.js"></script>
 <link href="../css/font-awesome.min.css" rel="stylesheet">
 <link href="../css/abc.css" rel="stylesheet">
 <script src="../js/bootstrap.min.js"></script>
 <link href="../css/abc2.css" rel="stylesheet">
 <link rel="stylesheet" href="../css/bootstrap.min.css">
 <link rel="stylesheet" href="../css/bootstrap-select.min.css">
 <script src="../js/bootstrap-select.min.js"></script>
 
 <link rel="stylesheet" href="../css/jquery.dataTables.min.css">
 <script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
 <script>
	$(document).ready(function(){
		$('#myTable').dataTable();
	});
 </script>
<script language="javascript">
</script>
</head>
<body>
<div class="container-fluid">
  <div class="row content">
	<?php 
    include("../includes/leftnav2.php");
    ?>
    <div class="col-sm-9 tab-pane fade in active" id="home">
      <h2 align="center"><i class="fa fa-cubes fa-lg"></i>Inventory</h2><br/>
      <div class="form-group" id="page-wrap" style="margin-left:10px;">
      <form class="form-horizontal" role="form" name="form1" action="" method="get">
      <div class="row">
        <div class="col-sm-4" align="right"><label class="control-label">Location:</label></div>
        <div class="col-sm-5"><select name="locationcode" id="locationcode" class="form-control selectpicker" data-live-search="true" onChange="document.frm1.submit();">
                    <option value="" selected="selected">Please Select </option>
                    <?php 
					$sql_chl="select * from access_location where uid='$_SESSION[userid]' and status='Y'";
					$res_chl=mysqli_query($link1,$sql_chl);
					while($result_chl=mysqli_fetch_array($res_chl)){
	                      $party_det=mysqli_fetch_array(mysqli_query($link1,"select name , city, state,id_type from asc_master where asc_code='$result_chl[location_id]'"));
	                    
                          ?>
                    <option data-tokens="<?=$party_det['name']." | ".$result_chl['location_id']?>" value="<?=$result_chl[location_id]?>" <?php if($result_chl[location_id]==$_REQUEST[locationcode])echo "selected";?> >
                       <?=$party_det['name']." | ".$party_det['city']." | ".$party_det['state']." | ".$result_chl['location_id']?>
                    </option>
                    <?php
						  }
					
                    ?>
                 </select>
              </div>
              <div class="col-sm-3"></div>
      </div>
      <div class="row">
        <div class="col-sm-4"><br/></div>
        <div class="col-sm-5"></div>
        <div class="col-sm-3"></div>
      </div>
      <div class="row">
        <div class="col-sm-4" align="right"><label class="control-label">Inventory Type:</label></div>
        <div class="col-sm-5"><select name="inventorytype" id="inventorytype" class="form-control" required>
                    <option value="" selected="selected">Please Select </option>
                    <option value="imei"<?php if($_REQUEST['inventorytype']=="imei"){ echo "selected";}?>><?=$imeitag?>INVENTORY</option>
                    <option value="qty"<?php if($_REQUEST['inventorytype']=="qty"){ echo "selected";}?>>QTY INVENTORY</option>
                 </select></div>
        <div class="col-sm-3"><input name="Submit" type="submit" class="btn btn-primary" value="GO"  title="Go!">
               <input name="pid" id="pid" type="hidden" value="<?=$_REQUEST[pid]?>"/>
               <input name="hid" id="hid" type="hidden" value="<?=$_REQUEST[hid]?>"/></div>
      </div>
      <div class="row">
        <div class="col-sm-4"><br/></div>
        <div class="col-sm-5"></div>
        <div class="col-sm-3"></div>
      </div>
      <?php if($_REQUEST['inventorytype']!=''){ ?>
      <?php if($_REQUEST['inventorytype']=="qty"){ ?>
      <div class="row">
        <div class="col-sm-12 table-responsive">
        <div style="float:right">
            <?php
			    //// get excel process id ////
				//echo $processid=getExlCnclProcessid("Inventory",$link1);
			    ////// check this user have right to export the excel report
			    //if(getExcelRight($_SESSION['userid'],$processid,$link1)==1){
			   ?>
        <strong>Excel Export</strong>&nbsp;&nbsp;&nbsp;&nbsp; <a href="excelexport.php?rname=<?=base64_encode("inventory")?>&rheader=<?=base64_encode("Inventory")?>&loc=<?=base64_encode($_GET['locationcode'])?>" title="Export inventory details in excel" style="float:right"><i class="fa fa-file-excel-o fa-2x" title="Export inventory details in excel"></i></a><br/><br/>
        <?php
				//}
				?>
                </div>
       <table  width="99%" id="myTable" class="table-striped table-bordered table-hover" align="center">
          <thead>
            <tr>
              <th><a href="#" name="entity_id" title="asc" ></a>S.No</th>
              <th data-class="expand"><a href="#" name="name" title="asc" ></a>Location Name</th>
              <th><a href="#" name="name" title="asc" ></a>Location Type</th>
              <th data-hide="phone"><a href="#" name="name" title="asc" class="not-sort"></a>Product Code</th>
              <th data-hide="phone,tablet"><a href="#" name="name" title="asc" class="not-sort"></a>Product Name</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Ok Qty</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Damage Qty</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Missing Qty</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Total Qty</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Current Price</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a>Value</th>
            </tr>
          </thead>
          <tbody>
            <?php
			$sno=0;
			$new_loc = "";
			$old_loc = "";
			$sql=mysqli_query($link1,"Select * from stock_status where $loc");
			while($row=mysqli_fetch_assoc($sql)){
				  $sno=$sno+1;
				  $new_loc = $row['asc_code'];
				  if($old_loc != $new_loc){
				  	$locdet=explode("~",getLocationDetails($row['asc_code'],"name,city,state,id_type",$link1));
				  }
	              $proddet=str_replace("~",",",getProductDetails($row['partcode'],"productname,productcolor",$link1));
				  $price = mysqli_fetch_assoc(mysqli_query($link1,"SELECT price FROM price_master where state='".$locdet[2]."' and location_type='".$locdet[3]."' and product_code='".$row['partcode']."' and status='active'"));
			?>
            <tr class="even pointer">
              <td><?php echo $sno;?></td>
              <td><?php echo $locdet[0].",".$locdet[1].",".$row['asc_code'];?></td>
              <td><?=getLocationType($locdet[3],$link1);?></td>
              <td><?php echo $row['partcode'];?></td>
              <td><?php echo $proddet;?></td>
              <td align="right"><?php echo $row['okqty'];?></td>
              <td align="right"><?php echo $row['broken'];?></td>
              <td align="right"><?php echo $row['missing'];?></td>
              <td align="right"><?php echo $total = $row['okqty']+$row['broken']+$row['missing'];?></td>
              <td align="right"><?=number_format($price["price"],'2')?></td>
              <td align="right"><?=number_format($price["price"]*$total,'2')?></td>
            </tr>
            <?php 
			$old_loc = $row['asc_code'];
			}?>
          </tbody>
          </table> 
         </div>
      </div>
      <?php }?>
      <?php if($_REQUEST['inventorytype']=="imei"){ ?>
      <div class="row">
        <div class="col-sm-12 table-responsive">
        <div style="float:right">
            <?php
			    //// get excel process id ////
				//echo $processid=getExlCnclProcessid("Inventory",$link1);
			    ////// check this user have right to export the excel report
			    //if(getExcelRight($_SESSION['userid'],$processid,$link1)==1){
					$head = $imeitag."Inventory";
			   ?>
        <strong>Excel Export</strong>&nbsp;&nbsp;&nbsp;&nbsp; <a href="excelexport.php?rname=<?=base64_encode("imeistock")?>&rheader=<?=base64_encode($head)?>&loc=<?=base64_encode($_GET['locationcode'])?>" title="Export inventory details in excel" style="float:right"><i class="fa fa-file-excel-o fa-2x" title="Export inventory details in excel"></i></a><br/><br/>
        <?php
				//}
				?>
                </div>
       <table  width="99%" id="myTable" class="table-striped table-bordered table-hover" align="center">
          <thead>
            <tr>
              <th><a href="#" name="entity_id" title="asc" ></a>S.No</th>
              <th data-class="expand"><a href="#" name="name" title="asc" ></a>Location Name</th>
              <th><a href="#" name="name" title="asc" ></a>Location Type</th>
              <th data-hide="phone"><a href="#" name="name" title="asc" class="not-sort"></a>Product Code</th>
              <th data-hide="phone,tablet"><a href="#" name="name" title="asc" class="not-sort"></a>Product Name</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a><?=$imeitag?>1</th>
              <th data-hide="phone,tablet"><a href="#" name="number" title="asc" class="not-sort"></a><?=$imeitag?>2</th>
              </tr>
          </thead>
          <tbody>
            <?php
			$sno=0;
			$sql=mysqli_query($link1,"Select * from billing_imei_data where $imeiloc")or die(mysqli_error($link1));
			while($row=mysqli_fetch_assoc($sql)){
				
				  $chek_owner=mysqli_fetch_assoc(mysqli_query($link1,"select owner_code,doc_no from billing_imei_data where imei1='".$row['imei1']."' order by id desc"));
				 
				  $chek_rcvin=mysqli_fetch_assoc(mysqli_query($link1,"select status from billing_master where challan_no='".$chek_owner['doc_no']."'"));
				  if($chek_rcvin['status']==""){
				  $chek_postatus=mysqli_fetch_assoc(mysqli_query($link1,"select status from vendor_order_master where po_no='".$chek_owner['doc_no']."'"));
				  $chek_rcvin2=mysqli_fetch_assoc(mysqli_query($link1,"select status from opening_stock_master where doc_no='".$chek_owner['doc_no']."'"));
					  $checkstatus=$chek_rcvin2['status'];
					  $checkstatus=$chek_postatus['status'];
				  }else{
					 $checkstatus=$chek_rcvin['status'];
				  }
			      if($chek_owner['owner_code']==$row['owner_code'] && $checkstatus=="Received"){
				  $sno=$sno+1;
				  $locdet=explode("~",getLocationDetails($row['owner_code'],"name,city,state,id_type",$link1));
	              $proddet=str_replace("~",",",getProductDetails($row['prod_code'],"productname,productcolor",$link1));
			?>
            <tr class="even pointer">
              <td><?php echo $sno;?></td>
              <td><?php echo $locdet[0].",".$locdet[1].",".$row['owner_code'];?></td>
              <td><?=getLocationType($locdet[3],$link1);?></td>
              <td><?php echo $row['prod_code'];?></td>
              <td><?php echo $proddet;?></td>
              <td align="left"><?php echo $row['imei1'];?></td>
              <td align="left"><?php echo $row['imei2'];?></td>
              </tr>
            <?php } }?>
          </tbody>
          </table> 
         </div>
      </div>
      <?php }?>
      <?php }?>
	  </form>   
      </div>  
    </div><!--close tab pane-->
  </div><!--close row content-->
</div><!--close container fluid-->
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>