Module: EffectiveMembershipsRegistrar

Extended by:
ActiveSupport::Concern
Included in:
Effective::Registrar
Defined in:
app/models/concerns/effective_memberships_registrar.rb

Overview

EffectiveMembershipsRegistrar

This is different cause its not an ActiveRecord one

Mark your registrar with include EffectiveMembershipsRegistrar

Mark your category model with effective_memberships_category to get all the includes

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#advance_period(period:, number:) ⇒ Object

These two could be overridden if we do non 1-year periods



58
59
60
# File 'app/models/concerns/effective_memberships_registrar.rb', line 58

def advance_period(period:, number:)
  period.advance(years: number).beginning_of_year
end

#assign!(owner, categories:, statuses: nil, date: nil, number: nil) ⇒ Object

Category is required, statuses are optional



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/concerns/effective_memberships_registrar.rb', line 67

def assign!(owner, categories:, statuses: nil, date: nil, number: nil)
  categories = Array(categories)
  statuses = Array(statuses)

  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expecting a membership category') if categories.blank? || categories.any? { |cat| !cat.class.respond_to?(:effective_memberships_category?) }
  raise('expecting an membership status (optional)') if statuses.present? && statuses.any? { |status| !status.class.respond_to?(:effective_memberships_status?) }

  # Default Date and next number
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Find or build a membership
  membership = owner.membership || owner.build_membership

  # Assign Dates
  membership.joined_on ||= date  # Only if not already present

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: categories.first) if number.blank?
  membership.number ||= number
  membership.number_as_integer ||= (Integer(number) rescue nil)

  # Builds the membership_categories and membership_statuses. Marks for destruction ones that don't exist.
  membership.assign_attributes(statuses: statuses, categories: categories)

  changed = membership.membership_categories.any? { |mc| mc.new_record? || mc.marked_for_destruction? }
  changed ||= membership.membership_statuses.any? { |ms| ms.new_record? || ms.marked_for_destruction? }

  if changed
    membership.registration_on = date # Always new registration_on

    owner.build_membership_history(start_on: date)
    owner.save!
  end

  # Assign member role
  add_member_role(owner)

  owner.update_membership_status!
end

#build_reclassify_fees(owner:, membership:, to:, from:, period:, date:) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/concerns/effective_memberships_registrar.rb', line 203

def build_reclassify_fees(owner:, membership:, to:, from:, period:, date:)
  existing = owner.membership_period_fee(category: from, period: period)

  if existing
    [
      owner.build_prorated_fee(date: date),
      owner.build_discount_fee(date: date, from: from)
    ]
  else
    owner.build_renewal_fee(category: to, period: period)
  end
end

#build_register_fees(owner:, membership:, date:) ⇒ Object



162
163
164
# File 'app/models/concerns/effective_memberships_registrar.rb', line 162

def build_register_fees(owner:, membership:, date:)
  owner.build_prorated_fee(date: date)
end

#build_reinstate_fees(owner:, membership:, date:) ⇒ Object



273
274
275
# File 'app/models/concerns/effective_memberships_registrar.rb', line 273

def build_reinstate_fees(owner:, membership:, date:)
  owner.build_prorated_fee(date: date)
end

#create_all_fees!(period: nil) ⇒ Object

This is intended to be run once per day in a rake task rake effective_memberships:create_fees Create Renewal and Late fees Assigns NIGS



473
474
475
476
477
478
479
480
481
482
# File 'app/models/concerns/effective_memberships_registrar.rb', line 473

def create_all_fees!(period: nil)
  period ||= current_period
  memberships = Effective::Membership.deep.with_unpaid_fees_through(period)

  memberships.find_each do |membership|
    create_fees!(membership, period: period); GC.start
  end

  true
end

#create_fees!(resource, period: nil) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'app/models/concerns/effective_memberships_registrar.rb', line 484

