Skip to content

Commit 2493ab0

Browse files
Merge pull request #5541 from cielf/add-filters-to-distribution-by-county
Add filters to distribution by county and take into account kitted items
2 parents 3099fc2 + 9ed19a1 commit 2493ab0

16 files changed

Lines changed: 482 additions & 122 deletions

‎app/controllers/distributions_by_county_controller.rb‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ class DistributionsByCountyController < ApplicationController
44

55
def report
66
setup_date_range_picker
7-
start_date = helpers.selected_range.first.utc.iso8601
8-
end_date = helpers.selected_range.last.utc.iso8601
97

10-
@breakdown = DistributionSummaryByCountyQuery.call(
11-
organization_id: current_organization.id,
12-
start_date: start_date,
13-
end_date: end_date
14-
)
8+
@dbc_info = View::DistributionsByCounty.from_params(params: params,
9+
organization: current_organization, helpers: helpers)
1510
end
1611
end

‎app/models/item.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ class Item < ApplicationRecord
5959

6060
scope :visible, -> { where(visible_to_partners: true) }
6161
scope :alphabetized, -> { order(:name) }
62+
scope :loose, -> { where(type: "ConcreteItem") }
6263
scope :by_base_item, ->(base_item) { where(base_item: base_item) }
6364
scope :by_reporting_category, ->(reporting_category) { where(reporting_category: reporting_category) }
65+
scope :by_name, ->(name) { where(name: name) }
6466
scope :by_partner_key, ->(partner_key) { where(partner_key: partner_key) }
6567

6668
scope :period_supplies, -> {
@@ -76,7 +78,7 @@ class Item < ApplicationRecord
7678
period_other: "period_other",
7779
period_underwear: "period_underwear",
7880
tampons: "tampons",
79-
other_categories: "other"
81+
other: "other"
8082
}, instance_methods: false, validate: { allow_nil: true }
8183

8284
def self.reporting_categories_for_select
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module View
2+
DistributionsByCounty = Data.define(
3+
:breakdown,
4+
:filters,
5+
:items,
6+
:reporting_categories
7+
) do
8+
class << self
9+
def filter_params(params)
10+
return {} unless params.key?(:filters)
11+
params
12+
.require(:filters)
13+
.permit(:by_item_id, :by_reporting_category, :date_range)
14+
end
15+
16+
def from_params(params:, organization:, helpers:)
17+
filters = filter_params(params)
18+
start_date = helpers.selected_range.first.utc.iso8601
19+
end_date = helpers.selected_range.last.utc.iso8601
20+
breakdown = DistributionSummaryByCountyQuery.call(
21+
organization_id: organization.id,
22+
start_date: start_date,
23+
end_date: end_date,
24+
reporting_category: filters[:by_reporting_category].presence,
25+
item_id: filters[:by_item_id].presence
26+
)
27+
28+
new(
29+
breakdown: breakdown,
30+
filters: filters,
31+
reporting_categories: Item.reporting_categories_for_select,
32+
items: organization.items.loose.alphabetized.select(:id, :name)
33+
)
34+
end
35+
end
36+
def selected_reporting_category
37+
filters[:by_reporting_category].presence
38+
end
39+
40+
def selected_item
41+
filters[:by_item_id].presence
42+
end
43+
end
44+
end

‎app/queries/distribution_summary_by_county_query.rb‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class DistributionSummaryByCountyQuery
55
SQL_MULTILINE_COMMENTS = /\/\*.*?\*\//
66

