<div class="page-container">
  <!-- BEGIN CONTENT -->
  <div class="page-content-wrapper">
    <!-- BEGIN CONTENT BODY -->
    <!-- BEGIN PAGE HEAD-->
    <div class="page-head">
      <div class="container-fluid">
        <!-- BEGIN PAGE TITLE -->
        <div class="page-title">
          <h1>Cortes de caja </h1>
        </div>
        <!-- END PAGE TITLE -->
      </div>
    </div>
    <!-- END PAGE HEAD-->
    <!-- BEGIN PAGE CONTENT BODY -->
    <div class="page-content">
      <div class="container-fluid">
        <!-- BEGIN PAGE BREADCRUMBS -->
        <ul class="page-breadcrumb breadcrumb">
          <%= render_breadcrumbs :tag => :li, :separator => ' <i class="fa fa-circle"></i> ' %>
        </ul>
        <!-- END PAGE BREADCRUMBS -->
        <!-- BEGIN PAGE CONTENT INNER -->
        <div class="page-content-inner">
          <div id="notice">
          <% if success %>
            <div class="alert alert-success">
              <p><%= success %></p>
            </div>
          <% elsif warning %>
            <div class="alert alert-warning">
              <p><%= warning %></p>
            </div>
          <% end %>
          </div>
          <div class="row">
            <div class="col-md-12">
              <%= hidden_field_tag 'title_for_print' %>
              <div class="portlet light ">
                <div class="portlet-title">
                  <div class="caption">
                    <i class="fa fa-list "></i>
                    <span class="caption-subject bold uppercase">Lista de cortes de caja</span>
                  </div>
                   <div class="actions">
                    <% if current_user.usertype == "G" %>
                      <% if can? :create, CashOut %>
                        <%= link_to new_open_cash_register_path, :remote => true, :class => 'btn btn bold green margin-top pull-right', :title => "Abrir caja registradora" do %> Abrir caja <i class="fa fa-plus"></i><% end %> <br>
                        <%= link_to get_open_cash_registers_path, remote: true, data: { toggle: "modal", target: "#get_open_cash_register" }, :class=>"btn bold green pull-right", :title=>"Realizar corte de caja", :style=> "margin-top:10px"  do %> Realizar corte de caja <i class="fa fa-plus"></i>
                        <% end %>
                      <% end %>
                    <% end %>
                    <% if current_user.usertype == "A" %>
                      <%= link_to opened_cash_registers_path, {:class=>"btn bold green pull-right"} do %> <i class="fa fa-search"></i>&nbsp Ver cajas abiertas <% end %>
                    <% end %>
                  </div>
                </div>
                <div class="portlet-body">
                  <!-- filtros de fecha si es Admin -->
                    <div class="form-horizontal" style="margin-bottom:35px">
                      <div class="row">
                        <div class="col-md-9">
                          <div class="form-group">
                            <%= label_tag :pointsale, "pointsale", {:class=>"col-md-2 control-label"} do %> Punto de venta
                            <% end %>
                            <div class="col-md-3">
                              <%= select_tag "pointsale", options_from_collection_for_select(Pointsale.vigentes, :id, :name, :selected => current_user.pointsale_id), :include_blank => "Todas", :disabled => (true unless current_user.usertype == 'A'),  class: "form-control select2 col-md-3" %>
                            </div>
                            <%= label_tag :begin_date, "Fecha", {:class=>"col-md-1 control-label"} do %> Desde
                            <% end %>
                            <div class="col-sm-2" style="padding-left:0px;padding-right:0px;">
                              <div class='input-group date' id='begin_date'>
                                <input id="start" type='text' class="form-control" value="<%= l(Date.today.beginning_of_month, format: '%d/%m/%Y') %>" />
                                  <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                  </span>
                              </div>
                            </div>
                            <%= label_tag :end_date, "Fecha", {:class=>"col-md-1 control-label"} do %> Hasta
                            <% end %>
                            <div class="col-sm-2" style="padding-left:0px;padding-right:0px;">
                              <div class='input-group date' id='end_date'>
                                <input id="end" type='text' class="form-control" value="<%= l(Date.today.end_of_month, format: '%d/%m/%Y') %>" />
                                  <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                  </span>
                              </div>
                            </div>
                            <button class="btn btn-icon-only blue pull-right" onclick="cashOutByPointsaleDate()">
                              <i class="fa fa-search"></i>
                            </button>
                          </div>
                        </div>
                      </div>
                    </div>
                    <input type='hidden' name='filter' id='filter' value='<%= @filter %>' >
                    <input type='hidden' name='current_page' id='current_page' value='<%= @current_page %>' >
                  <table class="table table-striped table-bordered table-hover tableadvancedprintable" id="cash_outs_table">
                    <thead>
                      <tr>
                        <th>#</th>
                        <% if current_user.usertype == "A" %>
                          <th>Punto de venta</th>
                        <% end %>
                        <th>Caja registradora</th>
                        <th>Ingresos</th>
                        <th>Egresos</th>
                        <th>Total</th>
                        <th>Fecha</th>
                        <th>Usuario</th>
                        <th>Recibió</th>
                        <th>Acciones</th>
                      </tr>
                    </thead>
                    <tbody>
                      <% @cash_outs.each_with_index do |cash_out, key| %>
                      <tr>
                        <td><%= cash_out.id %></td>
                        <% if current_user.usertype == "A" %>
                          <td>
                          <%= OpenCashRegister.get_pointsale(cash_out.open_cash_register_id, "open_cash_register").name %>
                          </td>
                        <% end %>
                        <td><%= cash_out.open_cash_register.cash_register.name %> </td>
                        <td><%= number_to_currency(cash_out.amount_in, precision: 2) %> </td>
                        <td><%= number_to_currency(cash_out.amount_out, precision: 2)  %></td>
                        <td><%= number_to_currency(cash_out.amount_in - cash_out.amount_out, precision: 2)  %></td>
                        <td><%= l(cash_out.created_at, :format => '%d/%B/%Y') %></td>
                        <td><%= cash_out.user.first_name %></td>
                        <td><%= (cash_out.received_by.blank? ? "" : cash_out.received_by.first_name) %></td>
                        <td class="text-center">
                          <%= link_to cash_out, {:class=>"btn btn-icon-only default filtros", :title=>"Ver corte de caja"} do %>
                            <i class="fa fa-search"></i>
                          <% end %>
                          <%= link_to print_cash_out_receipt_path(cash_out.id, format: 'pdf'), {:class=>"btn btn-icon-only default", :target => "blank"} do %>
                            <i class="fa fa-print"></i>
                          <% end %>
                        </td>
                      </tr>
                      <% end %>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- END PAGE CONTENT INNER -->
      </div>
    </div>
    <!-- END PAGE CONTENT BODY -->
    <!-- END CONTENT BODY -->
  </div>
  <!-- END CONTENT -->
</div>
<script>
  $(document).on("page:change", function() {
    $('#start').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY",
      defaultDate: moment().startOf('month').toDate()
    });

    $('#end').datetimepicker({
      icons: {
        date: "fa fa-calendar"
      },
      format: "DD/MM/YYYY",
      defaultDate: moment().endOf('month').toDate()
    });
  });

  function cashOutByPointsaleDate() {
    if ($("#start").val() && $("#end").val()) {
      var start = moment($("#start").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
      var end = moment($("#end").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
    } else {
      var start = null;
      var end = null;
    }
    var pointsale = $('#pointsale').val();
    if(pointsale == ""){
      pointsale = null;
    }
    App.blockUI({
      target: $("#cash_outs_table"),
      animate: true
    });
    $.ajax({
      type: "get",
      url:  '/find_cash_outs_by_date/' + pointsale + '/' + start + '/' + end,
      dataType: 'script',
      success: function(data) {
        window.setTimeout(function() {
          App.unblockUI($("#cash_outs_table"));
        }, 100);
      }
    });
  }
</script>
<!-- END CONTAINER
