Method: Osm::Invoice.get_for_section

Defined in:
lib/osm/invoice.rb

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Invoice>

Get invoices for a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the invoices for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

  • :include_archived (Boolean) — default: optional

    if true then archived invoices will also be returned

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osm/invoice.rb', line 50

def self.get_for_section(api, section, options={})
  require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  cache_key = ['invoice_ids', section_id]
  invoices = nil

  if !options[:no_cache] && cache_exist?(api, cache_key)
    ids = cache_read(api, cache_key)
    invoices = get_from_ids(api, ids, 'invoice', section, options, :get_for_section)
  end

  if invoices.nil?
    data = api.perform_query("finances.php?action=getInvoices&sectionid=#{section_id}&showArchived=true")
    invoices = Array.new
    ids = Array.new
    unless data['items'].nil?
      data['items'].map { |i| i['invoiceid'].to_i }.each do |invoice_id|
        invoice_data = api.perform_query("finances.php?action=getInvoice&sectionid=#{section_id}&invoiceid=#{invoice_id}")
        invoice = self.new_invoice_from_data(invoice_data)
        invoices.push invoice
        ids.push invoice.id
        cache_write(api, ['invoice', invoice.id], invoice)
      end
    end
    cache_write(api, cache_key, ids)
  end

  return invoices if options[:include_archived]
  return invoices.reject do |invoice|
    invoice.archived?
  end
end