11class DistributionSummaryByCountyQuery
2- DISTRIBUTION_BY_COUNTY_SQL = <<~SQL . squish . freeze
2+ CountySummary = Data . define ( :name , :quantity , :value )
3+
4+ # No need to send comments in the query
5+ SQL_MULTILINE_COMMENTS = /\/ \* .*?\* \/ /
6+
7+ DISTRIBUTION_BY_COUNTY_SQL = <<~SQL . squish . gsub ( SQL_MULTILINE_COMMENTS , "" ) . freeze
38 /* Calculate total item quantity and value per distribution */
49 WITH distribution_totals AS
510 (
611 SELECT DISTINCT d.id,
712 d.partner_id,
813 COALESCE(SUM(li.quantity) OVER (PARTITION BY d.id), 0) AS quantity,
9- COALESCE(SUM(COALESCE(i.value_in_cents, 0) * li.quantity) OVER (PARTITION BY d.id), 0) AS amount
14+ COALESCE(SUM(COALESCE(i.value_in_cents, 0) * li.quantity) OVER (PARTITION BY d.id), 0) AS value
1015 FROM distributions d
1116 JOIN line_items li ON li.itemizable_id = d.id AND li.itemizable_type = 'Distribution'
1217 JOIN items i ON i.id = li.item_id
@@ -21,7 +26,7 @@ class DistributionSummaryByCountyQuery
2126 (
2227 SELECT dt.id,
2328 dt.quantity,
24- dt.amount ,
29+ dt.value ,
2530 COALESCE(psa.client_share::float / 100, 1) AS percentage,
2631 COALESCE(c.name, 'Unspecified') county_name,
2732 COALESCE(c.region, 'ZZZ') county_region
@@ -34,7 +39,7 @@ class DistributionSummaryByCountyQuery
3439 even if all distributions have an associated county */
3540 SELECT 0 AS id,
3641 0 AS quantity,
37- 0 AS amount ,
42+ 0 AS value ,
3843 1 AS percentage,
3944 'Unspecified' AS county_name,
4045 'ZZZ' AS county_region
@@ -43,36 +48,42 @@ class DistributionSummaryByCountyQuery
4348 so we cast to an integer for rounding purposes */
4449 SELECT tbc.county_name AS name,
4550 SUM((tbc.quantity * percentage)::int) AS quantity,
46- SUM((tbc.amount * percentage)::int) AS amount
51+ SUM((tbc.value * percentage)::int) AS value
4752 FROM totals_by_county tbc
4853 GROUP BY county_name, county_region
4954 ORDER BY county_region ASC;
5055 SQL
5156
52- def initialize ( organization_id :, start_date : nil , end_date : nil )
53- @organization_id = organization_id
54- @start_date = start_date || "1000-01-01"
55- @end_date = end_date || "3000-01-01"
56- end
57+ class << self
58+ def call ( organization_id :, start_date :, end_date :)
59+ params = {
60+ organization_id : organization_id ,
61+ start_date : start_date || "1000-01-01" ,
62+ end_date : end_date || "3000-01-01"
63+ }
5764
58- def call
59- execute ( to_sql ( DISTRIBUTION_BY_COUNTY_SQL ) ) . to_a
60- end
65+ execute ( to_sql ( DISTRIBUTION_BY_COUNTY_SQL , **params ) ) . to_a . map ( &to_county_summary )
66+ end
6167
62- private
68+ private
6369
64- def execute ( sql )
65- ActiveRecord ::Base . connection . execute ( sql )
66- end
70+ def execute ( sql )
71+ ActiveRecord ::Base . connection . execute ( sql )
72+ end
6773
68- def to_sql ( query )
69- ActiveRecord ::Base . sanitize_sql_array (
70- [
71- query ,
72- organization_id : @organization_id ,
73- start_date : @start_date ,
74- end_date : @end_date
75- ]
76- )
74+ def to_sql ( query , organization_id :, start_date :, end_date :)
75+ ActiveRecord ::Base . sanitize_sql_array (
76+ [
77+ query ,
78+ organization_id : organization_id ,
79+ start_date : start_date ,
80+ end_date : end_date
81+ ]
82+ )
83+ end
84+
85+ def to_county_summary
86+ -> ( params ) { CountySummary . new ( **params ) }
87+ end
7788 end
7889end
0 commit comments