def create_fees!(resource, period: nil)
  period ||= current_period

  membership = (resource.class.respond_to?(:effective_memberships_owner?) ? resource.membership : resource)
  raise('expected a membership') unless membership.present? && membership.kind_of?(Effective::Membership)

  # Create Renewal Fees
  create_renewal_fees!(membership, period: period)

  # Create Late Fees
  create_late_fees!(membership, period: period)

  # Update Membership Status - Assign Not In Good Standing
  create_not_in_good_standing!(membership, period: period)

  true
end

#create_late_fees!(membership, period:) ⇒ Object



517
518
519
520
521
522
523
524
525
526
# File 'app/models/concerns/effective_memberships_registrar.rb', line 517

def create_late_fees!(membership, period:)
  membership.categories.select(&:create_late_fees?).map do |category|
    fee = membership.owner.build_late_fee(category: category, period: period)
    next if fee.blank? || fee.persisted?

    puts("Created late fee for #{membership.owner}") if debug?

    fee.save!
  end
end

#create_not_in_good_standing!(membership, period:) ⇒ Object



528
529
530
531
532
533
# File 'app/models/concerns/effective_memberships_registrar.rb', line 528

def create_not_in_good_standing!(membership, period:)
  return unless Time.zone.now.beginning_of_day == not_in_good_standing_date(period: period).beginning_of_day
  return unless membership.categories.any?(&:create_not_in_good_standing?)

  membership.owner.update_membership_status!
end

#create_renewal_fees!(membership, period:) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'app/models/concerns/effective_memberships_registrar.rb', line 502

def create_renewal_fees!(membership, period:)
  membership.categories.select(&:create_renewal_fees?).map do |category|
    existing = membership.owner.membership_period_fee(category: category, period: period, except: 'Renewal')
    next if existing.present? # This might be an existing Prorated fee

    fee = membership.owner.build_renewal_fee(category: category, period: period)
    raise("expected build_renewal_fee to return a fee for period #{period}") unless fee.kind_of?(Effective::Fee)
    next if fee.blank? || fee.persisted?

    puts("Created renewal fee for #{membership.owner}") if debug?

    fee.save!
  end
end

#current_periodObject



413
414
415
# File 'app/models/concerns/effective_memberships_registrar.rb', line 413

def current_period
  period(date: Time.zone.now)
end

#delete_fees!(resource) ⇒ Object

Called by applicant.approve!



455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'app/models/concerns/effective_memberships_registrar.rb', line 455

def delete_fees!(resource)
  owner = (resource.class.respond_to?(:effective_memberships_owner?) ? resource : resource.try(:owner))
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  # Delete unpurchased fees and orders
  owner.outstanding_fee_payment_fees.each { |fee| fee.mark_for_destruction }
  owner.outstanding_fee_payment_orders.each { |order| order.mark_for_destruction }
  owner.save!

  owner.outstanding_fee_payments.each { |fee_payment| fee_payment.destroy! }

  true
end

#fee_payment_purchased!(owner) ⇒ Object

Called in the after_purchase of fee payment



536
537
538
539
# File 'app/models/concerns/effective_memberships_registrar.rb', line 536

def fee_payment_purchased!(owner)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  owner.update_membership_status! if owner.membership.present?
end

#fees_paid!(owner, date: nil, order_attributes: nil) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/models/concerns/effective_memberships_registrar.rb', line 377

