kaui 0.6.6 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/Gemfile.lock +51 -37
  4. data/README.md +5 -0
  5. data/Rakefile +1 -1
  6. data/app/controllers/kaui/account_emails_controller.rb +26 -44
  7. data/app/controllers/kaui/account_tags_controller.rb +26 -38
  8. data/app/controllers/kaui/account_timelines_controller.rb +47 -50
  9. data/app/controllers/kaui/accounts_controller.rb +68 -148
  10. data/app/controllers/kaui/bundle_tags_controller.rb +32 -21
  11. data/app/controllers/kaui/bundles_controller.rb +44 -69
  12. data/app/controllers/kaui/chargebacks_controller.rb +31 -61
  13. data/app/controllers/kaui/charges_controller.rb +19 -21
  14. data/app/controllers/kaui/credits_controller.rb +22 -36
  15. data/app/controllers/kaui/custom_fields_controller.rb +15 -13
  16. data/app/controllers/kaui/engine_controller.rb +8 -10
  17. data/app/controllers/kaui/invoice_items_controller.rb +24 -41
  18. data/app/controllers/kaui/invoices_controller.rb +36 -46
  19. data/app/controllers/kaui/payment_methods_controller.rb +84 -28
  20. data/app/controllers/kaui/payments_controller.rb +64 -26
  21. data/app/controllers/kaui/refunds_controller.rb +27 -107
  22. data/app/controllers/kaui/subscriptions_controller.rb +88 -132
  23. data/app/controllers/kaui/tag_definitions_controller.rb +32 -79
  24. data/app/controllers/kaui/tags_controller.rb +16 -14
  25. data/app/controllers/kaui/transactions_controller.rb +25 -0
  26. data/app/helpers/kaui/money_helper.rb +8 -0
  27. data/app/helpers/kaui/subscription_helper.rb +115 -0
  28. data/app/helpers/kaui/uuid_helper.rb +9 -0
  29. data/app/models/kaui/ability.rb +2 -2
  30. data/app/models/kaui/account.rb +25 -42
  31. data/app/models/kaui/account_email.rb +11 -48
  32. data/app/models/kaui/account_timeline.rb +1 -5
  33. data/app/models/kaui/audit_log.rb +1 -15
  34. data/app/models/kaui/base.rb +1 -126
  35. data/app/models/kaui/bundle.rb +47 -14
  36. data/app/models/kaui/catalog.rb +2 -0
  37. data/app/models/kaui/chargeback.rb +24 -36
  38. data/app/models/kaui/credit.rb +5 -20
  39. data/app/models/kaui/custom_field.rb +9 -5
  40. data/app/models/kaui/invoice.rb +9 -53
  41. data/app/models/kaui/invoice_item.rb +8 -18
  42. data/app/models/kaui/invoice_payment.rb +24 -0
  43. data/app/models/kaui/payment.rb +14 -32
  44. data/app/models/kaui/payment_method.rb +36 -0
  45. data/app/models/kaui/rails_methods.rb +7 -9
  46. data/app/models/kaui/refund.rb +5 -20
  47. data/app/models/kaui/subscription.rb +7 -27
  48. data/app/models/kaui/tag.rb +10 -6
  49. data/app/models/kaui/tag_definition.rb +26 -34
  50. data/app/models/kaui/transaction.rb +30 -0
  51. data/app/models/kaui/user.rb +2 -0
  52. data/app/views/kaui/account_emails/_form.html.erb +8 -28
  53. data/app/views/kaui/account_emails/show.html.erb +10 -10
  54. data/app/views/kaui/account_tags/_account_tags_table.html.erb +20 -18
  55. data/app/views/kaui/account_tags/_form.html.erb +42 -0
  56. data/app/views/kaui/account_tags/edit.html.erb +1 -41
  57. data/app/views/kaui/account_tags/show.html.erb +6 -3
  58. data/app/views/kaui/account_timelines/show.html.erb +273 -302
  59. data/app/views/kaui/accounts/_form.html.erb +116 -0
  60. data/app/views/kaui/accounts/index.html.erb +13 -11
  61. data/app/views/kaui/accounts/new.html.erb +5 -0
  62. data/app/views/kaui/accounts/show.html.erb +101 -99
  63. data/app/views/kaui/bundle_tags/_bundle_tags_table.html.erb +29 -9
  64. data/app/views/kaui/bundle_tags/_form.html.erb +41 -0
  65. data/app/views/kaui/bundle_tags/edit.html.erb +2 -34
  66. data/app/views/kaui/bundle_tags/show.html.erb +6 -0
  67. data/app/views/kaui/bundles/index.html.erb +9 -9
  68. data/app/views/kaui/bundles/show.html.erb +17 -15
  69. data/app/views/kaui/bundles/transfer.html.erb +25 -24
  70. data/app/views/kaui/chargebacks/_form.html.erb +44 -0
  71. data/app/views/kaui/chargebacks/new.html.erb +3 -115
  72. data/app/views/kaui/charges/_form.html.erb +73 -0
  73. data/app/views/kaui/charges/new.html.erb +2 -91
  74. data/app/views/kaui/credits/_form.html.erb +73 -0
  75. data/app/views/kaui/credits/new.html.erb +2 -85
  76. data/app/views/kaui/invoice_items/edit.html.erb +77 -78
  77. data/app/views/kaui/invoices/show.html.erb +3 -10
  78. data/app/views/kaui/layouts/kaui_application.html.erb +0 -2
  79. data/app/views/kaui/payment_methods/_form.html.erb +30 -0
  80. data/app/views/kaui/payment_methods/_new_creditcard_payment_method.html.erb +19 -18
  81. data/app/views/kaui/payment_methods/_payment_methods_table.html.erb +8 -5
  82. data/app/views/kaui/payment_methods/index.html.erb +10 -10
  83. data/app/views/kaui/payment_methods/new.html.erb +2 -20
  84. data/app/views/kaui/payment_methods/show.html.erb +2 -1
  85. data/app/views/kaui/payments/_payments_table.html.erb +30 -22
  86. data/app/views/kaui/payments/index.html.erb +1 -1
  87. data/app/views/kaui/payments/new.html.erb +5 -5
  88. data/app/views/kaui/payments/show.html.erb +3 -0
  89. data/app/views/kaui/refunds/new.html.erb +96 -104
  90. data/app/views/kaui/subscriptions/_form.html.erb +36 -0
  91. data/app/views/kaui/subscriptions/_subscriptions_table.html.erb +39 -44
  92. data/app/views/kaui/subscriptions/edit.html.erb +24 -35
  93. data/app/views/kaui/subscriptions/index.html.erb +13 -12
  94. data/app/views/kaui/subscriptions/new.html.erb +6 -43
  95. data/app/views/kaui/subscriptions/show.html.erb +32 -25
  96. data/app/views/kaui/tag_definitions/_form.html.erb +10 -37
  97. data/app/views/kaui/tag_definitions/index.html.erb +3 -5
  98. data/app/views/kaui/tag_definitions/show.html.erb +1 -2
  99. data/app/views/kaui/transactions/_form.html.erb +77 -0
  100. data/app/views/kaui/transactions/new.html.erb +3 -0
  101. data/config/initializers/kilbill-client-init.rb +1 -1
  102. data/config/routes.rb +4 -15
  103. data/kaui.gemspec +8 -6
  104. data/lib/kaui/engine.rb +4 -1
  105. data/lib/kaui/version.rb +1 -1
  106. data/lib/kaui.rb +2 -2
  107. data/test/functional/kaui/account_emails_controller_test.rb +28 -0
  108. data/test/functional/kaui/account_tags_controller_test.rb +29 -0
  109. data/test/functional/kaui/account_timelines_controller_test.rb +21 -0
  110. data/test/functional/kaui/accounts_controller_test.rb +56 -50
  111. data/test/functional/kaui/bundle_tags_controller_test.rb +29 -0
  112. data/test/functional/kaui/bundles_controller_test.rb +58 -13
  113. data/test/functional/kaui/chargebacks_controller_test.rb +16 -5
  114. data/test/functional/kaui/charges_controller_test.rb +39 -0
  115. data/test/functional/kaui/credits_controller_test.rb +35 -0
  116. data/test/functional/kaui/custom_fields_controller_test.rb +21 -0
  117. data/test/functional/kaui/functional_test_helper.rb +61 -0
  118. data/test/functional/kaui/invoice_items_controller_test.rb +24 -10
  119. data/test/functional/kaui/invoices_controller_test.rb +41 -13
  120. data/test/functional/kaui/payment_methods_controller_test.rb +49 -0
  121. data/test/functional/kaui/payments_controller_test.rb +32 -5
  122. data/test/functional/kaui/refunds_controller_test.rb +40 -5
  123. data/test/functional/kaui/subscriptions_controller_test.rb +82 -0
  124. data/test/functional/kaui/tag_definitions_controller_test.rb +27 -53
  125. data/test/functional/kaui/tags_controller_test.rb +21 -0
  126. data/test/functional/kaui/transactions_controller_test.rb +32 -0
  127. data/test/integration/kaui/integration_test_helper.rb +31 -0
  128. data/test/integration/kaui/navigation_test.rb +43 -0
  129. data/test/killbill_test_helper.rb +143 -0
  130. data/test/test_helper.rb +10 -227
  131. data/test/unit/kaui/account_email_test.rb +19 -0
  132. data/test/unit/kaui/account_test.rb +7 -35
  133. data/test/unit/kaui/base_test.rb +5 -46
  134. data/test/unit/kaui/date_helper_test.rb +6 -5
  135. data/test/unit/kaui/invoice_item_test.rb +5 -17
  136. data/test/unit/kaui/invoice_payment_test.rb +42 -0
  137. data/test/unit/kaui/invoice_test.rb +15 -15
  138. data/test/unit/kaui/money_helper_test.rb +10 -0
  139. data/test/unit/kaui/payment_test.rb +42 -0
  140. data/test/unit/kaui/tag_definition_test.rb +5 -28
  141. data/test/unit/kaui/uuid_helper_test.rb +10 -0
  142. metadata +134 -159
  143. data/app/controllers/kaui/analytics_controller.rb +0 -28
  144. data/app/helpers/kaui/killbill_helper.rb +0 -611
  145. data/app/models/kaui/analytics.rb +0 -19
  146. data/app/models/kaui/analytics_sanity.rb +0 -7
  147. data/app/models/kaui/business_account.rb +0 -12
  148. data/app/models/kaui/business_field.rb +0 -6
  149. data/app/models/kaui/business_invoice.rb +0 -31
  150. data/app/models/kaui/business_invoice_item.rb +0 -17
  151. data/app/models/kaui/business_invoice_payment.rb +0 -25
  152. data/app/models/kaui/business_overdue_status.rb +0 -8
  153. data/app/models/kaui/business_snapshot.rb +0 -9
  154. data/app/models/kaui/business_subscription_transition.rb +0 -53
  155. data/app/models/kaui/business_tag.rb +0 -5
  156. data/app/models/kaui/charge.rb +0 -14
  157. data/app/models/kaui/entitlement.rb +0 -2
  158. data/app/models/kaui/event.rb +0 -13
  159. data/app/models/kaui/external_payment.rb +0 -15
  160. data/app/models/kaui/overdue_state.rb +0 -10
  161. data/app/models/kaui/payment_attempt.rb +0 -27
  162. data/app/models/kaui/product.rb +0 -24
  163. data/app/models/kaui/time_series_data.rb +0 -8
  164. data/app/views/kaui/analytics/account_snapshot.html.erb +0 -211
  165. data/app/views/kaui/analytics/index.html.erb +0 -17
  166. data/app/views/kaui/chargebacks/index.html.erb +0 -14
  167. data/app/views/kaui/chargebacks/show.html.erb +0 -19
  168. data/app/views/kaui/credits/index.html.erb +0 -14
  169. data/app/views/kaui/credits/show.html.erb +0 -19
  170. data/app/views/kaui/invoice_items/index.html.erb +0 -20
  171. data/app/views/kaui/invoice_items/show.html.erb +0 -31
  172. data/app/views/kaui/refunds/_refunds_table.html.erb +0 -31
  173. data/app/views/kaui/refunds/index.html.erb +0 -43
  174. data/app/views/kaui/refunds/show.html.erb +0 -33
  175. data/app/views/kaui/tag_definitions/edit.html.erb +0 -3
  176. data/test/dummy/test/fixtures/accounts.yml +0 -32
  177. data/test/dummy/test/fixtures/bill_cycle_days.yml +0 -3
  178. data/test/dummy/test/fixtures/bundles.yml +0 -4
  179. data/test/dummy/test/fixtures/invoice_items.yml +0 -13
  180. data/test/dummy/test/fixtures/invoices.yml +0 -11
  181. data/test/dummy/test/fixtures/overdue_states.yml +0 -8
  182. data/test/dummy/test/fixtures/payment_methods.yml +0 -5
  183. data/test/dummy/test/fixtures/plugin_info_properties.yml +0 -59
  184. data/test/dummy/test/fixtures/plugin_infos.yml +0 -2
  185. data/test/dummy/test/fixtures/refunds.yml +0 -9
  186. data/test/dummy/test/fixtures/tag_definitions.yml +0 -20
  187. data/test/functional/kaui/account_timeline_controller_test.rb +0 -9
  188. data/test/functional/kaui/extpayments_controller_test.rb +0 -9
  189. data/test/functional/kaui/home_controller_test.rb +0 -11
  190. data/test/integration/navigation_test.rb +0 -10
  191. data/test/kaui_test.rb +0 -7
  192. data/test/unit/helpers/kaui/account_timeline_helper_test.rb +0 -6
  193. data/test/unit/helpers/kaui/bundles_helper_test.rb +0 -6
  194. data/test/unit/helpers/kaui/chargebacks_helper_test.rb +0 -6
  195. data/test/unit/helpers/kaui/extpayments_helper_test.rb +0 -6
  196. data/test/unit/helpers/kaui/home_helper_test.rb +0 -6
  197. data/test/unit/helpers/kaui/invoices_helper_test.rb +0 -6
  198. data/test/unit/helpers/kaui/payments_helper_test.rb +0 -6
  199. data/test/unit/helpers/kaui/refunds_helper_test.rb +0 -6
  200. data/test/unit/kaui/overdue_state_test.rb +0 -18
  201. data/test/unit/kaui/refund_test.rb +0 -20
  202. data/test/unit/model_test.rb +0 -151
