<br/>
<%= javascript_include_tag 'select2.min', :plugin => "redmine_wktime" %>
<%= stylesheet_link_tag 'select2.min', :plugin => "redmine_wktime" %>
<script type="text/javascript">
	prevTab = 'tab-<%= "wkreport" %>';
	wkInvReportUrl = '<%=url_for(:controller => controller_name, :action => "edit", :tab => controller_name)%>';
	grpUrl="<%= "#{url_for(:controller => "wkreport", :action => 'getMembersbyGroup')}" %>";
	var label_tax = "<%= l(:label_tax) %>";
	var label_prod_item = "<%= l(:label_select_product_item) %>";
	var delete_all_row = "<%= l(:warn_delete_all_rows) %>";
	var controller_name = "<%= controller_name %>";
</script>
<%
	itemHash = { 'i' => l(:label_item), 'c' =>  l(:label_credit) }

	if controller.addExpenseType
		asset_hash = {'e' => l(:label_wkexpense)}
		itemHash.merge!(asset_hash)
	end

 	if  controller.addAdditionalTax
		tax_hash = {'t' => l(:label_tax)}
		itemHash.merge!(tax_hash)
   end

	 if  controller.addMaterialType
		material_hash = {'m' => l(:label_material)}
		itemHash.merge!(material_hash)
   end

	if  controller.addAssetType
		asset_hash = {'a' => l(:label_rental)}
		itemHash.merge!(asset_hash)
   end

	row_index = 1
	org_total = 0
	total = 0
	toCurrency = Setting.plugin_redmine_wktime['wktime_currency']
	isDisable = false
	isDisable = !isEditableInvoice(@invoice.id) unless @invoice.id.blank?
	@productItemsDD.prepend([l(:label_select_product_item), ""])
	@issuesDD.values.each{|value| value.prepend([l(:label_select_product_item), ""])} if @issuesDD.present?
	if @invoiceItem.blank? && controller.isInvoiceController
		invoiceItemDD = @issuesDD&.values&.first || []
		model_name = "Issue"
	else
		invoiceItemDD = @productItemsDD
		model_name = "WkProductItem"
	end
%>
<div class="wk-contextual">
	<h2 style="float:left;"><%= controller.getItemLabel %></h2>
	<% if !@preBilling && !isDisable %>
	<%= link_to l(:button_add_row), "#", { :onclick => "javascript:invoiceAddRow('invoiceTable', 'totalrow');",  :class => 'icon icon-add' } %>
	<% end %>
</div>

