From 15d968dff50b71d2eaf2e0448422667a88de66c0 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 10 May 2016 21:10:24 -0500 Subject: [PATCH] VRFS-3964 - manage payment features for teacher --- ruby/lib/jam_ruby/app/mailers/user_mailer.rb | 4 +- ...udent_test_drive_lesson_completed.html.erb | 2 +- ...udent_test_drive_lesson_completed.text.erb | 2 +- .../student_test_drive_lesson_done.html.erb | 4 +- .../student_test_drive_lesson_done.text.erb | 2 +- ruby/lib/jam_ruby/models/lesson_session.rb | 8 +-- .../jam_ruby/models/teacher_distribution.rb | 21 ++++-- ruby/lib/jam_ruby/models/user.rb | 11 ++- web/app/assets/javascripts/jam_rest.js | 2 +- .../AccountPaymentHistoryScreen.js.jsx.coffee | 72 ++++++++++++------- .../views/api_teacher_distributions/show.rabl | 10 ++- 11 files changed, 94 insertions(+), 44 deletions(-) diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index ddf1c46d8..1d539e16c 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -928,7 +928,7 @@ module JamRuby @lesson_session = lesson_session email = @student.email - subject = "You have used #{@student.remaining_test_drives} of 4 TestDrive lesson credits" + subject = "You have used #{@student.remaining_test_drives} of #{@student.total_test_drives} TestDrive lesson credits" unique_args = {:type => "student_test_drive_success"} sendgrid_category "Notification" @@ -980,7 +980,7 @@ module JamRuby @lesson_session = lesson_session email = @student.email - subject = "You have used all 4 TestDrive lesson credits" + subject = "You have used all TestDrive lesson credits" unique_args = {:type => "student_test_drive_success"} sendgrid_category "Notification" diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.html.erb index c0e1bcb4d..40ff80156 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.html.erb @@ -1,4 +1,4 @@ -<% provide(:title, "You have used #{@student.remaining_test_drives} of 4 TestDrive lesson credits") %> +<% provide(:title, "You have used #{@student.remaining_test_drives} of #{@student.total_test_drives} TestDrive lesson credits") %> <% provide(:photo_url, @teacher.resolved_photo_url) %> <% content_for :note do %> diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.text.erb index 37ea61020..3e3b6ea4f 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_completed.text.erb @@ -1,4 +1,4 @@ -You have used <%= @student.remaining_test_drives %> of 4 TestDrive lesson credits. +You have used <%= @student.remaining_test_drives %> of <%= @student.total_test_drives %> TestDrive lesson credits. <% if @student.has_rated_teacher(@teacher) %> Also, please rate your teacher at <%= @teacher.ratings_url %> now for today’s lesson to help other students in the community find the best instructors. diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.html.erb index b5e14e612..d99b8494a 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.html.erb @@ -1,11 +1,11 @@ -<% provide(:title, "You have used all 4 TestDrive lesson credits") %> +<% provide(:title, "You have used all TestDrive lesson credits") %>

Hello <%= @student.name %>,

