Class: Form1095B

Inherits:
ApplicationRecord show all
Defined in:
app/models/form1095_b.rb

Instance Method Summary collapse

Methods inherited from ApplicationRecord

descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?

Instance Method Details

#country_and_zipObject (private)



56
57
58
# File 'app/models/form1095_b.rb', line 56

def country_and_zip
  "#{data[:country]} #{data[:zip_code] || data[:foreign_zip]}"
end

#dataObject (private)

rubocop:enable Metrics/MethodLength



166
167
168
# File 'app/models/form1095_b.rb', line 166

def data
  @data ||= JSON.parse(form_data, { symbolize_names: true })
end

#form_data_schemaObject (private)



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
# File 'app/models/form1095_b.rb', line 130

def form_data_schema
  {
    "type": 'object',
    "required": %w[first_name middle_name last_name address city country coverage_months],
    "properties": {
      "first_name": { "type": 'string' },
      "middle_name": { "type": 'string' },
      "last_name": { "type": 'string' },
      "last_4_ssn": {
        "type": 'string',
        "pattern": '[0-9]{4}|^$'
      },
      "birth_date": {
        "type": 'string',
        "format": 'date'
      },
      "address": { "type": 'string' },
      "city": { "type": 'string' },
      "state": { "type": 'string' },
      "province": { "type": 'string' },
      "country": { "type": 'string' },
      "zip_code": { "type": 'string' },
      "foreign_zip": { "type": 'string' },
      "is_beneficiary": { "type": 'boolean' },
      "is_corrected": { "type": 'boolean' },
      "coverage_months": {
        "type": 'array',
        "items": { "type": 'boolean' },
        "minItems": 13,
        "maxItems": 13
      }
    }
  }
end

#generate_pdf(pdftk, tmp_file) ⇒ Object (private)

rubocop:disable Metrics/MethodLength



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
# File 'app/models/form1095_b.rb', line 82

def generate_pdf(pdftk, tmp_file)
  pdftk.fill_form(
    pdf_template_path,
    tmp_file,
    {
      "topmostSubform[0].Page1[0].Pg1Header[0].cb_1[1]": data[:is_corrected] && 2,
      "topmostSubform[0].Page1[0].Part1Contents[0].Line1[0].f1_01[0]": data[:first_name],
      "topmostSubform[0].Page1[0].Part1Contents[0].Line1[0].f1_02[0]": data[:middle_name],
      "topmostSubform[0].Page1[0].Part1Contents[0].Line1[0].f1_03[0]": data[:last_name],
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_04[0]": data[:last_4_ssn] || '',
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_05[0]": data[:last_4_ssn] ? '' : data[:birth_date],
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_06[0]": data[:address],
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_07[0]": data[:city],
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_08[0]": data[:state] || data[:province],
      "topmostSubform[0].Page1[0].Part1Contents[0].f1_09[0]": country_and_zip,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].f1_25[0]": data[:first_name],
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].f1_26[0]": middle_initial,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].f1_27[0]": data[:last_name],
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].f1_28[0]": data[:last_4_ssn] || '',
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].f1_29[0]": data[:last_4_ssn] ? '' : data[:birth_date],
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_01[0]": data[:coverage_months][0] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_02[0]": data[:coverage_months][1] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_03[0]": data[:coverage_months][2] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_04[0]": data[:coverage_months][3] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_05[0]": data[:coverage_months][4] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_06[0]": data[:coverage_months][5] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_07[0]": data[:coverage_months][6] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_08[0]": data[:coverage_months][7] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_09[0]": data[:coverage_months][8] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_10[0]": data[:coverage_months][9] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_11[0]": data[:coverage_months][10] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_12[0]": data[:coverage_months][11] && 1,
      "topmostSubform[0].Page1[0].Table1_Part4[0].Row23[0].c1_13[0]": data[:coverage_months][12] && 1
    },
    flatten: true
  )
  ret_pdf = tmp_file.read

  tmp_file.close
  tmp_file.unlink

  ret_pdf
rescue PdfForms::PdftkError => e
  # in case theres other errors generating the PDF
  Rails.logger.error e.message
  raise
end

#middle_initialObject (private)



60
61
62
# File 'app/models/form1095_b.rb', line 60

def middle_initial
  data[:middle_name] ? data[:middle_name][0] : ''
end

#pdf_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/form1095_b.rb', line 33

def pdf_file
  pdftk = PdfForms.new(Settings.binaries.pdftk)

  tmp_file = Tempfile.new("1095B-#{SecureRandom.hex}.pdf")

  unless File.exist?(pdf_template_path)
    Rails.logger.error "1095-B template for year #{tax_year} does not exist."
    raise "1095-B for tax year #{tax_year} not supported"
  end

  generate_pdf(pdftk, tmp_file)
end

#pdf_template_pathObject (private)



48
49
50
# File 'app/models/form1095_b.rb', line 48

def pdf_template_path
  "lib/form1095_b/templates/pdfs/1095b-#{tax_year}.pdf"
end

#proper_form_data_schemaObject (private)



170
171
172
173
174
# File 'app/models/form1095_b.rb', line 170

def proper_form_data_schema
  JSON::Validator.validate!(form_data_schema, form_data)
rescue JSON::Schema::ValidationError => e
  errors.add(:form_data, **e)
end

#txt_fileObject

methods



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/form1095_b.rb', line 18

def txt_file
  unless File.exist?(txt_template_path)
    Rails.logger.error "1095-B template for year #{tax_year} does not exist."
    raise "1095-B for tax year #{tax_year} not supported"
  end

  template_file = File.open(txt_template_path, 'r')

  rv = template_file.read % data.merge(txt_form_data)

  template_file.close

  rv
end

#txt_form_dataObject (private)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/form1095_b.rb', line 64

def txt_form_data
  text_data = {
    birth_date_field: data[:last_4_ssn] ? '' : data[:birth_date],
    state_or_province: data[:state] || data[:province],
    country_and_zip:,
    middle_init: middle_initial,
    corrected: data[:is_corrected] ? 'X' : '--'
  }

  data[:coverage_months].each_with_index do |val, ndx|
    field_name = "coverage_month_#{ndx}"
    text_data[field_name.to_sym] = val ? 'X' : '--'
  end

  text_data
end

#txt_template_pathObject (private)



52
53
54
# File 'app/models/form1095_b.rb', line 52

def txt_template_path
  "lib/form1095_b/templates/txts/1095b-#{tax_year}.txt"
end