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" belongs_to :school, class_name: "JamRuby::School" 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).where('school_id IS NULL').order('created_at desc') 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 not_collectable if is_test_drive? false elsif is_normal? !lesson_session.billing_should_retry else ! lesson_package_purchase.billing_should_retry 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.school = target.lesson_booking.school distribution end def amount amount_in_cents / 100.0 end def real_distribution_in_cents amount_in_cents - calculate_teacher_fee end def real_distribution (real_distribution_in_cents / 100.0) end def real_distribution_display '$%.2f' % real_distribution end def calculate_teacher_fee if is_test_drive? 0 else (amount_in_cents * (teacher.teacher.jamkazam_rate + 0.03)).round end 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 lesson_session.timed_description else lesson_package_purchase.description end end end end