Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Class Method Summary collapse

Class Method Details

.building_typesHash

Returns a hash of building types.

Returns:

  • (Hash)

    A hash of building types with their codes and descriptions.



85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/application_helper.rb', line 85

def self.building_types
  {
    '-1-1' => '',
    '100' => 'Common Building',
    '150' => 'Duplex',
    '200' => 'Lowrise/Garden Apartment',
    '300' => 'Mid/High Rise',
    '400' => 'Row/Townhouse',
    '500' => 'Single Family '
  }
end

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



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'app/helpers/application_helper.rb', line 625

def self.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 == 'ST'
      d = 7.5 if d > 7.5
    elsif area_code == 'BS' || area_code == 'BE' || area_code == 'CA'
      d = 10.0 if d > 10.0
    elsif area_code == 'UN'
      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_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'app/helpers/application_helper.rb', line 586

def self.calculate_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id)
  inspection_data = []
  possible = 0.0
  deductions = 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

  if inspection_data.count.zero? && area_code != 'CA'
    possible = 1
  else
    inspection_data.each do |inspection|
      item_code = inspection['item_code']
      item_properties = inspection['item_properties']
      if item_code[0] == 'C' && item_code[1] == 'A'
        possible += (item_properties['item_weight'] / 100).to_f
      else
        deductions += (item_properties['item_weight'] / 100).to_f
      end
    end
  end
  possible = (1.0 - deductions) if area_code != 'CA'
  possible = 0.0 if possible.negative?
  possible.to_f
end

.custom_sort(data, key) ⇒ Object



789
790
791
792
793
794
# File 'app/helpers/application_helper.rb', line 789

def self.custom_sort(data, key)
  data.sort_by! do |item|
    value = item[key]
    [ApplicationHelper.numeric_sort_at_beginning(value), value.downcase]
  end
end

.get_all_nspire_report_typesHash

Gets all NSPIRE report types.

Returns:

  • (Hash)

    A hash of all NSPIRE report types with keys and descriptions.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/helpers/application_helper.rb', line 183

def self.get_all_nspire_report_types
  {
    all_nspire: 'All NSPIRE',
    mass_certificate: 'Mass Certificate Report',
    lt_report: 'LT Defects Report',
    scoring_report: 'Score Report',
    defect_report: 'Defect Report',
    house_keeping_report: 'HouseKeeping Report',
    fixed_assets_excel: 'Fixed Assets Report',
    single_unit_report: 'Single Unit Report',
    defect_report_excel: 'Defect Report Excel',
    building_report: 'Building Report',
    defect_picture_report: 'Defect Picture Report',
    cover_report: 'Cover Report',
    priority_defect_report: 'Priority Defect Report',
    curb_appeal_report: 'Curb Appeal Report',
    lbp_visual_assessment_report: 'LBP Visual Assessment Report'
  }
end

.get_all_report_typesHash

Gets all report types.

Returns:

  • (Hash)

    A hash of all report types with keys and descriptions.



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
# File 'app/helpers/application_helper.rb', line 116

def self.get_all_report_types
  {
    all_upcs: 'All UPCS',
    all_nspire: 'All NSPIRE',
    nspire_mass_certificate: 'Mass Certificate Report(NSPIRE)',
    nspire_lt_report: 'LT Defects Report(NSPIRE)',
    nspire_scoring_report: 'Score Report(NSPIRE)',
    nspire_defect_report: 'Defect Report(NSPIRE)',
    nspire_house_keeping_report: 'HouseKeeping Report(NSPIRE)',
    nspire_single_unit_report: 'Single Unit Report(NSPIRE)',
    cover_report: 'Cover Report',
    defect_report: 'Defect Report',
    priority_repair_report: 'Priority Repair Report',
    scoring_report: 'Score Report',
    house_keeping_report: 'HouseKeeping Report',
    single_unit_report: 'Single Unit Report',
    building_report: 'Profile Report',
    mass_certificate: 'Mass Certificate Report',
    lt_report: 'LT Defects Report',
    lt_report_v2: 'LT Defects Report - WSHFC Version',
    defect_report_excel: 'Defect Report (Ms Excel)',
    defect_picture_report: 'Defect Picture Report',
    fixed_assets_excel: 'Fixed Assets Report (Excel)',
    unit_defect_count: 'Unit Defect Count',
    property_photos_excel: 'Photos Status(Excel)',
    upcs_forms_st_report: 'UPCS Form - Site (Excel)',
    upcs_forms_ca_report: 'UPCS Form - Common Area (Excel)',
    upcs_forms_bs_report: 'UPCS Form - Building System (Excel)',
    upcs_forms_be_report: 'UPCS Form - Building Exterior (Excel)',
    upcs_forms_un_report: 'UPCS Form - Unit (Excel)',
    curb_appeal_report: 'Curb Appeal Report'
  }
