Class: ReportsManager::ItemBasedInspectionReport

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

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type, inspectable_buildings, inspectable_units, report_category) ⇒ ItemBasedInspectionReport

Returns a new instance of ItemBasedInspectionReport.



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

def initialize(user_id, property_id, report_type, inspectable_buildings, inspectable_units, report_category)
  @user_id = user_id
  @property_id = property_id
  @report_type = report_type
  @inspectable_buildings = inspectable_buildings
  @inspectable_units = inspectable_units
  @report_category = report_category # 'curb_appeal' or 'house_keeping'
end

Instance Method Details

#callObject



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

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

  item = fetch_inspectable_item
  defects = DeficiencyDefinition.where(inspectable_item_id: item.id) if item.present?
  defect_ids = defects.present? ? defects.pluck("id") : []
  inspections = InspectionDetail.where(property_id: @property_id).where(deficiency_definition_id: defect_ids)
  buildings = @inspectable_buildings
  units = @inspectable_units

  inspections.each do |insp|
    temp = {}
    building = insp['building_id'].present? && buildings[insp['building_id']].present? ? buildings[insp['building_id']] : nil
    unit = insp['unit_id'].present? && units[insp['unit_id']].present? ? units[insp['unit_id']] : nil
    prop = insp['properties']
    level = prop['level'].present? ? prop['level'] : ''

    temp['line'] = index
    # Safe navigation for item_code
    temp['area'] = item.present? && item['item_code'].present? ? item['item_code'][0] + item['item_code'][1] : ''
    temp['sort_key'] = 0
    if temp['area'] == 'UNIT'
      temp['sort_key'] = 1
      temp['area_name'] = 'Unit'
    end

    # Standardize building/unit name population for both reports
    temp['building_name'] = building.present? ? building['building_name'] : 'N/A'
    temp['unit_name'] = unit.present? ? unit['unit_name'] : 'N/A'

    temp['item'] = item.present? ? item['title'] : backup_item_title
    temp['level'] = level
    temp['image_name'] = prop['image_name']
    temp['comments'] = insp['comments'].nil? ? '' : insp['comments'].gsub('?', ' ')
    temp['location'] = insp['location'].nil? ? '' : insp['location'].gsub('?', ' ')

    if defect_ids.include?(insp['deficiency_definition_id'])
      defect = defects.find { |d| d.id == insp['deficiency_definition_id'] }
      temp['defects'] = defect.present? ? defect['title'] : ''
    else
      temp['defects'] = ''
    end
    
    index += 1
    details.push(temp)
  end

  sorted_details = sort_details(details)

  view_data['details'] = sorted_details
  view_data['report_name'] = report_name_header
  view_data['orientation'] = 'L'
  # Safely handle property_name for filename generation
  prop_name = view_data['property']['property_name'].gsub('[^A-Za-z0-9]', '-') rescue 'Property'
  view_data['file_name'] = "#{prop_name}-#{report_file_suffix}"

  view_data
end