Method: Osm::GiftAid.update_donation

Defined in:
lib/osm/giftaid.rb

.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