end

.get_all_upcs_report_typesHash

Gets all UPCS report types.

Returns:

  • (Hash)

    A hash of all UPCS report types with keys and descriptions.



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
# File 'app/helpers/application_helper.rb', line 153

def self.get_all_upcs_report_types
  {
    all_upcs: 'All UPCS',
    cover_report: 'Cover Report',
    defect_report: 'Defect Report',
    priority_repair_report: 'Priority Repair Report',
    scoring_report: 'Score Report',
    house_keeping_report: 'HouseKeeping Report',
    single_unit_report: 'Single Unit Report',
    building_report: 'Profile Report',
    mass_certificate: 'Mass Certificate Report',
    lt_report: 'LT Defects Report',
    lt_report_v2: 'LT Defects Report - WSHFC Version',
    defect_report_excel: 'Defect Report (Ms Excel)',
    defect_picture_report: 'Defect Picture Report',
    fixed_assets_excel: 'Fixed Assets Report',
    unit_defect_count: 'Unit Defect Count',
    property_photos_excel: 'Photos Status(Excel)',
    upcs_forms_st_report: 'UPCS Form - Site (Excel)',
    upcs_forms_ca_report: 'UPCS Form - Common Area (Excel)',
    upcs_forms_bs_report: 'UPCS Form - Building System (Excel)',
    upcs_forms_be_report: 'UPCS Form - Building Exterior (Excel)',
    upcs_forms_un_report: 'UPCS Form - Unit (Excel)',
    curb_appeal_report: 'Curb Appeal Report'
  }
end

.get_area_impact_weight(area) ⇒ Object



418
419
420
421
422
423
424
425
# File 'app/helpers/application_helper.rb', line 418

def self.get_area_impact_weight(area)
  {
    'Low' => { 'unit' => 2.4, 'inside' => 2.2, 'outside' => 2.0 },
    'Moderate' => { 'unit' => 5.5, 'inside' => 5.0, 'outside' => 4.5 },
    'Severe' => { 'unit' => 14.8, 'inside' => 13.4, 'outside' => 12.2 },
    'Severe LT' => { 'unit' => 60.0, 'inside' => 54.5, 'outside' => 49.6 }
  }[area]
end

.get_certificate_listArray

Gets a list of certificate types.

Returns:

  • (Array)

    An array of certificate types with keys and display text.



70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/application_helper.rb', line 70

def self.get_certificate_list
  ret = []
  ret.push({ key: 'boilers', display_text: 'Boilers' })
  ret.push({ key: 'elevator', display_text: 'Elevator' })
  ret.push({ key: 'fire_alarm', display_text: 'Fire Alarm' })
  ret.push({ key: 'lead_based_paint_disclosure_form', display_text: 'Lead Based Paint Disclosure Form' })
  ret.push({ key: 'lead_based_paint_inspection_report', display_text: 'Lead Based Paint Inspection Report' })
  ret.push({ key: 'sprinkler_system', display_text: 'Sprinkler System' })

  ret
end

.get_criticality(criticality) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/helpers/application_helper.rb', line 384

def self.get_criticality(criticality)
  return 5.0 if criticality == 5

  return 3.0 if criticality == 4

  return 2.25 if criticality == 3

  return 1.25 if criticality == 2

  return 0.5 if criticality == 1

  0.0
end

.get_defect_details(ids, property_defects, type) ⇒ Array

Gets defect details for a report.

Parameters:

  • ids (Array)

    Array of defect IDs to retrieve details for.

  • property_defects (Array)

    Array of property defects.

  • type (String)

    The type of defect.

