@@ -46,10 +46,39 @@ def fetch
46
46
contributions_by_filer_id [ filer_id ] += result [ 'Total' ] . to_f
47
47
end
48
48
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
+ contributions_by_filer_id [ filer_id ] ||= 0
54
+ contributions_by_filer_id [ filer_id ] += result [ 'Starting_Balance' ] . to_f
55
+ end
56
+
49
57
contributions_by_filer_id . each do |filer_id , total_contributions |
50
58
candidate = @candidates_by_filer_id [ filer_id ]
51
59
candidate . save_calculation ( :total_contributions , total_contributions )
52
60
end
53
61
end
62
+
63
+ def starting_balances_by_filer_id
64
+ ActiveRecord ::Base . connection . execute ( <<-SQL ) . index_by { |r | r [ 'Filer_ID' ] }
65
+ WITH first_filing_after_start_dates AS (
66
+ -- Get the first report after the Start Date for each filer
67
+ SELECT "Filer_ID", MIN("Thru_Date") as "Thru_Date"
68
+ FROM "Summary", candidates
69
+ WHERE "Filer_ID" = "FPPC"::varchar
70
+ AND "Start_Date" IS NOT NULL
71
+ AND "Thru_Date" >= "Start_Date"
72
+ GROUP BY "Filer_ID"
73
+ )
74
+ SELECT "Summary"."Filer_ID", "Summary"."Thru_Date", "Amount_A" as "Starting_Balance"
75
+ FROM "Summary"
76
+ INNER JOIN first_filing_after_start_dates
77
+ ON first_filing_after_start_dates."Filer_ID" = "Summary"."Filer_ID"
78
+ AND first_filing_after_start_dates."Thru_Date" = "Summary"."Thru_Date"
79
+ WHERE "Form_Type" = 'F460'
80
+ AND "Line_Item" = '12';
81
+ SQL
82
+ end
54
83
end
55
84
0 commit comments