@@ -1,31 +1,31 @@
1
1
  class Kaui::AccountsController < Kaui::EngineController
2
+
2
3
  def index
3
- if params[:account_id].present?
4
- redirect_to kaui_engine.account_path(params[:account_id])
5
- end
6
4
  end
7
5
 
8
6
  def pagination
9
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
10
-
11
7
  search_key = params[:sSearch]
12
- if search_key.present?
13
- accounts = Kaui::KillbillHelper::search_accounts(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
14
- else
15
- accounts = Kaui::KillbillHelper::get_accounts(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
16
- end
17
- json[:iTotalDisplayRecords] = accounts.pagination_total_nb_records
18
- json[:iTotalRecords] = accounts.pagination_max_nb_records
8
+ offset = params[:iDisplayStart] || 0
9
+ limit = params[:iDisplayLength] || 10
10
+
11
+ accounts = Kaui::Account.list_or_search(search_key, offset, limit, options_for_klient)
12
+
13
+ json = {
14
+ :sEcho => params[:sEcho],
15
+ :iTotalRecords => accounts.pagination_max_nb_records,
16
+ :iTotalDisplayRecords => accounts.pagination_total_nb_records,
17
+ :aaData => []
18
+ }
19
19
 
20
20
  accounts.each do |account|
21
21
  json[:aaData] << [
22
- view_context.link_to(account.account_id, view_context.url_for(:action => :show, :id => account.account_id)),
23
- account.name,
24
- account.external_key,
25
- account.currency,
26
- account.city,
27
- account.country
28
- ]
22
+ view_context.link_to(view_context.truncate_uuid(account.account_id), view_context.url_for(:action => :show, :id => account.account_id)),
23
+ account.name,
24
+ account.external_key,
25
+ account.currency,
26
+ account.city,
27
+ account.country
28
+ ]
29
29
  end
30
30
 
31
31
  respond_to do |format|
@@ -33,163 +33,83 @@ class Kaui::AccountsController < Kaui::EngineController
33
33
  end
34
34
  end
35
35
 
36
- def show
37
- @key = params[:id]
38
- if @key.present?
39
- # Remove extra whitespaces
40
- @key.strip!
41
-
42
- begin
43
- @account = Kaui::KillbillHelper::get_account_by_key_with_balance_and_cba(@key, options_for_klient)
44
- rescue URI::InvalidURIError => e
45
- flash.now[:error] = "Error while retrieving the account for #{@key}: #{e.message}"
46
- render :action => :index and return
47
- rescue => e
48
- flash.now[:error] = "Error while retrieving the account for #{@key}: #{as_string(e)}"
49
- render :action => :index and return
50
- end
51
-
52
- if @account.present? and @account.is_a? Kaui::Account
53
- begin
54
- @tags = Kaui::KillbillHelper::get_tags_for_account(@account.account_id, false, "NONE", options_for_klient).sort { |tag_a, tag_b| tag_a.tag_definition_name.downcase <=> tag_b.tag_definition_name.downcase }
55
- @account_emails = Kaui::AccountEmail.where({ :account_id => @account.account_id }, options_for_klient)
56
- @overdue_state = Kaui::KillbillHelper::get_overdue_state_for_account(@account.account_id, options_for_klient)
57
- @payment_methods = Kaui::KillbillHelper::get_non_external_payment_methods(@account.account_id, options_for_klient)
58
- @bundles = Kaui::KillbillHelper::get_bundles_for_account(@account.account_id, options_for_klient)
59
-
60
- @subscriptions_by_bundle_id = {}
61
-
62
- @bundles.each do |bundle|
63
- @subscriptions_by_bundle_id[bundle.bundle_id.to_s] = (@subscriptions_by_bundle_id[bundle.bundle_id.to_s] || []) + bundle.subscriptions
64
- end
65
- rescue => e
66
- flash.now[:error] = "Error while retrieving account information for account: #{as_string(e)}"
67
- render :action => :index
68
- end
69
- else
70
- flash.now[:error] = "Account #{@account_id} not found: #{@account}"
71
- render :action => :index
72
- end
73
- else
74
- flash.now[:error] = "No id given"
75
- end
36
+ def new
37
+ @account = Kaui::Account.new
76
38
  end
77
39
 
78
- def payment_methods
79
- @account_id = params[:id]
80
- if @account_id.present?
81
- begin
82
- @payment_methods = Kaui::KillbillHelper::get_non_external_payment_methods(@account_id, options_for_klient)
83
- rescue => e
84
- flash.now[:error] = "Error while getting payment methods: #{as_string(e)}"
85
- end
86
- unless @payment_methods.is_a?(Array)
87
- flash[:notice] = "No payment methods for account_id '#{@account_id}'"
88
- redirect_to :action => :index
89
- return
90
- end
91
- else
92
- flash.now[:notice] = "No account_id given"
40
+ def create
41
+ @account = Kaui::Account.new(params[:account].delete_if { |key, value| value.blank? })
42
+
43
+ # Transform "1" into boolean
44
+ @account.is_migrated = @account.is_migrated == '1'
45
+ @account.is_notified_for_invoices = @account.is_notified_for_invoices == '1'
46
+
47
+ begin
48
+ @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
49
+ redirect_to account_path(@account.account_id), :notice => 'Account was successfully created'
50
+ rescue => e
51
+ flash.now[:error] = "Error while creating account: #{as_string(e)}"
52
+ render :action => :new
93
53
  end
94
54
  end
95
55
 
96
- def add_payment_method
97
- account_id = params[:id]
56
+ def show
98
57
  begin
99
- @account = Kaui::KillbillHelper::get_account(account_id, false, false, options_for_klient)
58
+ @account = Kaui::Account::find_by_id_or_key(params[:id], true, true, options_for_klient)
59
+ @overdue_state = @account.overdue(options_for_klient)
60
+ @bundles = @account.bundles(options_for_klient)
61
+ @tags = @account.tags(false, 'NONE', options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b }
62
+
63
+ @account_emails = Kaui::AccountEmail.find_all_sorted_by_account_id(@account.account_id, 'NONE', options_for_klient)
64
+ @payment_methods = Kaui::PaymentMethod.find_non_external_by_account_id(@account.account_id, true, options_for_klient)
100
65
  rescue => e
101
- flash.now[:error] = "Error while adding payment methods: #{as_string(e)}"
66
+ flash.now[:error] = "Error while retrieving account information: #{as_string(e)}"
67
+ render :action => :index and return
102
68
  end
103
- if @account.nil?
104
- flash[:error] = "Account not found for id #{account_id}"
105
- redirect_to :back
106
- else
107
- render "kaui/payment_methods/new"
69
+
70
+ @subscriptions_by_bundle_id = {}
71
+ @bundles.each do |bundle|
72
+ @subscriptions_by_bundle_id[bundle.bundle_id.to_s] = (@subscriptions_by_bundle_id[bundle.bundle_id.to_s] || []) + bundle.subscriptions
108
73
  end
109
74
  end
110
75
 
111
- def do_add_payment_method
112
- account_id = params[:id]
113
- # Needed in the failure case scenario
114
- @account = Kaui::KillbillHelper::get_account(account_id, false, false, options_for_klient)
115
-
116
- # Implementation example using standard credit card fields
117
- @card_type = params[:card_type]
118
- @card_holder_name = params[:card_holder_name]
119
- @expiration_year = params[:expiration_year]
120
- @expiration_month = params[:expiration_month]
121
- @credit_card_number = params[:credit_card_number]
122
- @address1 = params[:address1]
123
- @address2 = params[:address2]
124
- @city = params[:city]
125
- @country = params[:country]
126
- @postal_code = params[:postal_code]
127
- @state = params[:state]
128
- @is_default = params[:is_default]
129
- @reason = params[:reason]
130
- @comment = params[:comment]
131
-
132
- payment_method = KillBillClient::Model::PaymentMethod.new
133
- payment_method.account_id = account_id
134
- payment_method.plugin_name = params[:plugin_name] || Kaui.creditcard_plugin_name.call
135
-
136
- payment_method.plugin_info = {
137
- 'type' => 'CreditCard',
138
- 'ccType' => @card_type,
139
- 'ccName' => @card_holder_name,
140
- 'ccExpirationMonth' => @expiration_month,
141
- 'ccExpirationYear' => @expiration_year,
142
- 'ccLast4' => @credit_card_number[-4,4],
143
- 'address1' => @address1,
144
- 'address2' => @address2,
145
- 'city' => @city,
146
- 'country' => @country,
147
- 'zip' => @postal_code,
148
- 'state' => @state
149
- }
76
+ def set_default_payment_method
77
+ account_id = params[:id]
78
+ payment_method_id = params[:payment_method_id]
150
79
 
151
80
  begin
152
- Kaui::KillbillHelper::add_payment_method(@is_default == 1, payment_method, current_user, @reason, @comment, options_for_klient)
153
- flash[:notice] = 'Payment method created'
154
- redirect_to kaui_engine.account_timeline_path(account_id)
81
+ Kaui::PaymentMethod.set_default(payment_method_id, account_id, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
82
+ flash[:notice] = "Successfully set #{payment_method_id} as default"
155
83
  rescue => e
156
- flash.now[:error] = "Error while adding payment method: #{as_string(e)}"
157
- render "kaui/payment_methods/new"
84
+ flash[:error] = "Error while setting payment method #{payment_method_id} as default: #{as_string(e)}"
158
85
  end
159
- end
160
86
 
161
- def set_default_payment_method
162
- @account_id = params[:id]
163
- @payment_method_id = params[:payment_method_id]
164
- if @account_id.present? && @payment_method_id.present?
165
- begin
166
- @payment_methods = Kaui::KillbillHelper::set_payment_method_as_default(@account_id, @payment_method_id, current_user, params[:reason], params[:comment], options_for_klient)
167
- rescue => e
168
- flash[:error] = "Error while setting payment method as default #{@payment_method_id}: #{as_string(e)}"
169
- end
170
- else
171
- flash[:notice] = 'No account_id or payment_method_id given'
172
- end
173
- redirect_to :back
87
+ redirect_to account_path(account_id)
174
88
  end
175
89
 
176
90
  def toggle_email_notifications
91
+ account = Kaui::Account.new(:account_id => params[:id], :is_notified_for_invoices => params[:is_notified])
92
+
177
93
  begin
178
- @account = Kaui::KillbillHelper::update_email_notifications(params[:id], params[:is_notified], current_user, params[:reason], params[:comment], options_for_klient)
179
- flash[:notice] = "Email preferences updated"
94
+ account.update_email_notifications(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
95
+ flash[:notice] = 'Email preferences updated'
180
96
  rescue => e
181
- flash[:error] = "Error while switching email notifications #{invoice_id}: #{as_string(e)}"
97
+ flash[:error] = "Error while setting email notifications: #{as_string(e)}"
182
98
  end
183
- redirect_to :back
99
+
100
+ redirect_to account_path(account.account_id)
184
101
  end
185
102
 
186
103
  def pay_all_invoices
104
+ payment = Kaui::InvoicePayment.new(:account_id => params[:id])
105
+
187
106
  begin
188
- @account = Kaui::KillbillHelper::pay_all_invoices(params[:id], false, current_user, params[:reason], params[:comment], options_for_klient)
189
- flash[:notice] = "Successfully triggered a payment for all unpaid invoices"
107
+ payment.bulk_create(params[:is_external_payment], current_user.kb_username, params[:reason], params[:comment], options_for_klient)
108
+ flash[:notice] = 'Successfully triggered a payment for all unpaid invoices'
190
109
  rescue => e
191
110
  flash[:error] = "Error while triggering payments: #{as_string(e)}"
192
111
  end
193
- redirect_to :back
112
+
113
+ redirect_to account_path(payment.account_id)
194
114
  end
195
115
  end
@@ -1,39 +1,50 @@
1
1
  class Kaui::BundleTagsController < Kaui::EngineController
2
2
 
3
3
  def show
4
- bundle_id = params[:id]
5
- if bundle_id.present?
6
- begin
7
- tags = Kaui::KillbillHelper::get_tags_for_bundle(bundle_id, options_for_klient)
8
- rescue => e
9
- flash.now[:error] = "Error while retrieving tags information: #{as_string(e)}"
10
- end
11
- else
12
- flash.now[:error] = "No account id given"
4
+ bundle_id_or_key = params[:bundle_id]
5
+ account_id = params[:account_id]
6
+
7
+ begin
8
+ @bundle = Kaui::Bundle::find_by_id_or_key(bundle_id_or_key, account_id, options_for_klient)
9
+ @account = Kaui::Account::find_by_id_or_key(@bundle.account_id, false, false, options_for_klient)
10
+ @tags = @bundle.tags(true, 'FULL', options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b }
11
+ rescue => e
12
+ flash[:error] = "Error while getting tags: #{as_string(e)}"
13
+ redirect_to :back
13
14
  end
14
15
  end
15
16
 
16
17
  def edit
17
- @bundle_id = params[:bundle_id]
18
- begin
19
- @available_tags = Kaui::KillbillHelper::get_tag_definitions(options_for_klient).sort {|tag_a, tag_b| tag_a.name.downcase <=> tag_b.name.downcase }
18
+ bundle_id_or_key = params[:bundle_id]
19
+ account_id = params[:account_id]
20
20
 
21
- @bundle = Kaui::KillbillHelper::get_bundle(@bundle_id, options_for_klient)
22
- @tags = Kaui::KillbillHelper::get_tags_for_bundle(@bundle_id, options_for_klient)
21
+ begin
22
+ @bundle = Kaui::Bundle::find_by_id_or_key(bundle_id_or_key, account_id, options_for_klient)
23
+ @tag_names = (@bundle.tags(false, 'NONE', options_for_klient).map { |tag| tag.tag_definition_name }).sort
24
+ @available_tags = Kaui::TagDefinition.all_for_bundle(options_for_klient)
23
25
  rescue => e
24
- flash.now[:error] = "Error while retrieving tags information: #{as_string(e)}"
26
+ flash[:error] = "Error while editing tags: #{as_string(e)}"
27
+ redirect_to kaui_engine.bundle_tags_path(:bundle_id => bundle_id_or_key)
25
28
  end
26
29
  end
27
30
 
28
31
  def update
29
- begin
30
- bundle = Kaui::KillbillHelper::get_bundle(params[:bundle_id], options_for_klient)
31
- tags = params[:tags]
32
+ bundle_id = params[:bundle_id]
33
+
34
+ tags = []
35
+ params.each do |tag, tag_name|
36
+ tag_info = tag.split('_')
37
+ next if tag_info.size != 2 or tag_info[0] != 'tag'
38
+ tags << tag_info[1]
39
+ end
32
40
 
33
- Kaui::KillbillHelper::set_tags_for_bundle(bundle.bundle_id, tags, current_user, params[:reason], params[:comment], options_for_klient)
34
- redirect_to Kaui.bundle_home_path.call(bundle.bundle_id)
41
+ begin
42
+ bundle = Kaui::Bundle.new(:bundle_id => bundle_id)
43
+ bundle.set_tags(tags, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
44
+ redirect_to kaui_engine.bundle_tags_path(:bundle_id => bundle_id), :notice => 'Bundle tags successfully set'
35
45
  rescue => e
36
- flash.now[:error] = "Error while updating tags: #{as_string(e)}"
46
+ flash[:error] = "Error while updating tags: #{as_string(e)}"
47
+ redirect_to kaui_engine.bundle_tags_path(:bundle_id => bundle_id)
37
48
  end
38
49
  end
39
50
  end
@@ -1,30 +1,29 @@
1
1
  class Kaui::BundlesController < Kaui::EngineController
2
2
 
3
3
  def index
4
- if params[:bundle_id].present?
5
- redirect_to kaui_engine.bundle_path(params[:bundle_id])
6
- end
7
4
  end
8
5
 
9
6
  def pagination
10
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
11
-
12
7
  search_key = params[:sSearch]
13
- if search_key.present?
14
- bundles = Kaui::KillbillHelper::search_bundles(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
15
- else
16
- bundles = Kaui::KillbillHelper::get_bundles(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
17
- end
18
- json[:iTotalDisplayRecords] = bundles.pagination_total_nb_records
19
- json[:iTotalRecords] = bundles.pagination_max_nb_records
8
+ offset = params[:iDisplayStart] || 0
9
+ limit = params[:iDisplayLength] || 10
10
+
11
+ bundles = Kaui::Bundle.list_or_search(search_key, offset, limit, options_for_klient)
12
+
13
+ json = {
14
+ :sEcho => params[:sEcho],
15
+ :iTotalRecords => bundles.pagination_max_nb_records,
16
+ :iTotalDisplayRecords => bundles.pagination_total_nb_records,
17
+ :aaData => []
18
+ }
20
19
 
21
20
  bundles.each do |bundle|
22
21
  json[:aaData] << [
23
- view_context.link_to(bundle.bundle_id, view_context.url_for(:action => :show, :id => bundle.bundle_id)),
24
- view_context.link_to(bundle.account_id, view_context.url_for(:controller => :accounts, :action => :show, :id => bundle.account_id)),
25
- bundle.external_key,
26
- bundle.subscriptions.nil? ? '' : (bundle.subscriptions.map { |s| s.product_name }).join(', ')
27
- ]
22
+ view_context.link_to(view_context.truncate_uuid(bundle.bundle_id), view_context.url_for(:action => :show, :id => bundle.bundle_id)),
23
+ view_context.link_to(view_context.truncate_uuid(bundle.account_id), view_context.url_for(:controller => :accounts, :action => :show, :id => bundle.account_id)),
24
+ bundle.external_key,
25
+ bundle.subscriptions.nil? ? '' : (bundle.subscriptions.map { |s| s.product_name }).join(', ')
26
+ ]
28
27
  end
29
28
 
30
29
  respond_to do |format|
@@ -33,75 +32,51 @@ class Kaui::BundlesController < Kaui::EngineController
33
32
  end
34
33
 
35
34
  def show
36
- key = params[:id]
37
- if key.present?
38
- begin
39
- @bundle = Kaui::KillbillHelper::get_bundle_by_key(key, params[:account_id], options_for_klient)
40
-
41
- if @bundle.present?
42
- @account = Kaui::KillbillHelper::get_account(@bundle.account_id, false, false, options_for_klient)
43
- @subscriptions = @bundle.subscriptions
44
- else
45
- flash.now[:error] = "Bundle #{key} not found"
46
- render :action => :index
47
- end
48
- rescue => e
49
- flash.now[:error] = "Error while retrieving bundle information for #{key}: #{as_string(e)}"
50
- render :action => :index
51
- end
52
- else
53
- flash.now[:error] = "No id given"
35
+ begin
36
+ @bundle = Kaui::Bundle::find_by_id_or_key(params[:id], nil, options_for_klient)
37
+ @account = Kaui::Account::find_by_id(@bundle.account_id, false, false, options_for_klient)
38
+ @tags = @bundle.tags(false, 'NONE', options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b }
39
+ rescue => e
40
+ flash.now[:error] = "Error while retrieving bundle information: #{as_string(e)}"
54
41
  render :action => :index
55
42
  end
56
43
  end
57
44
 
58
45
  def transfer
59
- bundle_id = params[:id]
60
46
  begin
61
- @bundle = Kaui::KillbillHelper::get_bundle(bundle_id, options_for_klient)
62
- @account = Kaui::KillbillHelper::get_account_by_bundle_id(bundle_id, options_for_klient)
47
+ @bundle = Kaui::Bundle::find_by_id_or_key(params[:id], nil, options_for_klient)
48
+ @account = Kaui::Account::find_by_id(@bundle.account_id, false, false, options_for_klient)
63
49
  rescue => e
64
50
  flash.now[:error] = "Error while preparing to transfer bundle: #{as_string(e)}"
65
- end
66
- if @account.nil?
67
- flash.now[:error] = "Account not found for bundle id #{bundle_id}"
51
+ render :action => :index
68
52
  end
69
53
  end
70
54
 
71
55
  def do_transfer
72
- bundle_id = params[:id]
73
56
  key = params[:new_account_key]
74
- if key.present?
75
- begin
76
- result = Kaui::KillbillHelper.get_account_by_key(key, false, false, options_for_klient)
77
- rescue => e
78
- flash.now[:error] = "Error while retrieving account for #{key}: #{as_string(e)}"
79
- render :action => :index
80
- return
81
- end
82
- if bundle_id.present? && result.is_a?(Kaui::Account)
83
- @new_account = result
84
- begin
85
- Kaui::KillbillHelper::transfer_bundle(bundle_id, @new_account.account_id, false, true, current_user, params[:reason], params[:comment], options_for_klient)
86
- flash[:notice] = "Bundle transfered successfully"
87
- rescue => e
88
- flash[:error] = "Error transfering bundle #{as_string(e)}"
89
- end
90
- redirect_to Kaui.account_home_path.call(@new_account.external_key)
91
- return
92
- else
93
- flash.now[:error] = "Could not retrieve account #{result}"
94
- end
95
- else
96
- flash.now[:error] = "No account key given"
57
+ unless key.present?
58
+ flash.now[:error] = 'No new account key given'
59
+ render :action => :index and return
60
+ end
61
+
62
+ begin
63
+ # Retrieve the new account to get the account id
64
+ new_account = Kaui::Account::find_by_id_or_key(params[:new_account_key], false, false, options_for_klient)
65
+ rescue => e
66
+ flash.now[:error] = "Error while retrieving new account: #{as_string(e)}"
67
+ render :action => :index and return
97
68
  end
98
69
 
99
70
  begin
100
- @bundle = Kaui::KillbillHelper::get_bundle(bundle_id, options_for_klient)
101
- @account = Kaui::KillbillHelper::get_account_by_bundle_id(bundle_id, options_for_klient)
71
+ billing_policy = params[:billing_policy]
72
+
73
+ bundle = Kaui::Bundle::new(:bundle_id => params[:id], :account_id => new_account.account_id)
74
+ bundle.transfer(nil, billing_policy, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
75
+
76
+ redirect_to account_path(new_account.account_id), :notice => 'Bundle was successfully transferred'
102
77
  rescue => e
103
- flash.now[:error] = "Error while redirecting: #{as_string(e)}"
104
- render :transfer
78
+ flash.now[:error] = "Error while transferring bundle: #{as_string(e)}"
79
+ render :action => :index
105
80
  end
106
81
  end
107
82
  end
@@ -1,79 +1,49 @@
1
1
  class Kaui::ChargebacksController < Kaui::EngineController
2
2
 
3
- def show
4
- @payment_id = params[:id]
5
- if @payment_id.present?
6
- begin
7
- data = Kaui::KillbillHelper::get_chargebacks_for_payment(@payment_id, options_for_klient)
8
- rescue => e
9
- flash.now[:error] = "Error while getting chargeback information: #{as_string(e)}"
10
- end
11
- if data.present?
12
- @chargeback = Kaui::Chargeback.new(data)
13
- else
14
- Rails.logger.warn("Did not get back chargebacks #{response_body}")
15
- end
16
- else
17
- flash.now[:notice] = "No id given"
18
- end
19
- end
20
-
21
3
  def new
22
- @payment_id = params[:payment_id]
23
- @account_id = params[:account_id]
24
- @invoice_id = params[:invoice_id]
25
-
26
4
  begin
27
- @account = Kaui::KillbillHelper::get_account(@account_id, false, false, options_for_klient)
28
- @payment = Kaui::KillbillHelper::get_payment(@payment_id, options_for_klient)
29
- @invoice = Kaui::KillbillHelper::get_invoice(@invoice_id, true, "NONE", options_for_klient)
5
+ payment = Kaui::Payment::find_by_id(params[:payment_id], options_for_klient)
30
6
  rescue => e
31
- flash[:error] = "Error while starting a new chargeback: #{as_string(e)}"
32
- redirect_to kaui_engine.account_timeline_path(:id => params[:account_id])
7
+ flash[:error] = "Error while retrieving payment: #{as_string(e)}"
8
+ redirect_to :back
33
9
  end
34
10
 
35
- # The payment method may have been deleted
36
- @payment_method = Kaui::KillbillHelper::get_payment_method(@payment.payment_method_id, options_for_klient) rescue nil
37
-
38
- @chargeback = Kaui::Chargeback.new("paymentId" => @payment_id,
39
- "chargebackAmount" => @payment.amount)
11
+ @chargeback = Kaui::Chargeback.new(:payment_id => payment.payment_id,
12
+ :amount => payment.paid_amount_to_money.to_f,
13
+ :currency => payment.currency)
40
14
  end
41
15
 
42
16
  def create
43
- chargeback = Kaui::Chargeback.new(params[:chargeback])
44
- should_cancel_subs = (params[:cancel_all_subs] == "1")
45
-
46
- if chargeback.present?
47
- begin
48
- Kaui::KillbillHelper::create_chargeback(chargeback, current_user, params[:reason], params[:comment], options_for_klient)
49
- rescue => e
50
- flash[:error] = "Error while creating a new chargeback: #{as_string(e)}"
51
- redirect_to kaui_engine.account_timeline_path(:id => params[:account_id]) and return
52
- end
17
+ @chargeback = Kaui::Chargeback.new(params[:chargeback])
18
+ should_cancel_subs = (params[:cancel_all_subs] == '1')
53
19
 
54
- # Cancel all subscriptions on the account, if required
55
- if should_cancel_subs
56
- begin
57
- bundles = Kaui::KillbillHelper::get_bundles_for_account(params[:account_id], options_for_klient)
58
- bundles.each do |bundle|
59
- bundle.subscriptions.each do |subscription|
60
- # Already cancelled?
61
- next unless subscription.billing_end_date.blank?
20
+ begin
21
+ payment = @chargeback.chargeback(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
22
+ rescue => e
23
+ flash.now[:error] = "Error while creating a new chargeback: #{as_string(e)}"
24
+ render :action => :new and return
25
+ end
62
26
 
63
- # Cancel the entitlement immediately but use the default billing policy
64
- Kaui::KillbillHelper::delete_subscription(subscription.subscription_id, current_user, params[:reason], params[:comment], 'IMMEDIATE', nil, options_for_klient)
65
- end
27
+ # Cancel all subscriptions on the account, if required
28
+ if should_cancel_subs
29
+ begin
30
+ account = Kaui::Account.find_by_id(payment.account_id, false, false, options_for_klient)
31
+ account.bundles(options_for_klient).each do |bundle|
32
+ bundle.subscriptions.each do |subscription|
33
+ # Already cancelled?
34
+ next unless subscription.billing_end_date.blank?
35
+
36
+ # Cancel the entitlement immediately but use the default billing policy
37
+ entitlement = Kaui::Subscription.new(:subscription_id => subscription.subscription_id)
38
+ entitlement.cancel_entitlement_immediately(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
66
39
  end
67
- rescue => e
68
- flash[:error] = "Error while cancelling subscriptions: #{as_string(e)}"
69
- redirect_to kaui_engine.account_timeline_path(:id => params[:account_id]) and return
70
40
  end
41
+ rescue => e
42
+ flash[:error] = "Error while cancelling subscriptions: #{as_string(e)}"
43
+ render :action => :new and return
71
44
  end
72
-
73
- flash[:notice] = "Chargeback created"
74
- else
75
- flash[:error] = "No chargeback to process"
76
45
  end
77
- redirect_to kaui_engine.account_timeline_path(:id => params[:account_id])
46
+
47
+ redirect_to kaui_engine.account_timeline_path(:id => payment.account_id), :notice => 'Chargeback created'
78
48
  end
79
49
  end
@@ -1,34 +1,32 @@
1
1
  class Kaui::ChargesController < Kaui::EngineController
2
2
 
3
3
  def new
4
- @account_id = params[:account_id]
5
- @invoice_id = params[:invoice_id]
6
- begin
7
- @account = Kaui::KillbillHelper::get_account(@account_id, false, false, options_for_klient)
4
+ invoice_id = params[:invoice_id]
5
+ account_id = params[:account_id]
6
+ currency = params[:currency] || 'USD'
8
7
 
9
- if @invoice_id.present?
10
- @invoice = Kaui::KillbillHelper::get_invoice(@invoice_id, true, "NONE", options_for_klient)
11
- @charge = Kaui::Charge.new("accountId" => @account_id, "invoiceId" => @invoice_id)
12
- else
13
- @charge = Kaui::Charge.new("accountId" => @account_id)
8
+ if invoice_id.present?
9
+ begin
10
+ @invoice = Kaui::Invoice.find_by_id_or_number(invoice_id, true, 'NONE', options_for_klient)
11
+ account_id = @invoice.account_id
12
+ currency = @invoice.currency
13
+ rescue => e
14
+ flash.now[:error] = "Unable to retrieve invoice: #{as_string(e)}"
14
15
  end
15
- rescue => e
16
- flash.now[:error] = "Error while creating a charge: #{as_string(e)}"
17
16
  end
17
+
18
+ @charge = Kaui::InvoiceItem.new(:account_id => account_id, :invoice_id => invoice_id, :currency => currency)
18
19
  end
19
20
 
20
21
  def create
21
- charge = Kaui::Charge.new(params[:charge])
22
+ @charge = Kaui::InvoiceItem.new(params[:invoice_item].delete_if { |key, value| value.blank? })
22
23
 
23
- if charge.present?
24
- begin
25
- Kaui::KillbillHelper::create_charge(charge, params[:requested_date], current_user, params[:reason], params[:comment], options_for_klient)
26
- flash[:notice] = "Charge created"
27
- redirect_to kaui_engine.account_timeline_path(:id => charge.account_id)
28
- rescue => e
29
- flash.now[:error] = "Error while creating a charge: #{as_string(e)}"
30
- end
24
+ begin
25
+ @charge = @charge.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
26
+ redirect_to kaui_engine.invoice_path(:id => @charge.invoice_id), :notice => 'Charge was successfully created'
27
+ rescue => e
28
+ flash.now[:error] = "Error while creating a charge: #{as_string(e)}"
29
+ render :action => :new
31
30
  end
32
31
  end
33
-
34
32
  end