Returns:

  • (Array)

    An array of defect details.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'app/helpers/application_helper.rb', line 302

def self.get_defect_details(ids, property_defects, type)
  ret = []
  all_border = { 'borderBottomSize' => 5, 'borderTopSize' => 5, 'borderRightSize' => 5, 'borderLeftSize' => 5 }
  all_border_bg = { 'bgColor' => '000000', 'borderBottomSize' => 5, 'borderTopSize' => 5, 'borderRightSize' => 5, 'borderLeftSize' => 5 }
  if ids.present?
    property_defects.each do |defect|
      temp = {}

      next unless ids.include? defect['deficiency_definition_id']

      temp['comments'] = defect['location'] + ': ' + defect['comments']
      temp['A'] = type == 'A' ? all_border_bg : all_border
      temp['B'] = type == 'B' ? all_border_bg : all_border
      temp['C'] = type == 'C' ? all_border_bg : all_border
      temp['D'] = type == 'D' ? all_border_bg : all_border
      temp['E'] = type == 'E' ? all_border_bg : all_border
      temp['F'] = type == 'F' ? all_border_bg : all_border
      temp['G'] = type == 'G' ? all_border_bg : all_border
      temp['H'] = type == 'H' ? all_border_bg : all_border
      temp['I'] = type == 'I' ? all_border_bg : all_border

      temp['AA'] = type == 'A' ? 'bgcolor="#000000"' : ''
      temp['BB'] = type == 'B' ? 'bgcolor="#000000"' : ''
      temp['CC'] = type == 'C' ? 'bgcolor="#000000"' : ''
      temp['DD'] = type == 'D' ? 'bgcolor="#000000"' : ''
      temp['EE'] = type == 'E' ? 'bgcolor="#000000"' : ''
      temp['FF'] = type == 'F' ? 'bgcolor="#000000"' : ''
      temp['GG'] = type == 'G' ? 'bgcolor="#000000"' : ''
      temp['HH'] = type == 'H' ? 'bgcolor="#000000"' : ''
      temp['II'] = type == 'I' ? 'bgcolor="#000000"' : ''

      temp['location_1'] = ''
      temp['location_2'] = ''
      if !defect['building_id'].present? || defect['building_id'] == -1
        temp['location_1'] = 'Site'
      elsif defect['unit_id'].present?
        unit = Unit.find_by_old_id(defect['unit_id'])
        building = Building.find_by_old_id(defect['building_id'])

        temp['location_2'] = unit['unit_name'].present? ? unit['unit_name'] : 'Unit'
        temp['location_1'] = building['building_name'].present? ? building['building_name'] : 'Building'
      else
        item = InspectableItem.find(defect['inspectable_item_id'])
        item_code = ''

        item_code = item['item_code'][0] + item['item_code'][1] if item.present?
        building = Building.find_by_old_id(defect['building_id'])
        if item_code == 'CA'
          temp['location_2'] = building['building_name'].present? ? building['building_name'] : 'Building'
        else
          unit = Unit.find_by_old_id(defect['unit_id'])
          temp['location_1'] = building['building_name'].present? ? building['building_name'] : 'Building'
        end
      end
      ret.push(temp)
    end
  end

  ret
end

.get_defect_for_score_report(_property_id, inspection_details) ⇒ Hash

Gets defect details for a score report.

Parameters:

  • property_id (Integer)

    The property's ID.

  • inspection_details (Array)

    Array of inspection details.

Returns:

  • (Hash)

    A hash containing defect details.



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
# File 'app/helpers/application_helper.rb', line 23

