-
-
Notifications
You must be signed in to change notification settings - Fork 509
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 14 commits
1d93dcd
7ec4f2d
607bec6
ffa5b51
fb68000
28aa472
062d5a6
203730e
a8816a0
ac49fe5
fca526d
a3b1155
ac542f8
8a61604
a0270f9
c2f90ad
1fac9db
b55672e
e2c41a6
15c7c84
91b7f16
2b17110
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' => supplies_per_adult_per_month.round, | ||
'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) | ||
|
@@ -34,16 +35,16 @@ def distributed_supplies | |
end | ||
|
||
# @return [Integer] | ||
def total_supplies_distributed | ||
distributed_loose_supplies + distributed_adult_incontinence_items_from_kits | ||
end | ||
|
||
def monthly_supplies | ||
# NOTE: This is asking "per adult per month" but there doesn't seem to be much difference | ||
# in calculating per month or per any other time frame, since all it's really asking | ||
# is the value of the `distribution_quantity` field for the items we're giving out. | ||
organization | ||
.distributions | ||
.for_year(year) | ||
.joins(line_items: :item) | ||
.merge(Item.adult_incontinence) | ||
.average('COALESCE(items.distribution_quantity, 50)') | ||
total_supplies_distributed / 12.0 | ||
end | ||
|
||
def supplies_per_adult_per_month | ||
monthly_supplies / (adults_served_per_month.nonzero? || 1) | ||
end | ||
|
||
def types_of_supplies | ||
|
@@ -91,5 +92,57 @@ 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 | ||
total_quantity = organization | ||
.distributions | ||
.for_year(year) | ||
.joins(line_items: :item) | ||
.merge(Item.adult_incontinence) | ||
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 50)') | ||
total_quantity / 12.0 | ||
end | ||
|
||
def total_people_served_with_supplies_from_kits_per_month | ||
total_quantity = organization | ||
.distributions | ||
.for_year(year) | ||
.joins(line_items: {item: :kit}) | ||
.where.not(items: {kit_id: nil}) | ||
.merge(Item.adult_incontinence) | ||
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 1)') | ||
total_quantity / 12.0 | ||
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?