Skip to content

Commit d8ff16c

Browse files
committed
WIP: Add "Beginning Cash Balance" to total contributions
1 parent 3f5b1f6 commit d8ff16c

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

calculators/total_contributions_calculator.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def fetch
1717
WHERE "Filer_ID" = "FPPC"::varchar
1818
AND "Form_Type" = 'F460'
1919
AND "Line_Item" = '5'
20-
AND ("Start_Date" IS NULL OR "Rpt_Date" >= "Start_Date")
21-
AND ("End_Date" IS NULL OR "Rpt_Date" <= "End_Date")
20+
AND ("Start_Date" IS NULL OR "From_Date" >= "Start_Date")
21+
AND ("End_Date" IS NULL OR "Thru_Date" <= "End_Date")
2222
GROUP BY "Filer_ID"
2323
ORDER BY "Filer_ID"
2424
SQL
@@ -46,10 +46,42 @@ def fetch
4646
contributions_by_filer_id[filer_id] += result['Total'].to_f
4747
end
4848

49+
# For candidates where we specify a "Start Date", we include their initial
50+
# "Beginning Cash Balance" as a contribution so that our calculation of
51+
# their remaining balance matches up with their actual reported balance.
52+
starting_balances_by_filer_id.each do |filer_id, result|
53+
# Skip records with no Summary sheet for now, to prevent conflicting
54+
# `total_contributions` calculation with contribution_list_calculator.
55+
next unless contributions_by_filer_id.include?(filer_id)
56+
57+
contributions_by_filer_id[filer_id] += result['Starting_Balance'].to_f
58+
end
59+
4960
contributions_by_filer_id.each do |filer_id, total_contributions|
5061
candidate = @candidates_by_filer_id[filer_id]
5162
candidate.save_calculation(:total_contributions, total_contributions)
5263
end
5364
end
65+
66+
def starting_balances_by_filer_id
67+
ActiveRecord::Base.connection.execute(<<-SQL).index_by { |r| r['Filer_ID'] }
68+
WITH first_filing_after_start_dates AS (
69+
-- Get the first report after the Start Date for each filer
70+
SELECT "Filer_ID", MIN("Thru_Date") as "Thru_Date"
71+
FROM "Summary", candidates
72+
WHERE "Filer_ID" = "FPPC"::varchar
73+
AND "Start_Date" IS NOT NULL
74+
AND "Thru_Date" >= "Start_Date"
75+
GROUP BY "Filer_ID"
76+
)
77+
SELECT "Summary"."Filer_ID", "Summary"."Thru_Date", "Amount_A" as "Starting_Balance"
78+
FROM "Summary"
79+
INNER JOIN first_filing_after_start_dates
80+
ON first_filing_after_start_dates."Filer_ID" = "Summary"."Filer_ID"
81+
AND first_filing_after_start_dates."Thru_Date" = "Summary"."Thru_Date"
82+
WHERE "Form_Type" = 'F460'
83+
AND "Line_Item" = '12';
84+
SQL
85+
end
5486
end
5587

0 commit comments

Comments
 (0)