<?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');
 $tr_id = $_GET["id"];
  if(isset($_POST["update"]))
  { 
    
 
 
        $sql = "delete from adjusted_stock where tr_id = '".$tr_id."'";
        $statement = $connect->prepare($sql);
        $statement->execute();
    
       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>
  <?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_adjust.php">Item Adjustment</a></li>
              <li class="breadcrumb-item active">Edit Item Adjustment </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["update"]))
      {
         include('include/script.php');
        $ref_id = $_GET["id"];
        $statement = $connect->prepare("
          SELECT * FROM adjusted_stock s WHERE s.tr_id= :tr_id
            LIMIT 1
        ");
        $statement->execute(
          array(
            ':tr_id'       =>  $_GET["id"]
            )
          );
        $result = $statement->fetchAll();
        foreach($result as $row)
        {
        ?>
       
        <script>
        $(document).ready(function(){
         
          
          $('#acc_no').val("<?php echo $row["acc_no"]; ?>");
          
          
          
        });
        </script>
      
         
       <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 For</h2></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>
                   
                    <?php
                    $statement = $connect->prepare("
                      
                          select a.tr_id,a.dated,a.item_no,concat(t.type_name,' ',i.item_name)item_name,a.store_id,s.store_name,a.qty_in,a.qty_out
                          from adjusted_stock a,items i,store s,item_type t WHERE t.type_id=i.type_id and i.item_id=a.item_no and
                          s.store_id=a.store_id 
                          and a.tr_id = :tr_id
                    ");
                    $statement->execute(
                      array(
                        ':tr_id'       =>  $tr_id
                      )
                    );
                    $item_result = $statement->fetchAll();
                    $m = 0;
                    foreach($item_result as $sub_row)
                    {
                      $m = $m + 1;
                    ?>
                    <tr>
                      <td><span id="sr_no"><?php echo $m; ?></span></td>
                        <td><select name="item_no[]" id="item_no<?php echo $m; ?>" class="form-control input-sm item_no select2" required />
                        <option value="<?php echo $sub_row["item_no"]; ?>"><?php echo $sub_row["item_name"]; ?></option>
                          <?php echo fill_itemstock($connect);?>
                        </select></td>
                        <td><select name="store_id[]" id="store_id<?php echo $m; ?>" class="form-control input-sm from_store" required />
                        <option value="<?php echo $sub_row["store_id"]; ?>"><?php echo $sub_row["store_name"]; ?></option>
                          <?php echo fill_store($connect);?>
                        </select></td>
                        
                     <td><input type="text" name="qty_in[]" id="qty_in<?php echo $m; ?>" class="form-control input-sm number_only qty_in" value="<?php echo $sub_row["qty_in"]; ?>" required />
                      <td><input type="text" name="qty_out[]" id="qty_out<?php echo $m; ?>" class="form-control input-sm number_only qty_out" value="<?php echo $sub_row["qty_out"]; ?>" required />
                      
                      <td></td>
                    </tr>
                     <?php
                    }
                    ?>
                    
                  </table>
                   
                    <div align="right">
                    <button type="button" name="add_row" id="add_row" class="form-control btn btn-success btn-xs">+</button>
                  </div>
                  <div align="right">
                   
                      <br />
                          <br />
                            <br />
                          <br />
                            <br />
                      
                  </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="<?php echo $m; ?>" />
                  <input type="submit" name="update" id="update" class="form-control btn btn-info btn_save" value="Update" />
                </td>
              </tr>
          </table>
        </div>
      </form>
        </div>
              </div>
          </div>
      </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 
    </div>
    </div>
  
  
</body>
</html>
     
      <script>
      $(document).ready(function(){
        var final_total_amt = $('#total_amount').text();
        var count = "<?php echo $m; ?>";
        
       $(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"> <?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"><?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"></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" /></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);
          $('.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
      }
      }
      ?>