<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['hrm_id'])):
header('Location:../index.php');
endif;
$cval=0;
$grn_no=0;

if (isset($_GET['trn_no'])) { 
  $tran_no= trim($_GET['trn_no']);
}  //// if is set


if (isset($_POST['trn_no'])) { 
$tran_no= trim($_POST['trn_no']);

}

if ($tran_no<>0){

  $sqlsp=mysqli_query($con,"SELECT stock_adjust_temp_header.* ,stock_locations.loc_name AS floc
FROM stock_adjust_temp_header
LEFT JOIN stock_locations ON stock_locations.loc_id=stock_adjust_temp_header.from_loc_id
 WHERE tran_id=$tran_no ")or die(mysqli_error($con));
            $count=mysqli_num_rows($sqlsp); 
            if ($count>0) {
              while($rowsp=mysqli_fetch_array($sqlsp)){     
                 $from_loc_id=$rowsp['from_loc_id']; 
                 //$to_loc_id=$rowsp['to_loc_id']; 
                 $floc=$rowsp['floc']; 
                // $tloc=$rowsp['tloc'];  
                 $refnote=$rowsp['refnote'];     
             }  

          
            }// if ($count>0) {


}  ////    if ($tran_no<>0){

//////////////// Get Sequence Number ----------------------- 


//////////////////End of gettinh sequence Number ------------------
?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub | Stock Adjustments </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>
<style>
      div.scroll {
         display: block; 
            border: 0px solid red; 
            padding: 5px; 
            margin-top: 5px; 
            width: 100%; 
            height: 500px; 
            overflow-y: scroll; 
      }
    </style>


</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">        
          <div class="col-22 grid-margin">
              <div class="card">
                <div class="card-body" style="background-color:#DCF7B8; ">
              
                          
   <form class="forms-sample">                  
                    <div class="row">                     
                      <div class="col-md-12" >
                <div class="card">
                <div class="card-body" style="background-color:#DCF7B8; color:#054653 ">

                  <?php 
if ($tran_no<>0){

 // $grn_ref=$grnd_no;
echo "Adj Ref:"; echo str_repeat("&nbsp;", 1);  echo $tran_no; echo str_repeat("&nbsp;", 5); echo "From Location : "; echo str_repeat("&nbsp;", 1); echo $floc; 
echo str_repeat("&nbsp;", 5);
echo str_repeat("&nbsp;", 5); echo "Note : ";echo str_repeat("&nbsp;",1); echo $refnote;
echo str_repeat("&nbsp;", 5);


//echo date("Y-m-d",strtotime($grnd_date));
}
  ?>
      </div>
            </div>


                      </div>
       

            

</div>




   <table  width="100%">         


    <tr><td width="2%">Item   </td><td width="50%">

     <input type="hidden" class="form-control" id="trn_no" name="trn_no" value="<?php echo   $tran_no;  ?>" >
<select class="form-control select2" style="width: 100%;" name="item_id" id="item_id" required >
           <option value="0">~~~Select Item ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select * from view_item_master  ")or die(mysqli_error());          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['item_id'];?>"><?php echo $rowd['item_name'];?></option>        
          <?php }?>
          </select> 
</td><td width="2%">Qty</td><td width="10%">



                            <input type="number" class="form-control" id="qty" name="qty" >

                       
                        </td>

  </div> <!-------End of first form raw------->
  <td width="5%">
<div class="col-md-2">
             
                            <button type="submit" class="btn btn-primary" id="bb">Add</button>
                        </div>
  </div> <!-------End of first form raw------->
</div>

</table>


 </form>
</div>

  <form class="needs-validation" novalidate method="post" action="stock_adjust_final_add.php" enctype="multipart/form-data" > 
    <table width="100%" border="0">
  <tr>
    <td width="7%"></td>
    <td>
     <input type="hidden" class="form-control" id="t_no" name="t_no" value="<?php echo   $tran_no;  ?>" >
 <div> <button type="submit" class="btn btn-primary mr-2">Create Adjustment</button></div></td>
 <td align="right">

                    <button type="button" onclick="document.location='home.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back
                    
                  </button>
                  </td>
                  <td></td>
  </tr>
  </table>
    </form>   
             
                  <div class="scroll">    
                    <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Item</th>
                          <th>Qty</th>
                         
                          <th>Edit</th> 
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$tot_qty=0;
$query=mysqli_query($con,"SELECT item_master.item_name,stock_adjust_temp_detail.tran_qty,stock_adjust_temp_detail.tran_id ,item_master.item_id
FROM stock_adjust_temp_detail
LEFT JOIN item_master ON stock_adjust_temp_detail.item_id=item_master.item_id where stock_adjust_temp_detail.tran_id=$tran_no ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
               // $tran_id=$row['tran_id'];
                $item_name=$row['item_name']; 
                $tran_qty=$row['tran_qty'];
                 $item_id=$row['item_id'];

                 $tot_qty=$tot_qty + $row['tran_qty'];
                //$sup_name=$row['sup_name'];
              

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['item_name'];?></td>
                         <td><?php echo $row['tran_qty'];?></td>
                         
                         <td> <form method="post" action="#" enctype="multipart/form-data">
                            <input type="hidden" class="form-control pull-right" id="tran_no" name="tran_no"  value=" <?php echo $tran_no; ?>">

                            <input type="hidden" class="form-control pull-right" id="item_id" name="item_id"  value=" <?php echo $item_id; ?>">

         <button type="submit" formaction="stock_adjust_remove.php" class="btn btn-outline-secondary btn-rounded btn-icon" style="width: 20px; height: 20px;">
                        <i class="fas fa-trash text-danger" style="font-size: 12px;"></i>
                      </button></form></td>
 

</tr>

 <?php }  ?>

                      </tbody>
                      <tr>
                          <td><?php   ?></td>
                         <td><?php "Total"?></td>
                         <td><?php echo $tot_qty;?></td>

                      </tr>
                    </table>


               
            </div>
              </div>









  

         


        </div>  <!----- <div class="content-wrapper">------>
        </div>

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->

  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
  </div>
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });



</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })




 $("button").click(function(){

  //alert("aa");
                 var item_id = $("#item_id").val();
                 var qty = $("#qty").val();
                   var tran_no= $("#trn_no").val();
                  // alert(tran_no);

                         $.ajax({
                type: 'POST',
                dataType: "json",
                url: 'stock_adjust_ajax.php',
                        data: {
                        'adj_no' : tran_no,  
                        'item_id' : item_id , 
                        'qty' : qty ,  
                            

                },
                cache: false,
                success: function(result) {
                //alert(result);
                  document.getElementsByName('item_id')[1].focus();                           

                      },   ////~~~~~~~~~~end of sucess functions

                    

                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
          



   }) /// $("button").click(function(){


    $('#item_id').on('change', function(){

     window.setTimeout(function () { 
    document.getElementById('qty').focus(); 
}, 0); 
//document.getElementById("qty").focus();
//alert("aa");
         }); ///// $('#item_id').on('change', function(){



</script>