def self.get_defect_for_score_report(_property_id, inspection_details)
  observation = {}
  observation['ST'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0, 'NLT' => 0, 'LT' => 0, 'NHS' => 0, 'SD' => 0 }
  observation['BE'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0, 'NLT' => 0, 'LT' => 0, 'NHS' => 0, 'SD' => 0 }
  observation['BS'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0, 'NLT' => 0, 'LT' => 0, 'NHS' => 0, 'SD' => 0 }
  observation['CA'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0, 'NLT' => 0, 'LT' => 0, 'NHS' => 0, 'SD' => 0 }
  observation['UN'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0, 'NLT' => 0, 'LT' => 0, 'NHS' => 0, 'SD' => 0 }
  observation['total'] = { 'OD' => 0, 'NOD' => 0, 'NA' => 0, 'L1' => 0, 'L2' => 0, 'L3' => 0 }

  inspection_details.each do |inspection|
    prop = JSON.parse(inspection['properties'])
    item_code = inspection['item_code']
    area_code = item_code[0] + item_code[1]
    if inspection['deficiency_definition_id'].nil?
      if observation[area_code].present?
        if prop['status'].present?
          if prop['status'] == 'N/A'
            observation[area_code]['NA'] += 1
            observation['total']['NA'] += 1
          elsif prop['status'] == 'OD'
            observation[area_code]['OD'] += 1
            observation['total']['OD'] += 1
          elsif prop['status'] == 'NOD'
            observation[area_code]['NOD'] += 1
            observation['total']['NOD'] += 1
          end
        end
      else
        area_code[area_code]['NA'] += 1
      end
    else
      level = prop['level']
      if observation[area_code].present?
        if observation[area_code][level].present?
          observation[area_code][level] += 1
          observation['total'][level] += 1
          observation[area_code][inspection['type']] += 1 if level == 'L3'
        end
      end
    end
  end
  observation
end

.get_inside_area_impact_weightObject



436
437
438
439
440
441
442
443
# File 'app/helpers/application_helper.rb', line 436

def self.get_inside_area_impact_weight
  {
      'Advisory'=> 2.2,
      'Moderate-HS'=> 5.0,
      'Severe-HS'=> 13.4,
      'Severe-HS-LT'=> 54.5,
  }
end

.get_inspectable_buildings(_property_id, buildings = nil) ⇒ Object



463
464
465
466
467
468
469
# File 'app/helpers/application_helper.rb', line 463

def self.get_inspectable_buildings(_property_id, buildings = nil)
  buildings.each do |building|
    prop = building['properties']
    # buildings.delete(building.id) if prop['uninspectable'] && prop['uninspectable'] != 'None'
  end
  buildings
end

.get_inspected_units(_property_id, units = nil) ⇒ Object



455
456
457
458
459
460
461
# File 'app/helpers/application_helper.rb', line 455

def self.get_inspected_units(_property_id, units = nil)
  units.each do |unit|
    prop = unit.properties
    # units.delete(unit.id) if prop['uninspectable'] && prop['uninspectable'] != 'None'
  end
  units
end

.get_inspection_date(property) ⇒ String

Gets the inspection date for a property.

Parameters:

  • property (Object)

    The property to get the inspection date for.

Returns:

  • (String)

    The inspection date in the format "start_date - end_date" if available; otherwise, the property's updated date.



8
9
10
11
12
13
14
15
16
# File 'app/helpers/application_helper.rb', line 8

def self.get_inspection_date(property)
  props = property.properties
  if props['inspection_start_date'].present? && props['inspection_end_date'].present? &&
     props['inspection_start_date'] != props['inspection_end_date']
    props['inspection_start_date'] + ' - ' + props['inspection_end_date']
  else
    property.updated_at
  end
end

.get_inspire_report_typesHash

Gets NSPIRE report types.

Returns:

  • (Hash)

    A hash of NSPIRE report types with keys and descriptions.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/application_helper.rb', line 100

def self.get_inspire_report_types
  {
    nspire_mass_certificate: 'Mass Certificate Report(NSPIRE)',
    nspire_lt_report: 'LT Defects Report(NSPIRE)',
    nspire_scoring_report: 'Score Report(NSPIRE)',
    nspire_defect_report: 'Defect Report(NSPIRE)',
    nspire_house_keeping_report: 'HouseKeeping Report(NSPIRE)',
    nspire_single_unit_report: 'Single Unit Report(NSPIRE)',
    curb_appeal_report: 'Curb Appeal Report'

  }
end

.get_outside_area_impact_weightObject



445
446
447
448
449
450
451
452
# File 'app/helpers/application_helper.rb', line 445

def self.get_outside_area_impact_weight
  {
      'Advisory'=> 2.0,
      'Moderate-HS'=> 4.5,
      'Severe-HS'=> 12.2,
      'Severe-HS-LT'=> 49.6,
  }
end

.get_report_extHash

Gets report extensions.

