<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>Clientes deudores</h1>
				</div>
				<!-- END PAGE TITLE -->
			</div>
		</div>
		<!-- END PAGE HEAD-->
		<!-- BEGIN PAGE CONTENT BODY -->
		<div class="page-content">
			<div class="container-fluid">
				<%= link_to  debtors_path(:filter => @filter, :current_page => @current_page), {:class=>"btn blue-hoki pull-right margin-bottom-10"} do %>  <i class="fa fa-angle-left "></i> Regresar
				<% end %>
				<!-- 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">
							<div class="portlet light ">
								<div class="portlet-title">
									<div class="caption">
										<i class="fa fa-list "></i>
										<span class="caption-subject bold uppercase">Ventas al cliente <%= @custom.nick_name %></span>
										<span class="caption-helper"></span>
									</div>
								</div>
								<div class="portlet-body">
								<!-- tabs? -->
									<div class="tabbable-custom ">
										<ul class="nav nav-tabs ">
											<li class="active">
												<a href="#customer_sales" data-toggle="tab" aria-expanded="true"> Ventas </a>
											</li>
											<li class="">
												<a href="#abonos" data-toggle="tab" aria-expanded="false"> Abonos </a>
											</li>
										</ul>
										<div class="tab-content">
											<div class="tab-pane active" id="customer_sales">
												<div class="pull-right-margin-bottom-10">
													<!-- búsqueda por fecha-->
													<div class="row">
														<div class="col-md-11">
															<%= hidden_field_tag 'title_for_print' %>
															<div class="form-group">
																<%= 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"/>
																			<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"/>
																		<span class="input-group-addon">
																			<span class="glyphicon glyphicon-calendar"></span>
																		</span>
																	</div>
																</div>
															<button class="btn btn-icon-only blue" style="margin-left:25px" onclick="salesByDate()">
																<i class="fa fa-search"></i>
															</button>
															</div>
														</div>
													</div>
												<!-- fin búsqueda por fecha-->

												<!-- contenido customer sales -->
												<table class="table table-striped table-bordered table-hover tableadvancedprintable" id="sales_table">
													<thead>
														<tr>
															<th>#</th>
															<th>Código de venta</th>
															<% if current_user.usertype == 'A' %>
																<th>Punto de venta</th>
															<% end %>
															<th>Vendedor</th>
															<th>Fecha</th>
															<th>Tipo</th>
															<th>Productos</th>
															<th>Status</th>
															<th>Total</th>
															<th>Abonado</th>
															<th>Restante</th>
															<th width="15%">Acciones</th>
														</tr>
													</thead>
													<tbody id="customer_sales" >
													<% @sales.each_with_index do |sale, key| %>
														<tr class=<%= (sale.cancelled? ? 'danger' : (sale.paid? ? 'success' : '')) %>>
															<td><%= sale.id %></td>
															<td><%= sale.sale_code %> </td>
															<% if current_user.usertype == 'A' %>
																<td> <%= sale.get_pointsale.name %> </td>
															<% end %>
															<td><%= sale.user.first_name %> </td>
															<td><%= l(sale.date_sale, :format => '%d/%m/%Y') %></td>
															<td>
															<% if sale.saletype == "credit" && sale.credit_note.blank? %>
																Crédito
															<% elsif sale.saletype == "credit" && sale.credit_note.present? %>
																Crédito/vale
															<% else %>
																Contado
															<% end %>
															</td>
															<td><%= sale.products.count %></td>
															<td>
															<% case sale.status %>
																<% when "paid"%>
																	<span class="label label-success"> PAGADA </span>
																<% when "cancelled"%>
																	<span class="label label-danger"> CANCELADO </span>
																<% when "parcial"%>
																	<span class="label label-warning"> ABONADA </span>
																<% when "notpaid"%>
																	<span class="label label-default"> PENDIENTE PAGO </span>
															<% end %>
															</td>
															<td><%= number_to_currency(sale.total, precision: 2) %></td>

															<% credito = Credit.where(:sale_id => sale.id).first %>

															<% if credito.present? %>
																<% abonos = CreditPayment.where(:credit_id => credito.id, :status => 0).sum(:quantity) %>
																<% if abonos.present? %>
																	<td><%= number_to_currency(abonos, precision: 2) %></td>
																<% end %>
																<td><%= number_to_currency(credito.rest, precision: 2) %></td>
															<% else %>
																<td></td>
																<td></td>
															<% end %>

															<td class="text-center">
																<%= link_to sale, {:class=>"btn btn-icon-only default", :title=>"Ver venta"} do %>
																	<i class="fa fa-search"></i>
																<% end %>
															<% if can? :destroy, Sale %>
																<% daysToCancel = @pos_config.days_cancel_sale %>
																	<% if (sale.date_sale + daysToCancel.days >= Date.today) && !sale.cancelled? %>
																		<%= link_to sale , method: :delete, :class => "btn btn-icon-only btn-danger", :title=>"Cancelar venta", data: { confirm: '¿Esta seguro que desea cancelar la venta?'}   do %> <i class="fa fa-ban"></i>
																		<% end %>
																	<% end %>
															<% end %>
															</td>
														</tr>
													<% end %>
													</tbody>
												</table>
												</div>
											</div>
												<!-- fin contenido customer sales -->

												<!-- contenido abono -->
												<div class="tab-pane" id="abonos">
												<table class="table table-striped table-bordered table-hover tableadvancedprintable" id="sales_table">
													<thead>
														<tr>
															<th>Folio</th>
															<th>Fecha</th>
															<th>Deuda</th>
															<th>Abono</th>
															<th>Restante</th>
															<td>Status</td>
															<th width="15%">Acciones</th>
														</tr>
													</thead>
													<tbody id="abonos" >
													<% @credit.each_with_index do |credit, key| %>
														<% abonos = CreditPayment.where(:credit_id => credit.id) %>
															<% abonos.each_with_index do |abono, key| %>
															<tr class=<%= (credit.cancelled? ? 'danger' : (credit.active? ? 'success' : '')) %>>
																<td><%= credit.sale.sale_code %> </td>
																<td><%= abono.date_payment %></td>
																<td><%= number_to_currency(credit.total, precision: 2) %></td>
																<td><%= number_to_currency(abono.quantity, precision: 2) %></td>
																<td><%= number_to_currency(credit.rest, precision: 2) %></td>
																<td>
																	<% case abono.status %>
																	<% when "cancelled"%>
																		<span class="label label-danger"> CANCELADO </span>
																	<% when "active"%>
																		<span class="label label-success"> ACTIVO </span>
																	<% end %>
																</td>

																<td class="text-center">
																<% if !abono.cancelled? && abono.cash_registers_move.present? && abono.cash_registers_move.open_cash_register.open?%>
																		<%= link_to cash_move_delete_payment_path(:credit_payment_id => abono.id, :credit => credit.id) , method: :delete_credit_payment, :class => "btn btn-icon-only btn-danger", :title=>"Cancelar abono", data: { confirm: '¿Esta seguro que desea cancelar el abono?'}   do %> <i class="fa fa-ban"></i>
																		<% end %>
																<% end %>
																</td>
															</tr>
														<% end %>
													<% end %>
													</tbody>
												</table>
												</div>
												<!-- fin contenido abono -->
										</div>
								</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() {
		 App.init();
		 $('#begin_date').datetimepicker({
			icons: {
				date: "fa fa-calendar"
			},
			format: "DD/MM/YYYY",
			defaultDate: moment().startOf('month').toDate()
		 });

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

		var startDate = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY')
		var endDate = moment($("#end_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY');
		$('#title_for_print').val('Ventas del ' + startDate + ' al ' + endDate);
	});

	function salesByDate() {
		var start = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
		var end = moment($("#end_date").data("date"), "DD-MM-YYYY").format('YYYY-MM-DD HH:mm:ss');
		var cliente = '<%= @custom.id %>'
		App.blockUI({
				 target: $("#sales_table"),
				 animate: true
		});
		$.ajax({
			type: "get",
			url:  '/find_customer_sales_by_date/' + start + '/' + end + '/' + cliente,
			dataType: 'script',
			success: function(data) {
					window.setTimeout(function() {
						App.unblockUI($("#sales_table"));
						var startDate = moment($("#begin_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY')
						var endDate = moment($("#end_date").data("date"), "DD-MM-YYYY").format('DD/MM/YYYY');
						$('#title_for_print').val('Ventas del ' + startDate + ' al ' + endDate);
					}, 100);
			}
		});
	}
</script>
