Class: ReportsManager::DefectStatisticsReport

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

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type, property_buildings, property_units) ⇒ DefectStatisticsReport

Returns a new instance of DefectStatisticsReport.



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

def initialize(user_id, property_id, report_type, property_buildings, property_units)
  @user_id = user_id
  @property_id = property_id
  @report_type = report_type
  @property_buildings =property_buildings
  @property_units = property_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
# File 'app/services/reports_manager/defect_statistics_report.rb', line 13

def call
  property = Property.find_by_id(@property_id)
  property_name = property['property_name'].upcase

  buildings = @property_buildings
  units = @property_units

  header = [
    'Line',
    'Building Name',
    'Unit Name',
    'Defect Count'
  ]
  inspections = InspectionDetail.property_defects_of_unit(@property_id)
  data = {}
  inspections.each do |inspection|
    building_id = inspection['building_id']
    unit_id = inspection['unit_id']
    data[building_id] = {} if data[building_id].nil?
    data[building_id][unit_id] = [] if data[building_id][unit_id].nil?
    data[building_id][unit_id].push(inspection)
  end
  report_data = []

  index = 0
  units.each do |unit_id, unit|
    temp = {}
    index += 1
    building_id = unit['building_id']

    temp['Line'] = index
    temp['Building Name'] = buildings[unit['building_id']].present? ? buildings[unit['building_id']]['building_name'] : ''
    temp['Unit Name'] = unit['unit_name']
    temp['Defect Count'] = data[building_id].present? && data[building_id][unit_id].present? ? data[building_id][unit_id].count : 0

    report_data.push(temp)
  end
  report_data = report_data.sort_by { |k| k['Defect Count'] }
  report_data = report_data.reverse

  view_data = {}
  view_data['headers'] = header
  view_data['data'] = report_data
  view_data['report_title'] = property_name
  view_data['report_sub_title'] = 'Defect Count In Each Unit'
  view_data['report_filename'] = property['property_name'] + '-Unit_Defect_Count'
  view_data['work_sheet_name'] = 'unit-defect-count'
  view_data['user_id'] = @user_id
  view_data
end