Class: ReportsManager::NspireDefectReport

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

Constant Summary collapse

UNIT_SAMPLE_MAPPING =
{
  1..1 => 1,
  2..2 => 2,
  3..3 => 3,
  4..4 => 4,
  5..5 => 5,
  6..6 => 6,
  7..7 => 6,
  8..8 => 7,
  9..9 => 8,
  10..10 => 8,
  11..12 => 9,
  13..14 => 10,
  15..16 => 11,
  17..18 => 12,
  19..21 => 13,
  22..24 => 14,
  25..27 => 15,
  28..30 => 16,
  31..35 => 17,
  36..39 => 18,
  40..45 => 19,
  46..51 => 20,
  52..59 => 21,
  60..67 => 22,
  68..78 => 23,
  79..92 => 24,
  93..110 => 25,
  111..132 => 26,
  133..166 => 27,
  167..214 => 28,
  215..295 => 29,
  296..455 => 30,
  456..920 => 31,
  921..Float::INFINITY => 32 
}.freeze

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', inspection_details, inspectable_buildings, inspectable_units, items, defects) ⇒ NspireDefectReport

Returns a new instance of NspireDefectReport.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/reports_manager/nspire_defect_report.rb', line 5

def initialize(user_id, property_id, report_type, only_data = false, get_score_data = false, action_type = 'defect-report', inspection_details, inspectable_buildings, inspectable_units, items, defects)
  @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
  @inspection_details = inspection_details
  @inspectable_buildings = inspectable_buildings
  @inspectable_units = inspectable_units
  @items = items
  @defects = defects
end

Instance Method Details

#calculate_area_defects(area_data, property_inspection, inspected_buildings, property_id) ⇒ Object

Calculate area defects



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'app/services/reports_manager/nspire_defect_report.rb', line 284

def calculate_area_defects(area_data, property_inspection, inspected_buildings,property_id)
  areas = %w[OUTSIDE ST INSIDE]
  area_defects = Hash.new { |hash, key| hash[key] = [] }

  inspected_buildings.each do |inspected_building|
    building_inspection = property_inspection.select do |inspection|
      inspection['building_id'] == inspected_building['building_id'] && inspection['unit_id']
    end

    areas.each do |area|
      if area_data[area]
        area_defects[area].push(score_area(building_inspection, property_id, area, area_data[area]['area_code'], inspected_building['building_id']))
      end
    end
  end

  area_defects
end

#calculate_deficiencies(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id, possible) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'app/services/reports_manager/nspire_defect_report.rb', line 373

def calculate_deficiencies(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id, possible)
  inspection_data = []
  deductions = 0.0
  inspection_details.each do |inspection|
    item_area_code = inspection['item_code'][0] + inspection['item_code'][1]
    inspection_data.push(inspection) if item_area_code == area_code && !inspection['deficiency_deficiency_id'].nil? && inspection['deficiency_deficiency_id'] != -1
  end
  defects = {}
  inspection_data.each do |inspection|
    item_properties = inspection['item_properties']
    defect_properties = inspection['defect_properties']
    inspection_property = inspection['properties']

    item_weight = item_properties['item_weight'].to_i / 100
    defect_criticality = defect_properties['criticality']
    level = inspection_property['level']
    defect_code = inspection['deficiency_code']
    next if defects[defect_code].present?

    d = (item_weight * ApplicationHelper.get_criticality(defect_criticality) * ApplicationHelper.get_severity(level)).to_f * possible
    if area_code == 'INSIDE' || area_code == 'OUTSIDE' || area_code == 'ST'
      d = 10.0 if d > 10.0
    elsif area_code == 'UNIT'
      d = 5.0 if d > 5.0
    end
    deductions += d
    defects[defect_code] = (item_weight * ApplicationHelper.get_citicality(defect_criticality) * ApplicationHelper.get_severity(level)).to_f * possible
  end
  deductions.to_f
end

#calculate_score_and_update(temp, area_code, sort_key, dbl_point_deduction, defect_data, score_data) ⇒ Object

Scoring functions



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/services/reports_manager/nspire_defect_report.rb', line 224

def calculate_score_and_update(temp, area_code, sort_key, dbl_point_deduction, defect_data, score_data)

  if temp['area'] == 'ST'
    temp['area'] = 'OUTSIDE'
    temp['sort_key'] = 'A-' + sort_key
    temp['area_name'] = 'Outside'
  else
    temp['sort_key'] = "#{temp['area'][0]}-#{sort_key}"
    temp['area_name'] = temp['area'].capitalize
  end
  
  defect_data[temp['area']] = defect_data[temp['area']] + dbl_point_deduction.round(2)
end

#calculate_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/services/reports_manager/nspire_defect_report.rb', line 348

