Class: ReportsManager::SingleUnitReport

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

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type, items, defects, property_buildings) ⇒ SingleUnitReport

Returns a new instance of SingleUnitReport.



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

def initialize(user_id, property_id, report_type,items, defects, property_buildings)
  @user_id = user_id
  @property_id = property_id
  @report_type = report_type
  @items = items
  @defects = defects
  @property_buildings = property_buildings
end

Instance Method Details

#callObject

render_single_unit_report



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/services/reports_manager/single_unit_report.rb', line 15

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(@property_id)

  details = []
  inspection = InspectionDetail.get_units_inspections(@property_id)

  items = @items
  defects = @defects
  buildings = @property_buildings
  units = Unit.where(property_id: @property_id).order(building_id: :asc, unit_name: :asc)

  total_inspectable_units = Unit.total_inspectable_units(@property_id)

  units.each do |unit|
    temp = {}
    unit_prop = unit['properties']
    building = !unit['building_id'].empty? && buildings[unit['building_id']] ? buildings[unit['building_id']] : nil
    next unless building

    reason = unit_prop['uninspectable'] ? unit_prop['uninspectable'].strip.downcase : 'none'
    comment = unit_prop['comment']

    temp['building_name'] = building['building_name']
    temp['unit_name'] = unit['unit_name']
    unit_defects = []
    inspection_sort_keys = []

    inspection.each do |insp|
      next unless insp['unit_id'] == unit.id.to_s

      item = items[insp['inspectable_item_id']] || nil
      defect = insp['deficiency_definition_id'].present? ? defects[insp['deficiency_definition_id']] : nil
      prop = JSON.parse(insp['properties'])
      next unless item.present? && defect.present?

      level = prop['level'] || ''
      defect_prop = defect.present? ? defect['properties'] : nil
      item_prop = item['properties']
      criticality = defect_prop.present? ? defect_prop['criticality'].to_f : 0.0
      item_weight = item_prop['item_weight'].present? ? (item_prop['item_weight'].to_f / 100).to_f : 0.0

      t = {}
      t['item_name'] = item['title']
      t['defect_name'] = defect.present? ? defect['title'] : nil
      t['level'] = defect.present? ? defect['definition_type'] : ''

      t['comments'] = insp['comments'].present? ? insp['comments'].gsub('?', ' ') : ''
      t['location'] = insp['location'].present? ? insp['location'].gsub('?', ' ') : ''

      point_deduction = 0.0
      point_deduction = ( defect.deduction_point || ApplicationHelper.get_unit_area_impact_weight[t['level']].to_f) / total_inspectable_units.to_f

      inspection_sort_key = temp['building_name'] + '-' + temp['unit_name'] + '-' + t['item_name'] + '-' + t['defect_name'].to_s + t['level'].to_s

      if inspection_sort_keys.include?(inspection_sort_key)
        t['points'] = 0.0
      else
        inspection_sort_keys.push(inspection_sort_key)
        t['points'] = point_deduction.round(2)
      end

      unit_defects.push(t)
    end

    temp['sort_key'] = (temp['building_name'] + ' ' + temp['unit_name']).downcase
    temp['unit_defects'] = unit_defects
    temp['total_defects'] = unit_defects.count
    temp['reason'] = reason
    temp['comment'] = comment
    details.push(temp)
  end

  details = ApplicationHelper.custom_sort(details, 'sort_key')

  view_data['details'] = details
  view_data['report_name'] = 'SINGLE UNIT REPORT'
  view_data['orientation'] = 'L'
  view_data['file_name'] = view_data['property']['property_name'].gsub('[^A-Za-z0-9]', '-') + '-SingleUnit'

  view_data
end