<div class="page-header">
 <ul class="breadcrumb">
  <li><a href="<%= dashboard_path %>">Home</a></li>
  <li>Receipt List Report</li>
 </ul>
</div>

<div class="container">
    <%= render :partial=>'shift_sale_report_filter',
    		   :locals=>{ 	:period_type => true, :shift_name => false, :report_path =>reports_receipt_no_index_path} %>
	<hr />
</div>

<div class="container">
	<div class="row">
		<div class="col-md-12 text-right">
			<a href="javascript:export_to('<%=reports_receipt_no_index_path%>.xls')" class = "btn btn-default">Export to Excel</a>
		</div>	  			
	</div>
</div>

<div class="container margin-top-20">
	<div class="card row">
	  <table class="table table-striped">
	    <thead>
	      <tr> 
	        <th style='text-align:center;'>Date</th>
	        <th style='text-align:center;'>Receipt No</th>
	        <th style='text-align:center;'>Cashier Name</th>
		    <th style='text-align:center;'>Gross Sales</th>
		    <th style='text-align:center;'>Discount</th> 
		    <th style='text-align:center;'>Total Sales</th>
		    <% TaxProfile.all.each do |r|%>
		      <th style='text-align:center;'><%=r.name%></th>
		    <% end %>
		    <th style='text-align:center;'>Nett Sales</th>
	      </tr>
	    </thead>

	    <tbody> 
	    	<% total_sales = 0 %>
	    	<% net_sales = 0 %>
	    	<% @sale_data.each do |sale| %>
	    		<% total_sales = sale.total_amount.to_f - sale.total_discount.to_f%>
	    		<% net_sales = total_sales.to_f + sale.total_tax.to_f%>
	    		<tr>
		    		<td style='text-align:center;'><%= sale.receipt_date.strftime("#{sale.receipt_date.day.ordinalize} %b") rescue '-'  %></td>
		    		<td style='text-align:center;'><%=sale.receipt_no.to_s rescue ''%></td>
		    		<td style='text-align:center;'><%=Employee.find(sale.cashier_id).name rescue ''%></td>
		    		<td style='text-align:center;'><%= number_with_delimiter(sprintf("%.2f",sale.total_amount.to_f), :delimiter => ',') %></td>
		    		<td style='text-align:center;'><%= number_with_delimiter(sprintf("%.2f",sale.total_discount.to_f), :delimiter => ',') %></td>
		    		<td style='text-align:center;'><%= number_with_delimiter(sprintf("%.2f",total_sales.to_f), :delimiter => ',') %></td>
		    		<% sale.sale_taxes.each do |sale|%>
				        <td style='text-align:center;'><%= number_with_delimiter(sprintf("%.2f",sale.tax_payable_amount.to_f), :delimiter => ',') %></td>
				    <% end %>
		    		
		    		<td style='text-align:center;'><%= number_with_delimiter(sprintf("%.2f",net_sales.to_f), :delimiter => ',') %></td>
		    	</tr>
	    	<% end %>
	    </tbody>
	  </table>
	</div>
</div>

<%= paginate @sale_data %>

<script>
$(function(){
	var check_arr = [];   	
	var search = '<%= params[:period_type] %>';	
	if(search){
		if(parseInt(search) == 0){
			search_by_period();	
		}
		else{
			search_by_date();				
		}
	}else{
		search_by_period();		
	}
    $('#sel_period').change(function(){	   
	   search_by_period();	   
    });   
	function search_by_period(){
 	   var period = $('#sel_period').val();
 	   var period_type = 0;
 	   var from = "";
 	   var to = ""; 	
	}   
   
    $('#from').change(function(){
	   search_by_date();	   
    });
   
    $('#to').change(function(){
	   search_by_date();
    });      
    function search_by_date(){
	   var from = $('#from').val();
	   var to = $('#to').val();
	   var period = 0;
	   var period_type = 1;	   
	   if(to != '' && from != ''){
		   shift_name = from + ',' + to;
		   check_arr.push(to);		   
		   // console.log(check_arr.length)
		   if(check_arr.length == 1){ 		   			   	
		   }
		   if(check_arr.length == 3){
			   check_arr = [];
		   }
	   }	   
   }    
});
</script>