Returns:

  • (Hash)

    A hash of report extensions with report type keys and extensions.



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
# File 'app/helpers/application_helper.rb', line 206

def self.get_report_ext
  {

      cover_report: 'pdf',
      defect_report: 'pdf',
      priority_repair_report: 'pdf',
      scoring_report: 'pdf',
      house_keeping_report: 'pdf',
      single_unit_report: 'pdf',
      building_report: 'pdf',
      mass_certificate: 'pdf',
      lt_report: 'pdf',
      lt_report_v2: 'pdf',
      defect_report_excel: 'excel',
      defect_picture_report: 'pdf',
      fixed_assets_excel: 'excel',
      unit_defect_count: 'excel',
      property_photos_excel: 'excel',
      upcs_forms_st_report: 'excel',
      upcs_forms_ca_report: 'excel',
      upcs_forms_bs_report: 'excel',
      upcs_forms_be_report: 'excel',
      upcs_forms_un_report: 'excel',
      nspire_defect_report_excel: 'excel',
      priority_defect_report: 'pdf',
      curb_appeal_report: 'pdf',
      lbp_visual_assessment_report: 'pdf',
  }
end

.get_severity(level) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
# File 'app/helpers/application_helper.rb', line 406

def self.get_severity(level)
  if level == 'L1'
    return 0.25
  elsif level == 'L2'
    return 0.5
  elsif level == 'L3'
    return 1.0
  end

  0.0
end

.get_unit_area_impact_weightObject



427
428
429
430
431
432
433
434
# File 'app/helpers/application_helper.rb', line 427

def self.get_unit_area_impact_weight
  {
      'Low'=> 2.4,
      'Moderate'=> 5.5,
      'Severe'=> 14.8,
      'Severe LT'=> 60.0,
  }
end

.inspection_date_for_reports(property) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/application_helper.rb', line 284

def self.inspection_date_for_reports(property)
  property = property.as_json
  prop = property['properties']
  if prop['inspection_end_date'].present? && prop['inspection_start_date'].present? && prop['inspection_start_date'] != prop['inspection_end_date']
    prop['inspection_start_date'].to_date.strftime("%b %d, %Y") + '  -  ' + prop['inspection_end_date'].to_date.strftime("%b %d,%Y")
  elsif prop['inspection_end_date'].present? && prop['inspection_start_date'].present? && prop['inspection_start_date'] == prop['inspection_end_date']
    prop['inspection_start_date'].to_date.strftime("%b %d, %Y")
  else
    property['updated_at'].to_date.strftime("%b %d, %Y")
  end
end

.is_house_keeping_item_code(item_code) ⇒ Object



398
399
400
# File 'app/helpers/application_helper.rb', line 398

def self.is_house_keeping_item_code(item_code)
  item_code == 'UN05000001' || item_code == 'CA05000001'
end

.is_non_deductable_item_code(item_code) ⇒ Object



402
403
404
# File 'app/helpers/application_helper.rb', line 402

def self.is_non_deductable_item_code(item_code)
  item_code == 'UN0001' || item_code == 'IN0001' || item_code == 'UN0002' || item_code == 'IN0002' || item_code == 'HK01'
end

.key_merge_sort(array, key, _sort_flags = 0, string_keys = 0) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
# File 'app/helpers/application_helper.rb', line 751

def self.key_merge_sort(array, key, _sort_flags = 0, string_keys = 0)
  data = {}
  array.each do |obj|
    if string_keys == 1 then
      data[obj[key].to_s] = obj
    else
      data[obj[key]] = obj
    end
  end
  data
end

.lt_defectsObject



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/helpers/application_helper.rb', line 363

def self.lt_defects
  all_defects = DeficiencyDefinition.all
  lt_defects = {}
  all_defects.each do |defect|
    next unless defect['definition_type'].strip.upcase == 'LT' || defect['definition_type'].strip.upcase == 'SD'

    prop = defect['properties']
    lt_type = prop['LT_type'].present? ? prop['LT_type'].strip.upcase : nil

    next unless lt_type.present?

    unless lt_defects[lt_type].present?
      lt_defects[lt_type] = {}
      lt_defects[lt_type]['ids'] = []
    end
    lt_defects[lt_type]['name'] = defect['title']
    lt_defects[lt_type]['ids'].push(defect['id'])
  end
  lt_defects
