Class: Kaui::SubscriptionsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/subscriptions_controller.rb

Constant Summary

Constants included from EngineControllerUtil

EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#createObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/kaui/subscriptions_controller.rb', line 22

def create
  plan_name = params.require(:plan_name)
  @base_product_name = params[:base_product_name]
  @subscription = Kaui::Subscription.new(params.require(:subscription).delete_if { |key, value| value.blank? })

  begin
    @bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name)

    plan_details = plans_details.find { |p| p.plan == plan_name }
    raise "Unable to find plan #{plan_name}" if plan_details.nil?

    @subscription.plan_name = plan_name
    requested_date = params[:type_change] == "DATE" ? params[:requested_date].presence : nil

    # price override?
    override_fixed_price = plan_details.phases.first.prices.blank? rescue false
    override_recurring_price = !override_fixed_price
    phase_type = @bundle.nil? ? plan_details.phases.first.type : @bundle.subscriptions.first.phase_type
    overrides = price_overrides(phase_type, override_fixed_price, override_recurring_price)
    @subscription.price_overrides = overrides unless overrides.blank?

    # un-set product_category since is not needed if plan name exist
    @subscription.product_category = nil
    @subscription = @subscription.create(current_user.kb_username, params[:reason], params[:comment], requested_date, false, options_for_klient)
    redirect_to kaui_engine.(@subscription.), :notice => 'Subscription was successfully created'
  rescue => e
    @plans = plans_details.nil? ? [] : plans_details.map { |p| p.plan }
    flash.now[:error] = "Error while creating the subscription: #{as_string(e)}"
    render :new
  end
end

#destroyObject


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/kaui/subscriptions_controller.rb', line 95

def destroy
  requested_date = params[:requested_date].presence
  billing_policy = params[:policy].presence
  # START_OF_TERM is *not* a valid entitlement_policy and so would default to IMMEDIATE
  entitlement_policy = billing_policy && billing_policy == 'START_OF_TERM' ? 'IMMEDIATE' : billing_policy

  # true by default
  use_requested_date_for_billing = (params[:use_requested_date_for_billing] || '1') == '1'

  subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)

  subscription.cancel(current_user.kb_username, params[:reason], params[:comment], requested_date, entitlement_policy, billing_policy, use_requested_date_for_billing, options_for_klient)

  redirect_to kaui_engine.(subscription.), :notice => 'Subscription was successfully cancelled'
end

#editObject


54
55
56
57
58
59
# File 'app/controllers/kaui/subscriptions_controller.rb', line 54

def edit
  @subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
  _, plans_details = lookup_bundle_and_plan_details(@subscription)
  # Use a Set to deal with multiple pricelists
  @plans = Set.new.merge(plans_details.map { |p| p.plan })
end

#edit_bcdObject


119
120
121
# File 'app/controllers/kaui/subscriptions_controller.rb', line 119

def edit_bcd
  @subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
end

#newObject


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/kaui/subscriptions_controller.rb', line 3

def new
  @base_product_name = params[:base_product_name]
  @subscription = Kaui::Subscription.new(:bundle_id => params[:bundle_id],
                                         :account_id => params[:account_id],
                                         :product_category => params[:product_category] || 'BASE')

  @bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name)
  @plans = plans_details.map { |p| p.plan }

  if @plans.empty?
    if @subscription.product_category == 'BASE'
      flash[:error] = 'No available plan'
    else
      flash[:error] = "No available add-on for product #{@base_product_name}"
    end
    redirect_to kaui_engine.(@subscription.), :error => 'No available plan'
  end
end

#reinstateObject


111
112
113
114
115
116
117
# File 'app/controllers/kaui/subscriptions_controller.rb', line 111

def reinstate
  subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)

  subscription.uncancel(current_user.kb_username, params[:reason], params[:comment], options_for_klient)

  redirect_to kaui_engine.(subscription.), :notice => 'Subscription was successfully reinstated'
end

#restful_showObject


139
140
141
142
# File 'app/controllers/kaui/subscriptions_controller.rb', line 139

def restful_show
  subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
  redirect_to kaui_engine.(subscription.)
end

#showObject


135
136
137
# File 'app/controllers/kaui/subscriptions_controller.rb', line 135

def show
  restful_show
end

#updateObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/kaui/subscriptions_controller.rb', line 61

def update
  plan_name = params.require(:plan_name)

  requested_date = params[:type_change] == "DATE" ? params[:requested_date].presence : nil
  billing_policy = params[:type_change] == "POLICY" ? params[:policy].presence : nil

  wait_for_completion = params[:wait_for_completion] == '1'

  subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)

  input = { :planName => plan_name }

  # price override?
  current_plan = subscription.prices.select { |price| price['phaseType'] == subscription.phase_type }
  override_fixed_price = current_plan.last['recurringPrice'].nil?
  override_recurring_price = !override_fixed_price
  overrides = price_overrides(subscription.phase_type, override_fixed_price, override_recurring_price)
  input[:priceOverrides] = overrides unless overrides.blank?

  subscription.change_plan(input,
                           current_user.kb_username,
                           params[:reason],
                           params[:comment],
                           requested_date,
                           billing_policy,
                           nil,
                           wait_for_completion,
                           options_for_klient)

  redirect_to kaui_engine.(subscription.), :notice => 'Subscription plan successfully changed'
rescue => e
  redirect_to edit_subscription_path(params.require(:id)), :flash => { :error => "Error while changing subscription: #{as_string(e)}" }
end

#update_bcdObject


123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/kaui/subscriptions_controller.rb', line 123

def update_bcd
  input_subscription = params.require(:subscription)
  subscription =  Kaui::Subscription.new
  subscription.subscription_id = params.require(:id)
  subscription.bill_cycle_day_local = input_subscription["bill_cycle_day_local"]

  effective_from_date = params["effective_from_date"]

  subscription.update_bcd(current_user.kb_username, params[:reason], params[:comment], effective_from_date, nil, options_for_klient)
  redirect_to kaui_engine.(input_subscription["account_id"]), :notice => 'Subscription BCD was successfully changed'
end

#update_tagsObject


173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/kaui/subscriptions_controller.rb', line 173

def update_tags
  subscription_id = params.require(:id)
  subscription = Kaui::Subscription.find_by_id(subscription_id, options_for_klient)

  tags = []
  params.each do |tag|
    tag_info = tag.split('_')
    next if tag_info.size != 2 or tag_info[0] != 'tag'
    tags << tag_info[1]
  end

  Kaui::Tag.set_for_subscription(subscription_id, tags, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
  redirect_to kaui_engine.(subscription.), :notice => 'Subscription tags successfully set'
end

#validate_bundle_external_keyObject


144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/kaui/subscriptions_controller.rb', line 144

def validate_bundle_external_key
  json_response do
    external_key = params.require(:external_key)

    begin
      bundle = Kaui::Bundle.find_by_external_key(external_key, false, options_for_klient)
    rescue KillBillClient::API::NotFound
      bundle = nil
    end

    { :is_found => !bundle.nil? }
  end
end

#validate_external_keyObject


159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/kaui/subscriptions_controller.rb', line 159

def validate_external_key
  json_response do
    external_key = params.require(:external_key)

    begin
      subscription = Kaui::Subscription.find_by_external_key(external_key, options_for_klient)
    rescue KillBillClient::API::NotFound
      subscription = nil
    end

    { :is_found => !subscription.nil? }
  end
end