<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css_datatable.php"; ?>
<!-- bootstrap datepicker -->
<link rel="stylesheet" href="<?php echo $theme_link; ?>plugins/datepicker/datepicker3.css">
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

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

  <?php 
      /*Total Invoices*/
      $total_invoice=$this->db->query("SELECT COUNT(*) as total FROM db_sales WHERE db_sales.created_date=CURRENT_DATE() AND sales_code LIKE '%SL%';")->row()->total;
      /*Total Invoices Total*/
      $sal_total=$this->db->query("SELECT COALESCE(sum(grand_total),0) AS tot_sal_grand_total FROM db_sales WHERE db_sales.created_date=CURRENT_DATE() AND sales_code LIKE '%SL%';")->row()->tot_sal_grand_total;

      /*PAID AMOUNT*/
      $tot_received_amt=$this->db->select("COALESCE(SUM(paid_amount),0) AS paid_amount")->from("db_sales")->where("db_sales.created_date=CURRENT_DATE() AND sales_code LIKE '%SL%';")->get()->row()->paid_amount;

      $sales_due_total=$this->db->query("SELECT COALESCE(SUM(sales_due),0) AS sales_due FROM db_customers")->row()->sales_due;
      //$sales_due_total = $sal_total - $sal_return_total;
     
  ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?=$page_title;?>
        <small>View/Search Quotation Items</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="view_payments_modal">
    </div>

    <!-- Main content -->
    <?= form_open('#', array('class' => '', 'id' => 'table_form')); ?>
    <input type="hidden" id='base_url' value="<?=$base_url;?>">
    <section class="content">
      <!-- Small boxes (Stat box) -->
      <div class="row" hidden>
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-aqua">
            <div class="inner">
              <h3><label id="today_tot_inv_count"><?= $total_invoice;?></label></h3>

              <p><?= $this->lang->line('total_invoices'); ?></p>
            </div>
            <div class="icon">
              <i class="ion ion-bag"></i>
            </div>
            <a href="<?= base_url('reports/sales') ?>" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-green">
            <div class="inner">
              <h3><label id="today_tot_inv_sum"><?= $CI->currency($sal_total,$with_comma=true);?></label></h3>
              <p><?= $this->lang->line('total_invoices_amount'); ?></p>
            </div>
            <div class="icon">
              <i class="fa fa-plus-square-o"></i>
            </div>
            <a href="<?= base_url('reports/sales') ?>" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-yellow">
            <div class="inner">
              <h3><label id="today_tot_inv_recevied"><?= $CI->currency($tot_received_amt,$with_comma=true);?></label></h3>
             <p><?= $this->lang->line('total_received_amount'); ?></p>
            </div>
            <div class="icon">
              <i class="fa fa-undo"></i>
            </div>
            <a href="<?= base_url('reports/sales_return') ?>" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->
        <div class="col-lg-3 col-xs-6" id="dueArea" hidden>
          <!-- small box -->
          <div class="small-box bg-red">
            <div class="inner">
              <h3><label id="tot_inv_due"><?= $CI->currency($sales_due_total,$with_comma=true);?></label></h3>
              <p><?= $this->lang->line('total_sales_due'); ?></p>
            </div>
            <div class="icon">
              <i class="fa fa-hourglass-2 "></i>
            </div>
            <a href="<?= base_url('reports/profit_loss') ?>" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->
      </div>
      <!-- /.row -->
      <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>
              <?php if($CI->permissions('sales_add')) { ?>
              <div class="box-tools">

              </div>
              <?php } ?>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
            <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="formDate" name="formDate" 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="toDate" class="col-sm-4 control-label">To Date</label>
                          <div class="col-sm-8">
                            <input type="date" class="form-control" id="toDate" name="toDate" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                          </div>
                      </div>
                  </div>
                </div>
              </div>
              <br>
              <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>Quotation Date</th>
                  <th>Quotation Code</th>
                  <!-- <th><?= $this->lang->line('sales_status'); ?></th> -->
                  <th><?= $this->lang->line('reference_no'); ?></th>
                  <th><?= $this->lang->line('customer_name'); ?></th>
                  <!-- <th>Warehouse</th> -->
                  <!-- <th><?= $this->lang->line('total'); ?></th> -->
                  <!-- <th><?= $this->lang->line('paid_amount'); ?></th> -->
                  <!-- <th><?= $this->lang->line('due'); ?></th> -->
                  <!-- <th><?= $this->lang->line('payment_status'); ?></th> -->
                  <th><?= $this->lang->line('created_by'); ?></th>
                  <th><?= $this->lang->line('action'); ?></th>
                </tr>
                </thead>
                <tbody>
				
                </tbody>

              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </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>
  $(document).ready(function() {
    $('#example2').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: [
            {
                className: 'btn bg-red color-palette btn-flat delete_btn pull-left',
                text: 'Delete',
                action: function ( e, dt, node, config ) {
                    multi_delete();
                }
            },
            { extend: 'copy', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6]} },
            { extend: 'excel', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6]} },
            { extend: 'pdf', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6]} },
            { extend: 'print', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6]} },
            { extend: 'csv', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [1,2,3,4,5,6]} },
            { extend: 'colvis', className: 'btn bg-teal color-palette btn-flat',text:'Columns' },  
        ]
      },
      "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();
});
$("#formDate,#toDate").change(function(){
    //$('#example2').DataTable().destroy();
    fill_tbData();
});
</script>

