Class: ReportsManager::PropertyFixedAssetsReport

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

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(user_id, property_id, report_type) ⇒ PropertyFixedAssetsReport

Returns a new instance of PropertyFixedAssetsReport.



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

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

Instance Method Details

#callObject



11
12
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
# File 'app/services/reports_manager/property_fixed_assets_report.rb', line 11

def call
  property = Property.find_by_id(@property_id)
  property_name = property['property_name'].upcase
  inspection_date = ApplicationHelper.inspection_date_for_reports(property)
  details = FixedAsset.where(property_id: @property_id).deduplicated

  header = [
    'Line',
    'Building No',
    'Unit No',
    'Type',
    'Make',
    'Model',
    'Serial Number',
    'Bar Code Number',
    'Condition',
    'Updated Date'
  ]
  data = []

  if details.present?
    index = 1
    details.each do |detail|
      building = (Building.find_by(property_id: @property_id, id: detail['building_id'])) ||
        (Building.find_by(property_id: @property_id, old_id: detail['building_id']))

      next unless building.present?
      unit = (Unit.find_by(property_id: @property_id, building_id: building.id, id: detail['unit_id'])) ||
        (Unit.find_by(property_id: @property_id, building_id: building.id, old_id: detail['unit_id']))
      next unless unit.present?
      temp = {}
      temp['Line'] = index
      temp['Building No'] = building.present? ? building.building_name : ''
      temp['Unit No'] = unit.present? ? unit.unit_name : ''
      temp['Type'] = detail['type']
      temp['Make'] = detail['make']
      temp['Model'] = detail['model']
      temp['Serial Number'] = detail['serial_number']
      temp['Bar Code Number'] = detail['usig_tag_number']
      temp['Condition'] = detail['condition']
      temp['Updated Date'] = detail['updated_at'] ? detail['updated_at'].to_date.strftime("%b %d, %Y") : detail['updated_at'].to_date.strftime("%b %d, %Y")

      temp['sort_key'] = (temp['Building No'] + ' ' + temp['Unit No']).downcase

      data.push(temp)
    end
  end

  data = custom_sort(data, 'sort_key')

  view_data = {}
  view_data['headers'] = header
  view_data['data'] = data
  view_data['report_title'] = property_name
  view_data['report_sub_title'] = 'Inspection Date: ' + inspection_date
  view_data['report_filename'] = property_name + '-Fixed_Assets_Excel'
  view_data['work_sheet_name'] = 'fixed-assets'
  view_data['user_id'] = @user_id
  view_data['freeze_header'] = true
  view_data
end

#custom_sort(data, key) ⇒ Object



73
74
75
76
77
78
# File 'app/services/reports_manager/property_fixed_assets_report.rb', line 73

def custom_sort(data, key)
  data.sort_by! do |item|
    value = item[key]
    [numeric_sort_at_beginning(value), value.downcase]
  end
end

#numeric_sort_at_beginning(value) ⇒ Object



80
81
82
83
# File 'app/services/reports_manager/property_fixed_assets_report.rb', line 80

def numeric_sort_at_beginning(value)
  numeric_part = value[/^\d+/]
  numeric_part ? numeric_part.to_i : Float::INFINITY
end