Class: ReportsManager::DefectReport

Inherits:
ApplicationService show all
Defined in:
app/services/reports_manager/defect_report.rb

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type, only_data = false, get_score_data = false, action_type = 'defect-report') ⇒ DefectReport

Returns a new instance of DefectReport.



5
6
7
8
9
10
11
12
13
# File 'app/services/reports_manager/defect_report.rb', line 5

def initialize(user_id, property_id, report_type,
               only_data = false, get_score_data = false, action_type = 'defect-report')
  @user_id = user_id
  @property_id = property_id
  @report_type = report_type
  @action_type = action_type
  @get_score_data = get_score_data
  @only_data = only_data
end

Instance Method Details

#callObject



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/services/reports_manager/defect_report.rb', line 15

def call
  view_data = {}
  view_data['property_id'] = @property_id
  view_data['user_id'] = @user_id
  view_data['report_type'] = @report_type
  view_data['property'] =  Property.find_by_id(@property_id)
  details = []

  index = 1
  st_defect = 0.0
  be_defect = 0.0
  bs_defect = 0.0
  ca_defect = 0.0
  un_defect = 0.0

  inspection_details = InspectionDetail.get_details(@property_id)

  items = InspectableItem.get_all
  defects = DeficiencyDefinition.get_all
  buildings = Building.get_property_buildings(@property_id)
  units = Unit.get_property_units(@property_id)

  score_data = ApplicationHelper.score_areas(@property_id)
  total_buildings = buildings.count
  total_units = 0
  units.each do |_key, unit|
    prop = unit[:properties]
    total_units += 1 unless prop && prop['uninspectable'] && prop['uninspectable'].downcase.strip != 'none'
  end
  points_map = {}

  inspection_details.each do |inspection|
    temp = {}
    item = items[inspection['inspectable_item_id']] || nil
    defect = inspection['deficiency_definition_id'].present? &&
      defects[inspection['deficiency_definition_id']].present? ? defects[inspection['deficiency_definition_id']] : nil
    building = inspection['building_id'].present? && buildings[inspection['building_id']].present? ?
                   buildings[inspection['building_id']] : nil

    unit = inspection['unit_id'].present? && units[inspection['unit_id']].present? ?
               units[inspection['unit_id']] : nil

    prop = JSON.parse(inspection['properties'])
    level = prop['level'] || ''
    image_name = prop['image_name'] || nil

    next unless item && defect

    defect_prop = defect['properties']
    item_prop = item['properties']
    criticality = defect_prop && defect_prop['criticality'].present? ? defect_prop['criticality'].to_f : 0.0
    item_weight = item_prop['item_weight'].present? ? (item_prop['item_weight'].to_f / 100.0).to_f : 0.0

    temp['line'] = index
    temp['unit_id'] = inspection['unit_id']
    temp['building_id'] = inspection['building_id']
    temp['area'] = item.inspection_area.area_code.present? ? item.inspection_area.area_code : ''
    temp['sort_key'] = 0

    area_code = temp['area'].strip.upcase

    dbl_possible = score_data[area_code] ? score_data[area_code][:PossibleScore].to_f : 0.0
    temp['building_name'] = building && building['building_name'].present? ? building['building_name'] : 'N/A'
    temp['unit_name'] = unit && unit['unit_name'].present? ? unit['unit_name'] : 'N/A'
    temp['item'] = item['title']
    temp['defect'] = defect['title']

    temp['level'] = level.gsub('L', '') + '-' + defect['definition_type'].to_s

    temp['image_name'] = image_name
    sort_key = temp['building_name'] + '-' + temp['unit_name'] + '-' + temp['item'] + '-' + temp['defect'].to_s

    dbl_point_deduction = 0.0
    if area_code == 'ST'
      dbl_point_deduction = (ApplicationHelper.get_criticality(criticality) *
          ApplicationHelper.get_severity(level) * item_weight) * dbl_possible

      dbl_point_deduction = dbl_point_deduction.to_f
      dbl_point_deduction = 7.5 if dbl_point_deduction > 7

    elsif area_code == 'BS' || area_code == 'BE' || area_code == 'CA'
      dbl_point_deduction = ((ApplicationHelper.get_criticality(criticality) *
          ApplicationHelper.get_severity(level) * item_weight) * dbl_possible) / total_buildings

      dbl_point_deduction = dbl_point_deduction.to_f
      dbl_point_deduction = 10.9 if dbl_point_deduction > 10

    elsif area_code == 'UN'
      dbl_point_deduction = ((ApplicationHelper.get_criticality(criticality) *
          ApplicationHelper.get_severity(level).to_f *
          item_weight.to_f) *
          dbl_possible.to_f) / total_units

      dbl_point_deduction = dbl_point_deduction.to_f
      dbl_point_deduction = 5.0 if dbl_point_deduction > 5.0
    end

    int_point_deduction = dbl_point_deduction.to_f * 100

    int_point_deduction = int_point_deduction.to_f

    temp['points'] = if int_point_deduction == 0
                       '0'
                     elsif int_point_deduction < 5
                       '<0.05'
                     else
                       dbl_point_deduction.round(1)
                     end
    temp['points_sort'] = dbl_point_deduction.round(3)
    map_key = area_code + '-' + @property_id.to_s + '-' + inspection['building_id'].to_s + '-' +
              inspection['unit_id'].to_s + '-' + inspection['inspectable_item_id'].to_s + '-' +
              inspection['deficiency_id'].to_s
    if temp['area'] == 'ST'
      temp['sort_key'] = 'A-' + sort_key
      temp['area_name'] = 'Site'
      if !points_map[map_key].present?
        st_defect += dbl_point_deduction
      else
        existing_dbl_point_deduction = points_map[map_key]
        if existing_dbl_point_deduction < dbl_point_deduction
          st_defect -= existing_dbl_point_deduction
          st_defect += dbl_point_deduction
        end
      end
    elsif temp['area'] == 'BS'
      temp['sort_key'] = 'B-' + sort_key
      temp['area_name'] = 'Building System'
      if !points_map[map_key].present?
        bs_defect += dbl_point_deduction
      else
        existing_dbl_point_deduction = points_map[map_key]
        if existing_dbl_point_deduction < dbl_point_deduction
          bs_defect -= existing_dbl_point_deduction
          bs_defect += dbl_point_deduction
        end
      end
    elsif temp['area'] == 'BE'
      temp['sort_key'] = 'C-' + sort_key
      temp['area_name'] = 'Building Exterior'
      if !points_map[map_key].present?
        be_defect += dbl_point_deduction
      else
        existing_dbl_point_deduction = points_map[map_key]
        if existing_dbl_point_deduction < dbl_point_deduction
          be_defect -= existing_dbl_point_deduction
          be_defect += dbl_point_deduction
        end
      end
    elsif temp['area'] == 'CA'
      temp['sort_key'] = 'D-' + sort_key
      temp['area_name'] = 'Common Area'
      if !points_map[map_key].present?
        ca_defect += dbl_point_deduction
      else
        existing_dbl_point_deduction = points_map[map_key]
        if existing_dbl_point_deduction < dbl_point_deduction
          ca_defect -= existing_dbl_point_deduction
          ca_defect += dbl_point_deduction
        end
      end
    elsif temp['area'] == 'UN'
      temp['sort_key'] = 'E-' + sort_key
      temp['area_name'] = 'Dwelling Unit'

      if points_map[map_key].nil?
        un_defect += dbl_point_deduction
      else
        existing_dbl_point_deduction = points_map[map_key]
        if existing_dbl_point_deduction < dbl_point_deduction
          un_defect -= existing_dbl_point_deduction
          un_defect += dbl_point_deduction
        end
      end
    end
    points_map[map_key] = dbl_point_deduction
    temp['comments'] = inspection['comments'].gsub('?', ' ') # str_replace("?", " ", $insp['comments']);
    temp['location'] = inspection['location'].present? ? inspection['location'].gsub('?', ' ') : '' # str_replace("?", " ", $insp['location']);

    index += 1
    details.push(temp)
  end

  if st_defect > score_data['ST'][:PossibleScore]
    score_data['ST'][:Deductions] = score_data['ST'][:PossibleScore]
    score_data['ST'][:Score] = 0
  else
    score_data['ST'][:Deductions] = st_defect
    score_data['ST'][:Score] = score_data['ST'][:PossibleScore] - st_defect

  end

  if be_defect > score_data['BE'][:PossibleScore]
    score_data['BE'][:Deductions] = score_data['BE'][:PossibleScore]
    score_data['BE'][:Score] = 0
  else
    score_data['BE'][:Deductions] = be_defect
    score_data['BE'][:Score] = score_data['BE'][:PossibleScore] - be_defect
  end

  if bs_defect > score_data['BS'][:PossibleScore]
    score_data['BS'][:Deductions] = score_data['BS'][:PossibleScore]
    score_data['BS'][:Score] = 0
  else
    score_data['BS'][:Deductions] = bs_defect
    score_data['BS'][:Score] = score_data['BS'][:PossibleScore] - bs_defect
  end

  if ca_defect > score_data['CA'][:PossibleScore]
    score_data['CA'][:Deductions] = score_data['CA'][:PossibleScore]
    score_data['CA'][:Score] = 0
  else
    score_data['CA'][:Deductions] = ca_defect
    score_data['CA'][:Score] = score_data['CA'][:PossibleScore] - ca_defect
  end

  if un_defect > score_data['UN'][:PossibleScore]
    score_data['UN'][:Deductions] = score_data['UN'][:PossibleScore]
    score_data['UN'][:Score] = 0
  else
    score_data['UN'][:Deductions] = un_defect
    score_data['UN'][:Score] = score_data['UN'][:PossibleScore] - un_defect
  end
  details = details.sort_by { |k| k['sort_key'] }
  details = ApplicationHelper.key_merge_sort(details, 'sort_key')
  view_data['details'] = details

  site = 1
  unit = 1
  ca = 1
  bs = 1
  be = 1

  view_data['details'].each do |key, data|
    if data['image_name']
      case data['area']
      when 'ST'
        view_data['details'][key]['image_id'] = 'S' + site.to_s
        site += 1
      when 'BS'
        view_data['details'][key]['image_id'] = 'BS' + bs.to_s
        bs += 1
      when 'BE'
        view_data['details'][key]['image_id'] = 'BE' + be.to_s
        be += 1
      when 'CA'
        view_data['details'][key]['image_id'] = 'CA' + ca.to_s
        ca += 1
      when 'UN'
        view_data['details'][key]['image_id'] = 'UN' + unit.to_s
        unit += 1
      else
        break
      end
    else
      view_data['details'][key]['image_id'] = '-'
    end
  end

  return view_data['details'] if @only_data
  return score_data if @get_score_data

  if @action_type == 'defect-priority-report'
    view_data['report_name'] = 'REAC Prep Priority Report'
    view_data['orientation'] = 'L'
    view_data['file_name'] = view_data['property']['property_name'].gsub('[^A-Za-z0-9]', '-') + '-REAC Prep Priority'
  else
    view_data['report_name'] = 'DEFICIENCY REPORT'
    view_data['orientation'] = 'L'
    view_data['file_name'] = view_data['property']['property_name'].gsub('[^A-Za-z0-9]', '-') + '-Deficiency'
  end

  view_data
end