Class: Invoice

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

Overview

Schema Information

Table name: invoices

id              :integer(4)      not null, primary key
order_id        :integer(4)      not null
amount          :decimal(8, 2)   not null
invoice_type    :string(255)     default("Purchase"), not null
state           :string(255)     not null
active          :boolean(1)      default(TRUE), not null
created_at      :datetime
updated_at      :datetime
credited_amount :decimal(8, 2)   default(0.0)

Constant Summary

PURCHASE =

validates :order_id, :presence => true

'Purchase'
RMA =
'RMA'
INVOICE_TYPES =
[PURCHASE, RMA]
NUMBER_SEED =
3002001004005
CHARACTERS_SEED =
20

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Invoice) find_by_number(num)

find invoice based off the invoice's number

Parameters:

  • invoice (String)

    Number

Returns:



124
125
126
# File 'app/models/invoice.rb', line 124

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

+ (Invoice) generate(order_id, charge_amount, credited_amount = 0.0)

make an invoice object (not saved)

Parameters:

  • order (Integer)

    id

  • amount (Decimal)

    in dollars

Returns:



133
134
135
# File 'app/models/invoice.rb', line 133

def Invoice.generate(order_id, charge_amount, credited_amount = 0.0)
  Invoice.new(:order_id => order_id, :amount => charge_amount, :invoice_type => PURCHASE, :credited_amount => credited_amount)
end

+ (Integer) id_from_number(num)

invoice id calculated from the id of the number

Parameters:

  • (none)

Returns:

  • (Integer)

    invoice id calculated based off the id and preset values



116
117
118
# File 'app/models/invoice.rb', line 116

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

+ (Object) process_rma(return_amount, order)



197
198
199
200
201
202
203
204
205
# File 'app/models/invoice.rb', line 197

def self.process_rma(return_amount, order)
  transaction do
    this_invoice = Invoice.new(:order => order, :amount => return_amount, :invoice_type => RMA)
    this_invoice.save
    this_invoice.complete_rma_return
    this_invoice.payment_rma!
    this_invoice
  end
end

Instance Method Details

- (String) authorization_reference

call to find the confirmation_id sent by the payment processor.

Parameters:

  • (none)

Returns:

  • (String)

    id the payment processor sends you after authorization.



224
225
226
227
228
# File 'app/models/invoice.rb', line 224

def authorization_reference
  if authorization = payments.find_by_action_and_success('authorization', true, :order => 'id ASC')
    authorization.confirmation_id #reference
  end
end

- (Object) authorize_complete_order

(amount)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/invoice.rb', line 164

def authorize_complete_order#(amount)
  x = order.complete!
  now = Time.zone.now
  if batches.empty?
    batch = self.batches.create()
    transaction = CreditCardPayment.new()##  This is a type of transaction
    credit = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::REVENUE_ID, :debit => 0, :credit => amount, :period => "#{now.month}-#{now.year}")
    debit  = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::ACCOUNTS_RECEIVABLE_ID, :debit => amount, :credit => 0, :period => "#{now.month}-#{now.year}")
    transaction.transaction_ledgers.push(credit)
    transaction.transaction_ledgers.push(debit)
    batch.transactions.push(transaction)
    batch.save
    #puts batch.errors
  else
    raise error ###  something messed up I think
  end
end

- (Object) authorize_payment(credit_card, options = {})



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/invoice.rb', line 247

def authorize_payment(credit_card, options = {})
  options[:number] ||= unique_order_number
  transaction do
    authorization = Payment.authorize(integer_amount, credit_card, options)
    payments.push(authorization)
    if authorization.success?
      payment_authorized!
      authorize_complete_order
    else
      transaction_declined!
    end
    authorization
  end
end

- (Object) cancel_authorized_payment



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/invoice.rb', line 182

def cancel_authorized_payment
  batch       = batches.first
  now = Time.zone.now
  if batch# if not we never authorized the payment
    self.cancel!
    transaction = CreditCardCancel.new()##  This is a type of transaction
    debit   = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::REVENUE_ID, :debit => amount, :credit => 0, :period => "#{now.month}-#{now.year}")
    credit  = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::ACCOUNTS_RECEIVABLE_ID, :debit => 0, :credit => amount, :period => "#{now.month}-#{now.year}")
    transaction.transaction_ledgers.push(credit)
    transaction.transaction_ledgers.push(debit)
    batch.transactions.push(transaction)
    batch.save
  end
end

