Class: Osm::OnlinePayment::Schedule::PaymentsForMember

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

Constant Summary

Constants inherited from Model

Model::SORT_BY

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initializeObject

Initialize a new Schedule

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/online_payment.rb', line 312

Instance Method Details

#active_direct_debit?Boolean

Check if the member has an active direct debit for this schedule

Returns:

  • (Boolean)


353
354
355
# File 'lib/osm/online_payment.rb', line 353

def active_direct_debit?
  direct_debit.eql?(:active)
end

#latest_status_for(payment) ⇒ Boolean

Get the most recent status for a member’s payment

Parameters:

Returns:

  • (Boolean)


320
321
322
323
# File 'lib/osm/online_payment.rb', line 320

def latest_status_for(payment)
  @latest_status ||= Hash[ payments.map{ |k,v| [k, v.sort[0]] } ]
  @latest_status[payment.to_i]
end

#mark_payment_not_required(api, payment) ⇒ Boolean

Mark a payment as not required by the member

Parameters:

Returns:

  • (Boolean)

    whether the update was made in OSM



406
407
408
# File 'lib/osm/online_payment.rb', line 406

def mark_payment_not_required(api, payment)
  update_payment_status(api, payment, :not_required)
end

#mark_payment_paid_manually(api, payment, gift_aid = false) ⇒ Boolean

Mark a payment as paid by the member

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • payment (Osm::OnlinePayment::Schedule::Payment, Fixnum, #to_i)

    The payment (or it’s ID) to update

  • gift_aid (Boolean) (defaults to: false)

    Whether to update the gift aid record too

Returns:

  • (Boolean)

    whether the update was made in OSM



415
416
417
# File 'lib/osm/online_payment.rb', line 415

def mark_payment_paid_manually(api, payment, gift_aid=false)
  update_payment_status(api, payment, :paid_manually, gift_aid)
end

#mark_payment_required(api, payment) ⇒ Boolean

Mark a payment as required by the member

Parameters:

Returns:

  • (Boolean)

    whether the update was made in OSM



398
399
400
# File 'lib/osm/online_payment.rb', line 398

def mark_payment_required(api, payment)
  update_payment_status(api, payment, :required)
end

#over_due?(payment, date = nil) ⇒ Boolean

Check if a payment is over due (or will be over due on the passed date)

Parameters:

Returns:

  • (Boolean)

    whether the member’s payment is unpaid and the payment’s due date has passed



347
348
349
# File 'lib/osm/online_payment.rb', line 347

def over_due?(payment, date=nil)
  unpaid?(payment) && payment.past_due?(date)
end

#paid?(payment) ⇒ Boolean?

Check if the status of a member’s payment is considered paid

Parameters:

Returns:

  • (Boolean, nil)


328
329
330
331
332
# File 'lib/osm/online_payment.rb', line 328

def paid?(payment)
  status = latest_status_for(payment.to_i)
  return nil if status.nil?
  [:paid, :paid_manually, :received, :initiated].include?(status.status)
end

#unpaid?(payment) ⇒ Boolean?

Check if the status of a member’s payment is considered unpaid

Parameters:

Returns:

  • (Boolean, nil)


337
338
339
340
341
# File 'lib/osm/online_payment.rb', line 337

def unpaid?(payment)
  status = latest_status_for(payment.to_i)
  return nil if status.nil?
  [:required].include?(status.status)
end

#update_payment_status(api, payment, status, gift_aid = false) ⇒ Boolean

Update the status of a payment for the member in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • payment (Osm::OnlinePayment::Schedule::Payment, Fixnum, #to_i)

    The payment (or it’s ID) to update

  • status (Symbol)

    What to update the status to (:required, :not_required or :paid_manually)

  • gift_aid (Boolean) (defaults to: false)

    Whether to update the gift aid record too (only relevant when setting to :paid_manually)

Returns:

  • (Boolean)

    whether the update was made in OSM



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/osm/online_payment.rb', line 363

def update_payment_status(api, payment, status, gift_aid=false)
  payment_id = payment.to_i
  fail ArgumentError, "#{payment_id} is not a valid payment for the schedule." unless schedule.payments.map(&:id).include?(payment_id)
  fail ArgumentError, "status must be either :required, :not_required or :paid_manually. You passed in #{status.inspect}" unless [:required, :not_required, :paid_manually].include?(status)

  gift_aid = false unless payment.schedule.gift_aid?
  api_status = {
    required:       'Payment required',
    not_required:   'Payment not required',
    paid_manually:  'Paid manually',
  }[status]

  data = api.perform_query("ext/finances/onlinepayments/?action=updatePaymentStatus", {
    'sectionid' => schedule.section_id,
    'schemeid' => schedule.id,
    'scoutid' => member_id,
    'paymentid' => payment_id,
    'giftaid' => gift_aid,
    'value' => api_status,
  })

  data = data[payment_id.to_s]
  return false if data.nil?                     # No data (at all) for this payment
  data = PaymentStatus.build_from_json(data)
  return false if data.nil?                     # No history for payment so it didn't get updated
  data = data.sort[0]
  return false if data.nil?                     # No history for payment so it didn't get updated
  return false unless data.status.eql?(status)  # Latest status is not what we set
  return true
end