
  <% lastItemType = nil %>
  <% lastProjectId = nil %>
  <% invoiceItems.each do | entry | %>
    <% if entry.item_type != 'r' %>
      <% if !lastItemType.blank? && entry.item_type != lastItemType && lastProjectId == entry.project_id && lastItemType == 'C' %>
        <tr style="width:100%; border-top: solid thin; border-bottom: solid thin;">
          <td style="width:42%;" ></td>
          <td style="width:16%;" ></td>
          <td style="width:16%;" align="right" ><b><%=h l(:label_sub_total) %></b></td>
          <td style="width:16%;" align="right"><b><%=h "%.2f" % invoiceItems.where(:project_id => lastProjectId, :item_type => 'i').sum(:quantity) %></b></td>
          <td style="width:16%;" align="center" ><b><%=h entry.original_currency %></b></td>
          <td style="width:16%;" align="right" ><b><%=h "%.2f" % invoiceItems.where(:project_id => lastProjectId, :item_type => 'i').sum(:original_amount) %></b></td>
        </tr>
      <% end %>

      <% if !lastProjectId.blank? && lastProjectId != entry.project_id %>
        <tr style="width:100%; border-top: solid thin; border-bottom: solid thin;">
            <td style="width:42%;" ></td>
            <td style="width:16%;" ></td>
            <td style="width:16%;" align="right"><b><%=h l(:label_total) %></b></td>
            <td style="width:16%;" align="right" ><b><%=h "%.2f" %  invoiceItems.where(:project_id => lastProjectId).where.not(:item_type => 'r').sum(:quantity) %></b></td>
            <td style="width:16%;" align="center"><b><%=h entry.original_currency %></b></td>
            <td style="width:16%;" align="right" ><b><%=h "%.2f" % invoiceItems.where(:project_id => lastProjectId).where.not(:item_type => 'r').sum(:original_amount) %></b></td>
        </tr>
      <% end %>
        <tr style="width:100%;" >
          <td style="width:42%;" ><%=h entry.name %></td>
          <td style="width:16%;" ><%= getInvoiceItemType(entry) %></td>
          <td style="width:16%;" align="right" ><%=h ("%.2f" % entry.rate).to_s + (entry.item_type == 'i' || entry.item_type == 'c' || entry.item_type == 'm' || entry.item_type == 'a' || entry.item_type == 'e' ? '' : ( controller.addAdditionalTax ? '' : "%"))  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 %>

    <% lastItemType = entry.item_type%>
    <% lastProjectId = entry.project_id %>
  <% end %>

  <tr style="width:100%; border-top: solid thin; border-bottom: solid thin;">
      <td style="width:42%;" ></td>
      <td style="width:16%;" ></td>
      <td style="width:16%;" align="right"><b><%=h l(:label_total) %></b></td>
      <td style="width:16%;" align="right" ><b><%=h "%.2f" %  invoiceItems.where(:project_id => lastProjectId).where.not(:item_type => 'r').sum(:quantity) %></b></td>
      <td style="width:16%;" align="center"><b><%=h invoiceItems[0].original_currency %></b></td>
      <td style="width:16%;" align="right"><b><%=h "%.2f" % invoiceItems.where(:project_id => lastProjectId).where.not(:item_type => 'r').sum(:original_amount) %></b></td>
  </tr>