<script>
  function fill_tbData(){
      var from_date    = $('#formDate').val();
      var to_date    = $('#toDate').val();

      $.ajax({
        url:"<?php echo site_url('quotation_s/invoice_info_get')?>",
        type: 'post',
        data: {from_date:from_date,to_date:to_date},
        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;           
                var sum_grand = 0;
                var sum_paid =0;
                var sum_due =0;
                $.each(data.inv_info, function(){
                  var s_id = this['id'];

                  var txt_sales_code = this['sales_code'];
                  if(this['return_bit']!= null){
                    var info = "\n<span class='label label-danger' style='cursor:pointer'><i class='fa fa-fw fa-undo'></i>Return Raised</span>";
                  }else{
                    var info = '';
                  }
                  
                  var grand_total = parseFloat(this['grand_total']);
                  var paid_amount = parseFloat(this['paid_amount']);
                  var sales_due = (grand_total-paid_amount);
                  var sales_due_txt = sales_due.toFixed(2);

                  if(this['payment_status']=='Unpaid'){
                    var str= "<span class='label label-danger' style='cursor:pointer'>Unpaid </span>";
                  }else if(this['payment_status']=='Partial'){
                    var str="<span class='label label-warning' style='cursor:pointer'> Partial </span>";
                  }else if(this['payment_status']=='Paid'){
                    var str="<span class='label label-success' style='cursor:pointer'> Paid </span>";
                  }else {
                    var str="<span class='label label-default' style='cursor:pointer'>"+ this['payment_status'] +"</span>";
                  }

                  var str2 = '';

                  str2 += '<div class="btn-group" title="View Account"><a class="btn btn-primary btn-o dropdown-toggle" data-toggle="dropdown" href="#">Action <span class="caret"></span></a>';
                  str2 += '<ul role="menu" class="dropdown-menu dropdown-light pull-right">';
									str2 +='<li><a title="View Quotation" href="quotation_s/invoice/'+s_id+'" ><i class="fa fa-fw fa-eye text-blue"></i>View Quotation</a></li>';
									str2 +='<li><a style="cursor:pointer" title="Print Quotation ?" onclick="print_invoice('+s_id+')"><i class="fa fa-fw fa-file-text text-blue"></i>Quotation Invoice</a></li>';
                  str2 +='<li><a  target="_blank" href="Quotation_s/print_quotation/'+s_id+'"><i class="fa fa-fw fa-print text-blue"></i>Print</a></li>';
                  str2 +='<li><a title="Update Record ?" target="_blank" href="Quotation_s/pdf/'+s_id+'"><i class="fa fa-fw fa-file-pdf-o text-blue"></i>PDF</a></li>';
                  str2 +='<li><a style="cursor:pointer" title="Delete Record ?" onclick="delete_sales('+s_id+')">	<i class="fa fa-fw fa-trash text-red"></i>Delete</a></li>';
                  str2 +='</ul> </div>';

                      table.row.add(['<input type="checkbox" name="checkbox[]" value='+this['id']+' class="checkbox column_checkbox" >',this['sales_date']+"<br>"+this['created_time'],txt_sales_code+info,this['reference_no'], this['customer_name'], this['created_by'], str2]);
                      indexNo++;
                      sum_grand  += grand_total;
                      sum_paid += paid_amount;
                      sum_due  += sales_due;
                });
                table.draw();

                // $('#lbl_sum_grand').empty();
                // $('#lbl_sum_grand').append(sum_grand.toFixed(2));
                // $('#lbl_sum_paid').empty();
                // $('#lbl_sum_paid').append(sum_paid.toFixed(2));
                // $('#lbl_sum_due').empty();
                // $('#lbl_sum_due').append(sum_due.toFixed(2));
              }else {
                  $('#example2').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/quotation_s.js"></script>
<script type="text/javascript">
  function print_invoice(id){
  window.open("<?= base_url();?>quotation_p/print_quotation/"+id, "_blank", "scrollbars=1,resizable=1,height=500,width=500");
}
</script>

<!-- Make sidebar menu hughlighter/selector -->
<script>$(".quotation-list-active-li").addClass("active");</script>
		
</body>
</html>
