Class: Osm::OnlinePayment::Schedule

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

Defined Under Namespace

Classes: Payment, PaymentStatus, PaymentsForMember

Constant Summary collapse

SORT_BY =
[:section_id, :name, :id]
PAY_NOW_OPTIONS =
{
  -1 => 'Allowed at all times',
  0  => 'Permanently disabled',
  7  => 'Allowed within 1 week of due day',
  14 => 'Allowed within 2 weeks of due day',
  21 => 'Allowed within 3 weeks of due day',
  28 => 'Allowed within 4 weeks of due day',
  42 => 'Allowed within 6 weeks of due day',
  56 => 'Allowed within 8 weeks of due day',
}

Instance Attribute Summary collapse

Class Method Summary collapse

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 74

Instance Attribute Details

#account_idFixNum

Returns the ID of the bank account this schedule is tied to.

Returns:

  • (FixNum)

    the ID of the bank account this schedule is tied to



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

#annual_limitString

Returns the maximum amount you’ll be able to collect in a rolling 12 month period using this schedule.

Returns:

  • (String)

    the maximum amount you’ll be able to collect in a rolling 12 month period using this schedule



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

#archivedBoolean

Returns whether the schedule has been archived.

Returns:

  • (Boolean)

    whether the schedule has been archived



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

#descriptionString

Returns the description of what the schedule is for.

Returns:

  • (String)

    the description of what the schedule is for



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

#gift_aidBoolean

Returns whether payments made using this schedule are eligable for gift aid.

Returns:

  • (Boolean)

    whether payments made using this schedule are eligable for gift aid



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

#idFixNum

Returns the schedule’s ID.

Returns:

  • (FixNum)

    the schedule’s ID



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

#nameString

Returns the name of the schedule.

Returns:

  • (String)

    the name of the schedule



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

#pay_nowFixNum

Returns controls the use of the pay now feature in OSM, see the PAY_NOW_OPTIONS hash.

Returns:

  • (FixNum)

    controls the use of the pay now feature in OSM, see the PAY_NOW_OPTIONS hash



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

#paymentsArray<Payment>

Returns the payments which make up this schedule.

Returns:

  • (Array<Payment>)

    the payments which make up this schedule



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

#require_allBoolean

Returns whether to require all payments within the schedule by default.

Returns:

  • (Boolean)

    whether to require all payments within the schedule by default



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

#section_idFixNum

Returns the ID of the section the schedule belongs to.

Returns:

  • (FixNum)

    the ID of the section the schedule belongs to



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

Class Method Details

.get(api, section, schedule, options = {}) ⇒ Array<Osm::OnlinePayment::Schedule>

