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.



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 111

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.



150
151
152
153
154
155
156
# File 'app/controllers/admin/fixed_assets_controller.rb', line 150

def destroy
  @fixed_asset.destroy
  respond_to do |format|
    format.html { redirect_to admin_fixed_assets_url, 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.



100
101
102
103
104
# File 'app/controllers/admin/fixed_assets_controller.rb', line 100

def edit
  @property = Property.find_by_id(@fixed_asset.property_id)
  @building = Building.find_by_old_id(@fixed_asset.building_id) || Building.find(@fixed_asset.building_id)
  @unit = Unit.find_by_old_id(@fixed_asset.unit_id) || Unit.find(@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.



81
82
83
84
85
# File 'app/controllers/admin/fixed_assets_controller.rb', line 81

def index
  @fixed_assets = FixedAsset.search(params[:property_id]).page params[:page]

  render action: :index, layout: request.xhr?.nil?
end

#newObject

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



94
95
96
# File 'app/controllers/admin/fixed_assets_controller.rb', line 94

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.



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

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.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/admin/fixed_assets_controller.rb', line 133

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, 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