class Osm::GiftAid::Data

Constants

SORT_BY

Public Instance Methods

update(api) click to toggle source

Update data in OSM @param [Osm::Api] api The api to use to make the request @return [Boolean] whether the data was updated in OSM @raise [Osm::ObjectIsInvalid] If the Data is invalid

# File lib/osm/giftaid.rb, line 224
def update(api)
  raise Osm::ObjectIsInvalid, 'data is invalid' unless valid?
  require_ability_to(api, :write, :finance, section_id)
  term_id = Osm::Term.get_current_term_for_section(api, section_id).id

  updated = true
  fields = [
    ['tax_payer_name', 'parentname', tax_payer_name],
    ['tax_payer_address', 'address', tax_payer_address],
    ['tax_payer_postcode', 'postcode', tax_payer_postcode],
  ]
  fields.each do |field|
    if changed_attributes.include?(field[0])
      result = api.perform_query("giftaid.php?action=updateScout", {
        'scoutid' => member_id,
        'termid' => term_id,
        'column' => field[1],
        'value' => field[2],
        'sectionid' => section_id,
        'row' => 0,
      })
      if result.is_a?(Hash)
        (result['items'] || []).each do |i|
          if i['scoutid'] == member_id.to_s
            updated = false unless i[field[1]] == field[2]
          end
        end
      end
    end
  end
  reset_changed_attributes if updated

  donations.changes.each do |date, (was,now)|
    date = date.strftime(Osm::OSM_DATE_FORMAT)
    result = api.perform_query("giftaid.php?action=updateScout", {
      'scoutid' => member_id,
      'termid' => term_id,
      'column' => date,
      'value' => now,
      'sectionid' => section_id,
      'row' => 0,
    })
    if result.is_a?(Hash)
      (result['items'] || []).each do |i|
        if i['scoutid'] == member_id.to_s
          updated = false unless i[date] == now
        end
      end
    end
  end
  donations.clean_up! if updated

  Osm::Model.cache_delete(api, ['gift_aid_data', section_id, term_id]) if updated

  return updated
end