227 lines
7.1 KiB
Ruby
227 lines
7.1 KiB
Ruby
# represenst the type of lesson package
|
|
module JamRuby
|
|
class LessonBooking < ActiveRecord::Base
|
|
|
|
@@log = Logging.logger[LessonBooking]
|
|
|
|
attr_accessor :accepting
|
|
|
|
STATUS_REQUESTED = 'requested'
|
|
STATUS_CANCELED = 'canceled'
|
|
STATUS_APPROVED = 'approved'
|
|
|
|
STATUS_TYPES = [STATUS_REQUESTED, STATUS_CANCELED, STATUS_APPROVED]
|
|
|
|
LESSON_TYPE_FREE = 'single-free'
|
|
LESSON_TYPE_TEST_DRIVE = 'test-drive'
|
|
LESSON_TYPE_PAID = 'paid'
|
|
|
|
LESSON_TYPES = [LESSON_TYPE_FREE, LESSON_TYPE_TEST_DRIVE, LESSON_TYPE_PAID]
|
|
|
|
PAYMENT_STYLE_ELSEWHERE = 'elsewhere'
|
|
PAYMENT_STYLE_SINGLE = 'single'
|
|
PAYMENT_STYLE_WEEKLY = 'weekly'
|
|
PAYMENT_STYLE_MONTHLY = 'monthly'
|
|
|
|
PAYMENT_STYLES = [PAYMENT_STYLE_ELSEWHERE, PAYMENT_STYLE_SINGLE, PAYMENT_STYLE_WEEKLY, PAYMENT_STYLE_MONTHLY]
|
|
|
|
belongs_to :user, class_name: "JamRuby::User"
|
|
belongs_to :teacher, class_name: "JamRuby::User"
|
|
has_many :lesson_booking_slots, class_name: "JamRuby::LessonBookingSlot"
|
|
has_many :lesson_sessions, class_name: "JamRuby::LessonSession"
|
|
|
|
validates :user, presence: true
|
|
validates :teacher, presence: true
|
|
validates :lesson_type, presence: true, inclusion: {in: LESSON_TYPES}
|
|
validates :status, presence: true, inclusion: {in: STATUS_TYPES}
|
|
validates :recurring, inclusion: {in: [true, false]}
|
|
validates :sent_notices, inclusion: {in: [true, false]}
|
|
validates :card_presumed_ok, inclusion: {in: [true, false]}
|
|
validates :lesson_length, presence: true, inclusion: {in: [30, 45, 60, 90, 120]}
|
|
validates :payment_style, inclusion: {in: PAYMENT_STYLES}
|
|
validates :description, no_profanity: true, length: {minimum: 10, maximum: 20000}, presence: true
|
|
|
|
validate :validate_user
|
|
validate :validate_recurring
|
|
validate :validate_lesson_booking_slots
|
|
validate :validate_lesson_length
|
|
validate :validate_payment_style
|
|
validate :validate_accepted, :if => :accepting
|
|
|
|
after_create :after_create
|
|
def after_create
|
|
if card_presumed_ok && !sent_notices
|
|
send_notices
|
|
end
|
|
end
|
|
|
|
def validate_accepted
|
|
|
|
end
|
|
|
|
def send_notices
|
|
UserMailer.student_lesson_request(self).deliver
|
|
UserMailer.teacher_lesson_request(self).deliver
|
|
LessonBooking.where(id: id).update_all(sent_notices: true)
|
|
end
|
|
|
|
def display_type
|
|
if is_single_free?
|
|
"Free"
|
|
elsif is_test_drive?
|
|
"TestDrive"
|
|
elsif is_normal?
|
|
"Lesson Purchase"
|
|
end
|
|
end
|
|
|
|
# determine the price of this booking based on what the user wants, and the teacher's pricing
|
|
def booked_price
|
|
if is_single_free?
|
|
0
|
|
elsif is_test_drive?
|
|
LessonPackageType.test_drive.price
|
|
elsif is_normal?
|
|
teacher.teacher.booking_price(lesson_length, payment_style != PAYMENT_STYLE_MONTHLY)
|
|
end
|
|
end
|
|
|
|
def is_single_free?
|
|
lesson_type == LESSON_TYPE_FREE
|
|
end
|
|
|
|
def is_test_drive?
|
|
lesson_type == LESSON_TYPE_TEST_DRIVE
|
|
end
|
|
|
|
def is_normal?
|
|
lesson_type == LESSON_TYPE_PAID
|
|
end
|
|
|
|
def card_approved
|
|
LessonBooking.where(id: id).update_all(card_presumed_ok: true)
|
|
if !sent_notices
|
|
send_notices
|
|
end
|
|
end
|
|
|
|
def validate_user
|
|
if is_single_free?
|
|
if !user.has_free_lessons?
|
|
errors.add(:user, 'have no remaining free lessons')
|
|
end
|
|
|
|
#if !user.has_stored_credit_card?
|
|
# errors.add(:user, 'has no credit card stored')
|
|
#end
|
|
elsif is_test_drive?
|
|
if !user.has_test_drives?
|
|
errors.add(:user, "have no remaining test drives")
|
|
end
|
|
elsif is_normal?
|
|
#if !user.has_stored_credit_card?
|
|
# errors.add(:user, 'has no credit card stored')
|
|
#end
|
|
end
|
|
end
|
|
|
|
def validate_teacher
|
|
# shouldn't we check if the teacher already has a booking in this time slot, or at least warn the user
|
|
end
|
|
|
|
def validate_recurring
|
|
if is_single_free? || is_test_drive?
|
|
if recurring
|
|
errors.add(:recurring, "can not be true for this type of lesson")
|
|
end
|
|
end
|
|
|
|
false
|
|
end
|
|
|
|
def validate_lesson_booking_slots
|
|
if lesson_booking_slots.length == 0 || lesson_booking_slots.length == 1
|
|
errors.add(:lesson_booking_slots, "must have two times specified")
|
|
end
|
|
end
|
|
|
|
def validate_lesson_length
|
|
if is_single_free? || is_test_drive?
|
|
if lesson_length != 30
|
|
errors.add(:lesson_length, "must be 30 minutes")
|
|
end
|
|
end
|
|
end
|
|
|
|
def validate_payment_style
|
|
if is_normal?
|
|
if payment_style.nil?
|
|
errors.add(:payment_style, "can't be blank")
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
def self.book_free(user, teacher, lesson_booking_slots, description)
|
|
self.book(user, teacher, LessonBooking::LESSON_TYPE_FREE, lesson_booking_slots, false, 30, PAYMENT_STYLE_ELSEWHERE, description)
|
|
end
|
|
|
|
def self.book_test_drive(user, teacher, lesson_booking_slots, description)
|
|
self.book(user, teacher, LessonBooking::LESSON_TYPE_TEST_DRIVE, lesson_booking_slots, false, 30, PAYMENT_STYLE_ELSEWHERE, description)
|
|
end
|
|
|
|
def self.book_normal(user, teacher, lesson_booking_slots, description, recurring, payment_style, lesson_length)
|
|
self.book(user, teacher, LessonBooking::LESSON_TYPE_PAID, lesson_booking_slots, recurring, lesson_length, payment_style, description)
|
|
end
|
|
|
|
def self.book(user, teacher, lesson_type, lesson_booking_slots, recurring, lesson_length, payment_style, description)
|
|
|
|
lesson_booking = nil
|
|
LessonBooking.transaction do
|
|
|
|
lesson_booking = LessonBooking.new
|
|
lesson_booking.user = user
|
|
lesson_booking.card_presumed_ok = user.has_stored_credit_card?
|
|
lesson_booking.sent_notices = false
|
|
lesson_booking.teacher = teacher
|
|
lesson_booking.lesson_type = lesson_type
|
|
lesson_booking.lesson_booking_slots = lesson_booking_slots
|
|
lesson_booking.recurring = recurring
|
|
lesson_booking.lesson_length = lesson_length
|
|
lesson_booking.payment_style = payment_style
|
|
lesson_booking.description = description
|
|
lesson_booking.status = STATUS_REQUESTED
|
|
|
|
if lesson_booking.save
|
|
|
|
if lesson_booking.is_single_free?
|
|
remaining_free_lessons = user.remaining_free_lessons - 1
|
|
User.where(id: user.id).update_all(remaining_free_lessons: remaining_free_lessons)
|
|
user.remaining_free_lessons = remaining_free_lessons
|
|
|
|
elsif lesson_booking.is_test_drive?
|
|
remaining_test_drives = user.remaining_test_drives - 1
|
|
User.where(id: user.id).update_all(remaining_test_drives: remaining_test_drives)
|
|
user.remaining_test_drives = remaining_test_drives
|
|
end
|
|
|
|
msg = ChatMessage.create(user, nil, description, ChatMessage::CHANNEL_LESSON, nil, teacher, lesson_booking)
|
|
end
|
|
end
|
|
lesson_booking
|
|
end
|
|
|
|
def self.unprocessed(current_user)
|
|
LessonBooking.where(user_id: current_user.id).where(card_presumed_ok: false)
|
|
end
|
|
|
|
def home_url
|
|
APP_CONFIG.external_root_url + "/client#/jamclass"
|
|
end
|
|
|
|
def web_url
|
|
APP_CONFIG.external_root_url + "/client#/jamclass/lesson-request/" + id
|
|
end
|
|
end
|
|
end
|