Skip to content

Commit

Permalink
Make calculate_amount a private method
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Nov 12, 2024
1 parent 6b417b6 commit fafc857
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/controllers/pfmps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def new
def edit; end

def create
@pfmp = Pfmp.new(pfmp_params.merge(schooling: @schooling))
@pfmp = Pfmp.new

if PfmpManager.new(@pfmp).update(pfmp_params)
if PfmpManager.new(@pfmp).update(pfmp_params.merge(schooling: @schooling))
redirect_to student_path(@schooling.student),
notice: t("pfmps.new.success")
else
Expand Down
42 changes: 22 additions & 20 deletions app/services/pfmp_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def update(params)
end

def update!(params)
params = params.to_h.with_indifferent_access

Pfmp.transaction do
pfmp.update!(params)
transition!
recalculate_amounts! if params[:day_count].present?
transition!
end
end

Expand All @@ -47,23 +49,14 @@ def retry_incomplete_payment_request!

def rectify_and_update_attributes!(confirmed_pfmp_params, confirmed_address_params)
Pfmp.transaction do
PfmpManager.new(pfmp).update!(confirmed_pfmp_params)
update!(confirmed_pfmp_params)
pfmp.student.update!(confirmed_address_params)
pfmp.rectify!
end
end

def calculate_amount
return 0 if pfmp.day_count.nil?

[
pfmp.day_count * pfmp.wage.daily_rate,
pfmp.wage.yearly_cap - previously_locked_amount
].min
end

def previously_locked_amount
other_priced_pfmps
def previously_locked_amount(pfmp)
other_priced_pfmps(pfmp)
.map(&:amount)
.compact
.sum
Expand All @@ -78,31 +71,40 @@ def retry_payment_request!(reasons)

private

def calculate_amount(target_pfmp)
return 0 if target_pfmp.day_count.nil?

[
target_pfmp.day_count * target_pfmp.wage.daily_rate,
target_pfmp.wage.yearly_cap - previously_locked_amount(target_pfmp)
].min
end

def recalculate_amounts!
raise PfmpNotModifiableError unless pfmp.can_be_modified?

pfmp.all_pfmps_for_mef.lock!
pfmp.update!(amount: calculate_amount)
pfmp.update!(amount: calculate_amount(pfmp))
rebalance_other_pfmps!
end

def other_pfmps_for_mef
pfmp.all_pfmps_for_mef.excluding(pfmp)
def other_pfmps_for_mef(excluded_pfmp)
pfmp.all_pfmps_for_mef.excluding(excluded_pfmp)
end

def other_priced_pfmps
other_pfmps_for_mef
def other_priced_pfmps(pfmp)
other_pfmps_for_mef(pfmp)
.where.not(amount: nil)
end

def rebalancable_pfmps
@rebalancable_pfmps ||= other_pfmps_for_mef
@rebalancable_pfmps ||= other_pfmps_for_mef(pfmp)
.select(&:can_be_rebalanced?)
end

def rebalance_other_pfmps!
rebalancable_pfmps.each do |rebalancable_pfmp|
rebalancable_pfmp.update!(amount: PfmpManager.new(rebalancable_pfmp).calculate_amount)
rebalancable_pfmp.update!(amount: calculate_amount(rebalancable_pfmp))
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/pfmps/_payment_panel.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
= number_to_currency pfmp.wage.yearly_cap
de plafond maximum par an
%div.fr-mb-1w
= number_to_currency PfmpManager.new(pfmp).previously_locked_amount
= number_to_currency PfmpManager.new(pfmp).previously_locked_amount(pfmp)
déjà engagés par d'autres PFMPs sur cette scolarité.
4 changes: 2 additions & 2 deletions spec/services/pfmp_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
end

describe "#calculate_amount" do # rubocop:disable RSpec/MultipleMemoizedHelpers
subject(:amount) { described_class.new(pfmp.reload).calculate_amount }
subject(:amount) { described_class.new(pfmp.reload).send(:calculate_amount, pfmp) }

let(:establishment) { create(:establishment) }
let(:pfmp) do
Expand Down Expand Up @@ -202,7 +202,7 @@
end

def other_pfmps_for_mef
manager.send(:other_pfmps_for_mef)
manager.send(:other_pfmps_for_mef, pfmp)
end

context "when there is no other pfmp for that school year and mef" do
Expand Down

0 comments on commit fafc857

Please sign in to comment.