end

.merge(left, right) ⇒ Object



772
773
774
775
776
777
778
779
780
781
782
# File 'app/helpers/application_helper.rb', line 772

def self.merge(left, right)
  sorted = []
  until left.empty? || right.empty?
    sorted << if left.first <= right.first
                left.shift
              else
                right.shift
              end
  end
  sorted.concat(left).concat(right)
end

.mergesort(list) ⇒ Object



763
764
765
766
767
768
769
770
# File 'app/helpers/application_helper.rb', line 763

def self.mergesort(list)
  return list if list.size <= 1

  mid   = list.size / 2
  left  = list[0...mid]
  right = list[mid...list.size]
  ApplicationHelper.merge(ApplicationHelper.mergesort(left), ApplicationHelper.mergesort(right))
end

.normalize_percentages(score_details) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'app/helpers/application_helper.rb', line 549

def self.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

.nspire_report_listObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/helpers/application_helper.rb', line 261

def self.nspire_report_list
  [
    # 'priority_repair_report',
    'building_report',
    'cover_report',
    'scoring_report',
    'mass_certificate',
    'lt_report',
    'defect_report',
    'house_keeping_report',
    'single_unit_report',
    'defect_picture_report',
    'fixed_assets_excel',
    # 'unit_defect_count',
    # 'property_photos_excel',
    'defect_report_excel',
    'priority_defect_report',
    'curb_appeal_report',
  ]

end

.numeric_sort_at_beginning(value) ⇒ Object



784
785
786
787
# File 'app/helpers/application_helper.rb', line 784

def self.numeric_sort_at_beginning(value)
  numeric_part = value[/^\d+/]
  numeric_part ? numeric_part.to_i : Float::INFINITY
end

.remove_duplicate_defects(property_id) ⇒ Object



796
797
798
799
800
801
802
803
804
805
# File 'app/helpers/application_helper.rb', line 796

def self.remove_duplicate_defects(property_id)
  query = "DELETE FROM inspection_details
          WHERE property_id = #{property_id}
          AND id NOT IN (
            SELECT MAX(id)
            FROM inspection_details
            GROUP BY property_id, building_id, unit_id, inspectable_item_id, deficiency_definition_id, comments, location
            );"
  ActiveRecord::Base.connection.execute(query)
end

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



573
574
575
576
577
578
579
580
581
582
583
584
# File 'app/helpers/application_helper.rb', line 573

