Class: Osm::GiftAid::Data

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

Constant Summary collapse

SORT_BY =
[:section_id, :grouping_id, :last_name, :first_name]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#donationsDirtyHashy



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

attribute :member_id, :type => Integer

#first_nameString



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

attribute :member_id, :type => Integer

#grouping_idFixnum



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

attribute :member_id, :type => Integer

#last_nameString



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

attribute :member_id, :type => Integer

#member_idFixnum



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

attribute :member_id, :type => Integer

#section_idFixnum



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

attribute :member_id, :type => Integer

#tax_payer_addressString



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

attribute :member_id, :type => Integer

#tax_payer_nameString



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

attribute :member_id, :type => Integer

#tax_payer_postcodeString



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

attribute :member_id, :type => Integer

#totalString



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

attribute :member_id, :type => Integer

Instance Method Details

#update(api) ⇒ Boolean

Update data in OSM

Raises:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# 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