Class: ReportsManager::NspireDefectReport
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- ReportsManager::NspireDefectReport
- Defined in:
- app/services/reports_manager/nspire_defect_report.rb
Instance Method Summary collapse
-
#calculate_area_defects(area_data, property_inspection, inspected_buildings, property_id) ⇒ Object
Calculate area defects.
- #calculate_deficiencies(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id, possible) ⇒ Object
-
#calculate_score_and_update(temp, area_code, sort_key, dbl_point_deduction, defect_data, score_data) ⇒ Object
Scoring functions.
- #calculate_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id) ⇒ Object
- #calculate_total_scores(defects, inspected_buildings_count) ⇒ Object
- #call ⇒ Object
-
#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
constructor
A new instance of NspireDefectReport.
- #normalize_percentages(score_details) ⇒ Object
- #score_area(inspection_details, property_id, area_code, area_id, building_id = nil, unit_id = nil) ⇒ Object
- #score_areas(property_id, property_inspection) ⇒ Object
Methods inherited from ApplicationService
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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 238 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
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 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 327 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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 175 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 updated_defect = defect_data[temp['area']] + dbl_point_deduction defect_data[temp['area']] = updated_defect # Update the defect value for the specific area end |
#calculate_total_possible(inspection_details, _property_id, area_code, _area_id, _building_id, _unit_id) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 302 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
257 258 259 260 261 262 263 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 257 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 |
#call ⇒ Object
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 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 19 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 sort_keys = [] @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'] = 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 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 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 = details.sort_by { |k| k['sort_key'] } # details = ApplicationHelper.key_merge_sort(details, 'sort_key') view_data['details'] = details 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 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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 278 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
265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 265 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
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 |
# File 'app/services/reports_manager/nspire_defect_report.rb', line 193 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("properties->>'uninspectable' = ? OR properties->>'uninspectable' IS NULL", 'None') inspected_units = Unit.where(property_id: property_id).where("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 |