<!DOCTYPE html>
<html>

<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css_datatable.php"; ?>
</head>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!-- Left side column. contains the logo and sidebar -->
  
  <?php include"sidebar.php"; ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?=$page_title;?>
        <small>View/Search Cheque History</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active"><?=$page_title;?></li>
      </ol>
    </section>
    <div class="pay_now_modal">
    </div>
    <div class="pay_return_due_modal">
    </div>
    <!-- Main content -->
    <?= form_open('#', array('class' => '', 'id' => 'table_form')); ?>
    <input type="hidden" id='base_url' value="<?=$base_url;?>">
    <section class="content">
      <div class="row">
        <!-- ********** ALERT MESSAGE START******* -->
        <?php include"comman/code_flashdata.php"; ?>
        <!-- ********** ALERT MESSAGE END******* -->
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header with-border">
              <h3 class="box-title"><?=$page_title;?></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <div class="row">
                <div class="col-md-12">
                     <!-- Custom Tabs -->
                     <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs">
                           <li class="active"><a href="#tab_1" data-toggle="tab">Customer</a></li>
                           <li><a href="#tab_2" data-toggle="tab">Supplier</a></li>
                        </ul>
                        <div class="tab-content">
                           <div class="tab-pane active" id="tab_1">
                            <div class="row">
                              <div class="col-xs-12">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="formDate" class="col-sm-4 control-label">Form Date</label>
                                        <div class="col-sm-8">
                                          <input type="date" class="form-control" id="formDate1" name="formDate1" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo')); $date->modify('-30 day'); echo $date->format('Y-m-d');?>">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="toDate" class="col-sm-4 control-label">To Date</label>
                                        <div class="col-sm-8">
                                          <input type="date" class="form-control" id="toDate1" name="toDate1" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                  <div class="form-group">
                                    <label for="customer_id" class="col-sm-4 control-label">Customer</label>
                                      <div class="col-sm-8">         
                                        <select class="form-control select2" id="customer_id" name="customer_id" ">
                                          <option value="all">-All-</option>
                                          <?php
                                              $q1=$this->db->query("select * from db_customers where status=1");
                                              if($q1->num_rows()>0)
                                              {
                                                  foreach($q1->result() as $res1)
                                                {
                                                  echo "<option value='".$res1->id."'>".$res1->customer_name."</option>";
                                                }
                                              }
                                              else
                                              {
                                                  ?>
                                          <option value="">No Records Found</option>
                                          <?php
                                              }
                                              ?>
                                        </select>
                                      </div>
                                    </div>
                                </div>
                              </div>
                            </div>
                            <br>
                              <div class="row">
                                 <!-- right column -->
                                 <div class="col-md-12">
                                    <!-- form start -->
                                    <br>
                                    <div class="table-responsive">
                                      <table id="example2" class="table table-bordered table-striped" width="100%">
                                        <thead class="bg-primary ">
                                        <tr>
                                          <th class="text-center"> #
                                            <!-- <input type="checkbox" class="group_check checkbox" > -->
                                          </th>
                                          <th class="text-center">Cheque<br>No.</th>
                                          <th class="text-center">Received<br>Date</th>
                                          <th class="text-center">Clearance<br>Date</th>
                                          <th class="text-center">Customer<br>Name</th>
                                          <th class="text-center">Sales<br>Code</th>
                                          <th class="text-center">Amount</th>
                                          <th class="text-center">Bank</th>
                                          <th class="text-center">Cheque Type</th>
                                          <th class="text-center">Status</th>
                                          <th class="text-center"><?= $this->lang->line('action'); ?></th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                
                                        </tbody>
                                      </table>
                                    </div>
                                    <!-- /.box-body -->
                                 </div>
                                 <!--/.col (right) -->
                              </div>
                              <!-- /.row -->
                           </div>
                           <!-- /.tab-pane -->
                           <div class="tab-pane" id="tab_2">
                           <div class="row">
                              <div class="col-xs-12">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="formDate" class="col-sm-4 control-label">Form Date</label>
                                        <div class="col-sm-8">
                                          <input type="date" class="form-control" id="formDate2" name="formDate2" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo')); $date->modify('-30 day'); echo $date->format('Y-m-d');?>">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="toDate" class="col-sm-4 control-label">To Date</label>
                                        <div class="col-sm-8">
                                          <input type="date" class="form-control" id="toDate2" name="toDate2" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                  <div class="form-group">
                                    <label for="customer_id" class="col-sm-4 control-label">Supplier</label>
                                      <div class="col-sm-8">         
                                        <select class="form-control select2" id="supplier_id" name="supplier_id">
                                          <option value="all">-All-</option>
                                          <?php
                                              $q1=$this->db->query("select * from db_suppliers where status=1");
                                              if($q1->num_rows()>0)
                                              {
                                                  foreach($q1->result() as $res1)
                                                {
                                                  echo "<option value='".$res1->id."'>".$res1->supplier_name."</option>";
                                                }
                                              }
                                              else
                                              {
                                                  ?>
                                          <option value="">No Records Found</option>
                                          <?php
                                              }
                                              ?>
                                        </select>
                                      </div>
                                    </div>
                                </div>
                              </div>
                            </div>
                            <br>
                              <div class="row">
                                 <!-- right column -->
                                 <div class="col-md-12">
                                    <!-- form start -->
                                    <br>
                                    <div class="table-responsive">
                                      <table id="example3" class="table table-bordered table-striped" width="100%">
                                        <thead class="bg-primary ">
                                        <tr>
                                          <th class="text-center"> #
                                            <!-- <input type="checkbox" class="group_check checkbox" > -->
                                          </th>
                                          <th class="text-center">Cheque<br>No.</th>
                                          <th class="text-center">Received<br>Date</th>
                                          <th class="text-center">Clearance<br>Date</th>
                                          <th class="text-center">Supplier<br>Name</th>
                                          <th class="text-center">GRN<br>Code</th>
                                          <th class="text-center">Amount</th>
                                          <th class="text-center">Bank</th>
                                          <th class="text-center">Cheque Type</th>
                                          <th class="text-center">Status</th>
                                          <th class="text-center"><?= $this->lang->line('action'); ?></th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                
                                        </tbody>
                                      </table>
                                    </div>
                                    <!-- /.box-body -->
                                 </div>
                                 <!--/.col (right) -->
                              </div>
                              <!-- /.row -->
                              <!-- /.row -->
                           </div>
                           <!-- /.tab-pane -->
                        </div>
                        <!-- /.tab-content -->
                     </div>
                     <!-- nav-tabs-custom -->
                  </div>
              </div>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>

        <div class="modal fade" id="chqReturn-modal">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Return Details</h4>
              </div>
              <div class="modal-body">
                  <div class="row">
                    <div class="col-md-12">
                      <div class="box-body">
                        <div class="form-group" hidden>
                          <label for="chqID">ID</label>
                          <input type="text" class="form-control" id="chqID" name="chqID" placeholder="">
                        </div>
                        <div class="form-group">
                          <label for="discount_input">Return Reason</label>
                          <textarea class="form-control" id="return_reason" rows="3"></textarea>
                        </div>
                      </div>
                    </div>
                  </div>

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="btn_save_return">Return</button>
              </div>
            </div>
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
    <?= form_close();?>
  </div>
  <!-- /.content-wrapper -->
  <?php include"footer.php"; ?>
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- SOUND CODE -->
<?php include"comman/code_js_sound.php"; ?>
<!-- TABLES CODE -->
<?php include"comman/code_js_datatable.php"; ?>
<!-- bootstrap datepicker -->
<script src="<?php echo $theme_link; ?>plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript">
  //Date picker
    $('.datepicker').datepicker({
      autoclose: true,
    format: 'dd-mm-yyyy',
     todayHighlight: true
    });
