Class: ReportsManager::LbpVisualAssessmentReport

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

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type, inspectable_buildings, inspectable_units) ⇒ LbpVisualAssessmentReport

Returns a new instance of LbpVisualAssessmentReport.



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

def initialize(user_id, property_id, report_type, inspectable_buildings, inspectable_units)
  @user_id = user_id
  @property_id = property_id
  @report_type = report_type
  @inspectable_buildings = inspectable_buildings
  @inspectable_units = inspectable_units
end

Instance Method Details

#callObject



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

def call
  view_data = {}
  property = Property.find_by_id(@property_id)

  # Lead-based paint defects are linked to InspectableItem id 1177
  lbp_item = InspectableItem.find_by_id(1177)
  lbp_defect_ids = lbp_item.present? ? DeficiencyDefinition.where(inspectable_item_id: lbp_item.id).pluck(:id) : []

  # Collect unit IDs that have at least one LBP defect (these FAIL)
  failing_unit_ids = InspectionDetail
    .where(property_id: @property_id)
    .where(deficiency_definition_id: lbp_defect_ids)
    .where.not(unit_id: nil)
    .pluck(:unit_id)
    .map(&:to_s)
    .to_set

  buildings = @inspectable_buildings
  units = @inspectable_units

  # Build one row per unit
  units_data = []
  units.each do |_id, unit|
    building_id_str = unit['building_id'].to_s
    building = buildings[building_id_str]
    building_name = building.present? ? building['building_name'] : 'N/A'
    unit_id_str = unit['id'].to_s

    units_data.push({
      'building_name' => building_name,
      'building_id'   => building_id_str,
      'unit_name'     => unit['unit_name'],
      'unit_id'       => unit_id_str,
      'pass_fail'     => failing_unit_ids.include?(unit_id_str) ? 'FAIL' : 'PASS'
    })
  end

  # Natural sort: by building name then unit name
  units_data.sort_by! do |u|
    bldg = u['building_name'].to_s
    uname = u['unit_name'].to_s
    bldg_nums = bldg.scan(/\d+/).map(&:to_i)
    unit_nums = uname.scan(/\d+/).map(&:to_i)
    [bldg.gsub(/\d+/, ''), *bldg_nums, uname.gsub(/\d+/, ''), *unit_nums]
  end

  prop_properties = property['properties'] || {}
  inspection_date = ApplicationHelper.inspection_date_for_reports(property)
  inspection_type = prop_properties['inspection_type'].presence || 'Annual'

  view_data['property_id']     = @property_id
  view_data['user_id']         = @user_id
  view_data['report_type']     = @report_type
  view_data['property']        = property
  view_data['units_data']      = units_data
  view_data['inspection_date'] = inspection_date
  view_data['inspection_type'] = inspection_type
  view_data['report_name']     = 'LBP VISUAL ASSESSMENT'
  view_data['file_name']       = property['property_name'].gsub('[^A-Za-z0-9]', '-') + '-LBP-Visual-Assessment'

  view_data
end