<?php
  //invoice.php  
  include('db_con.php');
  include('function.php');
  
  if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
$dated = date('Y-m-d');
  if(isset($_POST["save"]))
  { 
    if($_POST['total_item'] > 0)
    {
     
     $sql = "SELECT MAX(tr_id) FROM adjusted_stock";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $tr_id = $statement->fetchColumn();
            $tr_id++;
    
 
    
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO adjusted_stock
          (tr_id,dated,acc_no,item_no,store_id,qty_in,qty_out,user_id)
          VALUES(:tr_id, :dated, :acc_no, :item_no, :store_id, :qty_in, :qty_out, :user_id)
        ");
        $statement->execute(
          array(
            
            ':tr_id'              =>  $tr_id,            
            ':dated'              =>  $dated, 
            ':acc_no'              =>  0,            
            ':item_no'              =>  trim($_POST["item_no"][$count]),
            ':store_id'              =>  trim($_POST["store_id"][$count]),
            ':qty_in'          =>  trim($_POST["qty_in"][$count]),
            ':qty_out'           =>  trim($_POST["qty_out"][$count]),
            ':user_id'           =>  $user_id            
          )
        );
      }
        $sql = "update adjusted_stock a, items i set a.cost=i.item_cost where a.item_no=i.item_id and a.tr_id = '".$tr_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
          
     
      header("location:item_adjust");
      }
  }
  
  ?>
<!DOCTYPE html>
<html>
<head>
  <title>New Item Adjust</title>
  <?php include('include/head.php'); ?>
  <link rel="stylesheet" href="css/datepicker.css">
  <link rel="stylesheet" href="css/jquery.datetimepicker.css">
  <link rel="stylesheet" href="css/jquery.datetimepicker.min.css">
  <link rel="stylesheet" href="css/jquery.jquery-ui.min.css">
  
  
</head>
<body>
  <?php include('include/header.php'); ?>
  <?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Inventory</a></li>
              <li class="breadcrumb-item"><a href="item_transfer">Item_Transfer</a></li>
              <li class="breadcrumb-item active"> Item Transfer New </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
      <?php
      if(isset($_GET["add"]))
      {
      ?>
         
       <form method="post" id="item_form">
      <br />
      
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td><h2 style="margin-top:10.5px">New Item Adjustment</h2></td><td>
               </td>
            </tr>
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-4">
                      <br />                    
              
               
                    <br />
                     
                    
                    </div>
                  </div>
                  
                   
               
                  
                  <table width="100%" id="item-table" class="table table-bordered">
                   <tr>
                      <th width="5%">Sr#.</th>
                      <th width="50%">Item Name</th>
                        <th width="20%">Store</th>
                      <th width="10%">Qty In</th>
                      <th width="10%">Qty Out</th>
                      <th width="5%"></th>
                      
                    </tr>
                   
                   
                    
                  </table>
                   
                  <div align="right">
                    <button type="button" name="add_row" id="add_row" class="form-control btn btn-success btn-xs">+</button>
                      <br />
                          <br />
                            <br />
                     
                  </div>
                   <div align="left">
                      
                 
                  </div>
                   <div align="left">
                      
                   <span><b>Stock Available : </b></span><span id="total_stock"></span>
                    <span id="item_detail"></span>
                   
                  </div>
                 
                  </td>
                  </tr>
                
             
              <tr>
               
              </tr>
              <tr>
                <td colspan="2"></td>
              </tr>
              <tr>
                <td colspan="2" align="center">
                  <input type="hidden" name="total_item" id="total_item" value="0" />
                  <input type="submit" name="save" id="save" class="form-control btn btn-info btn_save" value="Save" />
                </td>
              </tr>
          </table>
        </div>
      </form>
        </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 
    </div>
    </div>
  <?php include('include/script.php'); ?>
  
  <script src="js/jquery-ui.js"></script>
  <script src="js/jquery-ui.min.js"></script>
  
  
  
</body>
</html>
     
      <script>
      $(document).ready(function(){
       // var final_total_amt = $('#total_amount').text();
        var count = 0;
        
       $(document).on('click', '#add_row', function(){
          count++;
          $('#total_item').val(count);
          var html_code = '';
          html_code += '<tr id="row_id_'+count+'">';
          html_code += '<td><span id="sr_no">'+count+'</span></td>';          
          html_code += '<td><select name="item_no[]" id="item_no'+count+'" data-srno="'+count+'" class="form-control item_no select2" required> <?php echo fill_itemstock($connect);?></select></td>';
          html_code += '<td><select name="store_id[]" id="store_id'+count+'" data-srno="'+count+'" class="form-control store_id" required ><?php echo fill_store($connect); ?></select></td>';                                              
          html_code += '<td><input type="text" name="qty_in[]" id="qty_in'+count+'" data-srno="'+count+'" class="form-control qty_in" required ></td>';                                              
          html_code += '<td><input type="text" name="qty_out[]" id="qty_out'+count+'" data-srno="'+count+'" class="form-control input-sm number_only qty_out" required /></td>';          
         
          html_code += '<td><button type="button" name="remove_row" id="'+count+'" class="form-control btn btn-danger btn-xs remove_row">X</button></td>';
          html_code += '</tr>';
          $('#item-table').append(html_code);
          $('#store_id'+count).val("<?php echo $store_id;?>");
          $('.select2').select2();
          $('#item_no'+count).focus();
          
        });
   
    
        $(document).on('click', '.remove_row', function(){
          var row_id = $(this).attr("id");
          
          
          $('#row_id_'+row_id).remove();
          count--;
          $('#total_item').val(count);
        });
        
      });
      </script>
      <?php
      }
      
      ?>
