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


  $inv_no=0;

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


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

}
echo $inv_no;
if ($inv_no<>0){

  $sqlsp=mysqli_query($con,"SELECT inv_temp_header.*,customer_master.cus_name,stock_locations.loc_name
FROM inv_temp_header
LEFT JOIN stock_locations ON inv_temp_header.loc_id=stock_locations.loc_id
LEFT JOIN customer_master ON customer_master.cus_id=inv_temp_header.cus_id where invd_id=$inv_no ")or die(mysqli_error($con));
            $count=mysqli_num_rows($sqlsp); 
            if ($count>0) {
              echo "fff";
              while($rowsp=mysqli_fetch_array($sqlsp)){     
                 $loc_name=$rowsp['loc_name']; 
                 $invd_id=$rowsp['invd_id']; 
                 $invd_date=$rowsp['invd_date']; 
                 $loc_id=$rowsp['loc_id'];  
                  $cus_name=$rowsp['cus_name'];      
             }  

          
            }// if ($count>0) {

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






?>
<!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 | Sales 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 ($loc_id<>0){

  $loc_id=$loc_id;
echo "SR Ref:"; echo str_repeat("&nbsp;", 1);  echo $invd_id; echo str_repeat("&nbsp;", 5); echo "Location : "; echo str_repeat("&nbsp;", 1); echo $loc_name; 
echo str_repeat("&nbsp;", 5); echo "Inv Date : ";echo str_repeat("&nbsp;",1); 
echo date("Y-m-d",strtotime($invd_date));
echo str_repeat("&nbsp;", 5);

 echo "Customer : ";echo str_repeat("&nbsp;",1); 
echo $cus_name;

}
  ?>

            <div class="row">
            <div class="col-md-4 grid-margin stretch-card">
              <div class="card">
                <div class="card-body" style="background-color:#DCF7B8">
                              <form class="forms-sample">
                    <div class="form-group">
                      <label for="exampleInputUsername1">Item</label>
                       <input type="hidden" class="form-control" id="invref" name="invref" value="<?php echo   $invd_id;  ?>" >




                      <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 class="form-group">
             <div class="row"> <div class="result"></div>  </div>
              <div class="row">       
          <div class="result-unit"></div>
          <div class="result-qoh">QOH:</div></div>
           <input type="hidden" class="form-control" id="lqty" name="lqty"placeholder=" Qty">
        </div>
                    </div>
                     
                    <div class="form-group">
                      <label for="exampleInputEmail1">Qty</label>
                      <input type="number" class="form-control" id="qty" name="qty"placeholder=" Qty">
                       <input type="hidden" class="form-control" id="loc_id" name="loc_id"value=<?php echo $loc_id; ?> >
                    </div>

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

                      <div class="form-group">
                      <label for="exampleInputEmail1"> Discount</label>
                      <input type="number" class="form-control" id="dis" name="dis"placeholder=" Discount">
                      </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="srg_new_final_add.php" enctype="multipart/form-data" > 
                    <div class="form-group row">
                      <div class="table-responsive">
                       <input type="hidden" class="form-control" id="inv_no" name="inv_no" value="<?php  echo $inv_no;  ?>">
                        <input type="hidden" class="form-control" id="loc_id" name="loc_id" value="<?php  echo $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>
                      <div class="row">                     
  <div class="col-md-12">
  <div class="form-group row">
   <label class="col-sm-3 col-form-label">Discount: </label>
  <div class="col-sm-4">
 <input type="number" class="form-control" id="fdis" name="fdis" >
 
 </div>
 <button type="submit" formaction="inv_new1_discount.php" class="btn btn-primary mr-2">   Apply                                          </button>

 
<div class="col-md-8">
<div class="col-md-8">
                    <button type="submit" class="btn btn-primary mr-2">Create SR</button>
                    <button class="btn btn-light">Cancel</button>
                  </div>
                  </form>
                </div>
              </div>
            </div>
            </div>
                      <p class="card-description">
                    <button type="button" onclick="document.location='sri_new.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back
                    
                  </button>
                  </p>
   

        </div>  <!----- <div class="content-wrapper">------>
        </div>
        </div>
      </div>
    </div>
          <?php include('../includes/footer.php');?>
  </div>
  </div>

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

<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
          page_disp();

function page_disp() {
   
   var inv_no= $("#inv_no").val();
         // alert(grnd_no);
            $.ajax({
                type: 'POST',
                dataType: "json",
                url: 'inv_new1_disp.php',
                        data: {
                        'inv_no' : inv_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 >' + "Price" + '</td><td width=10%>' + "Discount"
                            + '</td><td>' + "Tot" + '</td><td>' + "Action" + '</td>');
                          $('#order-listing').append(row);



                          var tot_qty=0;
                          var tot_sales=0;
                          var total_sales=0;
                          var fdis_tot=0;
                          var total_sales_final=0;
                          var qty=0;

                    for (var i=0; i<result.length; i++) {
                      //alert(result[i].item_id);
                      var fdis=result[i].discount;
                      var itemid=result[i].item_id;
                      var tot=result[i].qty * result[i].sales_price;
                      var dis=result[i].item_dis;
                      var rawdis=(result[i].qty * result[i].sales_price) * dis/100;
                      qty=parseInt(result[i].qty);
                      tot_sales=tot - rawdis;

                      tot_qty = tot_qty + qty;
                      total_sales=total_sales + tot_sales;
                      fdis_tot = total_sales * fdis/100;
                      total_sales_final =total_sales - fdis_tot;


                     // alert(itemid);
                      var row = $('<tr><td id="test">' + result[i].item_name+ '</td><td>' + result[i].qty +  
                        '<td>' + result[i].sales_price +
                       '</td><td>' + result[i].item_dis + '</td><td>' + tot_sales + 
                       '</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" class="form-control pull-right" id="invref" name="invref"  value=' +  inv_no +  ' >                         <button type="submit" formaction="srg_new1_removeitem.php" class="btn btn-outline-secondary btn-rounded btn-icon" style="width: 15px; height: 15px;">                        <i class="fas fa-trash text-danger" style="font-size: 10px;"></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 >' + " " + '</td><td width=10%>' + " "
                            + '</td><td>' + total_sales.toFixed(2) + '</td><td>' + " " + '</td>');
                          $('#order-listing').append(row);


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


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

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

          






          ///////End of page refresh

           $("button").click(function(){
                //alert("Please Selectsssss Orderssss Type");
                //////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 //var category = 100;
                 var category = $("#item_id").val();
                 var issue_qty = $("#qty").val();
                 var invref= $("#invref").val();
                 //var costprice= $("#costprice").val();

                 $.ajax({
                type: 'POST',
                 dataType: "json",
                url: 'srg_new1_ajax.php',
                        data: {
                        'invref' : invref,  
                        'category' : category , 
                        'issue_qty' : issue_qty ,  
                       // 'costprice' : costprice ,      

                },
                cache: false,
                success: function(result) {
                 // alert("ff");
                
                 

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

                   page_disp();
              });  //// end of  $("button").click(function(){


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

                var item_id = $('#item_id').val();
                var loc_id = $('#loc_id').val();
               // alert(item_id);

                 $.ajax({
                 type: 'POST',
                 dataType: "json",
                 url: 'inv_new1_itemdisp.php',
                        data: {
                        'item_id' : item_id, 
                         'loc_id' : loc_id, 
                         },
                cache: false,
                success: function(result1) {
                   //alert(result1[0].description);
                    for (var i=0; i<result1.length; i++) {
                      //alert(result[i].item_id);
                      var itemid=result1[i].item_id;
                     // alert(itemid);
                     // $("#myTable tr").remove();
            document.getElementById("costprice").value = result1[i].sales_price;
            document.getElementById("dis").value = result1[i].discount_rate
                      $("#item-disp tr").remove();
                          var row = $('<tr class="bg-success text-black"><td width=10%>' + "Item" + '</td><td width=10%>' + "Unit" + '</td><td width=10%>'+ "QOH" + '</td><td width=10%>' + "ROL" +  '</td>');
               // $('#item-disp').append(row);
              
var qoh_disp="QOH : ".concat(result1[i].loc_qoh);
                    $(".result").html("Des : " . concat(result1[i].description)); 
                     $(".result-unit").html("Unit: ".concat(result1[i].unit_des) .  concat('&nbsp'));
                       $(".result-qoh").html(qoh_disp);
                      document.getElementById("lqty").value =result1[i].loc_qoh;
                     $("#qty").focus();   
                     
           // $("#sticker").focus();
            // document.getElementById("sticker").focus();

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

                       // $('#item-disp').append(row);
                    }
                    
                   

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

                


    //!!!!!!!!!!!!!!!!!!!!!!!!!!   Item Stock Display
$.ajax({
                 type: 'POST',
                 dataType: "json",
                 url: 'item_stock_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>');
                 $('#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>'  );

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

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




     //~~~~~~~~~~~~~~~~~~~~~~  end of  Item Stock Display


                 //alert(item_id);
                 }); ///// $('#item_id').on('change', function(){

              ///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 $('#qty').on('change', function(){
  // var qty = $('#qty').val();
  // var lqty = $('#lqty').val();

//var difqty=lqty - qty;
//alert(difqty);
//if (difqty<0)
//{   
//  alert("Cant Invoice this qty"); 
 //$("#qty").focus();
 // document.getElementById("qty").value =lqty;

 //}   

  });////


     
    });////end of $( document ).ready(function() {

function validateForm() { 

    //````````route````````````````````````````````````
    if($('#bname').val().trim().length == 0) {
           alert("Please enter the route you visited !!!");               
            return false;
    } ////``````````````````````````````````````````

    



 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //alert("Please Select Order Type");
    //Initialize Select2 Elements
    $('.select2').select2()

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



     
  })

</script>



