Class: ReturnAuthorization

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/return_authorization.rb

Overview

Schema Information

Table name: return_authorizations

id             :integer(4)      not null, primary key
number         :string(255)
amount         :decimal(8, 2)   not null
restocking_fee :decimal(8, 2)   default(0.0)
order_id       :integer(4)      not null
user_id        :integer(4)      not null
state          :string(255)     not null
created_by     :integer(4)
active         :boolean(1)      default(TRUE)
created_at     :datetime
updated_at     :datetime

Constant Summary

NUMBER_SEED =
1002003004005
CHARACTERS_SEED =
21

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Array[ReturnAuthorization]) admin_grid(params = {})

paginated results from the admin return authorization grid

Parameters:

  • (Optional params)

Returns:



143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/return_authorization.rb', line 143

def self.admin_grid(params = {})

  params[:page] ||= 1
  params[:rows] ||= SETTINGS[:admin_grid_rows]

  grid = includes([:return_items, :order, :user])#paginate({:page => params[:page]})

  grid = grid.where("return_authorizations.number LIKE ?",  "#{params[:number]}%")        if params[:number].present?
  grid = grid.where("orders.order_number LIKE ?",           "#{params[:order_number]}%")  if params[:order_number].present?
  grid = grid.where("return_authorizations.state LIKE ?",      params[:state])               if params[:state].present?
  grid
end

+ (ReturnAuthorization) find_by_number(num)

finds the ReturnAuthorization from the ReturnAuthorizations number. Is more optimal than the normal rails find by id

because if calculates the ReturnAuthorization's id which is indexed

Parameters:

  • represents (String)

    the ReturnAuthorization.number

Returns:



135
136
137
# File 'app/models/return_authorization.rb', line 135

def self.find_by_number(num)
  find(id_from_number(num))##  now we can search by id which should be much faster
end

+ (Integer) id_from_number(num)

determines the ReturnAuthorization id from the ReturnAuthorization.number

Parameters:

  • represents (String)

    the ReturnAuthorization.number

Returns:

  • (Integer)

    id of the ReturnAuthorization to find



126
127
128
# File 'app/models/return_authorization.rb', line 126

def self.id_from_number(num)
  num.to_i(CHARACTERS_SEED) - NUMBER_SEED
end

Instance Method Details

- (Object) initial

after you process an RMA you must manually add the variant back into the system!!!



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/return_authorization.rb', line 49

state_machine :initial => 'authorized' do
  #after_transition :to => 'received', :do => :process_receive
  #after_transition :to => 'cancelled', :do => :process_canceled
  before_transition :to => 'complete', :do => [:process_ledger_transactions, :mark_items_returned]

  event :receive do
    transition :to => 'received', :from => 'authorized'
  end
  event :cancel do
    transition :to => 'cancelled', :from => 'authorized'
  end
  event :complete do # do this after a payment was returned to the customer
    transition :to => 'complete', :from => ['authorized', 'received']
  end
end

- (Object) mark_items_returned



76
77
78
# File 'app/models/return_authorization.rb', line 76

def mark_items_returned
  return_items.map(&:mark_returned!)
end

- (String) order_number

number of the order that is returning the item

Parameters:

  • (none)

Returns:

  • (String)


84
85
86
# File 'app/models/return_authorization.rb', line 84

def order_number
  order.number
end

- (none) process_ledger_transactions

when the RMA is returned, before the ReturnAuthorization transitions to complete

the accounting peice logs the transactions.

Parameters:

  • (none)

Returns:

  • (none)


70
71
72
73
74
# File 'app/models/return_authorization.rb', line 70

def process_ledger_transactions
  ##  credit => cash
  ##  debit  => revenue
  Invoice.process_rma(amount - restocking_fee, order )
end

- (none) save_order_number

Called after_create. sets the ReturnAuthorization number

Parameters:

  • (none)

Returns:

  • (none)


117
118
119
120
# File 'app/models/return_authorization.rb', line 117

def save_order_number
  set_order_number
  save
end

- (none) set_number

Called before validation. sets the ReturnAuthorization number, if the id is nil the ReturnAuthorization number is bogus

Parameters:

  • (none)

Returns:

  • (none)


100
101
102
103
# File 'app/models/return_authorization.rb', line 100

def set_number
  return set_order_number if self.id
  self.number = (Time.now.to_i).to_s(CHARACTERS_SEED)## fake number for friendly_id validator
end

- (none) set_order_number

sets the order ReturnAuthorization based off constants and the ReturnAuthorization id

Parameters:

  • (none)

Returns:

  • (none)


109
110
111
# File 'app/models/return_authorization.rb', line 109

def set_order_number
  self.number = (NUMBER_SEED + id).to_s(CHARACTERS_SEED)
end

- (String) user_name

name of the user that is returning the item

Parameters:

  • (none)

Returns:

  • (String)


92
93
94
# File 'app/models/return_authorization.rb', line 92

def user_name
  user.name
end