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

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



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

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 46

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)

  # Delete any removed statuses
  membership.membership_statuses.each do |membership_status|
    next if statuses.include?(membership_status.status)
    membership_status.mark_for_destruction
  end

  # Build any additional statuses
  statuses.each do |status|
    membership.build_membership_status(status: status)
  end

  # Delete any removed categories
  membership.membership_categories.each do |membership_category|
    next if categories.include?(membership_category.category)
    membership_category.mark_for_destruction
  end

  # Build any additional categories
  categories.each do |category|
    membership.build_membership_category(category: category)
  end

  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
    save!(owner, date: date)
  end

  # Assign member role
  add_member_role(owner)

  owner.update_membership_status!
end

#bad_standing!(owner, reason:, date: nil) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/models/concerns/effective_memberships_registrar.rb', line 284

def bad_standing!(owner, reason:, date: 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') if owner.membership.bad_standing?

  # Date
  date ||= Time.zone.now
  membership = owner.membership

  membership.bad_standing = true
  membership.bad_standing_admin = true
  membership.bad_standing_reason = reason

  save!(owner, date: date)
end

#bad_standing_date(period:) ⇒ Object



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

def bad_standing_date(period:)
  Date.new(period.year, 3, 1) # Membership in bad standing after March 1st
  raise('to be implemented by app registrar')
end

#create_bad_standing!(membership, period:) ⇒ Object



436
437
438
439
# File 'app/models/concerns/effective_memberships_registrar.rb', line 436

def create_bad_standing!(membership, period:)
  return unless membership.categories.any?(&:create_bad_standing?)
  update_membership_status!(membership, period: period)
end

#create_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



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/models/concerns/effective_memberships_registrar.rb', line 391

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

  # Create Renewal Fees
  memberships.find_each { |membership| create_renewal_fees!(membership, period: period) }
  GC.start

  # Create Late Fees
  memberships.find_each { |membership| create_late_fees!(membership, period: period) }
  GC.start

  # Update Membership Status - Assign In Bad Standing
  memberships.find_each { |membership| create_bad_standing!(membership, period: period) }
  GC.start

  true
end

#create_late_fees!(membership, period:) ⇒ Object



425
426
427
428
429
430
431
432
433
434
# File 'app/models/concerns/effective_memberships_registrar.rb', line 425

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.purchased?

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

    fee.save!
  end
end

#create_renewal_fees!(membership, period:) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'app/models/concerns/effective_memberships_registrar.rb', line 410

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.purchased?

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

    fee.save!
  end
end

#current_periodObject



352
353
354
# File 'app/models/concerns/effective_memberships_registrar.rb', line 352

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

#fee_payment_purchased!(owner) ⇒ Object

Called in the after_purchase of fee payment



446
447
448
449
# File 'app/models/concerns/effective_memberships_registrar.rb', line 446

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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/concerns/effective_memberships_registrar.rb', line 316

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

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



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'app/models/concerns/effective_memberships_registrar.rb', line 300

def good_standing!(owner, date: 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 bad standing') unless owner.membership.bad_standing?

  # Date
  date ||= Time.zone.now
  membership = owner.membership

  membership.bad_standing = false
  membership.bad_standing_admin = false
  membership.bad_standing_reason = nil

  save!(owner, date: date)
end

#last_periodObject



356
357
358
# File 'app/models/concerns/effective_memberships_registrar.rb', line 356

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:)
  Date.new(period.year, 2, 1) # Fees are late after February 1st
  raise('to be implemented by app registrar')
end

#next_membership_number(owner, to:) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'app/models/concerns/effective_memberships_registrar.rb', line 337

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

#period(date:) ⇒ Object

Returns a date of Jan 1, Year



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

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



41
42
43
# File 'app/models/concerns/effective_memberships_registrar.rb', line 41

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/models/concerns/effective_memberships_registrar.rb', line 368

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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/concerns/effective_memberships_registrar.rb', line 154

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

  membership = owner.membership

  # Assign Category
  membership.registration_on = date

  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
    fee = owner.build_prorated_fee(date: date)
    raise('already has purchased prorated fee') if fee.purchased?

    fee = owner.build_discount_fee(date: date, from: from)
    raise('already has purchased discount fee') if fee.purchased?
  end

  save!(owner, date: date)
end

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



106
107
108
109
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
# File 'app/models/concerns/effective_memberships_registrar.rb', line 106

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.blank?
  membership.number = number
  membership.number_as_integer = (Integer(number) rescue nil)

  # 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
    fee = owner.build_prorated_fee(date: date)
    raise('already has purchased prorated fee') if fee.purchased?
  end

  # Assign member role
  add_member_role(owner)

  # Save owner
  save!(owner, date: date)
end

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/concerns/effective_memberships_registrar.rb', line 189

def reinstate!(owner, from:, 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?

  raise('expecting a from') if from.blank?
  raise('expected a from status or category') unless from.class.respond_to?(:effective_memberships_status?) || from.class.respond_to?(:effective_memberships_category?)

  date ||= Time.zone.now

  membership = owner.membership
  membership.membership_status(status: from).mark_for_destruction if from.class.respond_to?(:effective_memberships_status?)
  membership.membership_category(category: from).mark_for_destruction if from.class.respond_to?(:effective_memberships_category?)

  unless skip_fees
    fee = owner.build_prorated_fee(date: date)
    # This might already be present and purchased if joined and reinstated in the same period
  end

  save!(owner, date: date)
end

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



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'app/models/concerns/effective_memberships_registrar.rb', line 264

def remove!(owner, date: nil)
  raise('expecting a memberships owner') unless owner.class.respond_to?(:effective_memberships_owner?)
  raise('expected a member with a membership') unless owner.membership.present?

  # Date
  date ||= Time.zone.now

  # Remove Membership
  owner.membership.mark_for_destruction

  # 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)

  save!(owner, date: date)
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:)
  Date.new(date.year, 12, 1) # Fees roll over every December 1st
  raise('to be implemented by app registrar')
end

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



217
218
219
220
221
222
# File 'app/models/concerns/effective_memberships_registrar.rb', line 217

def status_add!(owner, status:, 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: owner.membership.statuses + Array(status))
end

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



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

def status_assign!(owner, status:, date: 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
    date ||= Time.zone.now

    membership.registration_on = date # Always new registration_on
    save!(owner, date: date)
  end

  true
end

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



210
211
212
213
214
215
# File 'app/models/concerns/effective_memberships_registrar.rb', line 210

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) ⇒ Object



224
225
226
227
228
229
# File 'app/models/concerns/effective_memberships_registrar.rb', line 224

def status_remove!(owner, status:, 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: owner.membership.statuses - Array(status))
end

#update_membership_status!(membership, period: nil) ⇒ Object



441
442
443
# File 'app/models/concerns/effective_memberships_registrar.rb', line 441

def update_membership_status!(membership, period: nil)
  membership.owner.update_membership_status!
end