def self.score_area(inspection_details, property_id, area_code, area_id, building_id = nil, unit_id = nil)
  possible = ApplicationHelper.calculate_total_possible(inspection_details, property_id, area_code, area_id, building_id, unit_id)
  deficiencies = ApplicationHelper.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) ⇒ Object


            Scoring  Related Functions



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'app/helpers/application_helper.rb', line 474

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

  areas.each do |area|
    area_data[area['area_code'].upcase] = area
  end

  property_units = Unit.where(property_id: property_id)
  property_buildings = Building.where(property_id: property_id)
  property_inspection = InspectionDetail.get_details(property_id) # .where(property_id: property_id)

  property_score  = {}
  site_inspection = []

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

  property_score = ApplicationHelper.score_area(site_inspection, property_id, 'ST', area_data['ST']['area_code']) if area_data['ST']
  unit_score = []
  if area_data['UN']
    inspected_units = ApplicationHelper.get_inspected_units(property_id, property_units)
    inspected_units.each do |inspected_unit|
      unit_inspection = []
      property_inspection.each do |inspection|
        unit_inspection.push(inspection) if inspection['unit_id'] == inspected_unit['unit_id']
      end
      unit_score.push(ApplicationHelper.score_area(unit_inspection, property_id, 'UN', area_data['UN']['area_code'], inspected_unit['building_id'], inspected_unit['unit_id']))
    end
  end

  inspected_buildings = ApplicationHelper.get_inspectable_buildings(property_id, property_buildings)

  ca_score = []
  be_score = []
  bs_score = []

  inspected_buildings.each do |inspected_building|
    building_inspection =  []
    property_inspection.each do |inspection|
      building_inspection.push(inspection) if inspection['building_id'] == inspected_building['building_id'] && inspection['unit_id']
    end

    if area_data['CA']
      ca_score.push(ApplicationHelper.score_area(building_inspection, property_id, 'CA', area_data['CA']['area_code'],
                                                 inspected_building['building_id']))
    end
    if area_data['BE']
      be_score.push(ApplicationHelper.score_area(building_inspection, property_id, 'BE', area_data['BE']['area_code'],
                                                 inspected_building['building_id']))
    end
    if area_data['BS']
      bs_score.push(ApplicationHelper.score_area(building_inspection, property_id, 'BS', area_data['BS']['area_code'],
                                                 inspected_building['building_id']))
    end
  end

  total_unit_area_score = ApplicationHelper.total_unit_area_score(unit_score)

  total_ca_area_score = ApplicationHelper.total_common_area_score(ca_score, inspected_buildings.count)

  total_exterior_area_score = ApplicationHelper.total_building_area_score(be_score, inspected_buildings.count)
  total_system_area_score = ApplicationHelper.total_building_area_score(bs_score, inspected_buildings.count)

  area_score = []
  area_score.push({ AreaWeight: 15, AreaID: 'ST', PossibleScore: (property_score[:possible]).round(2), Score: '', Deductions: property_score[:defect].round(2) })
  area_score.push({ AreaWeight: 15, AreaID: 'BE', PossibleScore: (total_exterior_area_score[:PossiblePoints]).round(2), Score: '', Deductions: total_exterior_area_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 20, AreaID: 'BS', PossibleScore: (total_system_area_score[:PossiblePoints]).round(2), Score: '', Deductions: total_system_area_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 15, AreaID: 'CA', PossibleScore: (total_ca_area_score[:PossiblePoints]).round(2), Score: '', Deductions: total_ca_area_score[:Deductions].round(2) })
  area_score.push({ AreaWeight: 35, AreaID: 'UN', PossibleScore: (total_unit_area_score[:PossiblePoints]).round(2), Score: '', Deductions: total_unit_area_score[:Deductions].round(2) })

  ApplicationHelper.normalize_percentages(area_score)
end

.total_building_area_score(scores, total_buildings) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'app/helpers/application_helper.rb', line 681

def self.total_building_area_score(scores, total_buildings)
  total_weight = 0.0
  total_score = 0.0
  total_possible = 0.0
  dbl_score = {}
  total_units = 0
  counter = 0
  scores.each do |obj|
    units = Unit.where(building_id: obj[:building_id])
    obj[:units] = units.count
    total_units += units.count
  end

  avg_unit = if total_buildings.zero?
               1
             else
               total_units / total_buildings
             end

  scores.each do |obj|
    units = obj[:units] != 0 ? obj[:units] : avg_unit
    dbl_score[counter] = (obj[:possible] * units).to_f
    total_weight += dbl_score[counter]
    total_possible += obj[:possible]
    counter += 1
  end

  total_possible = (total_possible / total_buildings).to_f

  counter = 0
  scores.each do |obj|
    dbl_score[counter] = ((obj[:possible] - obj[:defect]).to_f / obj[:possible]).to_f * (dbl_score[counter] / total_weight).to_f if total_weight != 0
    total_score += dbl_score[counter]
    counter += 1
  end

  {
    PossiblePoints: total_possible.to_f,
    Deductions: (total_possible - (total_possible * total_score)).to_f
  }
end

.total_common_area_score(ca_scores, int_common_areas) ⇒ Object



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'app/helpers/application_helper.rb', line 723

def self.total_common_area_score(ca_scores, int_common_areas)
  total_weight = 0.0
  total_score = 0.0
  score = {}
  counter = 0

  ca_scores.each do |ca_score|
    if ca_score[:possible].to_i != 0
      score[counter] = ca_score[:possible]
      total_weight += ca_score[:possible]
      counter += 1
    else
      ca_scores.delete(ca_score)
    end
  end
  total_possible = (total_weight / int_common_areas).to_f

  counter = 0
  ca_scores.each do |ca_score|
    total_score += ((ca_score[:possible] - ca_score[:defect]).to_f / ca_score[:possible]).to_f * (score[counter] / total_weight).to_f
    counter += 1
  end
  {
    PossiblePoints: total_possible.to_f,
    Deductions: (total_possible - (total_possible * total_score)).to_f
  }