def calculate_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id)
  inspection_data = []
  possible = 0.0

  inspection_details.each do |inspection|
    item_code = inspection['item_code']

    next unless item_code[2] != 'H' && item_code[3] != 'S'

    properties = inspection['properties']
    item_area_code = inspection['item_code'][0] + inspection['item_code'][1]
    if item_area_code == area_code
      if item_code[0] == 'C' && item_code[1] == 'A'
        inspection_data.push(inspection) if inspection['deficiency_deficiency_id'].nil? && properties['status'] && properties['status'] != 'N/A'
      else
        inspection_data.push(inspection) if inspection['deficiency_deficiency_id'] && properties['status'] && properties['status'] == 'N/A'
      end
    end
  end

  possible = 1 if inspection_data.count.zero?
  possible = 0.0 if possible.negative?
  possible.to_f
end

#calculate_total_scores(defects, inspected_buildings_count) ⇒ Object



303
304
305
306
307
308
309
# File 'app/services/reports_manager/nspire_defect_report.rb', line 303

def calculate_total_scores(defects, inspected_buildings_count)
  total_scores = {}
  defects.each do |area, area_defects|
    total_scores[area] = ApplicationHelper.total_building_area_score(area_defects, inspected_buildings_count)
  end
  total_scores
end

#callObject



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
# File 'app/services/reports_manager/nspire_defect_report.rb', line 56

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
  un_defect = 0.0
  defect_data = {
    'OUTSIDE' => 0.0,
    'INSIDE' => 0.0,
    'UNIT' => 0.0
  }

  items = @items
  defects = @defects
  buildings = @inspectable_buildings
  units = @inspectable_units  
  score_data = score_areas(@property_id,@inspection_details)
  total_buildings = buildings.count
  total_units = units.count
  reac_sample_value=unit_sample_value(total_units)

  sort_keys = []
  total_deduction_points_unit = 0.0

  @inspection_details.each do |inspection|
    temp = {}
    item = items[inspection['inspectable_item_id']] || nil
  
    defect = defects[inspection['deficiency_definition_id']]&.presence

    building = buildings[inspection['building_id']]&.presence
  
    unit = units[inspection['unit_id']]&.presence

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

    next unless item && defect


    temp['line'] = index
    temp['id'] = inspection['id']
    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 : item['item_code'].present? ? item['item_code'][0] + item['item_code'][1] : ''
    temp['sort_key'] = 0

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

    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['definition_type'] = ApplicationHelper.updated_defect_definition_type(defect['definition_type'])

    temp['image_name'] = image_name
    sort_key = temp['building_name'] + '-' + temp['unit_name'] + '-' + temp['item'] + '-' + temp['defect'].to_s
  
    dbl_point_deduction = 0.0
    impact_weight = ApplicationHelper.get_area_impact_weight(temp['definition_type'])[area_code == 'ST' ? 'outside' : area_code.downcase]
    if impact_weight
      dbl_point_deduction = (defect.deduction_point || impact_weight.to_f) / total_units.to_f
    end

    #Calculate REAC_Value
    reac_value = (defect.deduction_point || impact_weight.to_f) / reac_sample_value
    temp['reac_value'] = reac_value.round(2)
    
    temp['points'] = dbl_point_deduction.round(2)
    temp['points_sort'] = dbl_point_deduction.round(2)

    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

    calculate_score_and_update(temp, area_code, sort_key, dbl_point_deduction, defect_data, score_data)
  
    st_defect = defect_data['OUTSIDE']
    be_defect = defect_data['INSIDE']
    un_defect = defect_data['UNIT']

    temp['comments'] = inspection['comments'].gsub('?', ' ') # str_replace("?", " ", $insp['comments']);
    temp['location'] = inspection['location'].present? ? inspection['location'].gsub('?', ' ') : '' # str_replace("?", " ", $insp['location']);

    index += 1

    # checking duplicates
    if temp['unit_id'].present?
      if sort_keys.include?(temp['sort_key'])
       temp['points'] = 0.0
       details.push(temp)
       next
      end
      total_deduction_points_unit = total_deduction_points_unit + dbl_point_deduction.round(2)
      dummy_key = temp['sort_key']
      sort_keys.push(dummy_key)
    end

    details.push(temp)
  end

  defect_data.each do |area, area_defect|
    if area_defect > score_data[area][:PossibleScore]
      score_data[area][:Deductions] = score_data[area][:PossibleScore]
      score_data[area][:Score] = 0
    else
      score_data[area][:Deductions] = area_defect
      score_data[area][:Score] = score_data[area][:PossibleScore] - area_defect
    end
  end
  
  details  = @action_type == 'defect-priority-report' ? details.sort_by { |k| k['points'] }.reverse : details.sort_by { |k| k['sort_key'] }
  # details = ApplicationHelper.key_merge_sort(details, 'sort_key')
  view_data['details'] = details
  view_data['priority_report'] = @action_type == 'defect-priority-report' ? true : false
  outside_img = 1
  unit_img = 1
  inside_img = 1

  view_data['details'].each_with_index do |data, idx|
    if data['image_name'].present?
      case data['area_name']
      when 'Outside'
        view_data['details'][idx]['image_id'] = 'O' + outside_img.to_s
        outside_img += 1
      when 'Inside'
        view_data['details'][idx]['image_id'] = 'I' + inside_img.to_s
        inside_img += 1
      when 'Unit'
        view_data['details'][idx]['image_id'] = 'UN' + unit_img.to_s
        unit_img += 1
      else
        break
      end
    else
      view_data['details'][idx]['image_id'] = ''
    end
  end
  return view_data['details'] if @only_data

  score_data['OUTSIDE'][:Deductions] = defect_data['OUTSIDE'] if score_data['OUTSIDE'][:Deductions].present?
  score_data['INSIDE'][:Deductions] = defect_data['INSIDE'] if score_data['INSIDE'][:Deductions].present?
  score_data['UNIT'][:Deductions] = total_deduction_points_unit if score_data['UNIT'][:Deductions].present?

  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

