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

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


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

}

if ($grnd_no<>0){

  $sqlsp=mysqli_query($con,"SELECT * FROM grn_temp_header 
LEFT JOIN stock_locations ON grn_temp_header.grand_loc_id=stock_locations.loc_id
LEFT JOIN supplier_master ON grn_temp_header.sup_id=supplier_master.sup_id where grnd_no= $grnd_no ")or die(mysqli_error($con));
            $count=mysqli_num_rows($sqlsp); 
            if ($count>0) {
              while($rowsp=mysqli_fetch_array($sqlsp)){     
                 $loc_name=$rowsp['loc_name']; 
                 $invoice_no=$rowsp['invoice_no']; 
                 $grnd_date=$rowsp['grnd_date']; 
                 $grnd_loc_id=$rowsp['grand_loc_id'];      
             }  

          
            }// if ($count>0) {

}  ////if ($grnd_no<>0){
//echo $grnd_no;
?>

<!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>SalesMgt | Item Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>

</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
  <!-- partial -->
 <div class="main-panel">        
 <div class="content-wrapper">
 <div class="container-fluid">
 <div class="col-12 grid-margin">       
 <table id="item-disp" class="table table-sm">                   
 </table>
 
 </div>
          
          
 <div class="col- grid-margin">
 <div class="card">
 <div class="card-body"> 
 <?php 
if ($grnd_no<>0){

  $grn_ref=$grnd_no;
echo "Receipt Ref:"; echo str_repeat("&nbsp;", 1);  echo $grnd_no; echo str_repeat("&nbsp;", 5); echo "Location : "; echo str_repeat("&nbsp;", 1); echo $loc_name; 
echo str_repeat("&nbsp;", 5); echo "Receipt No : ";echo str_repeat("&nbsp;",1); 
echo $invoice_no;
echo str_repeat("&nbsp;", 5); echo "Receipt Date : ";echo str_repeat("&nbsp;",1); 
echo date("Y-m-d",strtotime($grnd_date));
}
  ?>
 
  <div class="row">
            <div class="col-md-4 grid-margin stretch-card">
              <div class="card">
                <div class="card-body" style="background-color:#DCF7B8">
                  <h4 class="card-title">Item Receipt Entry <?php echo $grnd_no;  ?></h4>

                  <form class="forms-sample">
                    <div class="form-group">
                      <label for="exampleInputUsername1">Item</label>
                       <input type="hidden" class="form-control" id="grnd_no" name="grnd_no" value="<?php echo   $grnd_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> 
                    </div>
                    <div class="form-group">
                      <label for="exampleInputEmail1">Qty</label>
                      <input type="number" class="form-control" id="qty" name="qty"placeholder="GRN Qty">
                    </div>

                      <!-- <div class="form-group">
                      <label for="exampleInputEmail1"> Price</label> -->
                      <input type="hidden" class="form-control" id="costprice" name="costprice"placeholder="Purchase Price">
                    <!--  </div>-->

                    <button type="button" class="btn btn-primary mr-2">Add Item</button>
                    <button class="btn btn-light">Cancel</button>
                    <div class="form-group"> 
                    <table  class="table" id="stock-listing" >  
                      
                        <th class="bg-primary text-white">         
                        <th>Location</th>
                        <th>QOH</th> 
                        </th>
                                 
                      </table>
                  </div>
                  </form>
                </div>
              </div>
            </div>
 
 
  <div class="col-md-8 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">       
                 <form class="needs-validation" novalidate method="post" action="item_grn_final_add.php" enctype="multipart/form-data" > 
                    <div class="form-group row">
                      <div class="table-responsive">
                       <input type="hidden" class="form-control" id="grn_no" name="grn_no" value="<?php  echo $grn_ref;  ?>">
                        <input type="hidden" class="form-control" id="loc_id" name="loc_id" value="<?php  echo $grnd_loc_id;  ?>">
                      <table  class="table" id="order-listing" >  
                      
                        <th class="bg-primary text-white">         
                        <th>Item</th>
                        <th>Qty</th>  
                        <th>Action</th>
                        </th>
                                 
                      </table>

                      </div>
                    </div>
                    <button type="submit" class="btn btn-primary mr-2">Create Item Receipt</button>
                    <button class="btn btn-light">Cancel</button>
                  </form>
                </div>
              </div>
            </div>
            </div>
    
   

        </div>  <!----- <div class="content-wrapper">------>
        </div>
        </div>
      </div>
    </div>
 
 
 
 
<?php include('../includes/commonjs.php');?>
 </body>
 <script>
    $( document ).ready(function() {
        ///alert("dddd");
        page_disp();
    });////end of $( document ).ready(function() {
    
    /////////////////////////////////////////////PAge display
    function page_disp() {
   //var grnd_no = 101;
   var grnd_no= $("#grnd_no").val();
         //alert(grnd_no);
            $.ajax({
                type: 'POST',
                dataType: "json",
                url: 'item_grn1_disp.php',
                        data: {
                        'grnd_no' : grnd_no,  
                        
                },
                cache: false,
                success: function(result) {
                  //alert(result.length);
                    document.getElementById("qty").value = '';
                     document.getElementById("costprice").value = '';
                    $("#order-listing tr").remove();

                            $("#item-disp tr").remove();
                           var row = $('<tr class="bg-success text-black"><td width=30%>' + "Item" + '</td><td width=10%>' + "Qty" + '</td><td width=10%>' + "QOH" + '</td><td width=10%>' + "Value" + '</td><td width=10%>' + "Action" + '</td>');
                          $('#order-listing').append(row);

                         var tot_qty=0;
                          var tot_val=0;



                    for (var i=0; i<result.length; i++) {
                      //alert(result[i].item_id);
                       var costval=result[i].cost_price * result[i].issue_qty ;
                      var itemid=result[i].item_id;
                       tot_qty = parseInt(tot_qty) + parseInt(result[i].issue_qty);
                       tot_val = parseInt(tot_val) + parseInt(costval);
                     // alert(itemid);
                      var row = $('<tr><td id="test">' + result[i].item_name+ '</td><td>' + result[i].issue_qty +  
                        '<td>' + result[i].loc_qoh + '</td><td>' + costval + 

                       '</td><td> <form method="post" action="#" enctype="multipart/form-data"> <input type="hidden" class="form-control pull-right" id="itemid" name="itemid"  value=' +  itemid +  ' >  <input type="hidden" id="grnd_no" name="grnd_no" value=' +  grnd_no +  '>  <button type="submit" formaction="item_grn1_removeitem.php" class="btn btn-outline-secondary btn-rounded btn-icon">                        <i class="fas fa-trash text-danger"></i>                      </button></form>'   );

                        $('#order-listing').append(row);
                    }
                    
                  var row = $('<tr class="bg-warning text-black"><td width=30%>' + "Total" + '</td><td width=10%>' + tot_qty + '</td><td width=10%>' + " " + '</td><td width=10%>' + tot_val + '</td><td width=10%>' + " " + '</td>');
                          $('#order-listing').append(row);   
                    
                    
                    

                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
}
    /////////////////////////////////////////////
    
    
    //////////////////////////////////////////////////////
               $("button").click(function(){
              //alert("Please Select Orderssss Type");
                //////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 //var category = 100;
                 var category = $("#item_id").val();
                 var issue_qty = $("#qty").val();
                 var grnd_no= $("#grnd_no").val();
                 var costprice=0;// $("#costprice").val();
                 
                //alert(category);
                //alert(issue_qty);
                //alert(grnd_no);
                //alert(costprice);
                 $.ajax({
                type: 'POST',
                 dataType: "json",
                url: 'item_grn1_ajax.php',
                        data: {
                        'grnd_no' : grnd_no,  
                        'category' : category , 
                        'issue_qty' : issue_qty ,  
                        'costprice' : costprice ,      

                },
                cache: false,
                success: function(result) {
                  page_disp();
                  
                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({


              });  //// end of  $("button").click(function(){
    
    ////////////////////////END OF BUTTON CLICK
    
    
    </script>
    
    
   <script>
  $(function () {
    //alert("Please Select Order Type");
    //Initialize Select2 Elements
    $('.select2').select2()

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



     
  })

</script>

<script>
   $('#item_id').on('change', function(){ 
        var item_id = $('#item_id').val();
      // alert(item_id);
       //item_grn1_itemdisp.php
        $.ajax({
                 type: 'POST',
                 dataType: "json",
                 url: 'item_grn1_itemdisp.php',
                        data: {
                        'item_id' : item_id,  
                         },
                cache: false,
                success: function(result1) {
                   //alert(result1[0].description);

 $("#stock-listing tr").remove();
                   var row = $('<tr class="bg-success text-black"><td width=10%>' + "stock_location" + '</td><td width=10%>' + "QOH" +  '</td><td width=10%>' + "Item" +  '</td>');
                 $('#stock-listing').append(row);


                    for (var i=0; i<result1.length; i++) {
                      //alert(result[i].item_id);
                      var itemid=result1[i].item_id;
                     // alert(itemid);
                     // $("#myTable tr").remove();

              




  var row = $('<tr class="table-success text-black"><td width=10%>' + result1[i].loc_name+ '</td><td width=10%>' + result1[i].loc_qoh + '</td><td width=10%>' + result1[i].item_name + '</td>'  );

                        $('#stock-listing').append(row);
                    }
                    
                   

                      }   ////~~~~~~~~~~end of sucess functions
                });  ///~~~~~~~~~~~~~~~~~~end of  $.ajax({
   })
    
</script>
