<?php 
session_start();
 if(!isset($_SESSION['aid']))
 {
   	echo '<script>location.href="../index.php"</script>';
 }

include "../connection/config.php";

//for header
$clquery="select * from company";
$clex=mysqli_query($con,$clquery);
$clf=mysqli_fetch_assoc($clex);

$dateString=$expiry=$clf['expiry'];
$de = new DateTime($dateString);
$formattedDate = $de->format('d/m/Y');

$regdate=$clf['regdate'];
$date= date( 'Y-m-d' );
//for expiry day
$date1 = new DateTime($date);
$date2 = new DateTime($expiry);
$interval = $date1->diff($date2);
$days= $interval->format('%a days');

//get ip
$ipsql="select * from adminlog";
$ipexe=mysqli_query($con,$ipsql);
$ipf=mysqli_fetch_assoc($ipexe);

$category="select * from item_category";
$catexe=mysqli_query($con,$category);


$editflag= (isset($_REQUEST['flag']) ? strip_tags($_REQUEST['flag']): 0);
if ($editflag==2)
{

$sidedit= (isset($_REQUEST['sid']) ? strip_tags($_REQUEST['sid']): 0);
	
$sd="select * from stock_details where sid='$sidedit' ";
$sdex=mysqli_query($con,$sd);
$sdf = mysqli_fetch_assoc($sdex);
                
$item=$sdf['iid']; 
$stockin=$sdf['stockin'];

// $stockout=$sdf['stockout'];       
// $cur_stock=$sdf['current_stock'];

$dead_stock=$sdf['dead_stock'];

//$stocktot=$sdf['stocktotal'];
$reorder_lvl=$sdf['reorder'];
$remark=$sdf['remark'];
$item=$sdf['iid'];

$fitem=mysqli_query($con,"select * from item_details where iid='$item'");
$fitem=mysqli_fetch_assoc($fitem);
$item1=$fitem['item_name'];
}
?>

<html><head>


    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Admin Console</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
     <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="dist/css/style.css">
    <link rel="stylesheet" href="css/print.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script>
	function isNumeric(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
	return false;
	
	return true;
	}

	</script>
	<style>
    @font-face {
    font-family: "Kerala";
    src: url("KERALI_1.TTF") format("truetype"); 
    }
	</style>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready (function(){
$("#country").change(function(){
var cn=$("#country").val();	

	$.ajax({
	url:"ajaxitem.php",
	type:"post",
	data:"category="+cn,
	success:function(d)
	 {
 $("#abcd").html(d);
	 }
});
});
});
</script>

<script>
    function addFields() {
        // retrieve the values of the two input fields
        var field1 = parseFloat(document.getElementById("field1").value);
        var field2 = parseFloat(document.getElementById("field2").value);

        // perform the addition operation
        var result = field1 + field2;

        // update the value of the output field
        document.getElementById("result").value = result;
    }
</script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </head>
      
  <body class="skin-green  sidebar-mini">
    <!-- Site wrapper -->
    <div class="wrapper">
 
      <!-- =============================================== -->

      <!-- Left side column. contains the sidebar -->
      
<header class="main-header">

<div class="row" style="background-color:#4d6c9c; color:#E1EBEE; text-align:center">
<div class="col-md-12"  style="margin-top:10px; ">

<div class="col-md-2"><b style="font-size:20px; float:left;">Billing Software<span style="font-size:10px;">ver1.23</span></b></div>  

<div class="col-md-2">License To : <?php echo $clf['company']; ?> <span style="float:right;">|<span></div>

<div class="col-md-2">Left : <?php echo $days;  ?><span style="float:right;">|<span></div>

<div class="col-md-2">Expiry : <?php echo $formattedDate; ?> <span style="float:right;">|<span></div>

<div class="col-md-2">Powered By : PROGRAMERS Inc <span style="float:right;">|<span></div>

<div class="col-md-1"> Ip - <?php echo $ipf['ip']; ?> </div>
</div><div>

  </header>