77
DISTRIBUTION_BY_COUNTY_SQL = <<~SQL.squish.gsub(SQL_MULTILINE_COMMENTS, "").freeze
8-
/* Calculate total item quantity and value per distribution */
9-
WITH distribution_totals AS
8+
/* Calculate total item quantity and value per distribution of "loose" items */
9+
10+
WITH loose_distribution_totals AS
1011
(
1112
SELECT DISTINCT d.id,
1213
d.partner_id,
@@ -17,9 +18,39 @@ class DistributionSummaryByCountyQuery
1718
JOIN items i ON i.id = li.item_id
1819
WHERE d.issued_at BETWEEN :start_date AND :end_date
1920
AND d.organization_id = :organization_id
21+
AND i.reporting_category LIKE CONCAT('%', :reporting_category , '%')
22+
AND i.id = CASE WHEN :item_id <> 0 THEN :item_id ELSE i.id END
2023
GROUP BY d.id, li.id, i.id
2124
),
22-
/* Match distribution totals with client share and counties.
25+
/* Calculate total item and value per distribution of items that happen to be in kits */
26+
kitted_distribution_totals AS (
27+
SELECT DISTINCT d.id,
28+
d.partner_id,
29+
COALESCE(SUM(li.quantity * kli.quantity) OVER (PARTITION BY d.id), 0) AS quantity,
30+
COALESCE(SUM(COALESCE(ki.value_in_cents, 0) * li.quantity * kli.quantity) OVER (PARTITION BY d.id), 0) AS value
31+
FROM distributions d
32+
INNER JOIN line_items li ON li.itemizable_id = d.id AND li.itemizable_type = 'Distribution'
33+
INNER JOIN items i ON i.id = li.item_id
34+
INNER JOIN line_items AS kli ON i.id = kli.itemizable_id AND kli.itemizable_type = 'Item'
35+
INNER JOIN items AS ki ON ki.id = kli.item_id
36+
WHERE d.issued_at BETWEEN :start_date AND :end_date
37+
AND d.organization_id = :organization_id
38+
AND ki.reporting_category LIKE CONCAT('%', :reporting_category , '%')
39+
AND ki.id = CASE WHEN :item_id <> 0 THEN :item_id ELSE ki.id END
40+
GROUP BY d.id, li.id, i.id, kli.id, ki.id
41+
),
42+
43+
/* Combine the loose and kitted */
44+
full_distribution_totals as (
45+
SELECT distinct COALESCE(ld.id,kd.id) as id,
46+
COALESCE(ld.partner_id, kd.partner_id) AS partner_id,
47+
COALESCE(ld.quantity,0) + COALESCE(kd.quantity, 0) as quantity,
48+
COALESCE(ld.value,0) + COALESCE(kd.value, 0) as value
49+
FROM loose_distribution_totals ld
50+
FULL OUTER JOIN kitted_distribution_totals kd ON ld.id = kd.id
51+
),
52+
53+
/* Match full distribution totals with client share and counties.
2354
If distribution has no associated county, set county name to "Unspecified"
2455
and set region to ZZZ so it will be last when sorted */
2556
totals_by_county AS
@@ -30,7 +61,7 @@ class DistributionSummaryByCountyQuery
3061
COALESCE(psa.client_share::float / 100, 1) AS percentage,
3162
COALESCE(c.name, 'Unspecified') county_name,
3263
COALESCE(c.region, 'ZZZ') county_region
33-
FROM distribution_totals dt
64+
FROM full_distribution_totals dt
3465
LEFT JOIN partners p ON p.id = dt.partner_id
3566
LEFT JOIN partner_profiles pp ON pp.partner_id = p.id
3667
LEFT JOIN partner_served_areas psa ON psa.partner_profile_id = pp.id
@@ -58,11 +89,13 @@ class DistributionSummaryByCountyQuery
5889
class << self
5990
# Timestamps are stored in Postgres without timezones so
6091
# start_date and end_date must be strings in UTC.
61-
def call(organization_id:, start_date: nil, end_date: nil)
92+
def call(organization_id:, start_date: nil, end_date: nil, reporting_category: nil, item_id: nil)
6293
params = {
6394
organization_id: organization_id,
6495
start_date: start_date || "1000-01-01",
65-
end_date: end_date || "3000-01-01"
96+
end_date: end_date || "3000-01-01",
97+
reporting_category: reporting_category,
98+
item_id: item_id
6699
}
67100

68101
execute(to_sql(DISTRIBUTION_BY_COUNTY_SQL, **params)).to_a.map(&to_county_summary)
@@ -74,13 +107,15 @@ def execute(sql)
74107
ActiveRecord::Base.connection.execute(sql)
75108
end
76109

77-
def to_sql(query, organization_id:, start_date:, end_date:)
110+
def to_sql(query, organization_id:, start_date:, end_date:, reporting_category:, item_id:)
78111
ActiveRecord::Base.sanitize_sql_array(
79112
[
80113
query,
81114
organization_id: organization_id,
82115
start_date: start_date,
83-
end_date: end_date
116+
end_date: end_date,
117+
reporting_category: reporting_category,
118+
item_id: item_id
84119
]
85120
)
86121
end

‎app/services/reports/other_products_report_service.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def distributed_products
2929
.distributions
3030
.for_year(year)
3131
.joins(line_items: :item)
32-
.merge(Item.other_categories)
32+
.merge(Item.other)
3333
.sum('line_items.quantity')
3434
end
3535

@@ -54,13 +54,13 @@ def money_spent
5454

5555
# @return [String]
5656
def product_list
57-
organization.items.other_categories.map(&:name).sort.uniq.join(', ')
57+
organization.items.other.map(&:name).sort.uniq.join(', ')
5858
end
5959

6060
# @return [Integer]
6161
def purchased_products
6262
@purchased_products ||= LineItem.joins(:item)
63-
.merge(Item.other_categories)
63+
.merge(Item.other)
6464
.where(itemizable: organization.purchases.for_year(year))
6565
.sum(:quantity)
6666
end
@@ -73,7 +73,7 @@ def total_products
7373
# @return [Integer]
7474
def donated_products
7575
@donated_products ||= LineItem.joins(:item)
76-
.merge(Item.other_categories)
76+
.merge(Item.other)
7777
.where(itemizable: organization.donations.for_year(year))
7878
.sum(:quantity)
7979
end

