class Invoice

Schema Information

Table name: invoices

id                  :integer          not null, primary key
tutor_id         :integer
user_id             :integer
invoiceable_id      :integer
created_at          :datetime
updated_at          :datetime
paid                :boolean          default(FALSE)
email_notifications :integer          default(0)
last_notification   :datetime
manually_billed     :boolean          default(FALSE)
payable_amount      :integer
description         :string(255)
university_id          :integer
invoiceable_type    :string(255)
charged             :boolean          default(FALSE)

Attributes

category_id[RW]

Attribute Accessors

date[RW]

Attribute Accessors

end_time[RW]

Attribute Accessors

location[RW]

Attribute Accessors

rate_in_cents[RW]

Attribute Accessors

session_length[RW]

Attribute Accessors

start_time[RW]

Attribute Accessors

course_id[RW]

Attribute Accessors

subject_id[RW]

Attribute Accessors

Public Instance Methods

assign_payable_amount() click to toggle source

It sets the payable amount.

# File app/models/invoice.rb, line 119
def assign_payable_amount
  self.payable_amount = calculate_payable_amount
end
calculate_payable_amount() click to toggle source

Calculate amount for tutoring session on hours length.

# File app/models/invoice.rb, line 124
def calculate_payable_amount
  if invoiceable.is_a? TutoringSession
    self.total_amount.cents - (500*session_length_hours)
  else
    self.total_amount.cents if self.tutor.present?
  end
end
chargeable_amount() click to toggle source

It returns the chargeable amount.

# File app/models/invoice.rb, line 199
def chargeable_amount
  total_amount
end
create_or_update_tutoring_session() click to toggle source

To create or update Invoice for tutoring session.

# File app/models/invoice.rb, line 67
def create_or_update_tutoring_session
  if self.manually_billed?
    ts = (self.invoiceable || self.build_invoiceable)
    ts.attributes = {
      course_id: course_id,
      subject_id: subject_id,
      date: date,
      start: start_time,
      end: end_time,
      category_id: category_id,
      rate_in_cents: rate_in_cents,
      tutor_id: User.current.id,
      user_id: user_id,
      location: location}
    ts.save!
  end
student() click to toggle source

Returns user as student

# File app/models/invoice.rb, line 160
def student
  user
end
date_must_be_past() click to toggle source

Validation to ensure date must be in past

# File app/models/invoice.rb, line 147
def date_must_be_past
  if end_time.present? and date.present?
    if Time.zone.parse("#{date} #{end_time}") > Time.zone.now
      if date.to_date == Date.today
        errors.add(:end_time, 'Must be less than current time')
      else
        errors.add(:date, 'Must be in past')
      end
    end
  end
end
pay!() click to toggle source

Update paid to true

# File app/models/invoice.rb, line 204
def pay!
  self.update_attribute(:paid, true)
end
pay_tutor() click to toggle source

Pay tutor

# File app/models/invoice.rb, line 165
def pay_tutor
  unless paid?
    if AccountBalance.credit(self.tutor, self, true, self.payable_amount)
      self.paid = true
      self.save(:validate => false)
      self.invoiceable.update_attribute(:occured, true) if invoiceable.is_a? TutoringSession
      InvoiceMailer.delay(queue: :mailer).paid_email(self)
      InvoiceMailer.delay(queue: :mailer).paid_email_tutor(self)
      self.create_activity key: 'payment.paytutor', owner: user, recipient: tutor, type: 'Notification'
      return true
    end
  end
end
rate() click to toggle source

Rate as currency object

# File app/models/invoice.rb, line 180
def rate
  Money.new(tutoring_session.try(:rate_in_cents), Money.default_currency)
end
send_invoice_email() click to toggle source

send notification email for Invoice

# File app/models/invoice.rb, line 99
def send_invoice_email
  InvoiceMailer.delay(queue: :mailer).create_email(self) if self.tutor.present?
  self.create_activity key: 'invoice.create', owner: tutor, recipient: user, type: 'Notification'
end
session_length_hours() click to toggle source

To get the session hours length.

# File app/models/invoice.rb, line 185
def session_length_hours
  (invoiceable.length / 60.0).round 2
end
set_tutor_rate_and_session_length() click to toggle source

if invoice is manually build than assign current user id as tutor id

# File app/models/invoice.rb, line 105
def set_tutor_rate_and_session_length
  if self.manually_billed?
    self.tutor_id = User.current.id
  end
end
start_time_must_be_less_than_end_time() click to toggle source

validation to ensure that start time must be less than end time

# File app/models/invoice.rb, line 138
def start_time_must_be_less_than_end_time
  if start_time.present? and end_time.present? and date.present?
    if Time.zone.parse("#{date} #{start_time}") >= Time.zone.parse("#{date} #{end_time}")
      errors.add(:end_time, 'must be greater than Start Time')
    end
  end
end
status() click to toggle source

Returns the status of Invoice.

# File app/models/invoice.rb, line 87
def status
  if paid?
    'Paid'
  elsif invoiceable.is_a? TutoringSession
      (invoiceable.occured.nil? or invoiceable.occured?) ? 'Pending' : 'Rejected'
  else
    'Pending'
  end

end
total_amount() click to toggle source

It returns the total amount.

# File app/models/invoice.rb, line 190
def total_amount
  if manually_billed?
    amount
  else
    self.invoiceable.total_amount
  end
end
update_tutor_billed_length() click to toggle source

Update invoiced minutes for this tutor

# File app/models/invoice.rb, line 112
def update_tutor_billed_length
  if invoiceable.is_a? TutoringSession
    new_length = self.tutor.tutoring_sessions.confirmed.joins(:invoice).sum('tutoring_sessions.end_time - tutoring_sessions.start_time')
  end
end
update_payable_amount() click to toggle source

It updates payable amount.

# File app/models/invoice.rb, line 133
def update_payable_amount
  update_columns(payable_amount: calculate_payable_amount)
end