<?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');
  accounts_update($connect);          
 
  ?>
<!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">
  
  <style>
    .rightside{text-align: right;}
    #item-table {
      font-family: arial;
      font-weight: bold;
      font-size: 12px;
    }
  </style>
</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="#">Receivables</a></li>
              <li class="breadcrumb-item"><a href="ar_cash_receipt.php">Cash Receipt</a></li>
              <li class="breadcrumb-item active">New Cash Receipt Voucher </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"]))
      {
      ?>
           <div align="right">
                     <button id="get_customers" class="btn btn-success">Load Customers</button>
                    
                    </div>
       <form method="post" id="data_form">
      <br />
      
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td colspan="2" align="center"><h2 style="margin-top:10.5px">New Cash Receipt Detail</h2></td>
            </tr>
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-4">  
                      <input type="text" name="dated" id="dated" class="form-control input-sm" readonly value="<?php echo $dated; ?>" AutoComplete="off" required />
                 
                   
                 
                  
                  
                    </div>
                    <div class="col-md-4">
                
                   
                      
                         <select name="sm_id" id="sm_id" class="form-control input-sm sm_id"  />
                           <?php echo fill_sm($connect);?>
                        </select>   

                      
                    </div>
                     <div class="col-md-4">
                
                   
                      
                         <select name="area" id="area" class="form-control input-sm area" />
                       
                          <?php echo fill_area($connect);?>
                        </select>
                       
                    </div>
                

                  </div>
               
               <br />
                  
                  <table width="100%"id="item-table" class="table table-bordered">
                   <tr>
                      <th width="5%">Sr#.</th>
                 
                      <th width="65%">From Customer</th>
                      <th width="15%">Pre-Balance</th>
                     
                      <th width="15%">Received</th>
                      
                      
                      
                      
                    </tr>
                   
                   
                    
                  </table>
                
                  
                  </td>
                  </tr>
                
             
              <tr>
                <td width="70%" align="right"><b>Total : <span id="tot_balance"></span><b></td><td align="right"><span id="total_amount"></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="0" />
                  <input type="hidden" name="btn_action" id="btn_action" />
                  <input type="submit" name="save" id="btn_save" 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>
  
  
  
</body>
</html>

 <script>
 
  $(document).ready(function(){
    $(window).keydown(function(event){
    if(event.keyCode == 13) {
    
      event.preventDefault();
    }
  });
    $('#dated').datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
  })
  
  </script>
 <script type="text/javascript">
      $(document).ready(function(){
        count = 0;
        function fetch_data()
           {
            
            var area = $('#area').val();
            var sm_id = $('#sm_id').val();
            
            $.ajax({  
                      url:"ar_cash_receipt_area_fetch.php",  
                      method:"POST", 
                      data:{area:area, sm_id:sm_id}, 
                      dataType:"json",
                      success:function(data){  
                  $('#item-table').append(data.rows);
                   $('#total_item').val(data.count);
                    $('#tot_balance').text(data.tot_balance);
                     count = data.count;
                      }  
                  }); 
           }
    
        
        function cal_final_total(count)
        {
        
          var total_amount = 0;
    
          
          for(j=1; j<=count; j++)
          {
            
            
       
            var amount = $('#amount'+j).val();
           
            
            
            
           
            
            
        
            if(amount > 0)
            {
              total_amount = parseFloat(total_amount) + parseFloat(amount);
              
             
               
             
            }
            
          
            
          }
          
        
          $('#total_amount').text(total_amount);
        }

        $(document).on('click','#get_customers', function(){
          $("#item-table tr").remove(); 
          fetch_data();
        });
        
        $(document).on('submit', '#data_form', function(event){
          event.preventDefault();
          cal_final_total(count);
           
           
          $('#btn_save').val('Wait...');
          $('#btn_save').attr('disabled','disabled');
          $('#btn_action').val('save');
          var _data = $(this).serialize();
          $.ajax({
            url:"ar_cash_receipt_area_action.php",
            method:"POST",
            data:_data,
            success:function(data)
            {
             $('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
              
             window.location = "ar_cash_receipt";
              
            }
          })
        });
        
        $(document).on('blur', '.amount', function(){

          cal_final_total(count);
         
        });
   });     
</script>
  <?php
}
?>