</script>
<script>
  $(document).ready(function() {
    $('#example2,#example3').DataTable({
      /* FOR EXPORT BUTTONS START*/
      dom:'<"row margin-bottom-12"<"col-sm-12"<"pull-left"l><"pull-right"fr><"pull-right margin-left-10 "B>>>tip',
      /* dom:'<"row"<"col-sm-12"<"pull-left"B><"pull-right">>> <"row margin-bottom-12"<"col-sm-12"<"pull-left"l><"pull-right"fr>>>tip',*/
      buttons: {
        buttons: [
            { extend: 'copy', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6,7,8]} },
            { extend: 'excel', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6,7,8]} },
            { extend: 'pdf', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6,7,8]} },
            { extend: 'print', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6,7,8]} },
            { extend: 'csv', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6,7,8]} },
            { extend: 'colvis', className: 'btn bg-teal color-palette btn-flat',text:'Columns' },  
        ]
      },
      "lengthMenu" : [
            [10,20, 25, 50,500, -1],
            [10,20, 25, 50,500, 'All'],
        ],
      "processing": true, //Feature control the processing indicator.
      "pageLength": 10,
      //"serverSide": true, //Feature control DataTables' server-side processing mode.
      "order": [], //Initial no order.
      "responsive": true,
      language: {
          processing: '<div class="text-primary bg-primary" style="position: relative;z-index:100;overflow: visible;">Processing...</div>'
      },
    });
    $('.deliver_chk').iCheck({
      checkboxClass: 'icheckbox_square-orange',
      /*uncheckedClass: 'bg-white',*/
      radioClass: 'iradio_square-orange',
      increaseArea: '10%' // optional
    });
         //call_code();
         //$(".delete_btn").hide();
  });
