module JamRuby class TeacherDistribution < ActiveRecord::Base belongs_to :teacher, class_name: "JamRuby::User", foreign_key: "teacher_id" belongs_to :teacher_payment, class_name: "JamRuby::TeacherPayment" belongs_to :lesson_session, class_name: "JamRuby::LessonSession" belongs_to :lesson_package_purchase, class_name: "JamRuby::LessonPackagePurchase" validates :teacher, presence: true validates :amount_in_cents, presence: true def self.index(current_user, params) limit = params[:per_page] limit ||= 100 limit = limit.to_i query = TeacherDistribution.where(teacher_id: current_user.id) current_page = params[:page].nil? ? 1 : params[:page].to_i next_page = current_page + 1 # will_paginate gem query = query.paginate(:page => current_page, :per_page => limit) if query.length == 0 # no more results {query: query, next_page: nil} elsif query.length < limit # no more results {query: query, next_page: nil} else {query: query, next_page: next_page} end end def self.create_for_lesson(lesson_session) distribution = create(lesson_session) distribution.lesson_session = lesson_session distribution end def self.create_for_lesson_package_purchase(lesson_package_purchase) distribution = create(lesson_package_purchase) distribution.lesson_package_purchase = lesson_package_purchase distribution end def self.create(target) distribution = TeacherDistribution.new distribution.teacher = target.teacher distribution.ready = false distribution.distributed = false distribution.amount_in_cents = target.lesson_booking.distribution_price_in_cents(target) distribution end def amount amount_in_cents / 100.0 end def student if lesson_session lesson_session.student else lesson_package_purchase.student end end def month_name lesson_package_purchase.month_name end def is_test_drive? lesson_session && lesson_session.is_test_drive? end def is_normal? lesson_session && !lesson_session.is_test_drive? end def is_monthly? !lesson_package_purchase.nil? end def description if lesson_session if lesson_session.lesson_booking.is_test_drive? "Test Drive session with #{lesson_session.lesson_booking.student.name} on #{lesson_session.scheduled_start.to_date}" elsif lesson_session.lesson_booking.is_normal? if lesson_session.lesson_booking.is_weekly_payment? || lesson_session.lesson_booking.is_monthly_payment? raise "Should not be here" else "A session with #{lesson_session.lesson_booking.student.name} on #{lesson_session.scheduled_start.to_date}" end end else "Monthly session for the month of #{lesson_package_purchase.month_name} with #{lesson_package_purchase.lesson_booking.student.name}" end end end end