Skip to content

Commit

Permalink
FIX SQL queries in AcquisitionReportService and children_served_repor…
Browse files Browse the repository at this point in the history
…t_service

* Fix bug with not using updated Item.Disposable scope because code is duplicated
  (added comment that this needs to be merged later)
* Rewrite ChildrenServedReportService for item itemizable changes
  (added comment that this code is almost exact duplicate of AcquisitionReportService)
  • Loading branch information
jimmyli97 committed Aug 15, 2024
1 parent e764763 commit 4286e69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 3 additions & 2 deletions app/services/reports/acquisition_report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ def distributed_disposable_diapers_from_kits
WHERE distributions.organization_id = ?
AND EXTRACT(year FROM issued_at) = ?
AND LOWER(base_items_in_kit.category) LIKE '%diaper%'
AND NOT (LOWER(base_items_in_kit.category) LIKE '%cloth%' OR LOWER(base_items_in_kit.name) LIKE '%cloth%')
AND NOT (((LOWER(base_items_in_kit.category) LIKE '%cloth%') OR (LOWER(base_items_in_kit.name) LIKE '%cloth%')))
AND NOT (LOWER(base_items_in_kit.category) LIKE '%adult%')
SQL
# TODO duplicated code from Item.disposable scope, and ChildrenServedReportService. merge when working on #3652

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
Expand Down
33 changes: 24 additions & 9 deletions app/services/reports/children_served_report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,33 @@ def total_children_served_with_loose_disposables
.for_year(year)
.joins(line_items: :item)
.merge(Item.disposable)
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 50)')
.sum('line_items.quantity / COALESCE(items.distribution_quantity, 50)') # if item.default_quantity changes, this should change too
end

def children_served_with_kits_containing_disposables
organization
.distributions
.for_year(year)
.joins(line_items: {item: :kit})
.merge(Item.disposable)
.where.not(items: {kit_id: nil})
.distinct
.count("kits.id")
organization_id = @organization.id
year = @year

# SUM assumes 1 person per kit, this will be changed by #4542
# if item.default_quantity changes, COALESCE(items_in_kit.distribution_quantity, 50) should change too
sql_query = <<-SQL
SELECT SUM((line_items.quantity * kit_line_items.quantity) / CAST(COALESCE(items_in_kit.distribution_quantity, 50) AS DECIMAL))
FROM distributions
INNER JOIN line_items ON line_items.itemizable_type = 'Distribution' AND line_items.itemizable_id = distributions.id
INNER JOIN items AS items_housing_a_kit ON items_housing_a_kit.id = line_items.item_id AND items_housing_a_kit.kit_id IS NOT NULL
INNER JOIN line_items AS kit_line_items ON kit_line_items.itemizable_id = items_housing_a_kit.id AND kit_line_items.itemizable_type = 'Item'
INNER JOIN items AS items_in_kit ON items_in_kit.id = kit_line_items.item_id
INNER JOIN base_items AS base_items_in_kit ON base_items_in_kit.partner_key = items_in_kit.partner_key
WHERE distributions.organization_id = ?
AND EXTRACT(year FROM issued_at) = ?
AND LOWER(base_items_in_kit.category) LIKE '%diaper%'
AND NOT (((LOWER(base_items_in_kit.category) LIKE '%cloth%') OR (LOWER(base_items_in_kit.name) LIKE '%cloth%')))
AND NOT (LOWER(base_items_in_kit.category) LIKE '%adult%')
SQL
# TODO duplicated code from Item.disposable scope, and AcquisitionReportService. merge when working on #3652
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql_query, organization_id, year])
result = ActiveRecord::Base.connection.execute(sanitized_sql)
result.first['sum'].to_f.ceil
end
end
end

0 comments on commit 4286e69

Please sign in to comment.