</script>
<script type="text/javascript">
$(document).ready(function() {
    //datatables
    fill_tbData();
});

$("#formDate1,#toDate1,#customer_id,#formDate2,#toDate2,#supplier_id").change(function(){
    fill_tbData();
});

// $("#supplier_id").change(function(){
//     alert("uwqyetuqwe");
// });

</script>

<script>
  function fill_tbData(){
    var formDate1    = $('#formDate1').val();
    var toDate1    = $('#toDate1').val();
    var customer_id = $('#customer_id').val();

    var formDate2    = $('#formDate2').val();
    var toDate2    = $('#toDate2').val();
    var supplier_id = $('#supplier_id').val();

      $.ajax({
        url:"<?php echo site_url('payment_types/cheque_info_get')?>",
        type: 'POST',
        data: {formDate1:formDate1,toDate1:toDate1,customer_id:customer_id,formDate2:formDate2,toDate2:toDate2,supplier_id:supplier_id},
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          
          table.clear();
            $.each(data.data_count_info, function(){
              var dataCount =parseInt(this['d_count']);
              //console.log(dataCount);
              // if(dataCount>0) { 

                var indexNo = 1;           
                $.each(data.chq_info, function(){
                  var cheque_type = this['cheque_type'];
                  var cheque_status = this['status'];

                  if(cheque_status=='RETURN'){
                    var str= "<span class='label label-danger' style='cursor:pointer'>Return </span>";
                  }else if(cheque_status=='OK'){
                    var str="<span class='label label-success' style='cursor:pointer'> Active </span>";
                  }

                  if(cheque_type=='inv'){
                    var str_2= "Invoice Payment";
                  }else if(cheque_type=='cus_pay'){
                    var str_2="Customer Payment";
                  }else if(cheque_type=='sup_pay'){
                    var str_2="Supplier Payment";
                  }else if(cheque_type=='grn'){
                    var str_2="Purchase Payment";
                  }

                  if (this['inv_on'] == null) {
                    var salesCode = '';
                  } else {
                    var salesCode = this['sales_code'];
                  }

                  var str3 = '';

                    if(cheque_status=='RETURN'){
                     // str3 +='&nbsp; &nbsp;<a title="Active" onclick="active_now('+this['id']+')"  name="btn_active" class="btn btn-success" id="btn_active"> Active </a>   ';
                     str3+='';
                    }else{
                      str3 +='&nbsp; &nbsp;<a title="Return" onclick="return_now('+this['id']+')" name="btn_return" class="btn btn-danger" id="btn_return"> Return </a>   ';
                    }


                      table.row.add([indexNo,this['cheque_no'],this['cheque_date'],this['cheque_clearance_date'], this['customer_name'], salesCode, this['amount'],this['bank'], str_2 ,str, str3]);
                      indexNo++;
                });
                table.draw();
              // }else {
              //     $('#example2').DataTable().destroy();
              //   toastr["warning"]("Something Wrong! , Data Not Found. ");
              // }
            });

            var table_2;
          table_2 = $('#example3').DataTable();
          
          table_2.clear();
            $.each(data.data_count_info, function(){
              var dataCount =parseInt(this['d_count']);
              //console.log(dataCount);
              //if(dataCount>0) { 

                var indexNo = 1;           
                $.each(data.sup_chq_info, function(){
                  var cheque_type = this['cheque_type'];
                  var cheque_status = this['status'];

                  if(cheque_status=='RETURN'){
                    var str= "<span class='label label-danger' style='cursor:pointer'>Return </span>";
                  }else if(cheque_status=='OK'){
                    var str="<span class='label label-success' style='cursor:pointer'> Active </span>";
                  }

                  if(cheque_type=='inv'){
                    var str_2= "Invoice Payment";
                  }else if(cheque_type=='cus_pay'){
                    var str_2="Customer Payment";
                  }else if(cheque_type=='sup_pay'){
                    var str_2="Supplier Payment";
                  }else if(cheque_type=='grn'){
                    var str_2="Purchase Payment";
                  }

                  if (this['grn_id'] == null) {
                    var purchaseCode = '';
                  } else {                    
                    var purchaseCode = this['purchase_code'];
                  }

                  var str3 = '';

                    if(cheque_status=='RETURN'){
                     // str3 +='&nbsp; &nbsp;<a title="Active" onclick="active_now('+this['id']+')"  name="btn_active" class="btn btn-success" id="btn_active"> Active </a>   ';
                     str3+='';
                    }else{
                      str3 +='&nbsp; &nbsp;<a title="Return" onclick="return_now('+this['id']+')" name="btn_return" class="btn btn-danger" id="btn_return"> Return </a>   ';
                    }


                    table_2.row.add([indexNo,this['cheque_no'],this['cheque_date'],this['cheque_clearance_date'], this['supplier_name'], purchaseCode, this['amount'],this['bank'], str_2 ,str, str3]);
                      indexNo++;
                });
                table_2.draw();
              // }else {
              //     $('#example3').DataTable().destroy();
              //   toastr["warning"]("Something Wrong! , Data Not Found. ");
              // }
            });
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
  }
</script>


<!-- <script src="<?php //echo $theme_link; ?>js/suppliers.js"></script> -->

<!-- Make sidebar menu hughlighter/selector -->
<script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>

<script>
  function return_now(id) {
    var select_id = id;
    swal({
        title: 'Are you sure?',
        text: 'Do you want to return this cheque?',
        icon: 'warning',
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
      if (willDelete) {
        $("#chqID").val(select_id);
        $('#chqReturn-modal').modal('show');
      } else {
        toastr["success"]("Cheque details is safe!");
      }
    });
  }
</script>

<script>
  function active_now(id) {
    var csrf_test_name ='<?php echo $this->security->get_csrf_hash();?>';
    var select_id = id;
    swal({
        title: 'Are you sure?',
        text: 'Do you want to active this cheque?',
        icon: 'warning',
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
      if (willDelete) {
        var formData = new FormData();

        formData.append("csrf_test_name", csrf_test_name);
        formData.append("select_id", select_id);

        $.ajax({
          type: 'post',
          url: '<?php echo $base_url; ?>payment_types/active_cheque',
          data: formData,
          processData: false,
          contentType: false,
          dataType: 'JSON',
          success: function(data){
              if(data=='success'){
                toastr["success"]("Cheque Actived!");
                fill_tbData();                
              }else if(data=='failed'){
                toastr["error"]("Sorry!,Error occured !!");
              } 
          },
          error: function (xhr, ajaxOptions, thrownError) {
              console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      } else {
        toastr["success"]("Cheque details is safe!");
      }
    });
  }
</script>

<script>
  $('#btn_save_return').on('click',function(){
    var csrf_test_name ='<?php echo $this->security->get_csrf_hash();?>';
    var chqID = $("#chqID").val();
    var return_reason = $("#return_reason").val();
    if(return_reason==""){
      toastr["error"]("Sorry!, Enter the Reason!");
    }else{
      $("#btn_save_return").attr("disabled", "disabled");
      var formData = new FormData();

      formData.append("csrf_test_name", csrf_test_name);
      formData.append("chqID", chqID);
      formData.append("return_reason", return_reason);

      $.ajax({
        type: 'post',
        url: '<?php echo $base_url; ?>payment_types/return_cheque',
        data: formData,
        processData: false,
        contentType: false,
        dataType: 'JSON',
        success: function(data){
            if(data=='success'){
              toastr["success"]("Cheque Returned!");
              fill_tbData();
              $("#btn_save_return").attr("disabled", false);
              $("#chqReturn-modal .close").click();
               
            }else if(data=='failed'){
              toastr["error"]("Sorry!,Error occured !!");
            } 
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
    }
  });
</script>
		
</body>
</html>