<aside class="main-sidebar">
          <!-- sidebar: style can be found in sidebar.less -->
          <section class="sidebar" style="height: auto;">
          <!-- Sidebar user panel -->
          <div class="user-panel"></div>
         
          <ul class="sidebar-menu">
            <li class="treeview "> 
            <a href="home.php"><span>Home</span><i class="fa fa-angle-left pull-right"></i></a></li>

            <li class="treeview "> 
            <a href="#"><span>Master</span><i class="fa fa-angle-left pull-right"></i></a>
            <ul class="treeview-menu">
            <li class=""><a href="item_category.php">Item Category</a></li>
            <li class=""><a href="item_details.php">Item Details</a></li>
            <li class=""><a href="customer.php">Customer Details</a></li>
            </ul> </li> 

            <li class="treeview "> 
            <a href="#"><span>Transaction</span><i class="fa fa-angle-left pull-right"></i></a>
            <ul class="treeview-menu">
            <li class=""><a href="stock_input.php">Stock Input</a></li>
            <li class=""><a href="billing.php">Billing</a></li>
            <!--<li class=""><a href="sales_return.php">Sales Return</a></li>-->
            </ul> </li> 

            <li class="treeview "> 
            <a href="#"><span>Reports</span><i class="fa fa-angle-left pull-right"></i></a>
            <ul class="treeview-menu">
            <li class=""><a href="item_report.php">Item List</a></li>  
            <li class=""><a href="sales_report.php">Sales Register</a></li>  
            <!-- <li class=""><a href="sales_return_report.php">Sales Return Register</a></li>   -->
            <li class=""><a href="stock_input_register.php">Stock Translation Register</a></li>   
            <li class=""><a href="stock_register.php">Stock Register</a></li>
            <li class=""><a href="stock_reorder.php">Item Reorder Register</a></li> 
            <li class=""><a href="dead_stock_register.php">Dead Stock Register</a></li>    
            </ul>
            </li> 


            <li class="treeview "> 
            <a href="#"><span>Settings</span><i class="fa fa-angle-left pull-right"></i></a>
            <ul class="treeview-menu">
            <li class=""><a href="company.php">Company Setup</a></li>  
            <li class=""><a href="password.php">Password Reset</a></li>  
            <li class=""><a href="logout.php">Logout</a></li>   
            </ul>
            </li>

            </ul>
        </section>
        <!-- /.sidebar -->
      </aside>
      <!-- =============================================== -->

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper" style="min-height: 700px;">
        <!-- Content Header (Page header) -->
        <section class="content-header"></section>
        <!-- Main content -->
        <section class="content">


		 <div class="row">
<!--------------------------------------------------------------------1st side------------------------------------------------------------------------------------>
            
      <div class="col-md-5">
      <div class="box" id="box_edit" style="margin-top:10px;">

      <div class="box-header with-border"><h3 class="box-title"> Add Stocks</h3></div>

      <form class="form-horizontal" action="stockcode.php" method="post" enctype="multipart/form-data" name="form1" autocomplete="off">

      <div class="box-body">
                           
      <input type="hidden" name="flag" value="<?php echo $editflag; ?>"/>
			<input type="hidden" name="sid" value="<?php echo $sidedit; ?>"/>

      <!-- <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Choose Category</label>
      <div class="col-sm-6">
      <select class="form-control" name="cat">
      <option value="<?php echo $item??''; ?>"><?php if($editflag==2) { echo $item1??'';} else { echo "--Select Category--"; } ?> </option> 
      <?php $c=mysqli_query($con,"select * from item_category");
      while($ce=mysqli_fetch_assoc($c)){ ?> 
      <option value="<?php echo $ce['cid']; ?>"><?php echo $ce['category']; ?></option>     
      <?php } ?>
      </select>
      </div></div> -->


      <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Choose Category</label>
      <div class="col-sm-6">
      <select class="form-control" name="item" id="country">
      <option value="<?php echo $item??''; ?>"><?php if($editflag==2) { echo $item1??'';} else { echo "--Select Category--"; } ?> </option> 
      <?php $c=mysqli_query($con,"select * from item_category");
      while($ce=mysqli_fetch_assoc($c)){ ?> 
      <option value="<?php echo $ce['cid']; ?>"><?php echo $ce['category']; ?></option>     
      <?php } ?>
      </select>
      </div></div>
      <div id="abcd"></div>

      <!-- <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Item Name</label>
      <div class="col-sm-6">
      <select class="form-control" name="item">
      <option value="<?php echo $item??''; ?>"><?php if($editflag==2) { echo $item1??'';} else { echo "--Select Item--"; } ?> </option> 
      <?php $cate=mysqli_query($con,"select * from item_details");
      while($cfe=mysqli_fetch_assoc($cate)){ ?> 
      <option value="<?php echo $cfe['iid']; ?>" ><?php echo $cfe['item_name']; ?></option>     
      <?php } ?>
      </select>
      </div></div> -->

      <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Stock In</label>
      <div class="col-sm-6"><input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $stockin??'';?>" <?php }  ?> name="stockin" id="field1" onkeyup="addFields()" required="" placeholder="Stock In"  ></div></div>

      <!-- <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Stock Out</label>
      <div class="col-sm-6"><input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $stockout??'';?>" <?php }  ?> name="stockout" required="" placeholder="Stock Out"  ></div></div>

      <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Current Stock</label>
      <div class="col-sm-6"><input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $cur_stock??'';?>" <?php }  ?> name="current_stock" required="" placeholder="Current Stock"  ></div></div> -->

      <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Dead Stock</label>
      <div class="col-sm-6"><input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $dead_stock??'';?>" <?php }  ?> name="dead_stock" id="field2" onkeyup="addFields()" required="" placeholder="Dead Stock"  ></div></div>

      <!-- <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Total Stock</label>
      <div class="col-sm-6"><input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $stocktot??'';?>" <?php }  ?> name="stocktot" required="" placeholder="Total Stock"  ></div></div> -->

      <!-- <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Reorder</label>
      <div class="col-sm-6"> -->
      <!-- <input type="text" class="form-control" <?php if($editflag==2){ ?> value="<?php echo $reorder_lvl??'';?>" <?php }  ?> name="relvl" required="" placeholder=" Reorder " > -->
      <!-- <input type="text" class="form-control" value="5" name="relvl" required="" placeholder=" Reorder " >
      </div></div> -->

      <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-4 control-label">Remark</label>
      <div class="col-sm-6">
      <textarea class="form-control" name="remark" placeholder="Remark"><?php if($editflag==2){ echo $remark??'';} ?></textarea>
      </div></div>

      <div class="form-group"> <div class="col-sm-6">
      <button type="submit" name="submit" class="btn btn-info" style="background-color:#4d6c9c; border-color:#4d6c9c; margin-left:220px;" >
      <?php if($editflag==2) { echo "Update";} else { ?> Submit <?php }?>
      </button></div></div></div>
      </div>
      </form></div>

