<div class="container-fluid">
	<div class="page-header">
	  	<ol class="breadcrumb">
		    <li class="breadcrumb-item"><a href="<%= root_path %>"><%= t("views.right_panel.button.home") %></a></li>
		    <li class="breadcrumb-item active"><%= t("views.right_panel.detail.hourly_saleitem_report") %></li>
		    <span class="float-right">
		      <%= link_to 'Back', dashboard_path %>
		    </span>
	  	</ol>
	</div>
	<div class="row">
		<div class="col-md-12">
			<%= render :partial=>'hourly_saleitem_report_filter',
			:locals=>{ 	:period_type => true, :shift_name => true,:payments => true, :report_path =>reports_hourly_saleitem_index_path} %>
			<hr />
			<div class="text-right">
				<a href="javascript:export_to('<%=reports_hourly_saleitem_index_path%>.xls')" class = "btn btn-info wave-effects"><%= t("views.btn.exp_to_excel") %></a>
			</div>
			<div class="margin-top-20">
				<div class="card">
					<div colspan="7" class="report-table-header">
						<%= t("views.right_panel.detail.from_date") %> : <%= @from.strftime("%Y-%b-%d") rescue '-' %>  - <%= t("views.right_panel.detail.to_date") %> : <%= @to.strftime("%Y-%b-%d")  rescue '-'%>
					</div>
				<% if @start_time.blank? || @end_time.blank? %>
					<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>
											<% if @start_time.blank? || @end_time.blank? %>
												<strong>Time :<%= sale.date_format %></strong>
											<% else %>
												<strong>Time : <%= "#{@start_time} - #{@end_time}" %></strong>
											<% end %>
                    </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>
                <!-- 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><%= number_format(sale.unit_price.to_i, precision:precision.to_i,delimiter:delimiter) %></td>
											<td><%= number_format(sale.grand_total.to_i, precision:precision.to_i,delimiter:delimiter) %></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><%= number_format(grand_total.to_i, precision:precision.to_i,delimiter:delimiter) %></strong>
																	<% grand_total = 0 %>
											  				</span>
											  		 	</td>
											  		</tr>
                        <% footer_arr.push(sale.sale_id) %>
                  <% else %>

                  <% end %>
                <% end %>
							</tbody>
            <% end %>
					</table>
				<% else %>
				<table class="table table-striped" id="items_table" border="0">
					<thead>
						<tr>
							<th colspan="7"> <%= t("views.right_panel.detail.from_time") %> : <%= @start_time rescue '-' %>  - <%= t("views.right_panel.detail.to_time") %> : <%= @end_time  rescue '-'%></th>
						</tr>
						<% if @shift_from %>
						<tr>
							<% if @shift_data.employee %>
							<% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %>
							<% end %>
							<th colspan="7"> <%= t("views.right_panel.detail.shift_name") %> =  <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )</th>
						</tr>
						<% end %>
						<tr>
							<th>&nbsp;</th>
							<th><%= t("views.right_panel.header.menu_category") %></th>
							<th><%= t("views.right_panel.detail.code") %></th>
							<th><%= t("views.right_panel.detail.product") %></th>
							<th><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %></th>
							<th><%= t("views.right_panel.detail.unit_price") %></th>
							<th><%= t("views.right_panel.detail.revenue") %></th>
						</tr>
					</thead>
					<tbody>
						<% acc_arr = Array.new %>
						<% cate_arr = Array.new %>
						<% p_qty = 0 %>
						<% sub_qty = 0 %>
						<% sub_total = 0 %>
						<% other_sub_total = 0 %>
						<% product_sub_total = 0 %>
						<% count = 0 %>
						<% row_count = 0 %>
						<% total_price = 0 %>
						<% cate_count = 0 %>
						<% acc_count = 0 %>
						<% grand_total = 0 %>
						<% total_qty = 0 %>
						<% total_amount = 0 %>
						<% discount = 0 %>
						<% total_item_foc = 0 %>
						<% total_item_dis = 0.0 %>
						<% total_tax = 0 %>
						<% unless @sale_data.blank? %>
						<% @sale_data.each do |sale| %>
						<% row_count += 1 %>
						<!-- 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
								total_item_foc += sale.grand_total*(-1)
							end %>

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

							<% if !acc_arr.include?(sale.account_id) %>
								<tr>
									<td><b><%= sale.account_name %></b></td>
									<td colspan="4">&nbsp;</td>
									<td><%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %></td>
									<td>
										<% @totalByAccount.each do |account, total| %>
										<% if sale.account_id == account %>
										<b><%= number_format(total, precision:precision.to_i,delimiter:delimiter) %></b>
										<% grand_total += total %>
										<% end %>
										<% end %>
									</td>
								</tr>
								 <% acc_arr.push(sale.account_id) %>
							<% end %>
							<tr>
								<td>&nbsp;</td>
								<% if !cate_arr.include?(sale.menu_category_id) %>
								<td><%= sale.menu_category_name %></td>
								<% cate_arr.push(sale.menu_category_id) %>
								<% else %>
								<td>&nbsp;</td>
								<% end %>
								<td><%= sale.item_code rescue '-' %></td>
								<td><%= sale.product_name rescue '-' %></td>
								<% if sale.status_type != "Discount" %>
									<td><%= sale.total_item rescue '-' %></td>
								<%else%>
										<td><%= sale.total_item*(-1) rescue '-' %></td>
								<% end %>

								<td><%= number_format(sale.unit_price ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
								<td><%= number_format(sale.grand_total ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
							</tr>
							<!-- sub total -->

						<% @menu_cate_count.each do |key,value| %>
							<% if sale.account_id == key %>
									<% count = count + 1 %>
									<% sub_total += sale.grand_total %>
									<% #sub_qty += sale.total_item %>
									<% if sale.status_type !="Discount" && (!sale.product_name.include? "FOC") && sale.status_type != "promotion"
									sub_qty += sale.total_item
								end %>
								 <% if sale.status_type =="Discount"
									sub_qty += sale.total_item*(-1)
								end %>

								 <% if sale.status_type == "promotion" && @type == "promotion"
									sub_qty += sale.total_item*(-1)
								end %>
								<% if count == value %>
									<tr>
										<td colspan="3">&nbsp;</td>
										<td><b>Total <%= sale.account_name %> Qty </b> </td>
										<td><b><%= sub_qty %></b></td>
										<td><%= t("views.right_panel.detail.sub_total") %></td>
										<td ><span class="underline"><%=  number_format(sub_total ,  precision:precision.to_i,delimiter:delimiter)%> </span></td>
									</tr>
									<% sub_total = 0.0%>
									<% sub_qty = 0 %>
									<% count = 0%>
								<% end %>
								<% end %>
						<% end %>
						<!-- end sub total -->
					<% end %>
					<!--Product Sale -->
						<% if @product.present?%>

							 <tr>
								<td><b>Product</b></td>
								<td colspan="4">&nbsp;</td>
								<td></td>
							</tr>
							<% @product.each do |product| %>
								<% if product.total_item > 0
									total_qty += product.total_item
								end %>
								<% grand_total +=product.grand_total
								p_qty += product.total_item%>
								<tr>
								<td>&nbsp;</td>
									<td>Product</td>
									<td><%= product.product_code rescue '-' %></td>
									<td><%= product.product_name rescue '-' %></td>
									<td><%= product.total_item rescue '-' %></td>
									<td> <%= number_format(product.unit_price ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
									<td> <%= number_format(product.grand_total ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
								</tr>
								<!-- sub total -->

										<% product_sub_total += product.grand_total %>
									<!-- end sub total -->
							<% end %>
							<tr>
									<td colspan="3">&nbsp;</td>
									<td><b>Total Product Qty </b> </td>
									<td><b><%= p_qty %></b></td>

								<td><%= t("views.right_panel.detail.sub_total") %></td>
								<td ><span><%=  number_format(product_sub_total ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
							</tr>
							<%end%>
					<!-- End Product Sale -->

						<!--Other Charges -->
						<% if @type == "other" || @other_charges.present?%>

							 <tr>
								<td><b>Other Charges</b></td>
								<td colspan="4">&nbsp;</td>
								<td></td>
							</tr>
							<% @other_charges.each do |other| %>
								<% if other.total_item > 0
									total_qty += other.total_item
								end %>
								<% grand_total +=other.grand_total%>
								<tr>
								<td>&nbsp;</td>
									<td>Other Charges</td>
									<td><%= other.item_code rescue '-' %></td>
									<td><%= other.product_name rescue '-' %></td>
									<td><%= other.total_item rescue '-' %></td>
									<td> <%= number_format(other.unit_price ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
									<td> <%= number_format(other.grand_total ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
								</tr>
								<!-- sub total -->

										<% other_sub_total += other.grand_total %>
									<!-- end sub total -->
							<% end %>
							<tr>
								<td colspan="5">&nbsp;</td>
								<td><%= t("views.right_panel.detail.sub_total") %></td>
								<td ><span><%=  number_format(other_sub_total ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
							</tr>
							<%end%>
						<!-- End Other Charges -->
						<tr style="border-top:2px solid grey;">
							<td colspan="3">&nbsp;</td>
							<td><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %></td>
							<td><span><%= total_qty%></span></td>
							<td><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %></td>
							<td><span><%= number_format(grand_total ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
						</tr>
						<% if @type =="" || @type =="all" || @type.nil? %>
							<tr class="foc_payment">
								<td colspan="5">&nbsp;</td>
								<td>Total FOC Amount</td>
								<td><span><%= number_format(@foc_data ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
							</tr>
							<tr class="foc_payment" style="border-top:2px solid grey;border-bottom:2px solid grey;">
								<td colspan="5">&nbsp;</td>
								<td style="border-bottom:2px solid grey;"><%= t("views.right_panel.detail.net_amount") %></td>
								<td style="border-bottom:2px solid grey;"><span><%= number_format(grand_total -@foc_data ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
							</tr>
						<% end %>
						<% end %>
						<% if @type == "other"%>

							 <tr>
								<td><b>Other Charges</b></td>
								<td colspan="4">&nbsp;</td>
								<td></td>
							</tr>
							<% @other_charges.each do |other| %>
								<% if other.total_item > 0
									total_qty += other.total_item
								end %>
								<% grand_total +=other.grand_total%>
								<tr>
										<td>&nbsp;</td>
									<td>Other Charges</td>
									<td><%= other.item_code rescue '-' %></td>
									<td><%= other.product_name rescue '-' %></td>
									<td><%= other.total_item rescue '-' %></td>
									<td> <%= number_format(other.unit_price ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
									<td> <%= number_format(other.grand_total ,  precision:precision.to_i,delimiter:delimiter) rescue '-'%></td>
								</tr>
								<!-- sub total -->

										<% other_sub_total += other.grand_total %>
									<!-- end sub total -->
							<% end %>
							<tr>
								<td colspan="5">&nbsp;</td>
								<td><%= t("views.right_panel.detail.sub_total") %></td>
								<td ><span><%=  number_format(other_sub_total ,  precision:precision.to_i,delimiter:delimiter)%></span></td>
							</tr>
							<%end%>
					</tbody>
				</table>
				<% end %>
				</div>
			</div>
		</div>
	</div>
</div>

<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 = "";

			show_shift_name(period,period_type,from,to,'shift_item');
		}

		// OK button is clicked
		$('#from').bootstrapMaterialDatePicker().on('beforeChange', function(e, date){
			new_date = new Date(date) ;
			month    = parseInt(new_date.getMonth()+1)
			from = new_date.getDate() + "-" + month + "-" + new_date.getFullYear();
			$('#from').val(from)
			search_by_date();
		});
		$('#to').bootstrapMaterialDatePicker().on('beforeChange', function(e, date){
			new_date = new Date(date) ;
			month    = parseInt(new_date.getMonth()+1)
			to = new_date.getDate() + "-" + month + "-" + new_date.getFullYear();
			$('#to').val(to)
			search_by_date();
		});

		function search_by_date(){
			from = $("#from").val();
	  		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){
					show_shift_name(period,period_type,from,to,'shift_item');
				}
				if(check_arr.length == 3){
					check_arr = [];
				}
			}

		}

		function show_shift_name(period,period_type,from,to,shift_item){
			var shift = $('#shift_name');
			if (from == '' && to == '') {
				from = $("#from").val();
	  		    to   = $("#to").val();
			}
			shift.empty();
			var selected = '';
			var str = '';
			var param_shift = '<%= params[:shift_name]%>';

			url = '<%= reports_get_shift_by_sale_item_path %>';
			console.log(url)
			$.get(url, {period :period, period_type :period_type, from :from, to :to, report_type :shift_item} , function(data){
				console.log(data)

				str = '<option value="0">--- All Shift ---</option>';
				$(data.message).each(function(index){

					var local_date = data.message[index].local_opening_date + ' - ' + data.message[index].local_closing_date;
					var sh_date = data.message[index].opening_date + ' - ' + data.message[index].closing_date;
					var shift_id = data.message[index].shift_id ;

					if(param_shift != ''){
						if(shift_id == param_shift){
							selected = 'selected = "selected"';
						}
						else{
							selected = '';
						}
					}
					else{
						selected = '';
					}


					str += '<option value="'+ shift_id +'" '+ selected +'>' + local_date + '</option>';

			   // console.log(sh_date)
			})
				shift.append(str);
			});
		}

	});

	function print_sale_items(){
		var period = $('#sel_period').val();
		var type = $('#sel_sale_type').val();
		var account = $('#account_type').val();
		var from = $('#from').val();
		var to = $('#to').val();
		var shift = $('#shift_name').val();
		// var flag = false;
		// alert($('#row_count').val())

		// if (flag == true) {
		  $.ajax({
			type: "POST",
			url: "<%= reports_print_sale_items_path %>",
			data: "period="+period+"&period_type="+type+"&account_type="+account+"&from="+from+"&to="+to+"&shift_name="+shift,
			success: function(data){
				if (data != null){
            		location.reload();
        		}
        		$("#loader").hide();
			}
		  });
		// }
	}
</script>