Get a payment schedules for a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the due badges for

  • schedule (Fixnum, #to_i)

    The ID of the payment schedule to get

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/osm/online_payment.rb', line 123

def self.get(api, section, schedule, options={})
  require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  schedule_id = schedule.to_i
  cache_key = ['online_payments', 'schedule', schedule_id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    data = cache_read(api, cache_key)
    return data if data.section_id.eql?(section_id)
  end

  data = api.perform_query("ext/finances/onlinepayments/?action=getPaymentSchedule&sectionid=#{section_id}&schemeid=#{schedule_id}&allpayments=true")
  schedule = new(
    id:            Osm::to_i_or_nil(data['schemeid']),
    section_id:    section_id,
    account_id:    Osm::to_i_or_nil(data['accountid']),
    name:          data['name'],
    description:   data['description'],
    archived:      data['archived'].eql?('1'),
    gift_aid:      data['giftaid'].eql?('1'),
    require_all:   data['defaulton'].eql?('1'),
    pay_now:       data['paynow'],
    annual_limit:  data['preauth_amount'],
  )

  (data['payments'] || []).each do |payment_data|
    payment = Payment.new(
      amount:   payment_data['amount'],
      archived: payment_data['archived'].eql?('1'),
      due_date: Osm::parse_date(payment_data['date']),
      name:     payment_data['name'].to_s,
      id:       Osm::to_i_or_nil(payment_data['paymentid']),
      schedule: schedule,
    )
    schedule.payments.push payment
  end 

  cache_write(api, cache_key, schedule)
  return schedule
end

.get_for_section(api, section, options = {}) ⇒ Array<Osm::OnlinePayment::Schedule>

Get all payment schedules for a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the due badges for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:



108
109
110
111
112
113
114
# File 'lib/osm/online_payment.rb', line 108

def self.get_for_section(api, section, options={})
  require_ability_to(api, :read, :finance, section, options)

  get_list_for_section(api, section, options).map do |schedule|
    get(api, section, schedule[:id], options)
  end
end

.get_list_for_section(api, section, options = {}) ⇒ Array<Hash>

Get a simple list of schedules for a section

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the due badges for

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • (Array<Hash>)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/osm/online_payment.rb', line 84

def self.get_list_for_section(api, section, options={})
  require_ability_to(api, :read, :finance, section, options)
  section_id = section.to_i
  cache_key = ['online_payments', 'schedule_list', section_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("ext/finances/onlinepayments/?action=getSchemes&sectionid=#{section_id}")
  data = data.is_a?(Hash) ? data['items'] : nil
  data ||= []
  data.map!{ |i| {id: Osm::to_i_or_nil(i['schemeid']), name: i['name'].to_s } }

  cache_write(api, cache_key, data)
  return data
end

Instance Method Details

#archived_paymentsArray<Osm::OnlinePayment::Schedule::Payment>

Get archived payments for the schedule



225
226
227
# File 'lib/osm/online_payment.rb', line 225

def archived_payments
  payments.select{ |p| p.archived? }
end

#archived_payments?Boolean

Check if there are any archived payments for the schedule

Returns:

  • (Boolean)


230
231
232
# File 'lib/osm/online_payment.rb', line 230

def archived_payments?
  payments.any?{ |p| p.archived? }
end

#current_paymentsArray<Osm::OnlinePayment::Schedule::Payment>

Get unarchived payments for the schedule



214
215
216
# File 'lib/osm/online_payment.rb', line 214

def current_payments
  payments.select{ |p| !p.archived? }
end

#current_payments?Boolean

Check if there are any unarchived payments for the schedule

Returns:

  • (Boolean)


219
220
221
# File 'lib/osm/online_payment.rb', line 219

def current_payments?
  payments.any?{ |p| !p.archived? }
end

#get_payments_for_members(api, term = nil, options = {}) ⇒ Array<Osm::OnlinePayment::Schedule::PaymentsForMember>

Get payments made by members for the schedule

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • term (Osm::Term, Fixnum, #to_i) (defaults to: nil)

    The term (or it’s id) to get details for (defaults to current term)

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/osm/online_payment.rb', line 170

def get_payments_for_members(api, term=nil, options={})
  require_ability_to(api, :read, :finance, section_id, options)

  if term.nil?
    section = Osm::Section.get(api, section_id, options)
    term = section.waiting? ? -1 : Osm::Term.get_current_term_for_section(api, section)
  end

  cache_key = ['online_payments', 'for_members', id, term.to_i]
  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("ext/finances/onlinepayments/?action=getPaymentStatus&sectionid=#{section_id}&schemeid=#{id}&termid=#{term.to_i}")
  data = data['items'] || []
  data.map! do |item|
    payments_data = {}
    payments.each do |payment|
      unless item[payment.id.to_s].nil?
        payments_data[payment.id] = PaymentStatus.build_from_json(item[payment.id.to_s], payment)
      end
    end

    PaymentsForMember.new(
      member_id:      Osm::to_i_or_nil(item['scoutid']),
      section_id:     section_id,
      grouping_id:    Osm::to_i_or_nil(item['patrolid']),
      first_name:     item['firstname'],
      last_name:      item['lastname'],
      start_date:     require_all ? Osm::parse_date(item['startdate']) : nil,
      direct_debit:   item['directdebit'].downcase.to_sym,
      payments:       payments_data,
      schedule:       self,
    )
  end

  cache_write(api, cache_key, data)
  return data
end

#to_sObject



234
235
236
# File 'lib/osm/online_payment.rb', line 234

def to_s
  "#{id} -> #{name}"
end