<% if !@invoiceItem.blank? %>
	<% projectID =  @invoiceItem.collect{|i| i.project_id}.uniq %>
	<% invoiceComp = getInvoiceComponents(@invoice.parent_id, @invoice.parent_type, projectID, controller.getOrderComponetsId ) %>
	<div id="time-rpt-dlg" style="overflow-x:auto;">
		<center><b><%= (controller.getHeaderLabel).upcase %></b></center>
		<div style="float:right">
			<table>
				<tr>
					<td>
						<% attachment = WkLocation.getMainLogo() %>
						<% if attachment.present? %>
							<%= image_tag(download_location_attachment_path(attachment, attachment.filename), class: 'filecontent image') %>
						<% end %>
					</td>
				</tr>
			</table>
		</div>
		<div style="clear:both;"></div>
		<table border="1" style="border-collapse:collapse;" cellspacing="0", align="center", frame="box">
				<tr style="width:100%; background-color: #f2f2f2">
					<th align="left"  width="25%"><%= l(:label_name_address_of,l(:label_supplier)) %></th>
					<th align="left"  width="25%"><%= l(:label_name_address_of,l(:label_customer)) %></th>
					<th align="left"  width="25%"><%= controller.getLabelInvNum %></th>
					<th align="left"  colspan ="3" width="25%"><%= controller.getDateLbl %></th>
				</tr>
				<tr style="width:100%;">
					<td  width="25%" style="white-space:pre"><%=h controller.getSupplierAddress(@invoice)  %></td>
					<td  width="25%" style="white-space:pre"><%=h controller.getCustomerAddress(@invoice) %> </td>
					<td  width="25%"><%=h @invoice.invoice_number %></td>
					<td  colspan ="3" width="25%"><%=h @invoice.invoice_date.to_formatted_s(:long) %></td>
				</tr>
				<% if controller.showContractSection %>
					<tr style="width:100%;">
						<th align="left" colspan="3" width="70%"><%= l(:label_cntrt_purchase_work_order) %></th>
						<th align="left" colspan="3" width="30%"><%= l(:label_period) %></th>
					</tr>
					<tr style="width:100%;">
						<td align="left" colspan="3" width="70%"><%=h controller.getOrderContract(@invoice)%></td>
						<td align="left" colspan="3" width="30%"><%=h @invoice.start_date.to_formatted_s(:long) + ' to ' + @invoice.end_date.to_formatted_s(:long) %></td>
					</tr>
				<% end %>

				<% if controller.addDescription %>
					<tr style="width:50%; background-color: #f2f2f2">
						<th align="left" colspan="6" width="30%"><%= l(:field_description) %></th>
					</tr>
					<tr style="width:50%;">
						<td align="left" colspan="6" width="30%"><%= textilizable @invoice.description || '' %></td>
					</tr>
				<% end %>

				<tr style="width:100%;">
					<td colspan="6">
						<table height="100%" style="width:100%; border-collapse: collapse;">
							<tr style="border-bottom:1pt solid black; width:100%; background-color: #f2f2f2">
								<th align="left" style="width:42%; border-bottom:1pt solid black;" ><%= l(:label_invoice_name) %></th>
								<th align="left" style="width:16%; border-bottom:1pt solid black;" ><%= l(:label_billing_type) %></th>
								<th align="right" style="width:16%; border-bottom:1pt solid black;" ><%= l(:label_rate) %></th>
								<th align="right" style="width:16%; border-bottom:1pt solid black;" ><%= l(:field_quantity) %></th>
								<th align="center" style="width:16%; border-bottom:1pt solid black;" ><%= l(:field_currency) %></th>
								<th align="right" style="width:16%;  border-bottom:1pt solid black;" ><%= l(:field_amount) %></th>
							</tr>
							<%
								projIDs = @invoiceItem.where(product_id: nil).where.not(:item_type => 'r').pluck(:project_id).uniq()
								prodIDs = @invoiceItem.where.not(product_id: nil).where.not(:item_type => 'r').pluck(:product_id).uniq()
							%>
							<% projIDs.each do |id| %>
								<% invoiceItems = @invoiceItem.where(product_id: nil, project_id: id).where.not(:item_type => 'r').order(:item_type) %>
								<%= render partial: "report_inv_items", locals:{invoiceItems: invoiceItems} %>
							<% end %>
							<% prodIDs.each do |id| %>
								<% invoiceItems = @invoiceItem.where(product_id: id).where.not(:item_type => 'r').order(:item_type) %>
								<%= render partial: "report_inv_items", locals:{invoiceItems: invoiceItems} %>
							<% end %>

							<% @invoiceItem.where(:item_type => 'r').each do | entry | %>
									<tr  style="width:100%;">
										<td style="width:42%;" ><%=h entry.name %></td>
										<td style="width:16%;" ><%=h (entry.item_type == 'i' && @invoice.invoice_type == 'I') ? l(:field_hours) : '' %></td>
										<td style="width:16%;" align="right"><%=h "%.2f" % entry.rate unless entry.rate.blank? %></td>
										<td style="width:16%;" align="right"><%=h "%.2f" % entry.quantity unless entry.quantity.blank? %></td>
										<td style="width:16%;" align="center"><%=h entry.original_currency %></td>
										<td style="width:16%;" align="right"><%=h "%.2f" % entry.original_amount unless entry.original_amount.blank?%></td>
									</tr>
							<% end %>
							</table>
					</td>
				</tr>
				<tr style="width:100%; border-top: hidden;">
					<td colspan="6">
						<table style="width:100%;">
							<tr style="width:100%;">
								<td style="width:42%;"></td>
								<td style="width:16%;"></td>
								<td style="width:16%;" align="right" ><b><%=h  l(:label_grand_total)   %></b></td>
								<td style="width:16%;" align="right"><b><%=h "%.2f" % @invoiceItem.sum(:quantity) %></b></td>
								<td style="width:16%;" align="center" ><b><%=h @invoiceItem[0].original_currency %></b></td>
								<td style="width:16%;" align="right" ><b><%=h "%.2f" % @invoiceItem.sum(:original_amount) %></b></td>
							</tr>
						</table>
					</td>
				</tr>
				<% if (Setting.plugin_redmine_wktime['wktime_hide_amount_in_words'].to_i != 1) %>
					<tr style="width:100%;">
						<td align="left" colspan="6" width="30%"><b><%= l(:label_amount_in_words) + " : "%></b><%= numberInWords(@invoiceItem.sum(:original_amount)) + " " + l(:label_only) + "." %></td>
					</tr>
				<% end %>
								<% if invoiceComp.present? %>
					<% if controller.addDescription %>
						<tr style="width:50%; background-color: #f2f2f2">
							<th align="left" colspan="6" width="30%"><%= l(:label_terms_cond) %></th>
						</tr>
					<% end %>
					<% invoiceComp.each do |comp| %>
							<tr style="width:100%;">
								<td align="left" colspan="2" width="250px"><b><%= comp[:name] %></b></td>
								<td align="left" colspan="4" width="500px"><%= comp[:value] %></td>
							</tr>
					<% end %>
				<% end %>
		</table>
	</div>
	<br/>
	<br/>
	<br/>
	<table width="100%">
		<tr width="100%">
			<td class="txt-align" style="float:left;"><b><%= l(:label_place) %> :</b> </td>
			<td class="txt-align"  style="float:right;"><b><%= l(:label_authorized_signatory) %></b></td>
		</tr>
		<tr><td class="txt-align" style="float:left;" ><b><%=  l(:label_date) %> :</b> </td></tr>
	</table>
<% else %>
	<label><b><%= l(:label_no_data) %></b></label>
<% end -%>