<!------------------------------------------------------------- end 1st side ------------------------------------------------------------------------------------------------------------> 
<!----------------------------------------------------------------- 2nd section--------------------------------------------------------------------------------------------------------->
              
      <div class="col-md-7">
      <div class="box" id="box_edit"  style="margin-top:10px;">
      <!-- <div class="box-header with-border"><h3 class="box-title">Stock</h3></div> -->
   
   <!-- search bar --->
  
   <form action="<?php $_SERVER['PHP_SELF']?>" method="post"> 

   <div class="col-md-12" style="margin-top:10px; margin-bottom:10px;">
   <div class="col-md-5">
    
   <select class="form-control" name="sitem" style=" margin-left:10px;">
   <option value="">--Select Item--</option>
   <?php $ite=mysqli_query($con,"select * from item_details");
   while($ife=mysqli_fetch_assoc($ite)){ ?>
   <option value="<?php echo $ife['iid']; ?>"><?php echo $ife['item_name']; ?></option>     
   <?php } ?>
   </select>  </div>

   <div class="col-md-5">
   <input type="date" name="date" class="form-control">
   </div>
 
   <div class="col-md-2">
   <button type="submit" name="search" style=" background:white; border-color:white; color:#4d6c9c;"><i class="fa fa-search" style="font-size:25px;"></i></button> </div>
   </div> 
  </form>  


