Class: Osm::GiftAid::Data

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

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


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

attribute :member_id, :type => Integer

#first_nameString


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

attribute :member_id, :type => Integer

#grouping_idFixnum


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

attribute :member_id, :type => Integer

#last_nameString


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

attribute :member_id, :type => Integer

#member_idFixnum


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

attribute :member_id, :type => Integer

#section_idFixnum


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

attribute :member_id, :type => Integer

#tax_payer_addressString


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

attribute :member_id, :type => Integer

#tax_payer_nameString


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

attribute :member_id, :type => Integer

#tax_payer_postcodeString


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

attribute :member_id, :type => Integer

#totalString


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

attribute :member_id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

Compare Data based on section_id, grouping_id, last_name then first_name


284
285
286
287
288
289
290
# File 'lib/osm/giftaid.rb', line 284

def <=>(another)
  result = self.section_id <=> another.try(:section_id)
  result = self.grouping_id <=> another.try(:grouping_id) if result == 0
  result = self.last_name <=> another.try(:last_name) if result == 0
  result = self.first_name <=> another.try(:last_name) if result == 0
  return result
end

#update(api) ⇒ Boolean

Update data in OSM

Raises:


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
280
# File 'lib/osm/giftaid.rb', line 225

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