‎app/views/distributions_by_county/report.html.erb‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<h1>
77
Estimated Distributions by County for <%= current_organization.name %>
88
</h1>
9+
<i>
10+
Please note that these estimates include any items that are within kits, but do not include the kits themselves. These are estimates only -- the totals may differ slightly from other distribution reports due to rounding when dividing them between the counties.
11+
</i>
912
</div>
1013
<div class="col-sm-6">
1114
<ol class="breadcrumb float-sm-right">
@@ -25,17 +28,33 @@
2528
<div class="row">
2629
<div class="col-12">
2730
<!-- Default box -->
28-
<div class="card">
29-
<div class="row">
30-
<div class="col-sm-3 col-6">
31+
<div class="card card-primary">
32+
<div class="card-body">
33+
3134
<div class="description-block border-right">
32-
<%= simple_form_for :filters, url: distributions_by_county_report_path(current_organization), method: :get do |f| %>
33-
<%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %><br>
34-
<%= filter_button %>
35+
<%= form_tag(distributions_by_county_report_path(current_organization), method: :get) do |f| %>
36+
<div class="row">
37+
<% if @dbc_info.items.present? %>
38+
<div class="form-group col-lg-2 col-md-2 col-sm-6 col-xs-12">
39+
<%= filter_select(label: "Item", scope: :by_item_id, collection: @dbc_info.items, selected: @dbc_info.selected_item) %>
40+
</div>
41+
<% end %>
42+
<div class="form-group col-lg-2 col-md-2 col-sm-6 col-xs-12">
43+
<%= filter_select(label: "Reporting Category", scope: :by_reporting_category, collection: @dbc_info.reporting_categories, selected: @dbc_info.selected_reporting_category) %>
44+
</div>
45+
46+
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
47+
<%= label_tag "Date Range", "Date Range" %>
48+
<%= render partial: "shared/date_range_picker", locals: {css_class: "form-control"} %>
49+
</div>
50+
</div>
51+
<div class="card-footer">
52+
<%= filter_button %>
53+
<%= clear_filter_button %>
54+
</div>
3555
<% end %>
36-
</div>
3756
<!-- /.description-block -->
38-
</div>
57+
3958
</div>
4059

4160
<div class="card-body table-responsive p-0">
@@ -48,7 +67,7 @@
4867
</tr>
4968
</thead>
5069
<tbody>
51-
<% @breakdown.each do |bd| %>
70+
<% @dbc_info.breakdown.each do |bd| %>
5271
<tr>
5372
<td><%= bd.name %></td>
5473
<td class="numeric"><%= number_with_delimiter(bd.quantity) %></td>
@@ -65,4 +84,5 @@
6584
</div>
6685
</div>
6786
</div>
87+
</div>
6888
</section>
129 KB
Loading
220 KB
Loading
680 KB
Loading

‎docs/user_guide/bank/reports_distributions_by_county.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33

44
![distributions_by_county_default_view](images/reports/reports_distributions_by_county_1.png)
55

6-
This report shows the total items and estimated total market value of the distributed Items for the time period.
6+
This report initially shows the total items and estimated total market value of the distributed Items for the time period.
77

88
We use the area served information entered in each partner's [profile](pm_partner_profiles.md) to allocate the items that have been distributed to them. If that information has not been provided, the items you've distributed are put into the 'Unspecified' category.
99

10-
11-
**N.B.** If you are using Kits, please note that this treats each Kit as an item, rather than counting the items within the Kits separately.
10+
**N.B. If you are using Kits, please note that this counts the items within the kits, rather than the kits. You can not use this report to determine the number of kits that have been distributed to each county.**
1211

1312
If you want to see a time period other than the default of 60 days prior to 30 days forward from today, change the date range and click "Filter". We recommend you use the little pop-up gizmo to enter your date range, as the format of the date range is very fussy.
1413

1514
![distributions_by_county_with_date_range_gizmo](images/reports/reports_distributions_by_county_2.png)
1615

16+
17+
Similarly, you can also filter by a single item, or by a reporting category, if, for example, you wanted to see distribution of disposable diapers, you would select "Disposalbe Diapers" from the Reporting Category menu, and then click "Filter"
18+
19+
![distributions_by_county_with_reporting_category_dropdown](images/reports/reports_distributions_by_county_3.png)
20+
21+
22+
To go back to the default values, click "Clear filters".
23+
1724
[Back to Annual Survey](reports_annual_survey.md)
1825

1926
[Next: Manufacturer Donations Report](reports_manufacturers_donations.md)

0 commit comments

Comments
 (0)