<?php 
if(isset($_POST['search']))
{
  $sitem=$_POST['sitem'];
  $date=$_POST['date'];

  $searchquery="select * from stock_details where iid='$sitem' or date='$date' ";
  $searchexe=mysqli_query($con,$searchquery);
 
?>

  <table id="example1" class="table table-bordered table-striped" align="center">
      <thead>
      <tr style="background-color:#fff; color:#4d6c9c;;">
          <th>Date</th>
          <th>Code</th>
          <!-- <th>Item</th> -->
          <th>Stock In</th>
          <!-- <th>Stock Out</th>
          <th>Current Stock</th> -->
          <th>Dead Stock</th>
          <!-- <th>Total Stock</th> -->
          <th>Remark</th>
          <th>Action</th>
      </tr></thead>
      <tbody>
      <?php while($searchfetch=mysqli_fetch_assoc($searchexe)){ 
        $iid=$searchfetch['iid'];
        $se=mysqli_query($con,"select * from item_details where iid='$iid'");
        $fe=mysqli_fetch_assoc($se); 
        ?>
      <tr> 
            <td><?php echo date('d-m-Y', strtotime($searchfetch['date']));?></td>
            <!-- <td><?php echo $searchfetch['iid'];?></td> -->
            <td><?php echo $fe['item_name'];?></td>
            <td><?php echo $searchfetch['stockin'];?></td>
            <!-- <td><?php echo $searchfetch['stockout'];?></td>
            <td><?php echo $searchfetch['current_stock'];?></td> -->
            <td><?php echo $searchfetch['dead_stock'];?></td>
            <!-- <td><?php echo $searchfetch['stocktotal'];?></td> -->
            <td><?php echo $searchfetch['remark'];?></td>
           <td>        
              <!-- <a href="stock_input.php?sid=<?php echo $searchfetch['sid']; ?>&flag=2"><i class="fa fa-pencil-square-o" aria-hidden="true" style="color:#4d6c9c;"></i></a> -->
              <a href="stock_delete.php?sid=<?php echo $searchfetch['sid']?>"><i class=" fa fa-trash" aria-hidden="true" style="color:#4d6c9c;"></i></a>
           </td> 
      </tr>  <?php } ?>                             
      </tbody>
      <tfoot></tfoot>
      </table>

<?php } 
else   {  ?>


          <table id="example1" class="table table-bordered table-striped" align="center">
          <thead>
          <tr style=" background-color:#fff; color:#4d6c9c;">
          <th>#</th>
          <th>Date</th>
          <!-- <th>Code</th> -->
          <th>Item</th>
          <th>StockIn</th>
          <!-- <th>Stock Out</th>
          <th>Current Stock</th> -->
          <th>DeadStock</th>
          <!-- <th>TotalStock</th> -->
          <th>Remark</th>
          <th>Action</th>
          </tr>
          </thead>
          <tbody>
                            
            <tr>  <?php

// PAGINATION
$num_per_page=20;
if(isset($_GET["page"]))
  {
$page=$_GET["page"];
  }
else 
  {
$page=1;
  }
 $start_from=($page-1) * 20;

            // Display all datas
            include "config1.php";
            $sqli = "SELECT * FROM stock_details ORDER BY sid DESC limit $start_from,$num_per_page";
            $stmt = $pdo->query($sqli);
            $data = $stmt->fetchAll();
            // Initialize counter
            $serial = count($data);
            
              
              foreach ($data as $row) { 
                $iid=$row['iid'];
                $s=mysqli_query($con,"select * from item_details where iid='$iid'");
                $f=mysqli_fetch_assoc($s); ?>
              <td><?php echo  $serial-- ?></td>                      
              <td><?php echo date('d-m-Y', strtotime($row['date']));?></td>
              <!-- <td><?php echo $row['iid'];?></td> -->
              <td><?php echo $f['item_name'];?></td>
              <td><?php echo $row['stockin'];?></td>
              <!-- <td><?php echo $row['stockout'];?></td>
              <td><?php echo $row['current_stock'];?></td> -->
              <td><?php echo $row['dead_stock'];?></td>
              <!-- <td><?php echo $row['stocktotal'];?></td> -->
              <td><?php echo $row['remark'];?></td>
              <td>
              <!-- <a href="stock_input.php?sid=<?php echo $row['sid']; ?>&flag=2"><i class="fa fa-pencil-square-o" aria-hidden="true" style="color:#4d6c9c;"></i></a> -->
              <a href="stock_delete.php?sid=<?php echo $row['sid']?>&iid=<?php echo $row['iid']?>"><i class=" fa fa-trash" aria-hidden="true" style="color:#4d6c9c;"></i></a>       
              </td>
              </tr>
              <?php }  ?>                               
              </tbody>    <tfoot></tfoot>
             </table>  
<?php 

//number pagination code 

$sqlp="SELECT * FROM stock_details ORDER BY sid DESC";
$exep=mysqli_query($con,$sqlp);
 $total_records=mysqli_num_rows($exep);
//echo $total_records;
 $total_pages=ceil($total_records/$num_per_page);
//echo $total_pages;  
                       
for($i=1; $i<=$total_pages; $i++)
{
echo "<b><a href='stock_input.php?page=".$i."' class='btn'>".$i."</a></b>"; echo " ";
}


} ?>
           </div>
           </div>
<!--------------------------------------------------------------------------end 2nd section-------------------------------------------------------------------------->

</div>
</div> 
     
         <!-- /.box -->
        
        </section><!-- /.content -->
</div><!-- /.content-wrapper -->

  <footer class="main-footer"><strong>Copyright © 2023 <a href="javascript:void(0)"> Perfect Uniform Llc </a></strong> All rights reserved.</footer>

    <!-- ./wrapper -->

    <!-- jQuery 2.1.4 -->
    <script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <!-- SlimScroll -->
    <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js"></script> 
 <!--<script>
 	function displayEdit(id)
	{
		$.ajax({
			type: "POST",
			url: "country_edit.php",
			data:{country_id: id},
			success: function(data){
				$('#box_edit').html(data);
			}
		});
	}
 </script>-->

</body></html>