@@ -17,8 +17,8 @@ def fetch
17
17
WHERE "Filer_ID" = "FPPC"::varchar
18
18
AND "Form_Type" = 'F460'
19
19
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")
22
22
GROUP BY "Filer_ID"
23
23
ORDER BY "Filer_ID"
24
24
SQL
@@ -46,10 +46,42 @@ 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
+ # 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
+
49
60
contributions_by_filer_id . each do |filer_id , total_contributions |
50
61
candidate = @candidates_by_filer_id [ filer_id ]
51
62
candidate . save_calculation ( :total_contributions , total_contributions )
52
63
end
53
64
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
54
86
end
55
87
0 commit comments