class Osm::GiftAid

Public Class Methods

get_data(api, section, term=nil, options={}) click to toggle source

Get donation data @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get the register for @param [Osm::Term, Fixnum, to_i, nil] term The term (or its ID) to get the register for, passing nil causes the current term to be used @!macro options_get @return [Array<Osm::GiftAid::Data>] representing the donations of each member

# 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={}) click to toggle source

Get donations @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get the structure for @param [Osm::Term, Fixnum, to_i, nil] term The term (or its ID) to get the structure for, passing nil causes the current term to be used @!macro options_get @return [Array<Osm::GiftAid::Donation>] representing the donations made

# 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={}) click to toggle source

Update information for a donation @param [Hash] data @option data [Osm::Api] :api The api to use to make the request @option data [Osm::Section] :section the section to update the register for @option data [Osm::Term, to_i, nil] :term The term (or its ID) to get the register for, passing nil causes the current term to be used @option data [Osm::Evening, DateTime, Date] :evening the evening to update the register on @option data [Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>, to_i, Array<#to_i>] :members the members (or their ids) to update @option data [Date, strftime] :donation_date the date the donation was made @option data [String, to_s] :amount the donation amount @option data [String, to_s] :note the description for the donation @return [Boolean] whether the update succedded @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing

# 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