-
-
Notifications
You must be signed in to change notification settings - Fork 537
4283 period supplies #4582
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
Merged
Merged
4283 period supplies #4582
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2b945e8
Add distributed_period_supplies_from_kits method modified from aquesi…
jadekstewart3 73e757c
Begin test set up for period supplies in kits
jadekstewart3 0e70618
Modify test set up to include distributions containing period supply …
jadekstewart3 392b434
Fix kit creation in the test set up so it returns the correct integer
jadekstewart3 65c47a1
Modify normal value report to report total period supplies including …
jadekstewart3 9b3c42c
Complete metric: Period supplies per adult per month
jadekstewart3 deed965
Create dontated items from kits method. Still need testing for this m…
jadekstewart3 2d64b4c
Create test data and testing for calculating the number of period sup…
jadekstewart3 b413e36
Add purchased_kits method and add a simple test for the method. Needs…
jadekstewart3 16e2da4
Fix method counting purchased kits
jadekstewart3 ca9831d
Added a method to calculate the number of purchased period supplies w…
jadekstewart3 7d0ae93
Fix calculations for period supply report, and modify test to reflect…
jadekstewart3 038e4cf
add the :with_items back in on creation of organization
jadekstewart3 e3240ed
Merge branch 'main' into 4238_period_supplies
jadekstewart3 bf472aa
Rubocop
jadekstewart3 46b2fcf
Merge branch '4238_period_supplies' of github.com:jadekstewart3/human…
jadekstewart3 87ac6af
Fix errors within spec
jadekstewart3 11ce2f7
Rubocop
jadekstewart3 daa09b0
Added specificity to kit_items_calculation by ensuring diaper and clo…
jadekstewart3 9528c08
Added another distribution with a total of 200 items to see if expect…
jadekstewart3 3cf19e1
Fix spelling error
jadekstewart3 552bbfc
Modify period supplies per adult per month calculations to ensure onl…
jadekstewart3 fc10668
Modify expect statement to align with new calculations
jadekstewart3 5417ec2
Remove methods and test set up to calculate per person per month
jadekstewart3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ def initialize(year:, organization:) | |
def report | ||
@report ||= {name: "Period Supplies", | ||
entries: { | ||
"Period supplies distributed" => number_with_delimiter(distributed_supplies), | ||
"Period supplies per adult per month" => monthly_supplies&.round || 0, | ||
"Period supplies distributed" => number_with_delimiter(total_distributed_period_supplies), | ||
"Period supplies per adult per month" => (monthly_supplies || 0 + distributed_period_supplies_from_kits_per_month)&.round, | ||
"Period supplies" => types_of_supplies, | ||
"% period supplies donated" => "#{percent_donated.round}%", | ||
"% period supplies bought" => "#{percent_bought.round}%", | ||
|
@@ -24,7 +24,7 @@ def report | |
end | ||
|
||
# @return [Integer] | ||
def distributed_supplies | ||
def distributed_loose_period_supplies | ||
@distributed_supplies ||= organization | ||
.distributions | ||
.for_year(year) | ||
|
@@ -33,6 +33,18 @@ def distributed_supplies | |
.sum("line_items.quantity") | ||
end | ||
|
||
def distributed_period_supplies_from_kits | ||
kit_items_calculation("distributions", "Distribution") | ||
end | ||
|
||
def distributed_period_supplies_from_kits_per_month | ||
distributed_period_supplies_from_kits / 12 | ||
end | ||
|
||
def total_distributed_period_supplies | ||
distributed_loose_period_supplies + distributed_period_supplies_from_kits | ||
end | ||
|
||
# @return [Integer] | ||
def monthly_supplies | ||
# NOTE: This is asking "per adult per month" but there doesn't seem to be much difference | ||
|
@@ -54,14 +66,14 @@ def types_of_supplies | |
def percent_donated | ||
return 0.0 if total_supplies.zero? | ||
|
||
(donated_supplies / total_supplies.to_f) * 100 | ||
(total_donated_supplies / total_supplies.to_f) * 100 | ||
end | ||
|
||
# @return [Float] | ||
def percent_bought | ||
return 0.0 if total_supplies.zero? | ||
|
||
(purchased_supplies / total_supplies.to_f) * 100 | ||
(total_purchased_supplies / total_supplies.to_f) * 100 | ||
end | ||
|
||
# @return [String] | ||
|
@@ -72,24 +84,67 @@ def money_spent_on_supplies | |
###### HELPER METHODS ###### | ||
|
||
# @return [Integer] | ||
def purchased_supplies | ||
def total_purchased_supplies | ||
@purchased_supplies ||= LineItem.joins(:item) | ||
.merge(Item.period_supplies) | ||
.where(itemizable: organization.purchases.for_year(year)) | ||
.sum(:quantity) | ||
|
||
@purchased_supplies + purchased_supplies_from_kits | ||
end | ||
|
||
def purchased_supplies_from_kits | ||
kit_items_calculation("purchases", "Purchase") | ||
end | ||
|
||
# @return [Integer] | ||
def total_supplies | ||
@total_supplies ||= purchased_supplies + donated_supplies | ||
@total_supplies ||= total_purchased_supplies + total_donated_supplies | ||
end | ||
|
||
# @return [Integer] | ||
def donated_supplies | ||
@donated_supplies ||= LineItem.joins(:item) | ||
def total_donated_supplies | ||
loose_donated_supplies = LineItem.joins(:item) | ||
.merge(Item.period_supplies) | ||
.where(itemizable: organization.donations.for_year(year)) | ||
.sum(:quantity) | ||
|
||
loose_donated_supplies + donated_supplies_from_kits | ||
end | ||
|
||
def donated_supplies_from_kits | ||
kit_items_calculation("donations", "Donation") | ||
end | ||
|
||
private | ||
|
||
def kit_items_calculation(itemizable_type, string_itemizable_type) | ||
organization_id = @organization.id | ||
year = @year | ||
|
||
# Sanitize and validate inputs | ||
itemizable_type = ActiveRecord::Base.connection.quote_table_name(itemizable_type) | ||
string_itemizable_type = ActiveRecord::Base.connection.quote(string_itemizable_type) | ||
|
||
sql_query = <<-SQL | ||
SELECT SUM(line_items.quantity * kit_line_items.quantity) | ||
FROM #{itemizable_type} | ||
INNER JOIN line_items ON line_items.itemizable_type = #{string_itemizable_type} AND line_items.itemizable_id = #{itemizable_type}.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 #{itemizable_type}.organization_id = ? | ||
AND EXTRACT(year FROM issued_at) = ? | ||
AND LOWER(base_items.category) LIKE '%menstrual supplies%' | ||
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. You've got two spaces instead of one between menstrual and supplies. I checked that out on local, and it makes my initial test work for the Period supplies distributed. |
||
AND NOT (LOWER(base_items.category) LIKE '%diaper%' OR LOWER(base_items.name) LIKE '%cloth%') | ||
AND kit_line_items.itemizable_type = 'Kit'; | ||
SQL | ||
|
||
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 | ||
end | ||
end |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is just going to give you monthly_supplies.round, if you have any. I'm not sure why we had the "|| 0" in the original -- maybe it's nil if there's no period supplies at all ? We should probably check out that case for distributed_period_supplies_from_kits_per_month too.