forked from codeforamerica/vita-min
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_file_az_intake.rb
242 lines (221 loc) · 12 KB
/
state_file_az_intake.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# == Schema Information
#
# Table name: state_file_az_intakes
#
# id :bigint not null, primary key
# account_number :string
# account_type :integer
# armed_forces_member :integer default("unfilled"), not null
# armed_forces_wages :integer
# bank_name :string
# charitable_cash :integer default(0)
# charitable_contributions :integer default("unfilled"), not null
# charitable_noncash :integer default(0)
# consented_to_terms_and_conditions :integer default("unfilled"), not null
# contact_preference :integer default("unfilled"), not null
# current_sign_in_at :datetime
# current_sign_in_ip :inet
# current_step :string
# date_electronic_withdrawal :date
# df_data_import_failed_at :datetime
# df_data_imported_at :datetime
# eligibility_529_for_non_qual_expense :integer default("unfilled"), not null
# eligibility_lived_in_state :integer default("unfilled"), not null
# eligibility_married_filing_separately :integer default("unfilled"), not null
# eligibility_out_of_state_income :integer default("unfilled"), not null
# email_address :citext
# email_address_verified_at :datetime
# failed_attempts :integer default(0), not null
# federal_return_status :string
# has_prior_last_names :integer default("unfilled"), not null
# hashed_ssn :string
# household_excise_credit_claimed :integer default("unfilled"), not null
# household_excise_credit_claimed_amt :integer
# last_sign_in_at :datetime
# last_sign_in_ip :inet
# locale :string default("en")
# locked_at :datetime
# made_az321_contributions :integer default("unfilled"), not null
# message_tracker :jsonb
# payment_or_deposit_type :integer default("unfilled"), not null
# phone_number :string
# phone_number_verified_at :datetime
# primary_birth_date :date
# primary_esigned :integer default("unfilled"), not null
# primary_esigned_at :datetime
# primary_first_name :string
# primary_last_name :string
# primary_middle_initial :string
# primary_received_pension :integer default("unfilled"), not null
# primary_received_pension_amount :decimal(12, 2)
# primary_suffix :string
# primary_was_incarcerated :integer default("unfilled"), not null
# prior_last_names :string
# raw_direct_file_data :text
# received_military_retirement_payment :integer default("unfilled"), not null
# received_military_retirement_payment_amount :decimal(12, 2)
# referrer :string
# routing_number :string
# sign_in_count :integer default(0), not null
# source :string
# spouse_birth_date :date
# spouse_esigned :integer default("unfilled"), not null
# spouse_esigned_at :datetime
# spouse_first_name :string
# spouse_last_name :string
# spouse_middle_initial :string
# spouse_received_pension :integer default("unfilled"), not null
# spouse_received_pension_amount :decimal(12, 2)
# spouse_suffix :string
# spouse_was_incarcerated :integer default("unfilled"), not null
# ssn_no_employment :integer default("unfilled"), not null
# tribal_member :integer default("unfilled"), not null
# tribal_wages :integer
# unfinished_intake_ids :text default([]), is an Array
# unsubscribed_from_email :boolean default(FALSE), not null
# was_incarcerated :integer default("unfilled"), not null
# withdraw_amount :integer
# created_at :datetime not null
# updated_at :datetime not null
# federal_submission_id :string
# primary_state_id_id :bigint
# spouse_state_id_id :bigint
# visitor_id :string
#
# Indexes
#
# index_state_file_az_intakes_on_email_address (email_address)
# index_state_file_az_intakes_on_hashed_ssn (hashed_ssn)
# index_state_file_az_intakes_on_primary_state_id_id (primary_state_id_id)
# index_state_file_az_intakes_on_spouse_state_id_id (spouse_state_id_id)
#
class StateFileAzIntake < StateFileBaseIntake
encrypts :account_number, :routing_number, :raw_direct_file_data
has_many :az322_contributions, dependent: :destroy
has_many :az321_contributions, dependent: :destroy
enum has_prior_last_names: { unfilled: 0, yes: 1, no: 2 }, _prefix: :has_prior_last_names
# TODO: decide what to do with was_incarcerated column; see if data science wants to keep the historic data
enum was_incarcerated: { unfilled: 0, yes: 1, no: 2 }, _prefix: :was_incarcerated
enum primary_was_incarcerated: { unfilled: 0, yes: 1, no: 2 }, _prefix: :primary_was_incarcerated
enum spouse_was_incarcerated: { unfilled: 0, yes: 1, no: 2 }, _prefix: :spouse_was_incarcerated
enum ssn_no_employment: { unfilled: 0, yes: 1, no: 2 }, _prefix: :ssn_no_employment
enum household_excise_credit_claimed: { unfilled: 0, yes: 1, no: 2 }, _prefix: :household_excise_credit_claimed
enum tribal_member: { unfilled: 0, yes: 1, no: 2 }, _prefix: :tribal_member
enum armed_forces_member: { unfilled: 0, yes: 1, no: 2 }, _prefix: :armed_forces_member
enum charitable_contributions: { unfilled: 0, yes: 1, no: 2 }, _prefix: :charitable_contributions
enum eligibility_married_filing_separately: { unfilled: 0, yes: 1, no: 2 }, _prefix: :eligibility_married_filing_separately
enum eligibility_529_for_non_qual_expense: { unfilled: 0, yes: 1, no: 2 }, _prefix: :eligibility_529_for_non_qual_expense
enum received_military_retirement_payment: { unfilled: 0, yes: 1, no: 2 }, _prefix: :received_military_retirement_payment
enum primary_received_pension: { unfilled: 0, yes: 1, no: 2 }, _prefix: :primary_received_pension
enum spouse_received_pension: { unfilled: 0, yes: 1, no: 2 }, _prefix: :spouse_received_pension
enum made_az321_contributions: { unfilled: 0, yes: 1, no: 2 }, _prefix: :made_az321_contributions
validates :made_az321_contributions, inclusion: { in: ["yes", "no"]}, on: :az321_form_create
validates :az321_contributions, length: { maximum: 3 }
def federal_dependent_count_under_17
self.dependents.select{ |dependent| dependent.age < 17 }.length
end
def federal_dependent_count_over_17_non_qualifying_senior
self.dependents.select{ |dependent| dependent.age >= 17 && !dependent.is_qualifying_parent_or_grandparent? }.length
end
def qualifying_parents_and_grandparents
self.dependents.select(&:is_qualifying_parent_or_grandparent?).length
end
def ask_months_in_home?
true
end
def disqualifying_df_data_reason
return :married_filing_separately if direct_file_data.filing_status == 3
w2_states = direct_file_data.parsed_xml.css('W2StateLocalTaxGrp W2StateTaxGrp StateAbbreviationCd')
:has_out_of_state_w2 if w2_states.any? do |state|
(state.text || '').upcase != state_code.upcase
end
end
def disqualifying_eligibility_rules
{
eligibility_lived_in_state: "no",
eligibility_married_filing_separately: "yes",
eligibility_out_of_state_income: "yes",
eligibility_529_for_non_qual_expense: "yes",
}
end
def disqualified_from_excise_credit_df?
agi_limit = if filing_status_mfj? || filing_status_hoh?
25000
elsif filing_status_single? || filing_status_mfs?
12500
end
agi_over_limit = direct_file_data.fed_agi > agi_limit
lacks_valid_ssn = primary.ssn.blank? || primary.has_itin?
agi_over_limit || lacks_valid_ssn
end
def incarcerated_filer_count
count = 0
if use_old_incarcerated_column?
count += 2 if was_incarcerated_yes?
else
count += 1 if primary_was_incarcerated_yes?
count += 1 if spouse_was_incarcerated_yes?
end
count
end
# TODO: remove once column ignored
def use_old_incarcerated_column?
!was_incarcerated_unfilled? && primary_was_incarcerated_unfilled?
end
def disqualified_from_excise_credit_fyst?
all_filers_incarcerated = was_incarcerated_yes? || (primary_was_incarcerated_yes? && spouse_was_incarcerated_yes?)
whole_credit_already_claimed = use_old_incarcerated_column? && household_excise_credit_claimed_yes?
all_filers_incarcerated || whole_credit_already_claimed || ssn_no_employment_yes? || direct_file_data.claimed_as_dependent?
end
def filing_status
return :head_of_household if direct_file_data&.filing_status == 5 # Treat qualifying_widow as hoh
super
end
def total_subtractions
lines = [:AZ140_LINE_20, :AZ140_LINE_21, :AZ140_LINE_22, :AZ140_LINE_23, :AZ140_LINE_24, :AZ140_LINE_25,
:AZ140_LINE_26, :AZ140_LINE_27, :AZ140_LINE_28, :AZ140_LINE_29, :AZ140_LINE_30, :AZ140_LINE_31,
:AZ140_LINE_32, :AZ140_LINE_33, :AZ140_LINE_34]
subtractions = 0
lines.each { |line| subtractions += self.calculator.line_or_zero(line) }
subtractions
end
def total_exemptions
lines = [:AZ140_LINE_38, :AZ140_LINE_39, :AZ140_LINE_40, :AZ140_LINE_41]
exemptions = 0
lines.each { |line| exemptions += self.calculator.line_or_zero(line) }
exemptions
end
def requires_hoh_qualifying_person_name?
filing_status == :head_of_household
end
def hoh_qualifying_person_name
return unless requires_hoh_qualifying_person_name?
if direct_file_data&.hoh_qualifying_person_name.present?
# Federal data is an unstructured string - split on first space and everything in the second group goes to last name
names = direct_file_data.hoh_qualifying_person_name.split(/ /, 2)
return {
:first_name => names[0],
:last_name => names[1]
}
end
# This is fallback logic in case the data is not given in the federal return
hoh_qualifying_dependents = self.dependents.select(&:is_hoh_qualifying_person?)
unless hoh_qualifying_dependents.empty?
six_plus_months_in_home = hoh_qualifying_dependents.reject { |dependent|
dependent[:months_in_home] < 6
}
hoh_qualifying_dependent = six_plus_months_in_home.max_by { |dependent|
[dependent[:months_in_home], -dependent.age]
}
if hoh_qualifying_dependent.nil?
hoh_qualifying_dependent = hoh_qualifying_dependents.select { |dependent|
dependent[:relationship] == "PARENT"
}.max_by(&:age)
end
{
:first_name => hoh_qualifying_dependent.first_name,
:last_name => hoh_qualifying_dependent.last_name
}
end
end
end