subscription-dashboard.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. define [
  2. "base"
  3. ], (App)->
  4. App.controller 'SuccessfulSubscriptionController', ($scope, sixpack) ->
  5. sixpack.convert 'plans-1610', () ->
  6. SUBSCRIPTION_URL = "/user/subscription/update"
  7. setupReturly = _.once ->
  8. recurly?.configure window.recurlyApiKey
  9. PRICES = {}
  10. App.controller "CurrenyDropdownController", ($scope, MultiCurrencyPricing, $q)->
  11. # $scope.plans = MultiCurrencyPricing.plans
  12. $scope.currencyCode = MultiCurrencyPricing.currencyCode
  13. $scope.changeCurrency = (newCurrency)->
  14. MultiCurrencyPricing.currencyCode = newCurrency
  15. App.controller "ChangePlanFormController", ($scope, $modal, MultiCurrencyPricing)->
  16. setupReturly()
  17. taxRate = window.taxRate
  18. $scope.changePlan = ->
  19. $modal.open(
  20. templateUrl: "confirmChangePlanModalTemplate"
  21. controller: "ConfirmChangePlanController"
  22. scope: $scope
  23. )
  24. $scope.$watch "pricing.currencyCode", ->
  25. $scope.currencyCode = MultiCurrencyPricing.currencyCode
  26. $scope.pricing = MultiCurrencyPricing
  27. # $scope.plans = MultiCurrencyPricing.plans
  28. $scope.currencySymbol = MultiCurrencyPricing.plans[MultiCurrencyPricing.currencyCode].symbol
  29. $scope.currencyCode = MultiCurrencyPricing.currencyCode
  30. $scope.prices = PRICES
  31. $scope.refreshPrice = (planCode)->
  32. if $scope.prices[planCode]?
  33. return
  34. $scope.prices[planCode] = "..."
  35. pricing = recurly.Pricing()
  36. pricing.plan(planCode, { quantity: 1 }).currency(MultiCurrencyPricing.currencyCode).done (price)->
  37. totalPriceExTax = parseFloat(price.next.total)
  38. $scope.$evalAsync () ->
  39. taxAmmount = totalPriceExTax * taxRate
  40. if isNaN(taxAmmount)
  41. taxAmmount = 0
  42. $scope.prices[planCode] = $scope.currencySymbol + (totalPriceExTax + taxAmmount)
  43. price = ""
  44. App.controller "ConfirmChangePlanController", ($scope, $modalInstance, $http)->
  45. $scope.confirmChangePlan = ->
  46. body =
  47. plan_code: $scope.plan.planCode
  48. _csrf : window.csrfToken
  49. $scope.inflight = true
  50. $http.post("#{SUBSCRIPTION_URL}?origin=confirmChangePlan", body)
  51. .then ->
  52. location.reload()
  53. .catch ->
  54. console.log "something went wrong changing plan"
  55. $scope.cancel = () ->
  56. $modalInstance.dismiss('cancel')
  57. App.controller "LeaveGroupModalController", ($scope, $modalInstance, $http)->
  58. $scope.confirmLeaveGroup = ->
  59. $scope.inflight = true
  60. $http({
  61. url: "/subscription/group/user",
  62. method: "DELETE",
  63. params: {admin_user_id: $scope.admin_id, _csrf: window.csrfToken}
  64. }).then ->
  65. location.reload()
  66. .catch ->
  67. console.log "something went wrong changing plan"
  68. $scope.cancel = () ->
  69. $modalInstance.dismiss('cancel')
  70. App.controller "UserSubscriptionController", ($scope, MultiCurrencyPricing, $http, sixpack, $modal) ->
  71. $scope.plans = MultiCurrencyPricing.plans
  72. freeTrialEndDate = new Date(subscription?.trial_ends_at)
  73. sevenDaysTime = new Date()
  74. sevenDaysTime.setDate(sevenDaysTime.getDate() + 7)
  75. freeTrialInFuture = freeTrialEndDate > new Date()
  76. freeTrialExpiresUnderSevenDays = freeTrialEndDate < sevenDaysTime
  77. $scope.view = 'overview'
  78. $scope.getSuffix = (planCode) ->
  79. planCode?.match(/(.*?)_(.*)/)?[2] || null
  80. $scope.subscriptionSuffix = $scope.getSuffix(window?.subscription?.planCode)
  81. if $scope.subscriptionSuffix == 'free_trial_7_days'
  82. $scope.subscriptionSuffix = ''
  83. $scope.isNextGenPlan = $scope.subscriptionSuffix in ['heron', 'ibis']
  84. $scope.shouldShowPlan = (planCode) ->
  85. $scope.getSuffix(planCode) not in ['heron', 'ibis']
  86. isMonthlyCollab = subscription?.planCode?.indexOf("collaborator") != -1 and subscription?.planCode?.indexOf("ann") == -1
  87. stillInFreeTrial = freeTrialInFuture and freeTrialExpiresUnderSevenDays
  88. if isMonthlyCollab and stillInFreeTrial
  89. $scope.showExtendFreeTrial = true
  90. else if isMonthlyCollab and !stillInFreeTrial
  91. $scope.showDowngradeToStudent = true
  92. else
  93. $scope.showBasicCancel = true
  94. setupReturly()
  95. recurly.Pricing().plan('student', { quantity: 1 }).currency(MultiCurrencyPricing.currencyCode).done (price)->
  96. totalPriceExTax = parseFloat(price.next.total)
  97. $scope.$evalAsync () ->
  98. taxAmmount = totalPriceExTax * taxRate
  99. if isNaN(taxAmmount)
  100. taxAmmount = 0
  101. $scope.currencySymbol = MultiCurrencyPricing.plans[MultiCurrencyPricing.currencyCode].symbol
  102. $scope.studentPrice = $scope.currencySymbol + (totalPriceExTax + taxAmmount)
  103. $scope.downgradeToStudent = ->
  104. body =
  105. plan_code: 'student'
  106. _csrf : window.csrfToken
  107. $scope.inflight = true
  108. $http.post("#{SUBSCRIPTION_URL}?origin=downgradeToStudent", body)
  109. .then ->
  110. location.reload()
  111. .catch ->
  112. console.log "something went wrong changing plan"
  113. $scope.cancelSubscription = ->
  114. body =
  115. _csrf : window.csrfToken
  116. $scope.inflight = true
  117. $http.post("/user/subscription/cancel", body)
  118. .then ->
  119. location.reload()
  120. .catch ->
  121. console.log "something went wrong changing plan"
  122. $scope.removeSelfFromGroup = (admin_id)->
  123. $scope.admin_id = admin_id
  124. $modal.open(
  125. templateUrl: "LeaveGroupModalTemplate"
  126. controller: "LeaveGroupModalController"
  127. scope: $scope
  128. )
  129. $scope.switchToCancelationView = ->
  130. $scope.view = "cancelation"
  131. $scope.exendTrial = ->
  132. body =
  133. _csrf : window.csrfToken
  134. $scope.inflight = true
  135. $http.put("/user/subscription/extend", body)
  136. .then ->
  137. location.reload()
  138. .catch ->
  139. console.log "something went wrong changing plan"