<h2>Invoices
  <%- if @buyer.present? -%>
    for account: <%= h @buyer.org_name %>
  <%- end -%>
</h2>

<table class="data">
  <thead>
    <tr>
      <th><%= sortable('friendly_id', 'Number', :admin_finance_invoices_path) %></th>
      <th><%= sortable('accounts.org_name', 'Account', :admin_finance_invoices_path) %></th>
      <th><%= sortable('period', 'Month', :admin_finance_invoices_path) %></th>
      <th>Cost</th>
      <th><%= sortable('state', 'State', :admin_finance_invoices_path) %></th>
      <th><!-- Action --></th>
    </tr>

    <tr class="search">
      <%= form_for @search, :as => :search, :url => '', :html => { :method => :get, :class => :search } do |s| -%>
        <th><%= s.text_field :number, :size => 17 %>
          <%= help_bubble 'number_wildcard' do %>
            You can use * as wildcard (eg. 2011-*-1) or search by exact number.
          <% end %>
        </th>
        <th>
          <%= s.text_field :buyer_query, :size => 20, :disabled => @buyer.present? %>
        </th>
        <th>
          <%= select_month @search.month_number.try(:to_i), :prefix => :search, :field_name => :month_number, :include_blank => true %>
          <%- years = Invoice.years %>
          <%= select_year @search.year.try(:to_i), :prefix => :search, :field_name => :year, :include_blank => true, :start_year => years.first, :end_year => years.last %>
        <th></th>
        <th><%= s.select :state, Invoice.state_machine.states.keys.collect(&:to_s).sort, :include_blank => true, :selected => @search.state %></th>
        <th><%= s.submit 'Search', :name => nil %> <%= s.submit 'Reset', :name => :search %></th>
      <%- end -%>
    </tr>
  </thead>
  <tbody class="invoices">
    <% @invoices.each do |invoice| %>

      <%= content_tag_for(:tr, invoice) do %>
        <td><%= invoice.friendly_id %></td>
        <td><%= link_to_buyer_or_deleted(invoice.buyer, :admin_buyers_account_invoices_path) %></td>
        <td><%= h invoice.name %></td>
        <td><%= price_tag(invoice.cost) %></td>
        <td><%= h invoice.state %></td>
        <td>
          <%= link_to 'Show Invoice',
              admin_finance_invoice_path(invoice),
              :title => "Show #{invoice.friendly_id}" -%>
        </td>
        <% end %>
    <% end %>

    <%- if @invoices.empty? -%>
      <tr class="no_results">
        <td colspan="6">
          No results.
          You can reset the search form by clicking on <%= button_to 'Reset', '', :value => '', :method => :get %>
        </td>
      </tr>
    <%- end -%>
  </tbody>
</table>

<%= will_paginate(@invoices) %>

<%= link_to_export_widget_for('Invoices') %>