- We hope you enjoyed your JamClass lesson today with <%= @teacher.name %>. You have now used all 4 TestDrive credits. + We hope you enjoyed your JamClass lesson today with <%= @teacher.name %>. You have now used all your TestDrive credits. <% if !@student.has_rated_teacher(@teacher) %> Please rate your teacher now for today’s lesson to diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.text.erb index 2d4d02829..f80c5c8cb 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/student_test_drive_lesson_done.text.erb @@ -1,4 +1,4 @@ -You have used all of your 4 TestDrive lesson credits. +You have used all of your TestDrive lesson credits. <% if @student.has_rated_teacher(@teacher) %> Also, please rate your teacher at <%= @teacher.ratings_url %> now for today’s lesson to help other students in the community find the best instructors. diff --git a/ruby/lib/jam_ruby/models/lesson_session.rb b/ruby/lib/jam_ruby/models/lesson_session.rb index cd35d803d..641d4bdb7 100644 --- a/ruby/lib/jam_ruby/models/lesson_session.rb +++ b/ruby/lib/jam_ruby/models/lesson_session.rb @@ -256,13 +256,13 @@ module JamRuby def test_drive_completed distribution = teacher_distribution - if distribution # not all lessons/payment charges have a distribution - distribution.ready = true - distribution.save(validate: false) - end if !sent_notices if success + if distribution # not all lessons/payment charges have a distribution + distribution.ready = true + distribution.save(validate: false) + end student.test_drive_succeeded(self) else student.test_drive_failed(self) diff --git a/ruby/lib/jam_ruby/models/teacher_distribution.rb b/ruby/lib/jam_ruby/models/teacher_distribution.rb index cec347b37..80b14fa01 100644 --- a/ruby/lib/jam_ruby/models/teacher_distribution.rb +++ b/ruby/lib/jam_ruby/models/teacher_distribution.rb @@ -14,7 +14,7 @@ module JamRuby limit ||= 100 limit = limit.to_i - query = TeacherDistribution.where(teacher_id: current_user.id) + query = TeacherDistribution.where(teacher_id: current_user.id).order('created_at desc') current_page = params[:page].nil? ? 1 : params[:page].to_i next_page = current_page + 1 @@ -31,6 +31,16 @@ module JamRuby 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 @@ -51,6 +61,7 @@ module JamRuby distribution.amount_in_cents = target.lesson_booking.distribution_price_in_cents(target) distribution end + def amount amount_in_cents / 100.0 end @@ -63,7 +74,7 @@ module JamRuby if is_test_drive? 0 else - (amount_in_cents * teacher.jamkazam_rate + 0.03).round + (amount_in_cents * teacher.teacher.jamkazam_rate + 0.03).round end end @@ -94,16 +105,16 @@ module JamRuby 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}" + "TestDrive session with #{lesson_session.lesson_booking.student.name} on #{lesson_session.scheduled_start.to_date.strftime('%B %d, %Y')}" 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}" + "Lesson with #{lesson_session.lesson_booking.student.name} on #{lesson_session.scheduled_start.to_date.strftime('%B %d, %Y')}" end end else - "Monthly session for the month of #{lesson_package_purchase.month_name} with #{lesson_package_purchase.lesson_booking.student.name}" + "Lessons for the month of #{lesson_package_purchase.month_name} with #{lesson_package_purchase.lesson_booking.student.name}" end end end diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 582857e1c..bd84e79e3 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -2058,6 +2058,15 @@ module JamRuby lesson_purchases.where('lesson_package_type_id in (?)', [LessonPackageType.test_drive_package_ids]).order('created_at desc').first end + def total_test_drives + purchase = most_recent_test_drive_purchase + if purchase + purchase.test_drive_count + else + 0 + end + end + def test_drive_succeeded(lesson_session) if self.remaining_test_drives <= 0 UserMailer.student_test_drive_lesson_done(lesson_session).deliver @@ -2083,7 +2092,7 @@ module JamRuby end def used_test_drives - 4 - remaining_test_drives + total_test_drives - remaining_test_drives end def has_rated_teacher(teacher) diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 4605d9eab..825e5d817 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -2446,7 +2446,7 @@ function listTeacherDistributions(options) { - if(options) { + if(!options) { options = {} } diff --git a/web/app/assets/javascripts/react-components/AccountPaymentHistoryScreen.js.jsx.coffee b/web/app/assets/javascripts/react-components/AccountPaymentHistoryScreen.js.jsx.coffee index d4b5deeef..47be61e3f 100644 --- a/web/app/assets/javascripts/react-components/AccountPaymentHistoryScreen.js.jsx.coffee +++ b/web/app/assets/javascripts/react-components/AccountPaymentHistoryScreen.js.jsx.coffee @@ -59,30 +59,22 @@ profileUtils = context.JK.ProfileUtils @incrementCurrentPage() @refresh() - activeListItems: () -> - tile = @activeTile() - if tile == @TILE_PAYMENTS_TO_YOU - this.state.teacher_distributions - else if tile == @TILE_PAYMENTS_TO_JAMKAZAM - this.state.sales - else - [] componentDidUpdate: (prevProps, prevState) -> #@iCheckify() - if @state.next == null + if @currentNext() == null @contentBodyScroller.off('scroll') - if @state.salesCurrentPage == 1 and @state.sales.length == 0 + if @state[@getCurrentPageName()] == 1 and @getCurrentList().length == 0 @endOfList.show() logger.debug("PaymentHistoryScreen: empty search") - else if @state.salesCurrentPage > 0 + else if @state[@getCurrentPageName()] > 0 logger.debug("end of search") @endOfList.show() else @registerInfiniteScroll(@contentBodyScroller) - if @activeTile(prevState.selected) != @activeTile() + if @activeTile(prevState.selected) != @activeTile() && @getCurrentList().length == 0 @refresh() checkboxChanged: (e) -> @@ -109,6 +101,8 @@ profileUtils = context.JK.ProfileUtils @refreshTeacherDistributions() else if @activeTile() == @TILE_PAYMENTS_TO_JAMKAZAM @refreshSales() + else + logger.debug("dropping refresh because no tile match", @activeTile) refreshSales: () -> @refreshing = true @@ -124,7 +118,7 @@ profileUtils = context.JK.ProfileUtils salesHistoryDone:(response) -> @refreshing = false - this.setState({next: response.next, sales: this.state.sales.concat(response.entries)}) + this.setState({salesNext: response.next, sales: this.state.sales.concat(response.entries)}) salesHistoryFail:(jqXHR) -> @refreshing = false @@ -132,7 +126,7 @@ profileUtils = context.JK.ProfileUtils teacherDistributionsDone:(response) -> @refreshing = false - this.setState({next: response.next, distributions: this.state.distributions.concat(response.entries)}) + this.setState({distributionsNext: response.next, distributions: this.state.distributions.concat(response.entries)}) teacherDistributionsFail:(jqXHR) -> @refreshing = false @@ -140,18 +134,17 @@ profileUtils = context.JK.ProfileUtils clearResults:() -> - this.setState({salesCurrentPage: 0, sales: [], distributionsCurrentPage: [], distributions: 0, next: null}) + this.setState({salesCurrentPage: 0, sales: [], distributionsCurrentPage: 0, distributions: [], salesNext: null, distributionsNext: null}) buildQuery:(page = @getCurrentPage()) -> - console.log("PAGE!", page) @currentQuery = this.defaultQuery(page) defaultQuery:(page = @getCurrentPage()) -> query = per_page: @LIMIT page: page + 1 - if this.state.next - query.since = this.state.next + if @currentNext() + query.page = @currentNext() query getCurrentPage: () -> @@ -173,16 +166,33 @@ profileUtils = context.JK.ProfileUtils else 1 + getCurrentList: () -> + if @activeTile() == @TILE_PAYMENTS_TO_JAMKAZAM + @state['sales'] + else if @activeTile() == @TILE_PAYMENTS_TO_YOU + @state['distributions'] + else + @state['sales'] + + currentNext: () -> + if @activeTile() == @TILE_PAYMENTS_TO_JAMKAZAM + @state.salesNext + else if @activeTile() == @TILE_PAYMENTS_TO_YOU + @state.distributionsNext + else + null + getInitialState: () -> { user: null, nextPager: null, salesCurrentPage: 0, distributionsCurrentPage: 0 - next: null, + salesNext: null, + distributionsNext: null sales: [], distributions: [] - selected: null + selected: 'payments to jamkazam' } onCancel: (e) -> @@ -205,7 +215,7 @@ profileUtils = context.JK.ProfileUtils paymentsToYou: () -> rows = [] - for paymentHistory in this.state.sales + for paymentHistory in @getCurrentList() paymentMethod = 'Stripe' if paymentHistory.distributed @@ -213,10 +223,24 @@ profileUtils = context.JK.ProfileUtils status = 'Paid' else date = paymentHistory.created_at - status = 'Collecting' - date = context.JK.formatDate(date, true) + if paymentHistory.not_collectable + status = 'Uncollectible' + else if !paymentHistory.teacher?.teacher?.stripe_account_id? + status = 'No Stripe Acct' + else + status = 'Collecting' + date = context.JK.formatDate(date, true) + description = paymentHistory.description + + if paymentHistory.teacher_payment? + amt = paymentHistory.teacher_payment.real_distribution_in_cents + else + amt = paymentHistory.real_distribution_in_cents + + displayAmount = ' $' + (amt/100).toFixed(2) + amountClasses = {status: status} row = @@ -259,7 +283,7 @@ profileUtils = context.JK.ProfileUtils paymentsToJamKazam: () -> rows = [] - for paymentHistory in this.state.sales + for paymentHistory in @getCurrentList() paymentMethod = 'Credit Card' if paymentHistory.sale? sale = paymentHistory.sale diff --git a/web/app/views/api_teacher_distributions/show.rabl b/web/app/views/api_teacher_distributions/show.rabl index f9e70ba3f..b7d0eda36 100644 --- a/web/app/views/api_teacher_distributions/show.rabl +++ b/web/app/views/api_teacher_distributions/show.rabl @@ -1,12 +1,18 @@ object @teacher_distribution -attributes :id, :description, :ready, :distributed, :created_at, :amount_in_cents, :real_distribution_in_cents +attributes :id, :description, :ready, :distributed, :created_at, :amount_in_cents, :real_distribution_in_cents, :not_collectable + +child(:teacher => :teacher) { + child(:teacher => :teacher) { + attributes :stripe_account_id + } +} child(:teacher_payment => :teacher_payment) { attributes :real_distribution_in_cents, :created_at child(:teacher_payment_charge => :teacher_payment_charge) { - attributes: last_billing_attempt_at + attributes last_billing_attempt_at } }