#normalize_percentages(score_details) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'app/services/reports_manager/nspire_defect_report.rb', line 324

def normalize_percentages(score_details)
  sum = 0.0
  data = {}
  score_details.each do |score_detail|
    sum += (score_detail[:PossibleScore] * score_detail[:AreaWeight]).to_f
  end

  score_details.each do |score_detail|
    possible = ((score_detail[:PossibleScore] * score_detail[:AreaWeight]).to_f / sum).to_f
    deduction = (score_detail[:Deductions] * possible).abs.to_f
    score = possible - deduction

    data[score_detail[:AreaID]] = {
        AreaWeight: score_detail[:AreaWeight].to_f,
        AreaID: score_detail[:AreaID],
        PossibleScore: (possible * 100).round(2).to_f,
        Score: (score * 100).round(2).to_f,
        Deductions: (deduction * 100).round(2).to_f

    }
  end
  data
end

#score_area(inspection_details, property_id, area_code, area_id, building_id = nil, unit_id = nil) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/services/reports_manager/nspire_defect_report.rb', line 311

def score_area(inspection_details, property_id, area_code, area_id, building_id = nil, unit_id = nil)
  possible = calculate_total_possible(inspection_details, property_id, area_code, area_id, building_id, unit_id)
  deficiencies = calculate_deficiencies(inspection_details, property_id, area_code, area_id, building_id, unit_id, possible)

  deficiencies = possible if deficiencies > possible

  {
      possible: possible,
      defect: deficiencies,
      building_id: building_id
  }
end

#score_areas(property_id, property_inspection) ⇒ Object



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
# File 'app/services/reports_manager/nspire_defect_report.rb', line 239

def score_areas(property_id,property_inspection)
  areas = InspectionArea.all
  area_data = {}

  areas.each do |area|
    area_data[area['area_code'].upcase] = area
  end
  inspected_buildings = Building.where(property_id: property_id).where("TRIM(properties->>'uninspectable') = ? OR properties->>'uninspectable' IS NULL", 'None')
  inspected_units = Unit.where(property_id: property_id).where("TRIM(properties->>'uninspectable') = ? OR properties->>'uninspectable' IS NULL", 'None')

  site_inspection = []

  property_inspection.each do |inspection|
    site_inspection.push(inspection) if inspection['building_id'].nil?
  end

  unit_score = []
  if area_data['UNIT']
    inspected_units.each do |inspected_unit|
      unit_inspection = []
      property_inspection.each do |inspection|
        unit_inspection.push(inspection) if inspection['unit_id'] == inspected_unit['id']
      end
      unit_score.push(score_area(unit_inspection, property_id, 'UNIT', area_data['UNIT']['area_code'], inspected_unit['building_id'], inspected_unit['unit_id']))
    end
  end
  area_defects = calculate_area_defects(area_data, property_inspection, inspected_buildings,property_id)

  total_unit_area_score = ApplicationHelper.total_unit_area_score(unit_score)

  total_scores = calculate_total_scores(area_defects, inspected_buildings.count)
  total_outside_score = total_scores['OUTSIDE']
  total_st_score = total_scores['ST']
  total_inside_score = total_scores['INSIDE']

  area_score = []
  area_score.push({ AreaWeight: 15, AreaID: 'OUTSIDE', PossibleScore: (total_outside_score[:PossiblePoints]).round(2), Score: '', Deductions: total_outside_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 15, AreaID: 'ST', PossibleScore: (total_st_score[:PossiblePoints]).round(2), Score: '', Deductions: total_st_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 20, AreaID: 'INSIDE', PossibleScore: (total_inside_score[:PossiblePoints]).round(2), Score: '', Deductions: total_inside_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 35, AreaID: 'UNIT', PossibleScore: (total_unit_area_score[:PossiblePoints]).round(2), Score: '', Deductions: total_unit_area_score[:Deductions].round(2) })

  normalize_percentages(area_score)
end

#unit_sample_value(total_units) ⇒ Object



404
405
406
407
408
409
# File 'app/services/reports_manager/nspire_defect_report.rb', line 404

def unit_sample_value(total_units)
  UNIT_SAMPLE_MAPPING.each do |range, sample_value|
    return sample_value if range.include?(total_units)
  end
  32 
end