Class: Osm::GiftAid

Inherits:
Object
  • Object
show all
Defined in:
lib/osm/giftaid.rb

Defined Under Namespace

Classes: Data, Donation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Initialize a new RegisterField

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/giftaid.rb', line 151

Class Method Details

.get_data(api, section, term = nil, options = {}) ⇒ Array<Osm::GiftAid::Data>

Get donation data

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 register for

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

    The term (or its ID) to get the register for, passing nil causes the current term to be used

  • 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

Returns:



45
46
47
48
49
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
82
83
84
85
86
87
# File 'lib/osm/giftaid.rb', line 45

def self.get_data(api, section, term=nil, options={})
  Osm::Model.require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['gift_aid_data', section_id, term_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return Osm::Model.cache_read(api, cache_key)
  end

  data = api.perform_query("giftaid.php?action=getGrid&sectionid=#{section_id}&termid=#{term_id}")

  to_return = []
  if data.is_a?(Hash) && data['items'].is_a?(Array)
    data = data['items']
    data.each do |item|
      if item.is_a?(Hash)
        unless item['scoutid'].to_i < 0  # It's a total row
          donations = {}
          item.each do |key, value|
            if key.match(Osm::OSM_DATE_REGEX)
              donations[Osm::parse_date(key)] = value
            end
          end
          to_return.push Osm::GiftAid::Data.new(
            :member_id => Osm::to_i_or_nil(item['scoutid']),
            :grouping_id => Osm::to_i_or_nil(item ['patrolid']),
            :section_id => section_id,
            :first_name => item['firstname'],
            :last_name => item['lastname'],
            :tax_payer_name => item['parentname'],
            :tax_payer_address => item['address'],
            :tax_payer_postcode => item['postcode'],
            :total => item['total'],
            :donations => donations,
          )
        end
      end
    end
    Osm::Model.cache_write(api, cache_key, to_return)
  end
  return to_return
end

.get_donations(api, section, term = nil, options = {}) ⇒ Array<Osm::GiftAid::Donation>

Get donations

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 structure for

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

    The term (or its ID) to get the structure for, passing nil causes the current term to be used

  • 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

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/osm/giftaid.rb', line 11

def self.get_donations(api, section, term=nil, options={})
  Osm::Model.require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['gift_aid_donations', section_id, term_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return Osm::Model.cache_read(api, cache_key)
  end

  data = api.perform_query("giftaid.php?action=getStructure&sectionid=#{section_id}&termid=#{term_id}")

  structure = []
  if data.is_a?(Array)
    data = (data.size == 2) ? data[1] : []
    if data.is_a?(Hash) && data['rows'].is_a?(Array)
      data['rows'].each do |row|
        structure.push Donation.new(
          :donation_date => Osm::parse_date(row['field']),
        )
      end
    end
  end

  Osm::Model.cache_write(api, cache_key, structure) unless structure.nil?
  return structure
end

.update_donation(data = {}) ⇒ Boolean

Update information for a donation

Parameters:

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

Options Hash (data):

  • :api (Osm::Api)

    The api to use to make the request

  • :section (Osm::Section)

    the section to update the register for

  • :term (Osm::Term, #to_i, nil)

    The term (or its ID) to get the register for, passing nil causes the current term to be used

  • :evening (Osm::Evening, DateTime, Date)

    the evening to update the register on

  • :members (Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>, #to_i, Array<#to_i>)

    the members (or their ids) to update

  • :donation_date (Date, #strftime)

    the date the donation was made

  • :amount (String, #to_s)

    the donation amount

  • :note (String, #to_s)

    the description for the donation

Returns:

  • (Boolean)

    whether the update succedded

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/osm/giftaid.rb', line 106

def self.update_donation(data={})
  raise Osm::ArgumentIsInvalid, ':section is missing' if data[:section].nil?
  raise Osm::ArgumentIsInvalid, ':donation_date is missing' if data[:donation_date].nil?
  raise Osm::ArgumentIsInvalid, ':amount is missing' if data[:amount].nil?
  raise Osm::ArgumentIsInvalid, ':note is missing' if data[:note].nil?
  raise Osm::ArgumentIsInvalid, ':members is missing' if data[:members].nil?
  raise Osm::ArgumentIsInvalid, ':api is missing' if data[:api].nil?
  api = data[:api]
  Osm::Model.require_ability_to(api, :write, :finance, data[:section])

  term_id = data[:term].nil? ? Osm::Term.get_current_term_for_section(api, data[:section]).id : data[:term].to_i
  section_id = data[:section].to_i

  data[:members] = [*data[:members]].map{ |member| member.to_i.to_s } # Make sure it's an Array of Strings

  response = api.perform_query("giftaid.php?action=update&sectionid=#{section_id}&termid=#{term_id}", {
    'scouts' => data[:members].inspect,
    'sectionid' => section_id,
    'donatedate'=> data[:donation_date].strftime(Osm::OSM_DATE_FORMAT),
    'amount' => data[:amount],
    'notes' => data[:note],
  })

  # The cached donations and data will be out of date - remove them
  Osm::Model.cache_delete(api, ['gift_aid_donations', section_id, term_id])
  Osm::Model.cache_delete(api, ['gift_aid_data', section_id, term_id])

  return response.is_a?(Array)
end