def fees_paid!(owner, date: nil, order_attributes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected a Hash of attributes') if order_attributes.present? && !order_attributes.kind_of?(Hash)

  # Date
  date ||= Time.zone.now

  period = period(date: date)
  period_end_on = period_end_on(date: date)

  if owner.outstanding_fee_payment_fees.present?
    order = Effective::Order.new(items: owner.outstanding_fee_payment_fees, user: owner)
    order.assign_attributes(order_attributes) if order_attributes.present?
    order.mark_as_purchased!
  end

  owner.update_membership_status!
  owner.membership.update!(fees_paid_period: period, fees_paid_through_period: period_end_on)
end

#in_good_standing!(owner, date: nil, notes: nil) ⇒ Object



369
370
371
372
373
374
375
# File 'app/models/concerns/effective_memberships_registrar.rb', line 369

def in_good_standing!(owner, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected owner to be not in good standing') unless owner.membership.not_in_good_standing?

  status_remove!(owner, status: not_in_good_standing_status!, date: date, notes: notes)
end

#last_periodObject



417
418
419
# File 'app/models/concerns/effective_memberships_registrar.rb', line 417

def last_period
  advance_period(period: current_period, number: -1)
end

#late_fee_date(period:) ⇒ Object



26
27
28
29
# File 'app/models/concerns/effective_memberships_registrar.rb', line 26

def late_fee_date(period:)
  Time.zone.local(period.year, 2, 1) # Fees are late after February 1st
  raise('to be implemented by app registrar')
end

#min_late_durationObject

A renewal fee must be at least this old to be considered late? Must be less than (late_fee_date - renewal_fee_date) but this is not enforced



38
39
40
# File 'app/models/concerns/effective_memberships_registrar.rb', line 38

def min_late_duration
  60.days
end

#min_not_in_good_standing_durationObject

A renewal fee must be at least this old to be considered nigs? Must be less than (nigs_date - renewal_fee_date) but this is not enforced



44
45
46
# File 'app/models/concerns/effective_memberships_registrar.rb', line 44

def min_not_in_good_standing_duration
  60.days
end

#next_membership_number(owner, to:) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'app/models/concerns/effective_memberships_registrar.rb', line 398

def next_membership_number(owner, to:)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  categories = Array(to)
  raise('expecting a membership category') unless categories.all? { |cat| cat.class.respond_to?(:effective_memberships_category?) }

  return nil if categories.none?(&:membership_number_required?)

  # Just a simple number right now
  number = (Effective::Membership.all.max_number || 0) + 1

  # Returns a string
  number.to_s
end

#not_in_good_standing!(owner, date: nil, notes: nil) ⇒ Object



361
362
363
364
365
366
367
# File 'app/models/concerns/effective_memberships_registrar.rb', line 361

def not_in_good_standing!(owner, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member') unless owner.membership.present?
  raise('expected owner to be in good standing') unless owner.membership.in_good_standing?

  status_add!(owner, status: not_in_good_standing_status!, date: date, notes: notes)
end

#not_in_good_standing_date(period:) ⇒ Object



31
32
33
34
# File 'app/models/concerns/effective_memberships_registrar.rb', line 31

def not_in_good_standing_date(period:)
  Time.zone.local(period.year, 3, 1) # Membership in bad standing after March 1st
  raise('to be implemented by app registrar')
end

#not_in_good_standing_statusObject

scope



49
50
51
# File 'app/models/concerns/effective_memberships_registrar.rb', line 49

def not_in_good_standing_status
  EffectiveMemberships.Status.where(title: 'Not In Good Standing')
end

#not_in_good_standing_status!Object



53
54
55
# File 'app/models/concerns/effective_memberships_registrar.rb', line 53

def not_in_good_standing_status!
  EffectiveMemberships.Status.where(title: 'Not In Good Standing').first!
end

#period(date:) ⇒ Object

Returns a date of Jan 1, Year



422
423
424
425
426
# File 'app/models/concerns/effective_memberships_registrar.rb', line 422

def period(date:)
  cutoff = renewal_fee_date(date: date) # period_end_on
  period = (date < cutoff) ? advance_period(period: date, number: 0) : advance_period(period: date, number: 1)
  period.to_date
end

#period_end_on(date:) ⇒ Object



62
63
64
# File 'app/models/concerns/effective_memberships_registrar.rb', line 62

def period_end_on(date:)
  period(date: date).end_of_year
end

#periods(from:, to: nil) ⇒ Object

This is only used for a form collection on admin memberships



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'app/models/concerns/effective_memberships_registrar.rb', line 429

def periods(from:, to: nil)
  to ||= Time.zone.now

  raise('expected to date') unless to.respond_to?(:strftime)
  raise('expected from date') unless from.respond_to?(:strftime)

  from = period(date: from)
  to = period(date: to)

  retval = []

  loop do
    retval << from
    from = advance_period(period: from, number: 1)
    break if from > to
  end

  retval
end

#periods_collection(from: nil, to: nil) ⇒ Object



449
450
451
452
# File 'app/models/concerns/effective_memberships_registrar.rb', line 449

def periods_collection(from: nil, to: nil)
  from ||= Time.zone.now - 10.years
  periods(from: from, to: to).reverse.map { |period| [period_end_on(date: period), period] }
end

#reclassify!(owner, to:, status: nil, date: nil, skip_fees: false) ⇒ Object



166
167
168
169
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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 166

def reclassify!(owner, to:, status: nil, date: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  from = owner.membership.category

  raise('expecting a to memberships category') unless to.class.respond_to?(:effective_memberships_category?)
  raise('expecting a from memberships category') unless from.class.respond_to?(:effective_memberships_category?)
  raise('expecting a memberships status') unless status.nil? || status.class.respond_to?(:effective_memberships_status?)
  raise('expected to and from to be different') if from == to

  date ||= Time.zone.now
  period = period(date: date)

  # Existing Membership
  membership = owner.membership

  # Last Changed Date
  membership.registration_on = date

  # Assign Category
  membership.build_membership_category(category: to)
  membership.membership_category(category: from).mark_for_destruction

  # Assign Status
  membership.build_membership_status(status: status) if status.present?

  unless skip_fees
    fees = build_reclassify_fees(owner: owner, membership: membership, to: to, from: from, period: period, date: date)
    raise('unexpected purchased fee') if Array(fees).compact.any?(&:purchased?)
  end

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#register!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 110

def register!(owner, to:, status: nil, date: nil, number: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expecting a memberships category') unless to.class.respond_to?(:effective_memberships_category?)
  raise('expecting a memberships status') unless status.nil? || status.class.respond_to?(:effective_memberships_status?)
  raise('owner has existing membership. use reclassify! instead.') if owner.membership.present?

  # Default Date and next number
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Build a membership
  membership = owner.build_membership

  # Assign Dates
  membership.joined_on ||= date  # Only if not already present
  membership.registration_on = date  # Always new registration_on

  # Assign Number (this could be nil)
  number = next_membership_number(owner, to: to) if number.nil?

  if number.present?
    membership.number = number
    membership.number_as_integer = (Integer(number) rescue nil)
  end

  # Assign Category
  membership.build_membership_category(category: to)

  # Assign Status
  membership.build_membership_status(status: status) if status.present?

  # Assign fees paid through period
  if skip_fees
    membership.fees_paid_period = period
    membership.fees_paid_through_period = period_end_on
  end

  # Or, Build Fees
  unless skip_fees
    fees = build_register_fees(owner: owner, membership: membership, date: date)
    raise('unexpected purchased fee') if Array(fees).compact.any?(&:purchased?)
  end

  # Assign member role
  add_member_role(owner)

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#reinstate!(owner, to: nil, date: nil, skip_fees: false) ⇒ Object



216
217
218
219
220
221
222
223
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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 216

def reinstate!(owner, to: nil, date: nil, skip_fees: false)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  history = owner.reinstatement_membership_history || owner.membership_histories.last
  raise('owner must have a removed membership history or some kind of history.') if history.blank?

  to ||= owner.reinstatement_membership_category
  raise('expecting a to memberships category') unless to.class.respond_to?(:effective_memberships_category?)

  statuses = Array(owner.reinstatement_membership_statuses)

  # Default Date
  date ||= Time.zone.now
  period = period(date: date)
  period_end_on = period_end_on(date: date)

  # Build a membership
  membership = owner.build_membership

  # Assign Dates
  membership.joined_on = date
  membership.registration_on = date

  # Assign Number (this could be nil)
  if history.number.present?
    membership.number = history.number
    membership.number_as_integer = (Integer(history.number) rescue nil)
  end

  # Assign Category
  membership.build_membership_category(category: to)

  # Assign Statuses
  statuses.each do |status|
    membership.build_membership_status(status: status)
  end

  # Assign fees paid through period
  if skip_fees
    membership.fees_paid_period = period
    membership.fees_paid_through_period = period_end_on
  end

  # Or, Build Fees
  unless skip_fees
    build_reinstate_fees(owner: owner, membership: membership, date: date)
    # This might already be present and purchased if joined and reinstated in the same period
  end

  # Assign member role
  add_member_role(owner)

  # Save owner
  owner.build_membership_history(start_on: date)
  owner.save!
end

#remove!(owner, statuses: nil, date: nil, notes: nil) ⇒ Object

When you remove, you can set a status(es) which may affect reinstatement applications



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'app/models/concerns/effective_memberships_registrar.rb', line 331

def remove!(owner, statuses: nil, date: nil, notes: nil)
  statuses = Array(statuses)

  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member with a membership') unless owner.membership.present?
  raise('expecting an membership status (optional)') if statuses.present? && statuses.any? { |status| !status.class.respond_to?(:effective_memberships_status?) }

  # Date
  date ||= Time.zone.now

  # Delete unpurchased fees and orders
  owner.outstanding_fee_payment_fees.each { |fee| fee.mark_for_destruction }
  owner.outstanding_fee_payment_orders.each { |order| order.mark_for_destruction }

  # Remove member role
  remove_member_role(owner)

  # Remove Membership
  owner.membership.mark_for_destruction

  # We do the save a bit differently here because we want to set the removed statuses
  history = owner.build_membership_history(start_on: date, notes: notes)

  if statuses.present?
    history.assign_attributes(statuses: statuses.map(&:to_s), status_ids: statuses.map(&:id))
  end

  owner.save!
end

#renewal_fee_date(date:) ⇒ Object



21
22
23
24
# File 'app/models/concerns/effective_memberships_registrar.rb', line 21

def renewal_fee_date(date:)
  Time.zone.local(date.year, 12, 1) # Fees roll over every December 1st
  raise('to be implemented by app registrar')
end

#status_add!(owner, status:, date: nil, notes: nil) ⇒ Object



284
285
286
287
288
289
# File 'app/models/concerns/effective_memberships_registrar.rb', line 284

def status_add!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, notes: notes, status: owner.membership.statuses + Array(status))
end

#status_assign!(owner, status:, date: nil, notes: nil) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/models/concerns/effective_memberships_registrar.rb', line 298

def status_assign!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)

  membership = owner.membership
  raise('owner must have an existing membership. use register! instead') if membership.blank?

  statuses = Array(status)
  raise('expected a memberships status') unless statuses.blank? || statuses.all? { |status| status.class.respond_to?(:effective_memberships_status?) }

  # Add
  statuses.each do |status|
    next if membership.membership_status(status: status).present?
    membership.build_membership_status(status: status)
  end

  # Remove
  membership.statuses.each do |existing|
    next if statuses.include?(existing)
    membership.membership_status(status: existing).mark_for_destruction
  end

  changed = membership.membership_statuses.any? { |ms| ms.new_record? || ms.marked_for_destruction? }

  if changed
    # Save owner
    owner.build_membership_history(start_on: date, notes: notes)
    owner.save!
  end

  true
end

#status_clear!(owner, date: nil) ⇒ Object



277
278
279
280
281
282
# File 'app/models/concerns/effective_memberships_registrar.rb', line 277

def status_clear!(owner, date: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, status: [])
end

#status_remove!(owner, status:, date: nil, notes: nil) ⇒ Object



291
292
293
294
295
296
# File 'app/models/concerns/effective_memberships_registrar.rb', line 291

def status_remove!(owner, status:, date: nil, notes: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('owner must have an existing membership. use register! instead') if owner.membership.blank?

  status_assign!(owner, date: date, notes: notes, status: owner.membership.statuses - Array(status))
end