Created
July 16, 2020 14:22
-
-
Save NGMarmaduke/6ac59b48b30a12f1ba9e1b9e22ac70f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PayRaiseReport | |
HEADER = %w{ | |
user_id | |
first_name | |
current_state | |
live_match_count | |
live_matches_with_custom_pay | |
immediate_pay_raise | |
new_rate | |
months_to_next_raise | |
hourly_rate_after_next_raise | |
share_count | |
unique_pay_amounts | |
cumulative_months_experience | |
max_continuous_months_experience | |
}.freeze | |
def self.perform(nannies: default_nannies) | |
CSV.open('pay_raises.csv', 'w') do |csv| | |
csv << HEADER | |
nannies.each do |nanny| | |
next if nanny.user.team_member? | |
csv << new(nanny).csv_row | |
end | |
end | |
end | |
def self.default_nannies | |
state = ShareProfile.nannies.afterschool.in_state('looking', 'matching', 'matched') | |
live_match = ShareProfile.nannies.afterschool.joins(:match_requests).merge(MatchRequest.live) | |
state | live_match | |
end | |
attr_reader :nanny | |
def initialize(nanny) | |
@nanny = nanny | |
end | |
def csv_row | |
[ | |
nanny.user_id, | |
nanny.first_name, | |
nanny.email, | |
nanny.current_state, | |
live_match_count, | |
live_matches_with_custom_pay, | |
immediate_pay_raise, | |
new_rate, | |
months_to_next_raise, | |
hourly_rate_after_next_raise, | |
share_count, | |
all_rates, | |
experience.cumulative_months, | |
experience.max_continuous_months | |
] | |
end | |
private | |
def all_rates | |
live_pay_rates.map(&:pence).uniq.join(', ') | |
end | |
def live_matches | |
@live_matches ||= nanny.match_requests.started.where('end_on IS NULL OR end_on > :now', now: Date.current).not_failed_contract | |
end | |
def live_match_count | |
@live_match_count ||= live_matches.size | |
end | |
def live_matches_with_custom_pay | |
live_pay_rates.count(&:custom?) | |
end | |
def live_pay_rates | |
live_matches.flat_map { |match| match.agreements.where(share: false).map(&:pay_rate) }.compact | |
end | |
def pay_rate | |
@pay_rate ||= live_pay_rates.find { |rate| !rate.custom? } || live_pay_rates.last | |
end | |
def new_rate | |
return estimated_rate.pence if live_match_count.zero? | |
pay_rate&.pence || 0 | |
end | |
def months_to_next_raise | |
return 0 if immediate_pay_raise == 100 | |
months_exp = experience.cumulative_months | |
if months_exp >= 6 | |
12 - months_exp | |
else | |
6 - months_exp | |
end | |
end | |
def hourly_rate_after_next_raise | |
return new_rate if immediate_pay_raise == 100 || pay_rate&.custom? | |
new_rate + 50 | |
end | |
def immediate_pay_raise | |
return pay_raise_for(estimated_rate) if live_match_count.zero? | |
return pay_raise_for(pay_rate) if pay_rate.present? | |
0 | |
end | |
def estimated_rate | |
@estimated_rate ||= AfterSchool::NannyRateEstimate.new(nanny: nanny).sole_pay_rate | |
end | |
def experience | |
@experience ||= AfterSchool::InternalExperience.new(nanny: nanny) | |
end | |
def pay_raise_for(rate) | |
rate.pay_rate_components.sum { |component| | |
if component.key.in?(%w{ six_months_internal_experience school_year_internal_experience }) | |
component.pence | |
else | |
0 | |
end | |
} | |
end | |
def share_count | |
live_matches.count { |match| match.agreements.any?(&:share?) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment