Class: Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PgSearch
Defined in:
app/models/asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_asset(organization_id, start_date, end_date) ⇒ Object

Total asset in organization



376
377
378
# File 'app/models/asset.rb', line 376

def self.all_asset(organization_id , start_date, end_date)
  self.where("organization_id =? and created_at >= ? and created_at <= ? ", organization_id,  start_date, end_date).count
end

.create_barcode(total_ticket, asset, asset_number) ⇒ Object

Bar Code implementation



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'app/models/asset.rb', line 350

def self.create_barcode(total_ticket , asset , asset_number)
  barcode = {}
  random_string_qrcode = asset_number
  @ticket = AssetTicket.create(:qrcode => random_string_qrcode , :genratenumber =>random_string_qrcode , :asset_id => asset)

  (0..total_ticket-1).each do|number|
    rand = SecureRandom.hex(6)
    @barcode = Barby::Code128B.new(random_string_qrcode)
    File.open("app/assets/code/#{rand}.png", 'w+'){|f|
      f.write @barcode.to_png(:margin => 1, :xdim => 1, :height => 55)
    }
    image_url = "app/assets/code/#{rand}.png"
    var = Rails.root + image_url
    barcode.store(var,@ticket.qrcode )
  end
  
  return barcode  
end

.create_qrcode(total_number, asset_id, asset_number) ⇒ Object

QR code implementation



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'app/models/asset.rb', line 332

def self.create_qrcode(total_number, asset_id , asset_number)
  qrcode ={}
  @asset_id = asset_id
  random_string_qrcode = asset_number
  @asset_ticket = AssetTicket.create(:qrcode => random_string_qrcode , :genratenumber =>random_string_qrcode , :asset_id => @asset_id)

  (0..total_number-1).each do |number|
    srand = SecureRandom.hex(6)
    @qr = RQRCode::QRCode.new(random_string_qrcode, :size => 2, :level => :m )
    png = @qr.to_img
    png.resize(90, 90).save("app/assets/code/#{srand}.png")
    qrcode.store(@qr,@asset_ticket.qrcode )
  end
  
  return  qrcode
end

.decode_file(params, organization) ⇒ Object



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
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
127
128
129
130
# File 'app/models/asset.rb', line 61

def self.decode_file(params,organization)
  count=0
  #   decode base64 string
  
  Rails.logger.info 'decoding base64 file'
  decoded_data = Base64.decode64(params[:imp][:base64])
  # create 'file' understandable by Paperclip
  data = StringIO.new(decoded_data)
  data.class_eval do
    attr_accessor :content_type, :original_filename
  end
  
  # set file properties
  
  data.content_type = params[:imp][:filetype]
  data.original_filename = params[:imp][:filename]
  
  new =  Importfile.new(:avatar => data)

  new.save

  #    open xlsx file
  xlsx = Roo::Spreadsheet.open(new.avatar.url)
  xlsx = Roo::Excelx.new(new.avatar.url)
  xlsx.default_sheet = xlsx.sheets.first
  #   getting data from file in hash loop 
  xlsx.each_with_index(name: 'Name' ,
    description: 'Description',
    current_location: 'Current location',
    is_new: 'New',
    is_traded: 'Equipment traded',
    electroic_need: 'Electric needs',
    volts: 'volts',
    model_no: 'Model number',
    serial_no: 'Serial number',
    sku: 'SKU',
    manufactured_date: 'Date manufactured',
    purchased_date: 'Date purchased',
    purchased_from: 'Purchased from',
    info: 'other info',
    replace_priority: 'Replacement priority',
    retire: 'Enable/Disable',
  
  ) do |hash, i|
    if i != 0
      count=count+1
      #      Sending data to db-table
      @asset = organization.assets.create(
        :name => hash[:name],
        :description => hash[:description],
        :current_location => hash[:current_location],
        :is_new => hash[:is_new],
        :is_traded => hash[:is_traded],
        :electroic_need => hash[:electroic_need],
        :volts => hash[:volts],
        :model_no => hash[:model_no],
        :serial_no => hash[:serial_no],
        :sku => hash[:sku],
        :manufactured_date => hash[:manufactured_date],
        :purchased_date => hash[:purchased_date],
        :purchased_from => hash[:purchased_from],
        :info => hash[:info],
        :replace_priority => hash[:replace_priority],
        :retire => hash[:retire] 
      )
    end

  end
  count
end

.default_template(param, organization) ⇒ Object



132
133
134
# File 'app/models/asset.rb', line 132

def self.default_template(param,organization)
    
end

.filter_data(organization, page, params) ⇒ Object



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
# File 'app/models/asset.rb', line 32

def self.filter_data(organization,page ,params)
  if params == "All"
    assets = organization.assets
  elsif params == "true"
    assets = organization.assets.where(:retire => true)
  elsif params == "false"
    assets = organization.assets.where(:retire => false)
  elsif params == "unassg"
    array_assigned = []
    organization.assets.each do |aa|
      if(aa.asset_assignments.count == 0)
        array_assigned << aa
      end
    end
    assets = array_assigned
  elsif params == "assg"
    array_unassigned = []
    organization.assets.each do |aa|
      if(aa.asset_assignments.count > 0)
        array_unassigned << aa
      end
    end
    assets = array_unassigned
  end
  assets
end

.find_asset_reminder(reminder) ⇒ Object

Start: Find Asset Reminder



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'app/models/asset.rb', line 432

def self.find_asset_reminder(reminder)
  
  if reminder.blank?
    re_time = 1.week
  elsif reminder =="1_week"
    re_time = 1.week
  elsif reminder =="2_week"
    re_time = 2.week
  elsif reminder =="1_month"
    re_time = 1.month
  elsif reminder =="2_month"
    re_time = 2.month
  elsif reminder =="3_month"
    re_time = 3.month
  elsif reminder =="4_month"
    re_time = 4.month
  elsif reminder =="5_month"
    re_time = 5.month
  elsif reminder =="6_month"
    re_time = 6.month
  end
  return re_time
end

.find_code(asset_id) ⇒ Object

Asset code for print



324
325
326
327
# File 'app/models/asset.rb', line 324

def self.find_code(asset_id)
  asset = self.find_by_id(asset_id)
  return asset.code_number
end

.get_asset(organization_id, asset_id) ⇒ Object

Getting asset for email



381
382
383
# File 'app/models/asset.rb', line 381

def self.get_asset(organization_id , asset_id)
  self.find_by_id_and_organization_id(asset_id , organization_id)
end

.info_incomplete(organization_id, start_date, end_date) ⇒ Object

incomplete information



418
419
420
421
422
423
424
425
426
427
428
429
# File 'app/models/asset.rb', line 418

def self.info_incomplete(organization_id, start_date, end_date)
  
  @asset = self.where("organization_id =? and created_at >= ? and created_at <= ?  ", organization_id,  start_date, end_date)
  array_asset = []
  
  @asset.each do |asset|
    if asset.name.blank? or asset.purchased_date.blank? or asset.purchased_date.blank? or asset.vendor_id.blank?  or   asset.serial_no.blank? or asset.model_no.blank?
      array_asset<< asset
    end
  end
  return array_asset
end

.new_asset_add(organization_id, start_date, end_date) ⇒ Object

Find new asset added in organzation



386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/models/asset.rb', line 386

def self.new_asset_add(organization_id, start_date, end_date)
  asset_details ={}
  assets = self.where("organization_id =? and created_at >= ? and created_at <= ? and  retire =?", organization_id, start_date , end_date, false)
  
  assets.each do |asset|
    user = User.find_by_id(asset.user_id)
    if user and user.first_name
      asset_details.store(asset , user.first_name)
    end
  end
  
  return asset_details
end

.subjectline(asset) ⇒ Object

Email Subject Line not use current due subject line provide by static



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'app/models/asset.rb', line 457

def self.subjectline(asset)
 
  if asset.repair_reminder.blank?
    re_time = "1 week"
  elsif asset.repair_reminder =="1_week"
    re_time = "1 week"
  elsif asset.repair_reminder =="2_week"
    re_time = "2 week"
  elsif asset.repair_reminder =="1_month"
    re_time = "1 month"
  elsif asset.repair_reminder =="2_month"
    re_time = "2 months"
  elsif asset.repair_reminder =="3_month"
    re_time = "3 months"
  elsif asset.repair_reminder =="4_month"
    re_time = "4 months"
  elsif asset.repair_reminder =="5_month"
    re_time = "5 months"
  elsif asset.repair_reminder =="6_month"
    re_time = "6 months"
  end
  sb = re_time + " "+ "left for"+ " "+ asset.name+ " " +"repair due date."
  return sb
end

.total_asset(organization_id) ⇒ Object

Start: Getting All asset without date



370
371
372
# File 'app/models/asset.rb', line 370

def self.total_asset(organization_id)
  self.where("organization_id =? ", organization_id)
end

.total_retire(organization_id, start_date, end_date) ⇒ Object

Total retire asset



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'app/models/asset.rb', line 401

def self.total_retire (organization_id , start_date, end_date)
  
  retired_details ={}
  assets = self.where("organization_id =? and created_at >= ? and created_at <= ? and retire =?", organization_id,start_date, end_date, true)
  assets.each do |asset|
    user = User.find_by_id(asset.user_id)
    if user and user.first_name
      retired_details.store(asset , user.first_name)
    else
      
    end
  end
  return retired_details
  
end

Instance Method Details

#calculate_repair_sumObject



287
288
289
290
# File 'app/models/asset.rb', line 287

def calculate_repair_sum
  repair = AssetRepairRequest.where(:asset_id => self.id, :status=> 2).map(&:vending_cost).compact.inject{|sum,x| sum.to_i + x.to_i }
  repair.to_i
end

#cost_of_ownershipObject



292
293
294
295
296
297
298
# File 'app/models/asset.rb', line 292

def cost_of_ownership
  if calculate_repair_sum.to_i == 0
    cost_of_ownership = purchased_cost.to_f
  else
    cost_of_ownership = purchased_cost.to_f + calculate_repair_sum.to_f
  end
end

#create_and_send_notification_asset(current_user, organization) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/models/asset.rb', line 270

def create_and_send_notification_asset(current_user, organization)
  managers = organization.managers(current_user)
  summary = "added a new asset: " + self.name
  organization.push_notification_user("CA" , self, summary , current_user  ,managers ,organization)

  type ="asset create"
  email_notify =  UserNotificationSetting.email_setting(current_user.id)
  if  email_notify == "immediately"
    seen = true
    UserNotificationSetting.create_asset_email(self, organization ,organization.setting.from_email, current_user)
    HistoryEmail.history_email(current_user.id ,type , seen,organization.id ,self.id)
  else
    seen= false
    HistoryEmail.history_email(current_user.id ,type , seen,organization.id,self.id)
  end
end

#create_repair_request(requested_by, vendor, comments) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'app/models/asset.rb', line 222

def create_repair_request(requested_by, vendor, comments)
  self.repair_requests.create({
      requested_by: requested_by,
      vendor: vendor,
      comments: comments,
      status: 0,
      requested_date: Time.new
    })
end

#current_locationObject



246
247
248
249
250
# File 'app/models/asset.rb', line 246

def current_location
  if self.location
    self.location.name
  end
end

#current_typeObject



252
253
254
255
# File 'app/models/asset.rb', line 252

def current_type
  type = DepreciationSetting.find_by_id(self.depreciation_settings_id)
  type.name
end

#get_purchase_vendorObject

Get purchase vendor for Asset details



305
306
307
# File 'app/models/asset.rb', line 305

def get_purchase_vendor
  User.find_by_id(self.purchase_vendor_id)
end

#get_vendorObject



300
301
302
# File 'app/models/asset.rb', line 300

def get_vendor
  User.find_by_id(self.vendor_id)
end

#last_assignmentObject



265
266
267
268
# File 'app/models/asset.rb', line 265

def last_assignment
  assignments = self.asset_assignments.order(:created_at)
  assignments.last
end

#pending_ticketsObject

Returns all repair tickets that are pending.



212
213
214
215
216
# File 'app/models/asset.rb', line 212

def pending_tickets
  self.repair_requests.where({
      status: 0 
    })
end

#repair_countObject



261
262
263
# File 'app/models/asset.rb', line 261

def repair_count
  self.repair_requests.where("status != -1 AND status != 0").count()
end

#send_for_repair(requested_by, vendor, comments, cost) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/models/asset.rb', line 232

def send_for_repair(requested_by, vendor, comments ,cost)
  self.repair_requests.create({
      requested_by: requested_by,
      responded_by: requested_by,
      vendor: vendor,
      comments: comments,
      status: 1,
      requested_date: Time.new,
      responded_date: Time.new,
      responder_comments: comments ,
      estimate_by_user: cost
    })
end

#ticket_in_progressObject

Ticket in progress



218
219
220
# File 'app/models/asset.rb', line 218

def ticket_in_progress
  self.repair_requests.where("status = ? or  status = ? or   status = ? ", 0 , 1  ,3)
end

#update_asset_notification(current_user, organization) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
# File 'app/models/asset.rb', line 310

def update_asset_notification(current_user, organization)
  managers = organization.managers(current_user)
  Notification.create_and_send_notification(
    :type => "CA",
    :source => self,
    :summary => "updated an asset: " + self.name,
    :notifier => current_user,
    :managers => managers,
    :organizationID => organization.id
  )
end

#upload(params) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/asset.rb', line 197

def upload(params)
  
  decoded_data = Base64.decode64(params[:image][:base64])
  data = StringIO.new(decoded_data)
  data.class_eval do
    attr_accessor :content_type, :original_filename
  end
  data.content_type = params[:image][:filetype]
  data.original_filename = params[:image][:filename]
  
  self.images.create(:asset_id => self.id, :image => data)
  
end

#vending_costObject



257
258
259
# File 'app/models/asset.rb', line 257

def vending_cost
  self.repair_requests.where.not(:vending_cost => nil).map(&:vending_cost).sum()
end