- (Object) capture_complete_order



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/invoice.rb', line 137

def capture_complete_order
  now = Time.zone.now
  if batches.empty?
    # this means we never authorized just captured payment
      batch = self.batches.create()
      transaction = CreditCardCapture.new()##  This is a type of transaction
      credit = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::REVENUE_ID, :debit => 0,     :credit => amount, :period => "#{now.month}-#{now.year}")
      debit   = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::CASH_ID,   :debit => amount, :credit => 0,      :period => "#{now.month}-#{now.year}")
      transaction.transaction_ledgers.push(credit)
      transaction.transaction_ledgers.push(debit)
      batch.transactions.push(transaction)
      batch.save
  else
    batch       = batches.first
    transaction = CreditCardReceivePayment.new()

    debit   = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::CASH_ID,                :debit => amount, :credit => 0,       :period => "#{now.month}-#{now.year}")
    credit  = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::ACCOUNTS_RECEIVABLE_ID, :debit => 0,      :credit => amount,  :period => "#{now.month}-#{now.year}")

    transaction.transaction_ledgers.push(credit)
    transaction.transaction_ledgers.push(debit)

    batch.transactions.push(transaction)
    batch.save
  end
end

- (Object) capture_payment(options = {})



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/models/invoice.rb', line 262

def capture_payment(options = {})
  transaction do
    capture = Payment.capture(integer_amount, authorization_reference, options)
    payments.push(capture)
    if capture.success?
      payment_captured!
      capture_complete_order
    else
      transaction_declined!
    end
    capture
  end
end

- (Object) complete_rma_return



207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/invoice.rb', line 207

def complete_rma_return
  batch       = batches.first || self.batches.create()
  now = Time.zone.now
  transaction = ReturnMerchandiseComplete.new()##  This is a type of transaction
  debit   = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::REVENUE_ID, :debit => amount, :credit => 0, :period => "#{now.month}-#{now.year}")
  credit  = order.user.transaction_ledgers.new(:transaction_account_id => TransactionAccount::CASH_ID, :debit => 0, :credit => amount, :period => "#{now.month}-#{now.year}")
  transaction.transaction_ledgers.push(credit)
  transaction.transaction_ledgers.push(debit)
  batch.transactions.push(transaction)
  batch.save
end

- (Integer) integer_amount

call to find out the amount of the invoice in cents

Parameters:

  • (none)

Returns:

  • (Integer)

    amount of the invoice in cents



242
243
244
245
# File 'app/models/invoice.rb', line 242

def integer_amount
  times_x_amount = amount.integer? ? 1 : 100
  (amount * times_x_amount).to_i
end

- (String) invoice_date(format = :us_date)

date the invoice was created (not the date it was paid)

Parameters:

  • date (Optional Symbol)

    format to be returned

Returns:

  • (String)

    formated date



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

def invoice_date(format = :us_date)
  I18n.localize(created_at, :format => format)
end

- (String) number

invoice number

Parameters:

  • (none)

Returns:

  • (String)

    invoice number calculated based off the id and preset values



108
109
110
# File 'app/models/invoice.rb', line 108

def number
  (NUMBER_SEED + id).to_s(CHARACTERS_SEED)
end

- (Array) order_billing_address_lines

the full order address as an array compacted.

Parameters:

  • (none)

Returns:

  • (Array)

    has ("name", "address1", "address2"(unless nil), "city state zip") or nil



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

def order_billing_address_lines
  order.bill_address.try(:full_address_array)
end

- (Array) order_ship_address_lines

the full shipping address as an array compacted.

Parameters:

  • (none)

Returns:

  • (Array)

    has ("name", "address1", "address2"(unless nil), "city state zip") or nil



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

def order_ship_address_lines
  order.ship_address.try(:full_address_array)
end

- (Boolean) succeeded?

call to find out if the transaction has succeeded.

Parameters:

  • (none)

Returns:

  • (Boolean)

    returns true if the invoice is paid or has been authorized for payment



234
235
236
# File 'app/models/invoice.rb', line 234

def succeeded?
  authorized? || paid?
end

- (User) user

find the user of the order associated to the invoice.

Parameters:

  • (none)

Returns:



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

def user
  order.user
end

- (Integer) user_id

find the user id of the order associated to the invoice.

Parameters:

  • (none)

Returns:

  • (Integer)

    represents the id of the user



280
281
282
# File 'app/models/invoice.rb', line 280

def user_id
  order.user_id
end