Class: Admin::FixedAssetsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/fixed_assets_controller.rb

Overview

Controller for managing fixed assets.

Instance Method Summary collapse

Instance Method Details

#createObject

POST /fixed_assets POST /fixed_assets.json Creates a new fixed asset.

Parameters:

  • fixed_asset_params (Hash)

    The parameters for creating the fixed asset.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/admin/fixed_assets_controller.rb', line 152

def create
  params = fixed_asset_params
  params[:created_by_id_id] = current_user.id
  params[:updated_by_id] = current_user.id
  @fixed_asset = FixedAsset.new(params)

  respond_to do |format|
    if @fixed_asset.save
      format.html { redirect_to admin_fixed_asset_path(@fixed_asset), notice: 'Fixed asset was successfully created.' }
      format.json { render :show, status: :created, location: @fixed_asset }
    else
      format.html { render :new }
      format.json { render json: @fixed_asset.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /fixed_assets/1 DELETE /fixed_assets/1.json Deletes an existing fixed asset.



191
192
193
194
195
196
197
198
# File 'app/controllers/admin/fixed_assets_controller.rb', line 191

def destroy      
  property_id = @fixed_asset.property_id
  @fixed_asset.destroy
  respond_to do |format|
    format.html { redirect_to admin_fixed_assets_path(property_id: property_id), notice: 'Fixed asset was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#download_sampleObject

GET /fixed_assets/download_sample Downloads a sample Excel file for importing fixed assets.



10
11
12
13
14
15
16
# File 'app/controllers/admin/fixed_assets_controller.rb', line 10

def download_sample
  send_file(
      "#{Rails.root}/public/sample/sample_fixed_assets.xlsx",
      filename: "sample_fixed_assets.xlsx",
      type: "application/xlsx"
  )
end

#editObject

GET /fixed_assets/1/edit Displays a form for editing a fixed asset.



141
142
143
144
145
# File 'app/controllers/admin/fixed_assets_controller.rb', line 141

def edit
  @property = Property.find_by_id(@fixed_asset.property_id)
  @building = Building.find(@fixed_asset.building_id) || Building.find_by_old_id(@fixed_asset.building_id)
  @unit = Unit.find(@fixed_asset.unit_id) || Unit.find_by_old_id(@fixed_asset.unit_id)
end

#import_assetsObject

POST /fixed_assets/import_assets Imports fixed assets from an Excel file.

Parameters:

  • year (String)

    The year for which the assets are being imported.

  • delete (Boolean)

    Whether to delete existing assets data.

  • property_id (Integer)

    The ID of the property to which the assets belong.

  • excel_file (File)

    The Excel file containing fixed assets data.



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
# File 'app/controllers/admin/fixed_assets_controller.rb', line 31

def import_assets
  year = params[:year]
  delete = params[:delete_assets_data]
  property_id = params[:property_id]
  uploaded_io = params[:excel_file]

  spreadsheet = Roo::Spreadsheet.open(uploaded_io)

  headers = nil
  if delete
    FixedAsset.where(property_id: property_id).destroy_all
  end
  spreadsheet.each do |row|
    if headers == nil
      headers = row
    else
      building_name = row[0]
      building = Building.where(building_name: building_name)
                     .where(property_id: property_id).first
      unit_name = row[1]
      unit = Unit.where(unit_name: unit_name)
                 .where(property_id: property_id).first

      if unit.present? && building.present?
        i = 2
        while i < row.size
          fixed_asset = FixedAsset.new
          fixed_asset.property_id = property_id
          fixed_asset.building_id = building.id
          fixed_asset.unit_id = unit.id
          fixed_asset.year = year
          fixed_asset.type = headers[i]
          fixed_asset.usig_tag_number = row[i+1]
          fixed_asset.serial_number = row[i+2]
          fixed_asset.condition = row[i+3]
          fixed_asset.make = row[i+4]
          fixed_asset.model = row[i+5]
          fixed_asset.save
          i = i + 6
        end
      end
    end
  end

  redirect_to admin_fixed_assets_path
end

#import_assets_viewObject

GET /fixed_assets/import_assets_view Displays a view for importing fixed assets.



20
21
22
# File 'app/controllers/admin/fixed_assets_controller.rb', line 20

def import_assets_view
  render 'admin/fixed_assets/import_assets'
end

#indexObject

GET /fixed_assets GET /fixed_assets.json Displays a list of fixed assets.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/admin/fixed_assets_controller.rb', line 82

def index
  if params[:property_id].present?
    property = Property.find_by_id(params[:property_id])

    # Deduplicate first: for each (unit, type) keep one record,
    # preferring iPad records (app_id NOT NULL) over import records.
    dedup_subquery = FixedAsset
      .select("DISTINCT ON (property_id, unit_id, COALESCE(type, '')) fixed_assets.*")
      .where(
        property_id: params[:property_id],
        building_id: property.building_ids,
        unit_id: property.unit_ids
      )
      .order(Arel.sql(
        "property_id, unit_id, COALESCE(type, ''), (app_id IS NULL) ASC, updated_at DESC"
      ))

    # Then apply the unit-name sort on the deduplicated result set.
    @fixed_assets = FixedAsset
      .from(dedup_subquery, :fixed_assets)
      .includes(:building, :unit)
      .joins("INNER JOIN units ON units.id::text = fixed_assets.unit_id")
      .order(Arel.sql(
        "CAST(
          COALESCE(
            NULLIF(REGEXP_REPLACE(units.unit_name, '[^0-9]', '', 'g'), ''),
            '0'
          ) AS BIGINT
        ),
        units.unit_name"
      ))
  else
    @fixed_assets = FixedAsset.includes(:building, :unit)
  end

  @fixed_assets = @fixed_assets.page(params[:page])

  if request.xhr?
    render action: :index ,layout: false
  else
    render action: :index, layout: true
  end

  # render action: :index, layout: request.xhr?.nil? && params[:page].present?
end

#newObject

GET /fixed_assets/new Displays a form for creating a new fixed asset.



135
136
137
# File 'app/controllers/admin/fixed_assets_controller.rb', line 135

def new
  @fixed_asset = FixedAsset.new
end

#showObject

GET /fixed_assets/1 GET /fixed_assets/1.json Displays the details of a specific fixed asset.



131
# File 'app/controllers/admin/fixed_assets_controller.rb', line 131

def show; end

#updateObject

PATCH/PUT /fixed_assets/1 PATCH/PUT /fixed_assets/1.json Updates an existing fixed asset.

Parameters:

  • fixed_asset_params (Hash)

    The parameters for updating the fixed asset.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/admin/fixed_assets_controller.rb', line 174

def update
  params = fixed_asset_params
  params[:updated_by_id] = current_user.id
  respond_to do |format|
    if @fixed_asset.update(params)
      format.html { redirect_to admin_fixed_assets_path(property_id: params[:property_id]), notice: 'Fixed asset was successfully updated.' }
      format.json { render :show, status: :ok, location: @fixed_asset }
    else
      format.html { render :edit }
      format.json { render json: @fixed_asset.errors, status: :unprocessable_entity }
    end
  end
end