<div style="clear:both"></div>
<div class="autoscroll">
	<table id="invoiceTable" class="list time-entries" style="margin-bottom: 4px;">
		<thead>
			<tr>
				<th class="lbl-txt-align: center" style="width: 5%">#</th>
				<% if !@projectsDD.blank? %>
					<th class="lbl-txt-align: center" style="width: 10%"><%= l(:field_project) %></th>
				<% else %>
					<th style="width: 0%"></th>
				<% end %>
				<% if @productItemsDD.present? %>
					<th class="lbl-txt-align: center" style="width: 20%"><%= l(:label_items) %></th>
				<% else %>
					<th style="width: 0%"></th>
				<% end %>
				<th class="lbl-txt-align: center" style="width: 20%"><%= l(:label_invoice_name) %></th>
				<th class="lbl-txt-align: center" style="width: 5%"><%= l(:label_item_type) %></th>
				<th class="lbl-txt-align: center" style="width: 11%"><%= l(:label_rate) %></th>
				<th class="lbl-txt-align: center" style="width: 11%"><%= l(:field_quantity) %></th>
				<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
					<th class="lbl-txt-align: center" style="width: 8%"><%= l(:label_received_quantity) %></th>
				<% end %>
				<th class="lbl-txt-align: center" style="width: 8%"><%= l(:field_original_amount) %></th>
				<th class="lbl-txt-align: center" style="width: 8%"><%= l(:field_amount) %></th>
				<th style="width: 5%"></th>
			</tr>
		</thead>
		<tbody>
			<% if @invoiceItem.present? || @unbilled && @invItems.present? || params[:loadUnBilled] %>
				<% if @invoiceItem.present? || params[:loadUnBilled] %>
					<%
						sqlString = "wk_invoice_items.item_type = 'i' or wk_invoice_items.item_type = 'c' or wk_invoice_items.item_type = 'm' or wk_invoice_items.item_type = 'a' or wk_invoice_items.item_type = 'e' "
						sqlString = sqlString + " or wk_invoice_items.item_type = 't'" if controller.addAdditionalTax
					%>
					<% @invoiceItem.where(sqlString).each do |entry| %>
						<% accProject = WkAccountProject.getAccByProjID(entry.project_id)
							exchangeRate = getExchangeRate(entry.original_currency, entry.currency)
							exchangeRate = exchangeRate.blank? ? 1 : exchangeRate
							if ["i", "e"].include?(entry.item_type) && controller.isInvoiceController
								invoiceItemDD = @issuesDD[entry.project_id] || []
								selected_itemDD_val = entry.try(:invoice_item_id)
								model_name = "Issue"
								issueItem = invoiceItemDD.detect{|e| e[1] == selected_itemDD_val}
								invoiceItemDD << [entry&.invoice_item&.subject || '', selected_itemDD_val] if !issueItem.present?
							elsif ["m", "a"].include?(entry.item_type) && controller.isInvoiceController
								invoiceItemDD = getItemDD(entry.item_type)
								invoiceItemDD.prepend([l(:label_select_product_item), ""])
								selected_itemDD_val = entry.try(:product_id).to_s+", "+entry.try(:invoice_item_id).to_s
								model_name = "WkInventoryItem"
							else
								invoiceItemDD = @productItemsDD
								selected_itemDD_val = entry.try(:product_id).to_s+", "+entry.try(:invoice_item_id).to_s
								model_name = "WkProductItem"
							end
						%>
						<tr onclick="row_id = this.rowIndex">
							<td class="lbl-txt-align: center" headers="item_index">
								<label id="item_index_<%= row_index.to_s %>"><%= row_index  %></label>
								<%=h hidden_field_tag("item_id_#{row_index}",   entry.id ) %>
								<%=h hidden_field_tag("exchangerate_amount_#{row_index}", exchangeRate ) %>
								<%=h hidden_field_tag("original_currency_#{row_index}", entry.original_currency ) %>
								<%= hidden_field_tag("currency_#{row_index}", entry.currency ) %>
								<%= hidden_field_tag("used_serial_no_#{row_index}") %>
								<%= hidden_field_tag("used_serialNo_obj_#{row_index}") %>
								<%= hidden_field_tag("product_serial_no_#{row_index}") %>
							</td>
							<% if !@projectsDD.blank? %>
								<td class="lbl-txt-align: center" headers="project_id">
									<%=h select_tag("project_id_#{row_index.to_s}", options_for_select(@projectsDD, :selected => entry.project_id), :disabled => isDisable) %>
									<%=h hidden_field_tag("product_id_#{row_index.to_s}",  entry.product_id )  %>
									<%=h hidden_field_tag("material_id_#{row_index.to_s}",  "" )  %>
								</td>
							<% else %>
								<td style="width: 0%"></td>
							<% end %>
							<% if @productItemsDD.present? %>
								<td class="lbl-txt-align: center" headers="product_item">
									<%= hidden_field_tag("invoice_item_type_#{row_index.to_s}", entry.try(:invoice_item_type) || model_name)  %>
									<%= select_tag("invoice_item_id_#{row_index.to_s}",
										options_for_select(invoiceItemDD, selected: selected_itemDD_val ),
										disabled: isDisable, class: "productItemsDD", style: "width:80%;")
									%>
									<span id="serial_number_img_<%= row_index.to_s %>" style="padding-left: 5px, padding-bottom: 0px" onclick="getUsedSerialNumber(this); return false;"><%= link_to image_tag('serialnumber.png', id: "serial_number_img_#{row_index.to_s}", plugin: "redmine_wktime") %></span>
								</td>
							<% else %>
								<td style="width: 0%"></td>
							<% end %>
							<td class="lbl-txt-align: center" headers="name">
								<%= text_field_tag "name_#{row_index}",  entry.name, :required => true, :style => "width:100%;", :disabled => isDisable %>
							</td>
							<td class="lbl-txt-align: center" headers="item_type">
								<%=h select_tag("item_type_#{row_index.to_s}", options_for_select(itemHash.invert, :selected => entry.item_type), :disabled => true, class: "item_types") %>
								<%=h hidden_field_tag("hd_item_type_#{row_index.to_s}", entry.item_type )  %>
							</td>
							<td class="lbl-txt-align: center" headers="rate">
								<%= entry.original_currency %> <%= text_field_tag "rate_#{row_index}", ("%.2f" % entry.rate) ,:maxlength => 14, :style => "width:85%;", required: true, :onchange => "addAmount(this.id)", :disabled => isDisable, class: "rates" %>
							</td>
							<td class="lbl-txt-align" style="display: flex;justify-content: center;align-items: center;" headers="quantity">
								<%= text_field_tag "quantity_#{row_index}", ("%.2f" % entry.quantity unless entry.quantity.blank?), :maxlength => 14, required: true, :style => "width:83%;", :onchange => "addAmount(this.id)", :disabled => isDisable %>
								<% if controller.showQuantityPopup && ("i" == entry.item_type && accProject&.first&.billing_type == 'TM' || entry.item_type == "e" && entry.spent_fors.any?) %>
									&nbsp;<%= link_to image_tag('withoutcommant.png', plugin: "redmine_wktime"), "javascript:showQuantityDetails(#{entry.id},'#{entry.item_type}');" %>
								<% end %>
							</td>
							<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
								<td style="text-align:right;" headers="received_quantity">
									<label id="received_quantity_<%= row_index.to_s %>"><%= entry.inventory_items.sum(:total_quantity) %></label>
								</td>
							<% end %>
							<td style="text-align:right;" headers="original_amount">
								<%= entry.original_currency %> <label id="original_amount_<%= row_index.to_s %>"><%= html_hours("%.2f" % entry.original_amount)  unless entry.original_amount.blank? %></label>
							</td>
							<td style="text-align:right;" headers="amount">
								<%= entry.currency %> <label id="amount_<%= row_index.to_s %>"><%= html_hours("%.2f" % entry.amount)  unless entry.amount.blank? %></label>
							</td>
							<% org_total = org_total + entry.original_amount unless entry.original_amount.blank? %>
							<% total = total + entry.amount %>
							<td>
								<% unless isDisable %>
									<%= link_to image_tag('delete.png'), "javascript:deleteRow('invoiceTable', 'totalrow');", :id => row_index,
										:data => {:confirm => l(:text_are_you_sure)},
										:title => l(:button_delete) %>
								<% end %>
							</td>
						</tr>
						<% row_index = row_index + 1 %>
					<% end %>
				<% end %>
				<% if @unbilled && @invItems.present? || params[:loadUnBilled] %>
					<%=h hidden_field_tag("populate_unbilled",   @unbilled )  %>
					<% @invItems.each do |key, value| %>
						<%
							value['item_amount'] ||= 0
							exchangeRate = getExchangeRate(@currency, toCurrency)
							exchangeRate = exchangeRate.blank? ? 1 : exchangeRate
							amount = (value['item_amount'] * exchangeRate)
							if ["i", "e"].include?(value['item_type']) && controller.isInvoiceController
							invoiceItemDD = @issuesDD[value['project_id']] || []
							selected_itemDD_val = value['issue_id'].to_s
							model_name = "Issue"
						elsif ["m", "a"].include?(value['item_type']) && controller.isInvoiceController
							invoiceItemDD = getItemDD(value['item_type'])
							invoiceItemDD.prepend([l(:label_select_product_item), ""])
							selected_itemDD_val = value['product_id'].to_s+", "+value['invoice_item_id'].to_s
							model_name = "WkInventoryItem"
						else
							invoiceItemDD = @productItemsDD
							model_name = "WkProductItem"
						end
						%>
						<tr onclick="  row_id  = this.rowIndex ">
							<td class="lbl-txt-align" headers="item_index" style="width: 5%">
								<label id="item_index_<%= row_index.to_s %>"><%= row_index  %></label>
								<%=h hidden_field_tag("item_id_#{row_index}", "" )  %>
								<%=h hidden_field_tag("entry_id_#{row_index}", value['milestone_id'] )  %>
								<%=h hidden_field_tag("creditfrominvoice_#{row_index}", value['creditfromInvoice'] ) %>
								<%=h hidden_field_tag("exchangerate_amount_#{row_index}", exchangeRate )  %>
								<%=h hidden_field_tag("original_currency_#{row_index}", @currency ) %>
								<%= hidden_field_tag("currency_#{row_index}", toCurrency ) %>
								<%= hidden_field_tag("used_serial_no_#{row_index}") %>
								<%= hidden_field_tag("used_serialNo_obj_#{row_index}") %>
								<%= hidden_field_tag("product_serial_no_#{row_index}") %>
							</td>
							<td class="lbl-txt-align: center" headers="project_id" style="width: 10%">
								<%=h select_tag("project_id_#{row_index.to_s}", options_for_select(@projectsDD, :selected=> value['project_id'] ), :disabled => @preBilling ) %>
								<%=h hidden_field_tag("product_id_#{row_index.to_s}",  value['product_id'] )  %>
								<%=h hidden_field_tag("material_id_#{row_index.to_s}",  value['material_id'].blank? ? "" : value['material_id'] )  %>
								<%= hidden_field_tag("expense_id_#{row_index.to_s}", value['expense_id'] ) %>
							</td>
							<% if @productItemsDD.present? %>
								<td class="lbl-txt-align: center" headers="product_item">
									<%= hidden_field_tag("invoice_item_type_#{row_index.to_s}", value['invoice_item_type'] || model_name)  %>
									<%= select_tag("invoice_item_id_#{row_index.to_s}",
										options_for_select(invoiceItemDD, selected: selected_itemDD_val),
										disabled: @preBilling, class: "productItemsDD", style: "width:80%;")
									%>
								</td>
							<% else %>
								<td style="width: 0%"></td>
							<% end %>
							<td class="lbl-txt-align: center" headers="name" style="width: 20%">
								<%= text_field_tag "name_#{row_index}", value['item_desc'], :required => true, :style => "width:100%;", :disabled => @preBilling %>
							</td>
							<td class="lbl-txt-align: center" headers="item_type" style="width: 5%">
								<%=h select_tag("item_type_#{row_index.to_s}", options_for_select(itemHash.invert, :selected => value['item_type'].blank? ? itemHash.invert  : value['item_type'] ), :disabled => @preBilling, class: "item_types") %>
								<%=h hidden_field_tag("hd_item_type_#{row_index.to_s}", value['item_type'] )  %>
							</td>
							<td class="lbl-txt-align: center" headers="rate" style="width: 11%">
								<%=  @currency %> <%= text_field_tag "rate_#{row_index}", value['rate'] ,:maxlength => 14, required: true, :style => "width:85%;", :onchange => "addAmount(this.id)", :disabled => @preBilling %>
							</td>
							<td class="lbl-txt-align" headers="quantity" style="width: 11%">
								<%= text_field_tag "quantity_#{row_index}", value['item_quantity'], :maxlength => 14, required: true, :style => "width:83%;", :onchange => "addAmount(this.id)", :disabled => @preBilling %>
								<% if controller.showQuantityPopup && (['i'].include?(value['item_type']) && value['billing_type'] == 'TM' || value['item_type'] == "e") %>
									<%= link_to image_tag('withoutcommant.png', plugin: "redmine_wktime"), "javascript: showUnbillQtyDetails(#{value['project_id']}, #{value['issue_id'].to_i}, '#{@invoice.start_date}', '#{@invoice.end_date}', #{@invoice.parent_id}, '#{@invoice.parent_type}', '#{value['item_type']}');" %>
								<% end %>
							</td>
							<td style="text-align:right;" headers="original_amount" style="width: 8%">
								<%= @currency %> <label id="original_amount_<%= row_index.to_s %>"><%= html_hours("%.2f"% value['item_amount']) %></label>
							</td>
							<td style="text-align:right;" headers="amount" style="width: 8%">
								<%= toCurrency %> <label id="amount_<%= row_index.to_s %>"><%= html_hours("%.2f"% amount) %></label>
							</td>
							<% org_total = org_total + value['item_amount'] %>
							<% total = total + amount %>
							<td style="width: 5%">
								<% if !@preBilling && !isDisable %>
								<%= link_to image_tag('delete.png'), "javascript:deleteRow('invoiceTable', 'totalrow');", :id => row_index,
										:data => {:confirm => l(:text_are_you_sure)},
										:title => l(:button_delete), :disabled => @preBilling %>
								<% end %>
							</td>
						</tr>
						<% row_index = row_index + 1 %>
					<% end %>
				<% end %>
			<% else %>
				<tr onclick="  row_id  = this.rowIndex ">
					<%
						exchangeRate = getExchangeRate(@currency, toCurrency)
						exchangeRate = exchangeRate.blank? ? 1 : exchangeRate
						exchange_amount = exchangeRate
					%>
					<td class="lbl-txt-align: center" headers="item_index">
						<label id="item_index_<%= row_index.to_s %>"><%= row_index  %></label>
						<%=h hidden_field_tag("exchangerate_amount_#{row_index}", exchange_amount ) %>
						<%=h hidden_field_tag("item_id_#{row_index}",   "" ) %>
						<%=h hidden_field_tag("original_currency_#{row_index}", @currency ) %>
						<%= hidden_field_tag("currency_#{row_index}", toCurrency ) %>
						<%= hidden_field_tag("used_serial_no_#{row_index}") %>
						<%= hidden_field_tag("used_serialNo_obj_#{row_index}") %>
						<%= hidden_field_tag("product_serial_no_#{row_index}") %>
					</td>
					<% if @projectsDD.present? %>
						<td class="lbl-txt-align: center" headers="project_id">
							<%=h select_tag("project_id_#{row_index.to_s}", options_for_select(@projectsDD)) %>
						</td>
					<% else %>
						<td style="width: 0%"></td>
					<% end %>
					<% if @productItemsDD.present? %>
						<td class="lbl-txt-align: center" headers="product_item">
							<%= hidden_field_tag("invoice_item_type_#{row_index.to_s}", model_name)  %>
							<%= select_tag("invoice_item_id_#{row_index.to_s}", options_for_select(invoiceItemDD), class: "productItemsDD") %>
							<span id="serial_number_img_<%= row_index.to_s %>" style="padding-left: 5px, padding-bottom: 0px" onclick="getUsedSerialNumber(this); return false;"><%= link_to image_tag('serialnumber.png', id: "serial_number_img_#{row_index.to_s}", plugin: "redmine_wktime") %></span>
						</td>
					<% end %>
					<td class="lbl-txt-align: center" headers="name">
						<%= text_field_tag "name_#{row_index}",  "", :required => true, :style => "width:100%;" %>
					</td>
					<td class="lbl-txt-align: center" headers="item_type">
						<%=h select_tag("item_type_#{row_index.to_s}", options_for_select(itemHash.invert), class: "item_types") %>
					</td>
					<td class="lbl-txt-align: center" headers="rate">
						<%=  @currency %> <%= text_field_tag "rate_#{row_index}",   "" ,:maxlength => 14, required: true, :style => "width:85%;", :onchange => "addAmount(this.id)" %>
					</td>
					<td class="lbl-txt-align" headers="quantity">
						<%= text_field_tag "quantity_#{row_index}", "", :maxlength => 14, required: true, :style => "width:83%;", :onchange => "addAmount(this.id)" %>
					</td>
					<td style="text-align:right;" headers="original_amount">
						<%= @currency %> <label id="original_amount_<%= row_index.to_s %>">0.00</label>
					</td>
					<td style="text-align:right;" headers="amount">
						<%= toCurrency %> <label id="amount_<%= row_index.to_s %>">0.00</label>
					</td>
					<% org_total = 0 %>
					<% total = 0 %>
					<td>
						<%= link_to image_tag('delete.png'), "javascript:deleteRow('invoiceTable', 'totalrow');", :id => row_index,
								:data => {:confirm => l(:text_are_you_sure)},
								:title => l(:button_delete) %>
					</td>
				</tr>
				<% row_index = row_index + 1 %>
			<% end %>
		</tbody>

		<tr>
			<td style="width: 5%"></td>
			<td style="width: <%= @projectsDD.present? ? "10%" : "0%" %>"></td>
			<td style="width: <%= @productItemsDD.present? ? "20%" : "0%" %>"></td>
			<td style="width: 20%"></td>
			<td style="width: 5%"></td>
			<td style="width: 11%"></td>
			<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
				<td style="width: 11%"></td>
			<% end %>
			<td style="text-align:right; width: 11%">
				<b><label><%= l(:label_sub_total) %> :</label></b>
			</td>
			<td style="text-align:right; width: 8%">
				<b><%= @invoiceItem.blank? ? @currency : (@invoiceItem[0].blank? ? @currency : @invoiceItem[0].original_currency) %> <label id="subtotal_org_amount">
					<%= html_hours("%.2f" % org_total) %>
				</label></b>
			</td>
			<td style="text-align:right; width: 8%"><b><%= toCurrency %> <label id="subtotal_amount"><%= html_hours("%.2f" % total) %></label></b></td>
			<td style="width: 5%"></td>
		</tr>
	</table>

	<% tax_index = 1 %>
	<% if !controller.addAdditionalTax %>
		<table id="taxTable" class="list time-entries">
			<% if @invoiceItem.present? %>
				<% @invoiceItem.where(:item_type => 't').each do |entry| %>
					<%
						tax_amount = getExchangedAmount( entry.original_currency, entry.original_amount)
						prodItemName = entry&.product&.name
					%>
					<tr>
						<td style="width: 5%">
							<%= hidden_field_tag("tax_project_id_#{tax_index}", entry.project_id) %>
							<%= hidden_field_tag("tax_invoice_item_id_#{tax_index}", entry.product_id) %>
						</td>
						<td class="lbl-txt-align: center" style="width: <%= entry&.project&.name.present? ? '10%' : '0%' %>"><%= entry&.project&.name if prodItemName.blank? %></td>
						<td style="width: <%= prodItemName.present? ? '20%' : '0%' %>"><%= prodItemName %></td>
						<td class="lbl-txt-align: center" style="width:20%;"><%=  entry.name %></td>
						<td class="lbl-txt-align: center" style="width: 5%"><label id="taxitem_type_<%= tax_index.to_s %>"><%= l(:label_tax) %></label></td>
						<td class="lbl-txt-align: center" style="width: 11%">
							<label id="taxrate_<%= tax_index.to_s %>"><%= html_hours("%.2f" % entry.rate) %> % </label>
							<%= hidden_field_tag("tax_rate_#{tax_index}", entry.rate ) %>
						</td>
						<td style="width: 11%"></td>
						<td  style="text-align:right; width: 8%" ><%= entry.original_currency %>
							<label id="org_taxamount_<%= tax_index.to_s %>"> <%= entry.original_amount.blank? ? "0.00" : html_hours("%.2f" % entry.original_amount) %></label>
						</td>
						<td  style="text-align:right; width: 8%" >
							<%= toCurrency %> <label id="taxamount_<%= tax_index.to_s %>"><%= entry.amount.blank? ? "0.00" : html_hours("%.2f" % entry.amount) %></label>
						</td>
						<td style="width: 5%"></td>
						<% org_total = org_total + entry.original_amount unless entry.original_amount.blank? %>
						<% total = total + entry.amount %>
					</tr>
					<% tax_index = tax_index + 1 %>
				<% end %>
			<% end %>

			<tr>
				<td style="width: 5%"></td>
				<td style="width: <%= @projectsDD.present? ? "10%" : "0%" %>"></td>
				<td style="width: <%= @productItemsDD.present? ? "20%" : "0%" %>"></td>
				<td style="width: 20%"></td>
				<td style="width: 5%"></td>
				<td style="width: 11%"></td>
				<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
					<td style="width: 11%"></td>
				<% end %>
				<td style="text-align:right; width: 11%">
					<b><label><%= l(:label_total) %> :</label></b>
				</td>
				<td style="text-align:right; width: 8%">
					<b><%= @invoiceItem.blank? ? @currency : (@invoiceItem[0].blank? ? @currency : @invoiceItem[0].original_currency) %> <label id="org_total_tax">
						<%= html_hours("%.2f" % org_total) %>
					</label></b>
				</td>
				<td style="text-align:right; width: 8%"><b><%= toCurrency %> <label id="total_tax"><%= html_hours("%.2f" % total) %></label></b></td>
				<td style="width: 5%"></td>
			</tr>
		</table>
	<% end %>
	<%
		round_index = 1
		isRundFOrInvItem = true
		invoiceroundItem = @invoiceItem.where(:item_type => 'r') if @invoiceItem.present?
	%>
	<% if isChecked("invoice_auto_round_gl") || invoiceroundItem.present? %>
		<table id="roundOffTable" class="list time-entries">
			<% if invoiceroundItem.present? %>
				<% invoiceroundItem.each do |entry| %>
					<% isRundFOrInvItem = false if round_index < 2 %>
					<tr class="round_tr">
						<td class="lbl-txt-align" style="width: 5%"><%=h hidden_field_tag("round_id#{round_index}", entry.id )  %></td>
						<td class="lbl-txt-align" style="<%= @projectsDD.present? ? "10%" : "0%" %>"><%=h hidden_field_tag("pjt_id#{round_index}", entry.project_id )  %></td>
						<td style="width: <%= @productItemsDD.present? ? "20%" : "0%" %>"></td>
						<td class="lbl-txt-align" style="width: 20%" ></td>
						<td class="lbl-txt-align" style="width: 5%"><%=  entry.name %></td>
						<td class="lbl-txt-align" style="width: 11%"></td>
						<td style="text-align:right; width: 11%"></td>
						<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
							<td style="width: 11%"></td>
						<% end %>
						<td style="text-align:right; width: 8%">
							<%= entry.original_currency %> <label id="round_org_amount"><%= html_hours("%.2f" % entry.original_amount) %></label>
						</td>
						<td style="text-align:right; width: 8%">
							<%= toCurrency %> <label id="round_amount"><%= html_hours("%.2f" % entry.amount) %></label>
						</td>
						<td style="width: 5%"></td>
						<% org_total = org_total + entry.original_amount %>
						<% total = total + entry.amount %>
					</tr>
						<% round_index = round_index + 1 %>
				<% end %>
			<% elsif isChecked("invoice_auto_round_gl") %>
				<% roundOff_amount = getExchangedAmount( @currency, (org_total.round - org_total)) %>
				<tr class="round_tr">
					<td class="lbl-txt-align" style="width: 5%"><%=h hidden_field_tag("round_id#{round_index}", "" ) %></td>
					<td class="lbl-txt-align" style="<%= @projectsDD.present? ? "10%" : "0%" %>"><%=h hidden_field_tag("pjt_id#{round_index}", "" ) %></td>
						<td style="width: <%= @productItemsDD.present? ? "20%" : "0%" %>"></td>
					<td class="lbl-txt-align" style="width: 20%" ></td>
					<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
						<td style="width: 11%"></td>
					<% end %>
					<td class="lbl-txt-align" style="width: 5%"><%=  l(:label_round_off) %></td>
					<td class="lbl-txt-align" style="width: 11%"></td>
					<td style="text-align:right; width: 11%"></td>
					<td style="text-align:right; width: 8%">
						<%= @currency %> <label id="round_org_amount"><%= html_hours("%.2f" % (org_total.round - org_total)) %></label>
					</td>
					<td style="text-align:right; width: 8%">
						<%= toCurrency %> <label id="round_amount"><%= html_hours("%.2f" % roundOff_amount) %></label>
					</td>
					<td style="width: 5%"></td>
					<% org_total = org_total + (org_total.round - org_total) %>
					<% total = total + roundOff_amount %>
				</tr>
					<% round_index = round_index + 1 %>
			<% end %>

				<tr>
					<td style="width: 5%"></td>
					<td style="width: <%= @projectsDD.present? ? "10%" : "0%" %>"></td>
					<td style="width: <%= @productItemsDD.present? ? "20%" : "0%" %>"></td>
					<td style="width: 20%"></td>
					<td style="width: 5%"></td>
					<td style="width: 11%"></td>
					<% if @invoice.id.present? && @invoice.invoice_type == 'SI'%>
						<td style="width: 11%"></td>
					<% end %>
					<td style="text-align:right; width: 11%">
						<b><label><%= l(:label_grand_total) %> :</label></b>
					</td>
					<td style="text-align:right; width: 8%">
						<b><%= @invoiceItem.blank? ? @currency : (@invoiceItem[0].blank? ? @currency : @invoiceItem[0].original_currency) %> <label id="inv_orginal_total">
							<%= html_hours("%.2f" % org_total) %>
						</label></b>
					</td>
					<td style="text-align:right; width: 8%">
						<b><%= toCurrency %> <label id="inv_total"><%= html_hours("%.2f" % total) %></label></b>
					</td>
					<td style="width: 5%"></td>
				</tr>
		</table>
	<% end %>
