Skip to content

Commit

Permalink
Merge branch 'md/usage-report' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
tvcam committed Aug 24, 2023
2 parents 638e70b + e2b4c8d commit ff9f440
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions app/services/usage_report_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cross_referral_from_primero_cases
adult_male_with_disability: clients_has_disability_from_primero.count(&:adult_male?),
child_female_with_disability: clients_has_disability_from_primero.count(&:child_female?),
child_male_with_disability: clients_has_disability_from_primero.count(&:child_female?),
provinces: []
provinces: Province.joins(:clients).where(clients: { id: clients_from_primero.map(&:id) }).uniq.pluck(:name).compact
}

data[:adult_female_without_disability] = data[:adult_female] - data[:adult_female_with_disability]
Expand All @@ -65,22 +65,22 @@ def clients_has_disability_from_primero
end

def clients_from_primero
@clients_from_primero ||= Client.reportable.where(synced_date: date_range).to_a
@clients_from_primero ||= Client.reportable.joins(:referrals).where(referrals: { created_at: date_range, referred_from: 'MoSVY External System' }).to_a.uniq
end

def cross_referral_to_primero_cases
data = {
total: clients_from_primero.count,
adult_female: clients_from_primero.count(&:adult_female?),
adult_male: clients_from_primero.count(&:adult_male?),
child_female: clients_from_primero.count(&:child_female?),
child_male: clients_from_primero.count(&:child_male?),
total_with_disability: clients_has_disability_from_primero.count,
adult_female_with_disability: clients_has_disability_from_primero.count(&:adult_female?),
adult_male_with_disability: clients_has_disability_from_primero.count(&:adult_male?),
child_female_with_disability: clients_has_disability_from_primero.count(&:child_female?),
child_male_with_disability: clients_has_disability_from_primero.count(&:child_female?),
provinces: []
adult_female: clients_to_primero.count(&:adult_female?),
adult_male: clients_to_primero.count(&:adult_male?),
child_female: clients_to_primero.count(&:child_female?),
child_male: clients_to_primero.count(&:child_male?),
total_with_disability: clients_has_disability_to_primero.count,
adult_female_with_disability: clients_has_disability_to_primero.count(&:adult_female?),
adult_male_with_disability: clients_has_disability_to_primero.count(&:adult_male?),
child_female_with_disability: clients_has_disability_to_primero.count(&:child_female?),
child_male_with_disability: clients_has_disability_to_primero.count(&:child_female?),
provinces: Province.joins(:clients).where(clients: { id: clients_to_primero.map(&:id) }).uniq.pluck(:name).compact
}

data[:adult_female_without_disability] = data[:adult_female] - data[:adult_female_with_disability]
Expand All @@ -90,6 +90,14 @@ def cross_referral_to_primero_cases
data
end

def clients_has_disability_to_primero
@clients_has_disability_to_primero ||= Client.joins(:risk_assessment).where(risk_assessments: { has_disability: true }, clients: { id: clients_to_primero.map(&:id) }).to_a.uniq
end

def clients_to_primero
@clients_to_primero ||= Client.reportable.joins(:referrals).where(referrals: { created_at: date_range, referred_to: 'MoSVY External System' }).to_a.uniq
end

def cross_referral_cases
data = {
total: cross_referral_clients.count,
Expand Down Expand Up @@ -121,23 +129,30 @@ def cross_referral_clients
end

def cross_referral_agencies
Referral.where(created_at: date_range, referred_from: organization.short_name).where("referred_to != ?", 'MoSVY External System').pluck(:referred_to).uniq
Referral.where(created_at: date_range, referred_from: organization.short_name).where("referred_to != ?", 'MoSVY External System').map { |referral| referral.ngo_name.presence || ngo_hash_mapping[referral.referred_to] }.join(', ')
end

def ngo_hash_mapping
ngos = Organization.pluck(:short_name, :full_name)
ngos << ["MoSVY External System", "MoSVY External System"]
ngos << ["external referral", "I don't see the NGO I'm looking for..."]
ngos.to_h
end

def synced_cases
data = {
signed_up_date: nil,
current_sharing: nil,
total: clients.count,
adult_female: clients.count(&:adult_female?),
adult_male: clients.count(&:adult_male?),
child_female: clients.count(&:child_female?),
child_male: clients.count(&:child_male?),
total_with_disability: clients_has_disability.count,
adult_female_with_disability: clients_has_disability.count(&:adult_female?),
adult_male_with_disability: clients_has_disability.count(&:adult_male?),
child_female_with_disability: clients_has_disability.count(&:child_female?),
child_male_with_disability: clients_has_disability.count(&:child_female?)
signed_up_date: organization.integrated_date,
current_sharing: organization.integrated?,
total: synced_clients.count,
adult_female: synced_clients.count(&:adult_female?),
adult_male: synced_clients.count(&:adult_male?),
child_female: synced_clients.count(&:child_female?),
child_male: synced_clients.count(&:child_male?),
total_with_disability: synced_clients_with_disability.count,
adult_female_with_disability: synced_clients_with_disability.count(&:adult_female?),
adult_male_with_disability: synced_clients_with_disability.count(&:adult_male?),
child_female_with_disability: synced_clients_with_disability.count(&:child_female?),
child_male_with_disability: synced_clients_with_disability.count(&:child_female?)
}

data[:adult_female_without_disability] = data[:adult_female] - data[:adult_female_with_disability]
Expand All @@ -149,6 +164,14 @@ def synced_cases
data
end

def synced_clients_with_disability
@synced_clients_with_disability ||= Client.joins(:risk_assessment).where(risk_assessments: { has_disability: true }, clients: { id: synced_clients.map(&:id) }).to_a.uniq
end

def synced_clients
@synced_clients ||= Client.reportable.where.not(external_id: [nil, '']).where(created_at: date_range).to_a
end

def added_cases
data = {
total: clients.count,
Expand Down

0 comments on commit ff9f440

Please sign in to comment.