<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
	<meta http-equiv="Content-type" content="application/vnd.ms-excel; charset=UTF-8">
</head>
<body>
<div class="row">
	<div class="col-md-12">
		<div class="margin-top-20">
			<div class="card">
				<div class="table-responsive">
          <div class="margin-top-20">
    				<div class="card">

    					<table class="table table-striped" border="0">
    						<% time_arr = Array.new %>
    						<% acc_arr = Array.new %>
                <% sale_item_count =0 %>
    						<% menu_cat_arr = Array.new %>
    						<% footer_arr = Array.new %>
    						<% count = 0 %>
    						<% waste_and_spoil_item_count = 0%>
                <% total_qty = 0 %>
                <% time_count = 0 %>
    						<% grand_total = 0 %>
      						<% @sale_data.each do |sale| %>
                  <% if !time_arr.include?(sale.date_format) %>
                  <% sale_item_count =1 %>
                    <% time_count = time_count + 1 %>
                    <thead>
                      <td>&nbsp;</td>
                      <tr>
                          <td>
                            <strong>Time :<%= sale.date_format %></strong>
                          </td>
                        <td></td>
                        <td></td>
                          <td colspan="3" style="text-align:right"><strong></strong></td>
                      </tr>
                      <tr>
                          <td><strong>Menu Category</strong></td>
                          <td><strong>Item Code</strong></td>
                          <td><strong>Item Name</strong></td>
                          <td><strong>Qty</strong></td>
                          <td><strong>Price</strong></td>
                          <td><strong>Total Price</strong></td>
                        </tr>
                    </thead>
                    <% time_arr.push(sale.date_format) %>
                    <% menu_cat_arr.clear %>
                    <% count = 0 %>
                  <% else %>
                    <% sale_item_count =sale_item_count +1 %>
                  <% end %>
                  <tbody>
    								<% if @print_settings.precision.to_i > 0
    									precision = @print_settings.precision
    								else
    									precision = 0
    								end
    								#check delimiter
    								if @print_settings.delimiter
    									delimiter = ","
    							    else
    									delimiter = ""
    							    end
    								 %>
                    <!-- all total qty sum -->
                      <% if sale.status_type != "Discount" && sale.status_type != "foc" && sale.status_type != "promotion"
                        total_qty += sale.total_item
                      end %>
                      <% if sale.status_type == "foc" && sale.price > 0
                        total_qty += sale.total_item
                      end %>
                      <% if sale.status_type == "Discount"
                        total_qty += sale.total_item*(-1)
                      end %>
                      <% if sale.status_type =="promotion"  && @type == "promotion"
                        total_qty += sale.total_item*(-1)
                      end %>

                    <!-- end all total qty -->
    								<% if sale.status_type == "foc" && sale.grand_total < 0
    									grand_total += sale.grand_total*(-1)
    								end %>

    								<% if sale.status_type == "Discount" &&  sale.grand_total < 0
    									grand_total += sale.grand_total*(-1)
    								end %>

    								<% if sale.status_type == "promotion" &&  sale.grand_total < 0
    									grand_total += sale.grand_total*(-1)
    								end %>

    								<% grand_total += sale.grand_total %>

    							  	<% if !sale.item_code.nil?%>
    							  		<% waste_and_spoil_item_count += sale.qty.to_i %>
    								  	<tr>
    								  		<% if !menu_cat_arr.include?(sale.menu_category_name) %>
    											<td><%= sale.menu_category_name %></td>
    											<% menu_cat_arr.push(sale.menu_category_name) %>
    										<% else %>
    											<td>&nbsp;</td>
    										<% end %>
                          <td><%= sale.item_code %></td>
    								  		<td><%= sale.product_name %></td>
    								  		<td><%= sale.total_item.to_i %></td>
    								  		<td><%= sale.unit_price.to_i %></td>
    								  		<td><%= sale.grand_total.to_i %></td>
    											<td><%= sale.date_format %></td>
    								  	</tr>
    							  	<% end %>
                    <!-- new tr -->
                    <% count = count + 1 %>
                    <% @hourly_total_qty.each do |hr| %>
                      <% if hr["date"].to_s == sale.date_format.to_s && hr["total_qty"].to_i ==sale_item_count%>
    													<tr>
    														<td></td>
    											  			<td colspan="2" style="text-align:right"> <strong>Total Qty: </strong></td>
    											  			<td>
    											  				<span class="underline" style="text-align:right">
    											  					<strong><%= total_qty.to_i %></strong>
    											  					<% total_qty = 0%>
    											  				</span></td>
    											  			<td style="text-align:right"> <strong>Grand Total: </strong></td>
    											  			<td >
    											  				<span class="underline" style="text-align:right">
    											  					<strong><%= grand_total.to_i %></strong>
    																	<% grand_total = 0 %>
    											  				</span>
    											  		 	</td>
    											  		</tr>
                            <% footer_arr.push(sale.sale_id) %>
                      <% else %>
                      <% end %>
                    <% end %>
    							</tbody>
                <% end %>
    					</table>
    				</div>
    			</div>
        </div>
</body>
</html>