</div>
<div class="wk-contextual">
	<% if !@preBilling && !isDisable %>
	<%= link_to l(:button_add_row), "#", { :onclick => "javascript:invoiceAddRow('invoiceTable', 'totalrow');",  :class => 'icon icon-add' } %>
	<% if controller.addAllRows %>
		<%= link_to l(:label_add_all_rows), request.params.merge(populate_items: '1'),  data: {:confirm => l(:warn_add_all_rows)},  class: 'icon icon-add' %>
		<%= link_to l(:label_delete_all_rows), "#", { :onclick => "javascript:deleteAllRows('invoiceTable', 'totalrow');",  :class => 'icon icon-add'} %>
	<% end %>
<% end %>
</div>
<div style="clear:both"></div>
<%=h hidden_field_tag("totalrow",  row_index-1 )  %>
<%=h hidden_field_tag("taxrow",  tax_index-1 )  %>
<%=h hidden_field_tag("invoice_id",  (params[:loadUnBilled].present? || controller.addAllRows && params[:populate_items]) ? params[:invoice_id] : @invoice.id )  %>
<%=h hidden_field_tag("parent_type", @invoice.parent_type)  %>
<%=h hidden_field_tag("parent_id", @invoice.parent_id)  %>
<%= hidden_field_tag("additional_item_type", call_hook(:modify_product_log_type)) %>
<%= submit_tag l(:button_save), :hidden => false, :id => 'items_save', :onclick => "return saveEntity()", :disabled => (@preBilling) if !@preBilling %>
<% if !@preBilling && controller.addUnbilledItems && @invPaymentItems.blank? && @invoice&.id.present? %>
	&nbsp;&nbsp;<%= link_to l(:label_add_unbilled), request.params.merge(loadUnBilled: "true"),  :class => 'icon icon-report' %>
