Skip to content

Commit

Permalink
CST-2575: allow payment-frozen participants to be transferred back to…
Browse files Browse the repository at this point in the history
… their original cohort
  • Loading branch information
ltello committed May 25, 2024
1 parent 5452885 commit 4d687dd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 169 deletions.
58 changes: 35 additions & 23 deletions app/services/induction/amend_participant_cohort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# source_cohort_start_year: 2021,
# schedule:).save
#
# - For cohort changes on participants whose current cohort has been payments_frozen and are moved to
# - For cohort changes on participants whose current cohort has been payments_frozen and are transferred to
# the active registration cohort, it will automatically flag the participant_profile as
# cohort_changed_after_payments_frozen: true
#
Expand Down Expand Up @@ -63,23 +63,22 @@ class AmendParticipantCohort
},
on: :start

validate :target_cohort_payments_active

validates :participant_profile,
presence: true,
on: :start

validate :target_cohort_start_year_matches_schedule

validates :participant_profile,
active_participant_profile: true,
unfinished_training_participant_profile: true
active_participant_profile: true

validate :eligible_to_be_moved_after_payments_frozen, if: :cohort_changed_after_payments_frozen
validate :target_cohort_payments_active, unless: :back_to_payments_frozen_cohort?
validate :transfer_from_payments_frozen_cohort, if: :transfer_from_payments_frozen_cohort?
validate :transfer_to_payments_frozen_cohort, if: :back_to_payments_frozen_cohort?

validates :participant_declarations,
absence: { message: :billable_or_submitted },
unless: :cohort_changed_after_payments_frozen
unless: :payments_frozen_transfer?

validates :induction_record,
presence: {
Expand All @@ -98,10 +97,6 @@ class AmendParticipantCohort

delegate :school, to: :induction_record, allow_nil: true

def cohort_changed_after_payments_frozen
source_cohort&.payments_frozen? && target_cohort == Cohort.active_registration_cohort
end

def save
return false unless valid?(:start)
return true if in_target?(induction_record)
Expand All @@ -116,6 +111,14 @@ def initialize(*)
@target_cohort_start_year = (@target_cohort_start_year || @schedule&.cohort_start_year).to_i
end

def back_to_payments_frozen_cohort?
participant_profile&.cohort_changed_after_payments_frozen? && target_cohort&.payments_frozen?
end

def billable_declarations_in_cohort?(cohort)
participant_profile.participant_declarations.where(cohort:).billable.exists?
end

def current_induction_record_updated?
ActiveRecord::Base.transaction do
Induction::ChangeInductionRecord.call(induction_record:,
Expand Down Expand Up @@ -166,14 +169,7 @@ def participant_declarations
return false unless participant_profile
return @participant_declarations if instance_variable_defined?(:@participant_declarations)

@participant_declarations = if cohort_changed_after_payments_frozen

else
participant_profile
.participant_declarations
.billable_or_changeable
.exists?
end
@participant_declarations = participant_profile.participant_declarations.billable_or_changeable.exists?
end

def schedule
Expand All @@ -198,15 +194,31 @@ def target_school_cohort
@target_school_cohort ||= SchoolCohort.find_by(school:, cohort: target_cohort)
end

def payments_frozen_transfer?
transfer_from_payments_frozen_cohort? || back_to_payments_frozen_cohort?
end

def transfer_from_payments_frozen_cohort?
source_cohort&.payments_frozen? && target_cohort == Cohort.active_registration_cohort
end

alias_method :cohort_changed_after_payments_frozen, :transfer_from_payments_frozen_cohort?

# Validations
def eligible_to_be_moved_after_payments_frozen

def target_cohort_payments_active
errors.add(:target_cohort_start_year, :payments_frozen) if target_cohort&.payments_frozen?
end

def transfer_from_payments_frozen_cohort
unless participant_profile.eligible_to_change_cohort_and_continue_training?
errors.add(:participant_profile, :participant_not_eligible_to_be_moved)
errors.add(:participant_profile, :not_eligible_to_be_transferred_from_current_cohort)
end
end

def target_cohort_payments_active
errors.add(:target_cohort_start_year, :payments_frozen) if target_cohort&.payments_frozen?
def transfer_to_payments_frozen_cohort
errors.add(:participant_profile, :billable_declarations_in_cohort) if billable_declarations_in_cohort?(source_cohort)
errors.add(:participant_profile, :no_billable_declarations_in_cohort) unless billable_declarations_in_cohort?(target_cohort)
end

def target_cohort_start_year_matches_schedule
Expand Down

This file was deleted.

6 changes: 4 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,15 @@ en:
no_default_induction_programme: "No default induction programme set for %{start_academic_year} academic year by school %{school_name}"
school_cohort_not_setup: "%{start_academic_year} academic year not setup by school %{school_name}"
participant_declarations:
exist: "The participant must have no declarations"
billable_or_submitted: "The participant has billable or submitted declarations"
completed: "The participant has not had a 'completed' declaration submitted for them. Therefore you cannot update their outcome."
participant_profile:
billable_declarations_in_cohort: "The participant has billable declarations in their current cohort"
no_billable_declarations_in_cohort: "The participant has no billable declarations in destination cohort"
blank: "Not registered"
not_active: "Not active"
not_eligible_to_be_moved: "Not eligible to be moved from their current cohort"
not_eligible_to_be_transferred_back: "Not eligible to be transferred back to their original cohort"
not_eligible_to_be_transferred_from_current_cohort: "Not eligible to be transferred from their current cohort"
source_cohort_start_year:
invalid: "Invalid value. Must be an integer between %{start} and %{end}"
target_cohort_start_year:
Expand Down
Loading

0 comments on commit 4d687dd

Please sign in to comment.