-
Notifications
You must be signed in to change notification settings - Fork 14
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
Process Form 497 Page 2 (Late Expenditures) #31
Changes from 1 commit
4aef139
8971186
cb0a68f
174b49b
9c2d890
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 |
---|---|---|
|
@@ -16,6 +16,16 @@ def fetch | |
ORDER BY "Filer_ID", "Filer_NamL", "Bal_Name", "Sup_Opp_Cd", "Report_Num" DESC | ||
SQL | ||
|
||
late_expenditures = ActiveRecord::Base.connection.execute(<<-SQL) | ||
SELECT DISTINCT ON ("Filer_ID", "Filer_NamL", "Bal_Name") | ||
"Filer_ID", "Filer_NamL", "Bal_Name", SUM("Amount") AS "Total_Amount" | ||
FROM "efile_COAK_2016_497" | ||
WHERE "Bal_Name" IS NOT NULL | ||
AND "Form_Type" = 'F497P2' | ||
GROUP BY "Filer_ID", "Filer_NamL", "Bal_Name", "Report_Num" | ||
ORDER BY "Filer_ID", "Filer_NamL", "Bal_Name", "Report_Num" DESC | ||
SQL | ||
|
||
supporting_by_measure_name = {} | ||
opposing_by_measure_name = {} | ||
|
||
|
@@ -29,6 +39,37 @@ def fetch | |
end | ||
end | ||
|
||
late_expenditures.each do |row| | ||
sup_opp_cd = guess_whether_committee_supports_measure(row['Filer_ID'], row['Bal_Name']) | ||
if sup_opp_cd == 'S' | ||
supporting_by_measure_name[row['Bal_Name']] ||= [] | ||
existing_idx = supporting_by_measure_name[row['Bal_Name']].find_index do |existing_row| | ||
existing_row['Filer_ID'].to_s == row['Filer_ID'].to_s | ||
end | ||
|
||
if existing_idx | ||
supporting_by_measure_name[row['Bal_Name']][existing_idx]['Total_Amount'] += | ||
row['Total_Amount'] | ||
else | ||
supporting_by_measure_name[row['Bal_Name']] << row | ||
end | ||
elsif sup_opp_cd == 'O' | ||
opposing_by_measure_name[row['Bal_Name']] ||= [] | ||
opposing_by_measure_name[row['Bal_Name']] << row | ||
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 doesn't look right, aren't you appending the row twice? 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. Yep, good catch. |
||
|
||
existing_idx = opposing_by_measure_name[row['Bal_Name']].find_index do |existing_row| | ||
existing_row['Filer_ID'].to_s == row['Filer_ID'].to_s | ||
end | ||
|
||
if existing_idx | ||
opposing_by_measure_name[row['Bal_Name']][existing_idx]['Total_Amount'] += | ||
row['Total_Amount'] | ||
else | ||
opposing_by_measure_name[row['Bal_Name']] << row | ||
end | ||
end | ||
end | ||
|
||
[ | ||
# { bal_name => rows } , calculation name | ||
[supporting_by_measure_name, :supporting_organizations], | ||
|
@@ -77,4 +118,29 @@ def committee_from_expenditure(expenditure) | |
|
||
committee | ||
end | ||
|
||
# Form 497 Page 2 (Late Expenditures) includes the ballot measure name and | ||
# committee ID, but does not indicate whether that expenditure was in support | ||
# or opposition of the ballot measure. | ||
# | ||
# This is not perfect, but it should get us pretty close. | ||
def guess_whether_committee_supports_measure(committee_id, bal_name) | ||
@_guess_cache ||= | ||
begin | ||
guesses = ActiveRecord::Base.connection.execute(<<-SQL) | ||
SELECT "Filer_ID", "Bal_Name", "Sup_Opp_Cd" | ||
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 could use SELECT DISTINCT rather than GROUP BY, its probably its processed about the same but I think it is clearer that is what you are doing. 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. Yeah, I didn't use the Report_Num here because it seemed unlikely to me that the Sup_Opp_Cd would change between amendments. |
||
FROM "efile_COAK_2016_E-Expenditure" | ||
WHERE "Bal_Name" IS NOT NULL | ||
GROUP BY "Filer_ID", "Bal_Name", "Sup_Opp_Cd" | ||
SQL | ||
|
||
guesses.index_by do |row| | ||
row.values_at('Filer_ID', 'Bal_Name').map(&:to_s) | ||
end | ||
end | ||
|
||
if row = @_guess_cache[[committee_id.to_s, bal_name]] | ||
row['Sup_Opp_Cd'] | ||
end | ||
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.
Does this ever come up empty? In that case, we just don't count the money?
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.
seems like if we can't guess then we drop it. Its not likely to happen too often.
On a related note, I think I saw the same ballot measure referred by two different names.
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.
Yep, we drop it. I created #35 as a issue to track our tracking of how many records / how much money we skip because we can't find a match. We can keep adding more to that issue as we introduce more fuzzy matching into our processing.