<% end %>
<% if controller.isInvPaymentLink && !@invoiceItem.blank? && !@invoice.id.blank?%>
	&nbsp;&nbsp;<%= link_to l(:label_pay_invoice), url_for(:controller => controller.getPaymentController, :action => 'edit', :load_payment => true, :related_to => @invoice.parent_type, :related_parent => @invoice.parent_id, :project_id => 0 ),  :class => 'icon icon-report' %>
<% end %>
<% unless @invoiceItem.blank? || @invoice.id.blank? %>
	<% other_formats_links do |f| %>
		<%= link_to l(:label_export_invoice), "javascript: openInvReportPopup();" %>&nbsp; |
		<%= f.link_to 'PDF', url: {controller: controller_name, action: 'export', invoice_id: @invoice.id} %>&nbsp;
		<%= f.link_to 'CSV', url: {controller: controller_name, action: 'export', invoice_id: @invoice.id} %>
	<% end %>
<% end %>
<% if !@invoice.id.blank? && controller.isInvPaymentLink  %>
	<br/><br/>
	<h2><%= l(:label_payments) %></h2>
	<%= render :partial => 'inv_payment_items_list' %>
<% end %>

<div id="quantity-dlg">
	<table id=qunatityTable class=qunatity-entries>
	</table>
</div>

<div id="serialno-dlg" style="display:none">
	<%= text_area_tag "inv_serial_no",'', size: "35x5", :style => "width:95%;"%>
	<%= hidden_field_tag("item_serial_no")  %>
	<p><%= l(:label_serial_number_note) %></p>
	<span class="icon icon-warning warning" id="warn_serial_number" style="background-color: unset; border: 0;display:none;"><%= l(:warn_sn_range) %></span></td>
</div>