-
-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4513 incontinence supplies #4794
base: main
Are you sure you want to change the base?
Changes from 4 commits
1d93dcd
7ec4f2d
607bec6
ffa5b51
fb68000
28aa472
062d5a6
203730e
a8816a0
ac49fe5
fca526d
a3b1155
ac542f8
8a61604
a0270f9
c2f90ad
1fac9db
b55672e
e2c41a6
15c7c84
91b7f16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,9 @@ def initialize(year:, organization:) | |
def report | ||
@report ||= { name: 'Adult Incontinence', | ||
entries: { | ||
'Adult incontinence supplies distributed' => number_with_delimiter(distributed_supplies), | ||
'Adult incontinence supplies per adult per month' => monthly_supplies&.round || 0, | ||
'Adult incontinence supplies distributed' => number_with_delimiter(distributed_loose_supplies + distributed_adult_incontinence_items_from_kits), | ||
'Adults Assisted Per Month' => adults_served_per_month.round, | ||
'Adult incontinence supplies per adult per month' => (monthly_supplies&.round || 0) / (adults_served_per_month.round.nonzero? || 1), | ||
'Adult incontinence supplies' => types_of_supplies, | ||
'% adult incontinence supplies donated' => "#{percent_donated.round}%", | ||
'% adult incontinence bought' => "#{percent_bought.round}%", | ||
|
@@ -24,7 +25,7 @@ def report | |
end | ||
|
||
# @return [Integer] | ||
def distributed_supplies | ||
def distributed_loose_supplies | ||
@distributed_supplies ||= organization | ||
.distributions | ||
.for_year(year) | ||
|
@@ -47,6 +48,7 @@ def monthly_supplies | |
end | ||
|
||
def types_of_supplies | ||
# require 'pry'; binding.pry | ||
organization.items.adult_incontinence.map(&:name).uniq.sort.join(', ') | ||
end | ||
|
||
|
@@ -91,5 +93,54 @@ def donated_supplies | |
.where(itemizable: organization.donations.for_year(year)) | ||
.sum(:quantity) | ||
end | ||
|
||
def distributed_adult_incontinence_items_from_kits | ||
organization_id = @organization.id | ||
year = @year | ||
|
||
sql_query = <<-SQL | ||
SELECT SUM(line_items.quantity * kit_line_items.quantity) | ||
FROM distributions | ||
INNER JOIN line_items ON line_items.itemizable_type = 'Distribution' AND line_items.itemizable_id = distributions.id | ||
INNER JOIN items ON items.id = line_items.item_id | ||
INNER JOIN kits ON kits.id = items.kit_id | ||
INNER JOIN line_items AS kit_line_items ON kits.id = kit_line_items.itemizable_id | ||
INNER JOIN items AS kit_items ON kit_items.id = kit_line_items.item_id | ||
INNER JOIN base_items ON base_items.partner_key = kit_items.partner_key | ||
WHERE distributions.organization_id = ? | ||
AND EXTRACT(year FROM issued_at) = ? | ||
AND LOWER(base_items.category) LIKE '%adult%' | ||
AND NOT (LOWER(base_items.category) LIKE '%cloth%' OR LOWER(base_items.name) LIKE '%cloth%') | ||
AND kit_line_items.itemizable_type = 'Kit'; | ||
SQL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be excluding cloth (Adult Cloth Diapers count as AI) and should be excluding wipes. See Item.adult_incontinence. |
||
|
||
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql_query, organization_id, year]) | ||
|
||
result = ActiveRecord::Base.connection.execute(sanitized_sql) | ||
|
||
result.first['sum'].to_i | ||
end | ||
|
||
def adults_served_per_month | ||
total_people_served_with_loose_supplies_per_month + total_people_served_with_supplies_from_kits_per_month | ||
end | ||
|
||
def total_people_served_with_loose_supplies_per_month | ||
organization | ||
.distributions | ||
.for_year(year) | ||
.joins(line_items: :item) | ||
.merge(Item.adult_incontinence) | ||
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 50)') / 12 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is giving you an integer for each item/distribution combo - I found that it was significantly undercounting with our seed data. Changing 50 to 50.0 gave me the value I expected in my case where I had added just enough to get to 600 AI items. But this was in a case where they were all blank distribution quantities -- I suspect you need to make sure items.distribution_quantity reads as double as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside - we may want to talk to the stakeholders about fractional people per month - because at small volumes, it's going to change the supplies per month depending on whether we do or not. We should, in any case, have how we handle it make sense to them. |
||
end | ||
|
||
def total_people_served_with_supplies_from_kits_per_month | ||
organization | ||
.distributions.for_year(year) | ||
.joins(line_items: {item: :kit}) | ||
.merge(Item.adult_incontinence) | ||
.where.not(items: {kit_id: nil}) | ||
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 1)') / 12 | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the issue, " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick question, also I apologize for missing that bit! Is it a safe assumption that the distribution_quantity is equal to the "quantity per individual"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Sorry for the terminology ambiguity -- they are the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this is giving you kits that are identified as adult_incontinence, rather than kits that have adult incontinence items in them? Though you used the same pattern as for the disposables... Huh. |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this not total_supplies_distributed?