end

.total_unit_area_score(unit_scores) ⇒ Object



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'app/helpers/application_helper.rb', line 658

def self.total_unit_area_score(unit_scores)
  total_weight = 0.0
  total_score = 0.0
  score = {}
  counter = 0
  unit_scores.each do |unit_score|
    score[counter] = unit_score[:possible]
    total_weight += unit_score[:possible].to_i
    counter += 1
  end
  total_possible = (total_weight / unit_scores.count).to_f

  counter = 0
  unit_scores.each do |unit_score|
    total_score += ((unit_score[:possible].to_f - unit_score[:defect].to_f) / unit_score[:possible].to_f).to_f * (score[counter] / total_weight).to_f
    counter += 1
  end
  {
    PossiblePoints: total_possible.to_f,
    Deductions: (total_possible - (total_possible * total_score)).to_f
  }
end

.upcs_report_listObject



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
# File 'app/helpers/application_helper.rb', line 236

def self.upcs_report_list
  [

      'cover_report',
      'defect_report',
      'priority_repair_report',
      'scoring_report',
      'house_keeping_report',
      'single_unit_report',
      'building_report',
      'mass_certificate',
      'lt_report',
      'lt_report_v2',
      'defect_report_excel',
      'defect_picture_report',
      # 'fixed_assets_excel',
      'unit_defect_count',
      'property_photos_excel',
      'upcs_forms_st_report',
      'upcs_forms_ca_report',
      'upcs_forms_bs_report',
      'upcs_forms_be_report',
      'upcs_forms_un_report'
  ]
end

.update_buildings_details_in_property(property_id) ⇒ Object



845
846
847
848
849
850
# File 'app/helpers/application_helper.rb', line 845

def self.update_buildings_details_in_property(property_id)
  property = Property.find_by_id(property_id)
  property.details['total_buildings'] = property.total_buildings
  property.details['inspectable_buildings'] = property.total_inspectable_buildings
  property.save
end

.update_buildings_units_details_in_property(property_id) ⇒ Object



859
860
861
862
863
864
865
866
# File 'app/helpers/application_helper.rb', line 859

def self.update_buildings_units_details_in_property(property_id)
  property = Property.find_by_id(property_id)
  property.details['total_buildings'] = property.total_buildings
  property.details['inspectable_buildings'] = property.total_inspectable_buildings
  property.details['total_units'] = property.total_units
  property.details['inspectable_units'] = property.total_inspectable_units
  property.save
end

.update_fixed_asset_details(property_id) ⇒ Object



807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'app/helpers/application_helper.rb', line 807

def self.update_fixed_asset_details(property_id)
  query_to_update_building_id = <<~SQL 
    UPDATE fixed_assets
    SET building_id = buildings.id
    FROM buildings
    WHERE fixed_assets.building_id = buildings.old_id
    AND buildings.old_id IS NOT NULL
    AND buildings.property_id = #{property_id};
  SQL

  query_to_update_unit_id = <<~SQL 
    UPDATE fixed_assets
    SET unit_id = units.id
    FROM units
    WHERE fixed_assets.unit_id = units.old_id
    AND units.old_id IS NOT NULL
    AND units.property_id = #{property_id};
  SQL

  ActiveRecord::Base.connection.execute(query_to_update_building_id)
  ActiveRecord::Base.connection.execute(query_to_update_unit_id)
end

.update_units_details_in_property(property_id) ⇒ Object



852
853
854
855
856
857
# File 'app/helpers/application_helper.rb', line 852

def self.update_units_details_in_property(property_id)
  property = Property.find_by_id(property_id)
  property.details['total_units'] = property.total_units
  property.details['inspectable_units'] = property.total_inspectable_units
  property.save
end

.updated_defect_definition_type(defect) ⇒ Object



830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'app/helpers/application_helper.rb', line 830

def self.updated_defect_definition_type(defect)
  case defect
  when "Severe-HS-LT"
    "Severe LT"
  when "Severe-HS"
    "Severe"
  when "Moderate-HS"
    "Moderate"
  when "Advisory"
    "Low"
  else
    defect
  end
end