<?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'];
    $user_acc_no = $_SESSION['user_acc_no'];
}else{
  header('Location: login');
  die();
}
$dated = date('Y-m-d');    
  
  
  ?>
<!DOCTYPE html>
<html>
<head>
  <title><?php  if(basename(__FILE__, '.php'=='pr_new.php')){echo 'New Return Purchase';}?></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="#">Payables</a></li>
              <li class="breadcrumb-item"><a href="pr">Purchase Return</a></li>
              <li class="breadcrumb-item active">New Purchase Return </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"]))
      {
      ?>
       <script>
        $(document).ready(function(){
          
          $('#due_day').val(0);
          
         });
        </script>
        
      <form method="post" id="data_form" AutoComplete="off" />
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td colspan="2" align="center"><h2 style="margin-top:10.5px">New Purchase Return</h2></td>
            </tr>
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-8">
                      <br />
                        <select name="v_id" id="v_id" class="form-control input-sm v_id select2"  required />
                           <?php echo fill_party($connect);?>
                        </select>
                        <br />
                        <br />
                       
                        
                        <input type="text" name="description" id="description" class="form-control description" placeholder="Enter Description"  />
                        <br />
                        
                    </div>
                    <div class="col-md-4">
                      
                      
                   
                     <br />
                     
                     <select name="gst" id="gst" class="form-control gst" required />
                    
                      
                      <option value="OFF">GST OFF</option>
                      <option value="ON">GST ON</option>
                      </select>
                      <br />
                    
                   
                      <input type="text" name="inv_no" id="inv_no" class="form-control input-sm" placeholder="Invoice No." />
                      <br />
                      
                      
                      <input type="text" name="dated" id="dated" class="form-control input-sm" readonly placeholder="Select Date" AutoComplete="off" value="<?php echo $dated;?>" />
                      <br />
                      
                    
                 
                    </div>
                  </div>
                  <br />
                  <table width="100%" id="invoice-item-table" class="table table-bordered">
                     <tr>
                      <th width="5%">Sr</th>
                      
                      <th width="22%">Select Type</th>
                      <th width="23%">Item Name</th>
                      <th width="10%">Pcs/Ctn</th>
                      <th width="10%">Qty</th>
                      <th width="15%">Rate</th>
                      <th width="15%">Amount</th>
                      
                    </tr>
                  </table>
                    <div align="left">
                    <span id="item_info"></span>
                    <span id="item_detail"></span>
                  </div>
                  <div align="right">
                    <button type="button" name="add_row" id="add_row" class="form-control btn btn-success btn-xs">Add More Item</button>
                  </div>
                   
                </td>
              </tr>
            
               <tr>
                <td align="right"><b>Gross Amount</td>
                <td align="right"><b><span id="total_amount"></span></b></td>
              </tr>
               <tr>
                <td width="80%"align="right"><b>GST <span id="gst_pr"></span>(%) :</td>
                <td width="80%" align="right"><b><input type="text" name="gst_amount" id="gst_amount" class="form-control gst_amount" style="text-align:right" readonly></b></td>
              </tr>
               
             
              
               <tr>
                <td width="80%"><input type="text" name="discount_dsc" id="discount_dsc" class="form-control discount_dsc" placeholder="Less Detail :"></td>
                <td width="80%" align="right"><input type="text" name="discount" id="discount" class="form-control discount" placeholder="Less Amount" style="text-align:right"></td>
              </tr>
                <tr>
                <td width="80%"><input type="text" name="freight_dsc" id="freight_dsc" class="form-control freight_dsc" placeholder="Add Detail :"></td>
                <td width="80%" align="right"><input type="text" name="freight" id="freight" class="form-control freight" placeholder="Add Amount" style="text-align:right"></td>
              </tr>
             
              <tr>
                <td width="80%"align="right"><b>Net Amount :</td>
                <td width="80%" align="right"><b><span id="net_total"></span></b></td>
              </tr>
              <tr>
                <td colspan="2">
                </td>
              </tr>
              <tr>
                <td colspan="2" align="center">
                  <input type="hidden" name="total_item" id="total_item" value="1" />
                  <input type="hidden" name="first_row" id="first_row" class="first_row" value="0" /> 
                  <input type="hidden" name="btn_action" id="btn_action" />
                  <input type="hidden" name="gst_pr" class="gst_pr" />
                  <input type="submit" name="btn_action" id="btn_action" class="form-control btn btn-primary " 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>
  
<datalist id="datalist1">
  
</datalist>
</body>
</html>
 
 <script>
 
  $(document).ready(function(){
    $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
    $('#dated').datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
  })
  $('.select2').select2();
    
  </script>
     
      <script>
      $(document).ready(function(){
       var total_amount = $('#total_amount').text();
        var count = 1;
        var first_row = $('#first_row').val();
        if(first_row ==0)
        {
           var html_code = '';
          html_code += '<tr id="row_id_'+count+'">';         
          html_code += '<td><span id="sr_no">'+count+'</span></td>';
           
          html_code += '<td><input type="hidden" name="item_no[]" id="item_no'+count+'" data-srno="'+count+'" class="form-control input-sm item_no" required><select name="type_id[]" id="type_id'+count+'" data-srno="'+count+'" class="form-control input-sm type_id select2" required><?php echo fill_itemtype($connect);?></select></td>';
         html_code += '<td><input type="text" name="item_name[]" id="item_name'+count+'" data-srno="'+count+'" class="form-control input-sm number_only item_name" list="datalist1" required></td>';
         
         html_code += '<td><input type="text" name="ctn[]" id="ctn'+count+'" data-srno="'+count+'" class="form-control input-sm number_only ctn" required></td>';
          html_code += '<td><input type="text" name="qty[]" id="qty'+count+'" data-srno="'+count+'" class="form-control input-sm number_only qty" required></td>';
          html_code += '<td><input type="text" name="rate[]" id="rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only rate" required></td>';
          html_code += '<td><input type="text" name="amount[]" id="amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm amount" />';
          
          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>';
          $('#invoice-item-table').append(html_code);
          $('.select2').select2();         
          $('#total_item').val(count);
          first_row++;
          $('#first_row').val(first_row);  
      
          
          $('#v_id').focus();
          }
        $(document).on('click', '#add_row', function(){
         count++;
           $('#item_detail').html('');
           $('#item_info').html('');
          $('#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><input type="hidden" name="item_no[]" id="item_no'+count+'" data-srno="'+count+'" class="form-control input-sm item_no" required><select name="type_id[]" id="type_id'+count+'" data-srno="'+count+'" class="form-control input-sm type_id select2" required><?php echo fill_itemtype($connect);?></select></td>';
         html_code += '<td><input type="text" name="item_name[]" id="item_name'+count+'" data-srno="'+count+'" class="form-control input-sm number_only item_name" list="datalist1" required></td>';
           html_code += '<td><input type="text" name="ctn[]" id="ctn'+count+'" data-srno="'+count+'" class="form-control input-sm number_only ctn" required></td>';
          html_code += '<td><input type="text" name="qty[]" id="qty'+count+'" data-srno="'+count+'" class="form-control input-sm number_only qty" required></td>';
          html_code += '<td><input type="text" name="rate[]" id="rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only rate" required></td>';
          html_code += '<td><input type="text" name="amount[]" id="amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm amount" />';
          
          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>';
          $('#invoice-item-table').append(html_code);      
          
          $('.select2').select2(); 
          $('#type_id'+count).focus();   
         
        });
        
      $(document).on('click', '.remove_row', function(){
          var row_id = $(this).attr("id");
         
          $('#row_id_'+row_id).remove();
           cal_final_total(count);
          count--;
          $('#total_item').val(count);
        });
       
       $(document).on('blur', '.gst', function(){
          var gst = $('#gst').val();
          var gst_pr = 0;
           $('#gst_pr').text(gst_pr);
            $('.gst_pr').val(gst_pr);
          cal_final_total(count);
          if(gst =='ON')
          {
         
          $.ajax({
                url:"gst_data.php",
                method:"POST",
                data:{gst:gst},
                dataType:"json",
                success:function(data){
                     $('#gst_pr').text(data.gst_value);
                     $('.gst_pr').val(data.gst_value);
                     cal_final_total(count);
                     
                }
           });
         }
        });
       $(document).on('select2:select','.select2', function(e){
            var x = $(this).val();
            $.ajax({
              url:"find_item.php",
              method:"POST",
              data:{type_id:x},
              dataType:"text",
              success:function(data)
              {
                
                $('#item_name'+count).val('');
                $('#datalist1').html(data);
                
               //$('#item_name'+count).editableSelect();
              }
           
         })
      });
       $(document).on('blur', '.qty', function(){
                var item_no = $('#item_no'+count).val();
                var v_id = $('#v_id').val();
               
                
                $.ajax({
                  url:"purchase_history.php",
                  method:"POST",
                  data:{item_no:item_no, v_id:v_id},
                  dataType:"json",
                  success:function(data)
                  {   
                     if(item_no > 0)
                    {
                   // $('#rate'+count).val(data.rate);
                    
                    $('#item_detail').html(data.item_detail);
                    }
                    
                    
                  }
                })
             });
        
        
    $(document).on('blur', '.item_name', function(){
          var item_id = 0;
          var btn_action = 'check';
          var type_id = $('#type_id'+count).val();
          var item_name = $('#item_name'+count).val();
          $.ajax({
                  url:"check_item.php",
                  method:"POST",
                  data:{btn_action:btn_action,type_id:type_id,item_name:item_name},
                  dataType:"json",
                  success:function(data)
                  {
                  item_id = data.item_id;
                  if(item_id > 0)
                    {
                      $('#item_no'+count).val(data.item_id);      
                    }else{$('#item_no'+count).val(0);$('#item_info').text('New Item Will Be add');}
                  
                  }
                
                })
          
        }); 
       $(document).on('blur', '.rate', function(){
         var item_id = $('#item_no'+count).val(); 
         if(item_id ==0)
                    {
                      if(confirm("Are you sure you want to add this item?"))
                      {
                        var cost = prompt("Please enter Item Cost", "");
                        if(cost !=null)
                        {
                          var item_cost = cost;
                        }
                        var btn_action = 'add';
                        var type_id = $('#type_id'+count).val();
                        var item_name = $('#item_name'+count).val();
                        $.ajax({
                          url:"create_item.php",
                          method:"POST",
                          data:{type_id:type_id, item_name:item_name, item_cost:item_cost, btn_action:btn_action},
                          dataType:"json",
                          success:function(data)
                          {
                            $('#item_no'+count).val(data.item_id);
                            alert('Item Added');
                           
                           
                          }
                        })
                      }
                      else
                      {
                        return false;
                      }
                    }
                  
          
        }); 
      
       $(document).on('blur', '.type_id', function(){
            
            x = $('#type_id'+count).val();            
            $.ajax({
              url:"find_item.php",
              method:"POST",
              data:{type_id:x},
              dataType:"text",
              success:function(data)
              {
                
               $('#item_name'+count).html(data);
               $('#item_name'+count).editableSelect();
               $('#item_name'+count).focus();
                }
              
            })
            
            });
        function cal_final_total(count)
        {
         var final_item_amount = 0;
         var  final_dis_amount = 0;
         
          for(j=1; j<=count; j++)
          {
            var qty = 0;
            var rate = 0;
            var amount = 0;
           
           
            var total_amount = 0;
           
           
            qty = $('#qty'+j).val();
            if(qty > 0)
            {
              rate = $('#rate'+j).val();
              
              if(rate > 0)
              {
                amount = parseFloat(qty) * parseFloat(rate);
                $('#amount'+j).val(amount);           
                
                
           
                final_item_amount = parseFloat(final_item_amount) + parseFloat(amount);
           
               
                                           
               
               
              
              }
            }
          }
          var new_final_item_amount = Number(final_item_amount).toFixed(2);
          $('#total_amount').text(new_final_item_amount);
          
          
          var net_total = 0;
          var gst_amount= 0;
         gst_pr = $('.gst_pr').val();
          net_total = parseFloat(final_item_amount) ;
          if(gst_pr > 0)
          {
            gst_amount = parseFloat(final_item_amount) * parseFloat(gst_pr)/100;
          }
         
          $('#gst_amount').val(gst_amount);
          net_total = parseFloat(final_item_amount) + parseFloat(gst_amount);
          
         // $('#total_item_discount').text(final_dis_amount);
          // $('#total_item_discount').text(final_dis_amount);
       var discount = $('#discount').val();
         var freight = $('#freight').val();
           if(discount==''){discount=0;}
         if(freight==''){freight=0;}
         net_total = parseFloat(net_total) -  parseFloat(discount) + parseFloat(freight);
     //   net_total = new Number(net_total).toLocaleString("en-US");
        // gst_amount = new Number(gst_amount).toLocaleString("en-US");
       var new_net_total =  Number(net_total).toFixed(2);
       var new_gst_amount = Number(gst_amount).toFixed(2);
          $('#net_total').text(new_net_total);  
        
            $('#gst_amount').val(new_gst_amount); 
          
          
          
            
          
        }        
        
       
      $(document).on('submit', '#data_form',function(event){
      event.preventDefault();
          cal_final_total(count);
          var net_total = $('#net_total').text();
          if(net_total < 1)
            {alert('Invoice Not Acceptable');
            return false;
            }
        $('#btn_save').val('Wait...');
        $('#btn_save').attr('disabled','disabled');
        $('#btn_action').val('save');
        var _data = $(this).serialize();
        $.ajax({
          url:"pr_action.php",
          method:"POST",
          data:_data,
          success:function(data)
          {
           $('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
           
            window.location = "pr";
          }
        })
        });
        $(document).on('blur', '.description', function(){
           var elmnt = document.getElementById("type_id"+count);
          elmnt.scrollIntoView();
          $('#type_id'+count).focus();
        });
         $(document).on('blur', '.amount', function(){
          $('#add_row').focus();
        });
        $(document).on('blur', '.v_id', function(){
          $('#type_id'+count).focus();
        });
      
        $(document).on('blur', '.rate', function(){
          cal_final_total(count);
        });
        
       
        $(document).on('blur', '.discount', function(){
          cal_final_total(count);
        });
        $(document).on('blur', '.freight', function(){
           cal_final_total(count);
        });
         $(document).on('click', '#save', function(){
             cal_final_total(count);
         });
       
       
      });
      </script>
      <?php
      }
      
      ?>     