Module: EffectiveMembershipsOwner
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/effective_memberships_owner.rb
Overview
EffectiveMembershipsOwner
Mark your owner model with effective_memberships_owner to get all the includes
Defined Under Namespace
Modules: Base, ClassMethods
Instance Method Summary collapse
- #after_build_fee(fee) ⇒ Object
- #assign_member_role ⇒ Object
- #build_discount_fee(from:, date: nil) ⇒ Object
- #build_early_fee(category:, period:) ⇒ Object
- #build_late_fee(category:, period:) ⇒ Object
- #build_membership_history(start_on: nil, notes: nil) ⇒ Object
-
#build_prorated_fee(date: nil) ⇒ Object
Instance Methods.
- #build_renewal_fee(category:, period:, late_on: nil, not_in_good_standing_on: nil) ⇒ Object
-
#build_title_fee(title:, fee_type:, period:, price: nil, tax_exempt: nil, qb_item_name: nil, category: nil) ⇒ Object
Only thing optional is category, late_on and not_in_good_standing_on.
- #first_membership_history(category: nil) ⇒ Object
- #last_membership_history(category: nil) ⇒ Object
- #max_fees_paid_period ⇒ Object
- #max_fees_paid_through_period ⇒ Object
- #membership_category_start_on(category: :current) ⇒ Object
- #membership_fees_paid? ⇒ Boolean
- #membership_history_continuous? ⇒ Boolean
-
#membership_history_errors ⇒ Object
Point out busted data.
- #membership_history_on(date) ⇒ Object
-
#membership_period_fee(category:, period:, except: nil) ⇒ Object
These should be singular fees anyway.
- #membership_removed? ⇒ Boolean
- #membership_removed_on ⇒ Object
- #not_in_good_standing_fees ⇒ Object
- #outstanding_applicant_submit_fees ⇒ Object
- #outstanding_fee_payment_fees ⇒ Object
- #outstanding_fee_payment_orders ⇒ Object
- #outstanding_fee_payments ⇒ Object
- #outstanding_renewal_fees ⇒ Object
-
#reclassification_expires_on(from: nil, to: nil) ⇒ Object
Checked by the applicant on the select step.
- #registrar_action_categories(action) ⇒ Object
- #registrar_action_statuses(action) ⇒ Object
- #reinstatement_expires_on(from: nil) ⇒ Object
- #reinstatement_membership_category ⇒ Object
-
#reinstatement_membership_history ⇒ Object
The membership history we would reinstate to.
- #reinstatement_membership_statuses ⇒ Object
-
#removed_membership_history ⇒ Object
The membership history that is removed.
-
#rollback_membership_history! ⇒ Object
Delete the last membership history.
-
#update_member_role! ⇒ Object
This can be called by a script to recalculate the owner role based on current membership.
-
#update_membership_status! ⇒ Object
Called by the registrar.
Instance Method Details
#after_build_fee(fee) ⇒ Object
204 205 206 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 204 def after_build_fee(fee) # Nothing to do. Hook to adjust fee. end |
#assign_member_role ⇒ Object
80 81 82 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 80 def assign_member_role membership_present? ? add_role(:member) : remove_role(:member) end |
#build_discount_fee(from:, date: nil) ⇒ Object
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 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 234 def build_discount_fee(from:, date: nil) raise('must have an existing membership') unless membership.present? raise('existing membership category may not be same as from') if membership.category == from date ||= Time.zone.now price = from.discount_fee(date: date) period = EffectiveMemberships.Registrar.period(date: date) category = membership.category fee = fees.find { |fee| fee.fee_type == 'Discount' && fee.period == period && fee.category == category } || fees.build() return fee if fee.purchased? fee.assign_attributes( fee_type: 'Discount', category: category, from_category: from, period: period, price: price, tax_exempt: from.tax_exempt, qb_item_name: from.qb_item_name ) after_build_fee(fee) fee end |
#build_early_fee(category:, period:) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 345 def build_early_fee(category:, period:) raise('must have an existing membership') unless membership.present? # Return existing but do not build yet fee = fees.find { |fee| fee.fee_type == 'Early' && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name } return fee if fee&.purchased? # Only continue if there is an early renewal fee for the same period renewal_fee = fees.find { |fee| fee.fee_type == 'Renewal' && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name } return if fee.blank? && (renewal_fee.blank? || renewal_fee.early? == false) # Build the early fee fee ||= fees.build() fee.assign_attributes( fee_type: 'Early', category: category, period: period, price: category.early_fee.to_i, tax_exempt: category.tax_exempt, qb_item_name: category.qb_item_name ) after_build_fee(fee) fee end |
#build_late_fee(category:, period:) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 373 def build_late_fee(category:, period:) raise('must have an existing membership') unless membership.present? # Return existing but do not build yet fee = fees.find { |fee| fee.fee_type == 'Late' && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name } return fee if fee&.purchased? # Only continue if there is a late renewal fee for the same period renewal_fee = fees.find { |fee| fee.fee_type == 'Renewal' && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name } return if fee.blank? && (renewal_fee.blank? || renewal_fee.late? == false) # Build the late fee fee ||= fees.build() fee.assign_attributes( fee_type: 'Late', category: category, period: period, price: category.late_fee.to_i, tax_exempt: category.tax_exempt, qb_item_name: category.qb_item_name ) after_build_fee(fee) fee end |
#build_membership_history(start_on: nil, notes: nil) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 443 def build_membership_history(start_on: nil, notes: nil) raise('expected membership to be present') unless membership.present? # The date of change start_on ||= Time.zone.now # End the other membership histories membership_histories.each { |history| history.end_on ||= start_on } # Snapshot of the current membership at this time history = membership_histories.build( start_on: start_on, end_on: nil, removed: membership.marked_for_destruction?, notes: notes ) unless history.removed? history.assign_attributes( number: membership.number, categories: membership.categories.map(&:to_s), category_ids: membership.categories.map(&:id), statuses: membership.statuses.map(&:to_s), status_ids: membership.statuses.map(&:id) ) end history end |
#build_prorated_fee(date: nil) ⇒ Object
Instance Methods
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 209 def build_prorated_fee(date: nil) raise('must have an existing membership') unless membership.present? date ||= Time.zone.now price = membership.category.prorated_fee(date: date) period = EffectiveMemberships.Registrar.period(date: date) category = membership.category fee = fees.find { |fee| fee.fee_type == 'Prorated' && fee.period == period && fee.category == category } || fees.build() return fee if fee.purchased? fee.assign_attributes( fee_type: 'Prorated', category: category, period: period, price: price, tax_exempt: category.tax_exempt, qb_item_name: category.qb_item_name ) after_build_fee(fee) fee end |
#build_renewal_fee(category:, period:, late_on: nil, not_in_good_standing_on: nil) ⇒ Object
294 295 296 297 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 294 def build_renewal_fee(category:, period:, late_on: nil, not_in_good_standing_on: nil) raise('must have an existing membership') unless membership.present? # Sanity check. # If there's already a purchased or unpurchased Prorated (or other membership period advancing fee) in this period # We shouldn't be building renewal fees for the same period a prorated fee is purchased in prorated = membership_period_fee(category: category, period: period, except: 'Renewal') raise('must not have an existing membership_period (prorated) fee in this period') if prorated.present? fee = fees.find { |fee| fee.fee_type == 'Renewal' && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name } return fee if fee&.purchased? # Build the renewal fee fee ||= fees.build() # Late on this period's late date, or 60 days, whichever is later late_on ||= if category.create_late_fees? late_date = EffectiveMemberships.Registrar.late_fee_date(period: period) duration = EffectiveMemberships.Registrar.min_late_duration min_date = ((fee.created_at || Time.zone.now) + duration) if duration.present? [late_date, min_date].compact.max end # NIGS on this period's late date, or 60 days, whichever is later not_in_good_standing_on ||= if category.create_not_in_good_standing? nigs_date = EffectiveMemberships.Registrar.not_in_good_standing_date(period: period) duration = EffectiveMemberships.Registrar.min_not_in_good_standing_duration min_date = ((fee.created_at || Time.zone.now) + duration) if duration.present? [nigs_date, min_date].compact.max end fee.assign_attributes( fee_type: 'Renewal', category: category, period: period, price: category.renewal_fee.to_i, tax_exempt: category.tax_exempt, qb_item_name: category.qb_item_name, late_on: late_on, not_in_good_standing_on: not_in_good_standing_on, ) after_build_fee(fee) fee end |
#build_title_fee(title:, fee_type:, period:, price: nil, tax_exempt: nil, qb_item_name: nil, category: nil) ⇒ Object
Only thing optional is category, late_on and not_in_good_standing_on
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 262 def build_title_fee(title:, fee_type:, period:, price: nil, tax_exempt: nil, qb_item_name: nil, category: nil) fee = fees.find do |fee| fee.fee_type == fee_type && fee.period == period && fee.title == title && (category.blank? || fee.category_id == category.id && fee.category_type == category.class.name) end return fee if fee&.purchased? # Build the title fee fee ||= fees.build() fee.assign_attributes( title: title, fee_type: fee_type, category: category, period: period, price: price, tax_exempt: tax_exempt, qb_item_name: qb_item_name ) after_build_fee(fee) fee end |
#first_membership_history(category: nil) ⇒ Object
485 486 487 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 485 def first_membership_history(category: nil) membership_histories.find { |history| category.blank? || history.membership_category_ids.include?(category.id) } end |
#last_membership_history(category: nil) ⇒ Object
489 490 491 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 489 def last_membership_history(category: nil) membership_histories.reverse.find { |history| category.blank? || history.membership_category_ids.include?(category.id) } end |
#max_fees_paid_period ⇒ Object
123 124 125 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 123 def max_fees_paid_period fees.select { |fee| fee.membership_period_fee? && fee.purchased? }.map(&:period).max end |
#max_fees_paid_through_period ⇒ Object
127 128 129 130 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 127 def max_fees_paid_through_period return nil if max_fees_paid_period.blank? EffectiveMemberships.Registrar.period_end_on(date: max_fees_paid_period) end |
#membership_category_start_on(category: :current) ⇒ Object
473 474 475 476 477 478 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 473 def membership_category_start_on(category: :current) category = membership&.categories&.first if category == :current raise('expected a category') unless category.present? membership_histories.find { |history| history.membership_category_ids.include?(category.id) }&.start_on || membership&.joined_on end |
#membership_fees_paid? ⇒ Boolean
90 91 92 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 90 def membership_fees_paid? outstanding_fee_payment_fees.blank? && membership && membership.fees_paid? end |
#membership_history_continuous? ⇒ Boolean
531 532 533 534 535 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 531 def membership_history_continuous? membership_histories.all? do |history| history.end_on.blank? || (history.end_on.present? && history.removed?) || membership_histories.find { |h| h.start_on == history.end_on }.present? end end |
#membership_history_errors ⇒ Object
Point out busted data
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 494 def membership_history_errors return unless membership.present? return unless membership_histories.present? errors = [] history = membership_histories.first last_history = membership_histories.last was_removed = membership_histories.any? { |history| history.removed? } # Check membership joined on date matches first history start date if membership.joined_on != history.start_on && !was_removed errors << "The joined date #{membership.joined_on.strftime('%F')} does not match the first history start date of #{history.start_on.strftime('%F')}. Please change the first history start date to #{membership.joined_on.strftime('%F')} or update the joined date above." end # Check that there is a membership history row if the registered date is unique if membership.joined_on != membership.registration_on && membership_histories.none? { |mh| mh.start_on == membership.registration_on } errors << "The last changed date #{membership.registration_on.strftime('%F')} is missing a history with this date. Please create a history with a start date of #{membership.registration_on.strftime('%F')} or update the last changed date above." end # Check numbers if membership.number.present? && membership_histories.none? { |history| history.number == membership.number } errors << "The membership number ##{membership.number} is missing a history with this number. Please create a history with the #{membership.number} number or update the membership number above." end # Check that the last history does not have an end_on date if last_history.end_on.present? && !last_history.removed? errors << "The most recent history must have a blank end date. Please remove the end date of the most recent history entry or create another history." elsif membership_histories.any? { |history| history.end_on.blank? && history != last_history } errors << "The end date must be present for all past histories. Please add the end date to all histories, except the most recent history." elsif !membership_history_continuous? errors << "The start and end dates are overlapping or non-continuous. Please make sure each history start date has a matching history end date" end errors end |
#membership_history_on(date) ⇒ Object
480 481 482 483 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 480 def membership_history_on(date) raise('expected a date') unless date.respond_to?(:strftime) membership_histories.find { |history| (history.start_on..history.end_on).cover?(date) } # Ruby 2.6 supports endless ranges end |
#membership_period_fee(category:, period:, except: nil) ⇒ Object
These should be singular fees anyway.
289 290 291 292 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 289 def membership_period_fee(category:, period:, except: nil) raise('expected except to be a string like Renewal') if except.present? && !except.kind_of?(String) fees.find { |fee| fee.membership_period_fee? && fee.period == period && fee.category_id == category.id && fee.category_type == category.class.name && (except.blank? || fee.fee_type != except) } end |
#membership_removed? ⇒ Boolean
132 133 134 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 132 def membership_removed? membership.blank? && membership_histories.any? { |history| history.removed? } end |
#membership_removed_on ⇒ Object
136 137 138 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 136 def membership_removed_on removed_membership_history.start_on if membership_removed? end |
#not_in_good_standing_fees ⇒ Object
119 120 121 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 119 def not_in_good_standing_fees fees.select { |fee| fee.not_in_good_standing? } end |
#outstanding_applicant_submit_fees ⇒ Object
94 95 96 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 94 def outstanding_applicant_submit_fees fees.select { |fee| fee.applicant_submit_fee? && !fee.purchased? } end |
#outstanding_fee_payment_fees ⇒ Object
98 99 100 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 98 def outstanding_fee_payment_fees fees.select { |fee| fee.fee_payment_fee? && !fee.purchased? } end |
#outstanding_fee_payment_orders ⇒ Object
115 116 117 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 115 def outstanding_fee_payment_orders orders.select { |order| order.parent_type.to_s.include?('FeePayment') && !order.purchased? } end |
#outstanding_fee_payments ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 106 def outstanding_fee_payments fee_payments = EffectiveMemberships.FeePayment.all fee_payments = fee_payments.where(user: self) if self.class.respond_to?(:effective_memberships_user?) fee_payments = fee_payments.where(organization: self) if self.class.respond_to?(:effective_memberships_organization?) fee_payments.select { |fee_payment| fee_payment.in_progress? && fee_payment.orders.none?(&:purchased?) } end |
#outstanding_renewal_fees ⇒ Object
102 103 104 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 102 def outstanding_renewal_fees fees.select { |fee| fee.fee_type == 'Renewal' && !fee.purchased? } end |
#reclassification_expires_on(from: nil, to: nil) ⇒ Object
Checked by the applicant on the select step
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 141 def reclassification_expires_on(from: nil, to: nil) return unless membership.present? # User/owner might respond to this date = try(:admin_reclassification_expires_on) return date if date.present? # When we last reclassified to our current category history = last_membership_history(category: membership.membership_categories.first) reclassified_on = (history&.start_on || membership.registration_on || membership.joined_on) # Otherwise consider the membership categories expires_on = membership.categories.select { |category| category.reclassification_expires_in.present? }.map do |category| reclassified_on + category.reclassification_expires_in end.min expires_on end |
#registrar_action_categories(action) ⇒ Object
196 197 198 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 196 def registrar_action_categories(action) EffectiveMemberships.Category.sorted.all end |
#registrar_action_statuses(action) ⇒ Object
200 201 202 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 200 def registrar_action_statuses(action) EffectiveMemberships.Status.sorted.all.where.not(id: EffectiveMemberships.Registrar.not_in_good_standing_status) end |
#reinstatement_expires_on(from: nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 160 def reinstatement_expires_on(from: nil) return unless membership_removed? # User/owner might respond to this date = try(:admin_reinstatement_expires_on) return date if date.present? # When we were removed removed_on = removed_membership_history.start_on # Consider any membership statuses that have reinstatement expires in expires_on = removed_membership_history.membership_statuses.select { |status| status.reinstatement_expires_in.present? }.map do |status| removed_on + status.reinstatement_expires_in end.min expires_on end |
#reinstatement_membership_category ⇒ Object
188 189 190 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 188 def reinstatement_membership_category reinstatement_membership_history&.membership_categories&.first end |
#reinstatement_membership_history ⇒ Object
The membership history we would reinstate to. Should be the one just before removed.
184 185 186 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 184 def reinstatement_membership_history membership_histories.reverse.find { |history| !history.removed? } if membership_removed? end |
#reinstatement_membership_statuses ⇒ Object
192 193 194 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 192 def reinstatement_membership_statuses reinstatement_membership_history&.membership_statuses end |
#removed_membership_history ⇒ Object
The membership history that is removed. Has exit statuses that may affect reinstatement fees.
179 180 181 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 179 def removed_membership_history membership_histories.reverse.find { |history| history.removed? } if membership_removed? end |
#rollback_membership_history! ⇒ Object
Delete the last membership history
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 425 def rollback_membership_history! raise('expected membership to be present') unless membership.present? (history, last) = membership_histories.last(2) raise('expected 2 or more membership histories') unless history.present? && last.present? last.mark_for_destruction history.assign_attributes(end_on: nil) membership.assign_attributes( number: history.number, categories: history.membership_categories, statuses: history.membership_statuses ) save! end |
#update_member_role! ⇒ Object
This can be called by a script to recalculate the owner role based on current membership
85 86 87 88 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 85 def update_member_role! assign_member_role save! end |
#update_membership_status! ⇒ Object
Called by the registrar.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'app/models/concerns/effective_memberships_owner.rb', line 402 def update_membership_status! raise('expected membership to be present') unless membership.present? # Assign fees paid through period membership.fees_paid_period = max_fees_paid_period() membership.fees_paid_through_period = max_fees_paid_through_period() # Add or remove Not In Good Standing status in_good_standing = membership.in_good_standing? not_in_good_standing = membership.not_in_good_standing? if in_good_standing && not_in_good_standing_fees.present? EffectiveMemberships.Registrar.not_in_good_standing!(self) elsif not_in_good_standing && not_in_good_standing_fees.blank? EffectiveMemberships.Registrar.in_good_standing!(self) end build_membership_history() if membership_histories.blank? save! end |