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,51 +1,37 @@
1
1
  class Kaui::CreditsController < Kaui::EngineController
2
- def show
3
- @account_id = params[:account_id]
4
- @invoice_id = params[:invoice_id]
5
2
 
6
- if params.has_key?(:account_id)
3
+ def new
4
+ invoice_id = params[:invoice_id]
5
+ account_id = params[:account_id]
6
+ amount = params[:amount]
7
+ currency = params[:currency] || 'USD'
8
+
9
+ if invoice_id.present?
7
10
  begin
8
- # invoice id can be nil for account level credit
9
- data = Kaui::KillbillHelper::get_credits(@account_id, @invoice_id, options_for_klient)
11
+ @invoice = Kaui::Invoice.find_by_id_or_number(invoice_id, true, 'NONE', options_for_klient)
12
+ account_id = @invoice.account_id
13
+ amount ||= @invoice.balance
14
+ currency = @invoice.currency
10
15
  rescue => e
11
- flash.now[:error] = "Error getting credit information: #{as_string(e)}"
12
- end
13
- if data.present?
14
- @credit = Kaui::Credit.new(data)
15
- else
16
- Rails.logger.warn("Did not get back external payments #{response_body}")
16
+ flash.now[:error] = "Unable to retrieve invoice: #{as_string(e)}"
17
17
  end
18
- else
19
- flash.now[:notice] = "No id given"
20
- end
21
- end
22
-
23
- def new
24
- @account_id = params[:account_id]
25
- @invoice_id = params[:invoice_id]
26
-
27
- begin
28
- @account = Kaui::KillbillHelper::get_account(@account_id, false, false, options_for_klient)
29
- @invoice = Kaui::KillbillHelper::get_invoice(@invoice_id, true, "NONE", options_for_klient) unless @invoice_id.nil?
30
- rescue => e
31
- flash.now[:error] = "Error while starting to create credit: #{as_string(e)}"
32
18
  end
33
19
 
34
- credit_amount = @invoice.balance unless @invoice.nil?
35
-
36
- @credit = Kaui::Credit.new("accountId" => @account_id, "invoiceId" => @invoice_id,
37
- "creditAmount" => credit_amount, "effectiveDate" => Date.parse(Time.now.to_s).to_s)
20
+ # TODO Specifying a custom currency is not supported yet
21
+ @credit = Kaui::Credit.new(:invoice_id => invoice_id,
22
+ :account_id => account_id,
23
+ :credit_amount => amount)
38
24
  end
39
25
 
40
26
  def create
41
- credit = params[:credit]
27
+ @credit = Kaui::Credit.new(params[:credit].delete_if { |key, value| value.blank? })
28
+
42
29
  begin
43
- Kaui::KillbillHelper::create_credit(credit, current_user, params[:reason], params[:comment], options_for_klient)
44
- account = Kaui::KillbillHelper::get_account(credit['account_id'], false, false, options_for_klient)
45
- flash[:notice] = "Credit created"
30
+ @credit = @credit.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
31
+ redirect_to kaui_engine.invoice_path(:id => @credit.invoice_id), :notice => 'Credit was successfully created'
46
32
  rescue => e
47
- flash[:error] = "Error while starting to create credit: #{as_string(e)}"
33
+ flash.now[:error] = "Error while creating a credit: #{as_string(e)}"
34
+ render :action => :new
48
35
  end
49
- redirect_to Kaui.account_home_path.call(credit['account_id'])
50
36
  end
51
37
  end
@@ -4,26 +4,28 @@ class Kaui::CustomFieldsController < Kaui::EngineController
4
4
  end
5
5
 
6
6
  def pagination
7
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
8
-
9
7
  search_key = params[:sSearch]
10
- if search_key.present?
11
- custom_fields = Kaui::KillbillHelper::search_custom_fields(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
12
- else
13
- custom_fields = Kaui::KillbillHelper::get_custom_fields(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
14
- end
15
- json[:iTotalDisplayRecords] = custom_fields.pagination_total_nb_records
16
- json[:iTotalRecords] = custom_fields.pagination_max_nb_records
8
+ offset = params[:iDisplayStart] || 0
9
+ limit = params[:iDisplayLength] || 10
10
+
11
+ custom_fields = Kaui::CustomField.list_or_search(search_key, offset, limit, options_for_klient)
12
+
13
+ json = {
14
+ :sEcho => params[:sEcho],
15
+ :iTotalRecords => custom_fields.pagination_max_nb_records,
16
+ :iTotalDisplayRecords => custom_fields.pagination_total_nb_records,
17
+ :aaData => []
18
+ }
17
19
 
18
20
  custom_fields.each do |custom_field|
19
21
  json[:aaData] << [
20
- custom_field.name,
21
- custom_field.value
22
- ]
22
+ custom_field.name,
23
+ custom_field.value
24
+ ]
23
25
  end
24
26
 
25
27
  respond_to do |format|
26
28
  format.json { render :json => json }
27
29
  end
28
30
  end
29
- end
31
+ end
@@ -6,18 +6,18 @@ class Kaui::EngineController < ApplicationController
6
6
  # Common options for the Kill Bill client
7
7
  def options_for_klient(options = {})
8
8
  {
9
- # TODO Kaui doesn't support multi-tenancy yet
10
- :api_key => KillBillClient.api_key,
11
- :api_secret => KillBillClient.api_secret,
12
- :username => current_user.kb_username || KillBillClient.username,
13
- :password => current_user.password || KillBillClient.password,
14
- :session_id => current_user.kb_session_id
9
+ # TODO Kaui doesn't support multi-tenancy yet
10
+ :api_key => KillBillClient.api_key,
11
+ :api_secret => KillBillClient.api_secret,
12
+ :username => current_user.kb_username || KillBillClient.username,
13
+ :password => current_user.password || KillBillClient.password,
14
+ :session_id => current_user.kb_session_id
15
15
  }.merge(options)
16
16
  end
17
17
 
18
18
  # Used for auditing purposes
19
19
  def current_user
20
- super rescue Kaui.config[:default_current_user]
20
+ super
21
21
  end
22
22
 
23
23
  def current_ability
@@ -28,9 +28,7 @@ class Kaui::EngineController < ApplicationController
28
28
  protected
29
29
 
30
30
  def as_string(e)
31
- if e.is_a?(RestClient::Exception)
32
- "#{e.message}, server response: #{as_string_from_response(e.response)}".split(/\n/).take(5).join("\n")
33
- elsif e.is_a?(KillBillClient::API::ResponseError)
31
+ if e.is_a?(KillBillClient::API::ResponseError)
34
32
  "Error #{e.response.code}: #{as_string_from_response(e.response.body)}"
35
33
  else
36
34
  e.message
@@ -1,59 +1,42 @@
1
1
  class Kaui::InvoiceItemsController < Kaui::EngineController
2
- def index
3
- if params[:invoice_item_id].present? and params[:invoice_id].present?
4
- redirect_to kaui_engine.invoice_item_path(params[:invoice_item_id], :invoice_id => params[:invoice_id])
5
- end
6
- end
7
-
8
- def show
9
- find_invoice_item
10
- end
11
2
 
12
3
  def edit
13
- find_invoice_item
14
- end
4
+ invoice_item_id = params[:id]
5
+ invoice_id = params[:invoice_id]
15
6
 
16
- def update
17
- @invoice_item = Kaui::KillbillHelper.new_invoice_item(params[:kill_bill_client_model_invoice_item])
18
7
  begin
19
- Kaui::KillbillHelper.adjust_invoice(@invoice_item, current_user, params[:reason], params[:comment], options_for_klient)
20
- flash[:notice] = "Adjustment item created"
21
- redirect_to kaui_engine.invoice_path(@invoice_item.invoice_id)
8
+ invoice = Kaui::Invoice.find_by_id_or_number(invoice_id, true, 'NONE', options_for_klient)
22
9
  rescue => e
23
- flash.now[:error] = "Error while updating the invoice item: #{as_string(e)}"
24
- render :action => "edit"
10
+ flash[:error] = "Error while getting information for invoice #{invoice_id}: #{as_string(e)}"
11
+ redirect_to :back
25
12
  end
13
+
14
+ @invoice_item = invoice.items.find { |ii| ii.invoice_item_id == invoice_item_id }
26
15
  end
27
16
 
28
- def destroy
17
+ def update
18
+ @invoice_item = Kaui::InvoiceItem.new(params[:invoice_item])
19
+
29
20
  begin
30
- Kaui::KillbillHelper.delete_cba(params[:account_id], params[:invoice_id], params[:id], current_user, params[:reason], params[:comment], options_for_klient)
31
- flash[:notice] = "CBA deleted"
32
- redirect_to kaui_engine.invoice_path(params[:invoice_id])
21
+ invoice = @invoice_item.update(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
22
+ redirect_to kaui_engine.invoice_path(invoice.invoice_id), :notice => 'Adjustment item was successfully created'
33
23
  rescue => e
34
- flash[:error] = "Error while deleting the CBA: #{as_string(e)}"
35
- redirect_to kaui_engine.invoice_path(params[:invoice_id])
24
+ flash.now[:error] = "Error while adjusting invoice item: #{as_string(e)}"
25
+ render :action => :edit
36
26
  end
37
27
  end
38
28
 
39
- private
29
+ def destroy
30
+ @invoice_item = Kaui::InvoiceItem.new(:invoice_item_id => params[:id],
31
+ :invoice_id => params[:invoice_id],
32
+ :account_id => params[:account_id])
40
33
 
41
- def find_invoice_item
42
- invoice_item_id = params[:id]
43
- invoice_id = params[:invoice_id]
44
- if invoice_item_id.present? and invoice_id.present?
45
- begin
46
- @invoice_item = Kaui::KillbillHelper.get_invoice_item invoice_id, invoice_item_id, options_for_klient
47
- rescue => e
48
- flash[:error] = "Error while trying to find the invoice item: #{as_string(e)}"
49
- end
50
- unless @invoice_item.present?
51
- flash[:error] = "Invoice for id #{invoice_id} and invoice item id #{invoice_item_id} not found"
52
- render :action => :index
53
- end
54
- else
55
- flash[:error] = "Both invoice item and invoice ids should be specified"
56
- render :action => :index
34
+ begin
35
+ @invoice_item.delete(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
36
+ redirect_to kaui_engine.invoice_path(@invoice_item.invoice_id), :notice => 'CBA item was successfully deleted'
37
+ rescue => e
38
+ flash.now[:error] = "Error while deleting CBA item: #{as_string(e)}"
39
+ render :action => :edit
57
40
  end
58
41
  end
59
42
  end
@@ -1,4 +1,5 @@
1
1
  class Kaui::InvoicesController < Kaui::EngineController
2
+
2
3
  def index
3
4
  if params[:invoice_id].present?
4
5
  redirect_to kaui_engine.invoice_path(params[:invoice_id])
@@ -6,25 +7,25 @@ class Kaui::InvoicesController < Kaui::EngineController
6
7
  end
7
8
 
8
9
  def pagination
9
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
10
+ json = {:sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => []}
10
11
 
11
12
  search_key = params[:sSearch]
12
13
  if search_key.present?
13
- invoices = Kaui::KillbillHelper::search_invoices(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
14
+ invoices = Kaui::Invoice.find_in_batches_by_search_key(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
14
15
  else
15
- invoices = Kaui::KillbillHelper::get_invoices(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
16
+ invoices = Kaui::Invoice.find_in_batches(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
16
17
  end
17
18
  json[:iTotalDisplayRecords] = invoices.pagination_total_nb_records
18
- json[:iTotalRecords] = invoices.pagination_max_nb_records
19
+ json[:iTotalRecords] = invoices.pagination_max_nb_records
19
20
 
20
21
  invoices.each do |invoice|
21
22
  json[:aaData] << [
22
- view_context.link_to(invoice.invoice_id, view_context.url_for(:controller => :invoices, :action => :show, :id => invoice.invoice_id)),
23
- invoice.invoice_number,
24
- view_context.format_date(invoice.invoice_date),
25
- view_context.humanized_money_with_symbol(Kaui::Base.to_money(invoice.amount, invoice.currency)),
26
- view_context.humanized_money_with_symbol(Kaui::Base.to_money(invoice.balance, invoice.currency))
27
- ]
23
+ view_context.link_to(view_context.truncate_uuid(invoice.invoice_id), view_context.url_for(:controller => :invoices, :action => :show, :id => invoice.invoice_id)),
24
+ invoice.invoice_number,
25
+ view_context.format_date(invoice.invoice_date),
26
+ view_context.humanized_money_with_symbol(invoice.amount_to_money),
27
+ view_context.humanized_money_with_symbol(invoice.balance_to_money)
28
+ ]
28
29
  end
29
30
 
30
31
  respond_to do |format|
@@ -34,54 +35,43 @@ class Kaui::InvoicesController < Kaui::EngineController
34
35
 
35
36
  def show
36
37
  invoice_id_or_number = params[:id]
37
- if invoice_id_or_number.present?
38
- begin
39
- @invoice = Kaui::KillbillHelper.get_invoice(invoice_id_or_number, true, "FULL", options_for_klient)
40
- if @invoice.present?
41
- @invoice_id = @invoice.invoice_id
42
- @account = Kaui::KillbillHelper.get_account(@invoice.account_id, false, false, options_for_klient)
43
- @payments = Kaui::KillbillHelper.get_payments_for_invoice(@invoice_id, options_for_klient)
44
- @payment_methods = {}
45
- @payments.each do |payment|
46
- # The payment method may have been deleted
47
- @payment_methods[payment.payment_id] = Kaui::KillbillHelper::get_payment_method(payment.payment_method_id, options_for_klient) rescue nil
38
+ unless invoice_id_or_number.present?
39
+ flash.now[:error] = 'No id given'
40
+ render :index and return
41
+ end
48
42
 
49
- #get the refunds for the payment
50
- payment.refunds = Kaui::KillbillHelper::get_refunds_for_payment(payment.payment_id, options_for_klient) rescue []
51
- end
43
+ begin
44
+ @invoice = Kaui::Invoice.find_by_id_or_number(invoice_id_or_number, true, 'FULL', options_for_klient)
45
+ @invoice_id = @invoice.invoice_id
46
+ @account = Kaui::Account.find_by_id(@invoice.account_id, false, false, options_for_klient)
47
+ @payments = Kaui::Invoice.new(:invoice_id => @invoice_id).payments(false, 'FULL', options_for_klient)
48
+ @payment_methods = Kaui::PaymentMethod.payment_methods_for_payments(@payments, options_for_klient)
52
49
 
53
- @subscriptions = {}
54
- @bundles = {}
55
- @cba_items_not_deleteable = []
56
- if @invoice.items.present?
57
- @invoice.items.each do |item|
58
- @cba_items_not_deleteable << item.linked_invoice_item_id if item.description =~ /account credit/ and item.amount < 0
50
+ @subscriptions = {}
51
+ @bundles = {}
52
+ @cba_items_not_deleteable = []
53
+ if @invoice.items.present?
54
+ @invoice.items.each do |item|
55
+ @cba_items_not_deleteable << item.linked_invoice_item_id if item.description =~ /account credit/ and item.amount < 0
59
56
 
60
- unless item.subscription_id.nil? || @subscriptions.has_key?(item.subscription_id)
61
- @subscriptions[item.subscription_id] = Kaui::KillbillHelper.get_subscription(item.subscription_id, options_for_klient)
62
- end
63
- unless item.bundle_id.nil? || @bundles.has_key?(item.bundle_id)
64
- @bundles[item.bundle_id] = Kaui::KillbillHelper.get_bundle(item.bundle_id, options_for_klient)
65
- end
57
+ unless item.subscription_id.nil? || @subscriptions.has_key?(item.subscription_id)
58
+ @subscriptions[item.subscription_id] = Kaui::Subscription::find_by_id(item.subscription_id, options_for_klient)
66
59
  end
67
- else
68
- flash.now[:error] = "Invoice items for #{@invoice_id} not found"
60
+ unless item.bundle_id.nil? || @bundles.has_key?(item.bundle_id)
61
+ @bundles[item.bundle_id] = Kaui::Bundle::find_by_id(item.bundle_id, options_for_klient)
69
62
  end
70
- else
71
- flash.now[:error] = "Invoice #{invoice_id_or_number} not found"
72
- render :action => :index
73
63
  end
74
- rescue => e
75
- flash.now[:error] = "Error while getting information for invoice #{invoice_id_or_number}: #{as_string(e)}"
64
+ else
65
+ flash.now[:error] = "Invoice items for #{@invoice_id} not found"
76
66
  end
77
- else
78
- flash.now[:error] = "No id given"
67
+ rescue => e
68
+ flash.now[:error] = "Error while getting information for invoice #{invoice_id_or_number}: #{as_string(e)}"
79
69
  end
80
70
  end
81
71
 
82
72
  def show_html
83
73
  begin
84
- render :text => Kaui::KillbillHelper.get_invoice_html(params[:id], options_for_klient)
74
+ render :text => Kaui::Invoice.as_html(params[:id], options_for_klient)
85
75
  rescue => e
86
76
  flash.now[:error] = "Error rendering invoice html #{params[:id]}: #{as_string(e)}"
87
77
  render :action => :index
@@ -1,30 +1,31 @@
1
- require 'kaui/killbill_helper'
2
-
3
1
  class Kaui::PaymentMethodsController < Kaui::EngineController
2
+
4
3
  def index
5
4
  end
6
5
 
7
6
  def pagination
8
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
9
-
10
7
  search_key = params[:sSearch]
11
- if search_key.present?
12
- payment_methods = Kaui::KillbillHelper::search_payment_methods(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
13
- else
14
- payment_methods = Kaui::KillbillHelper::get_payment_methods(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
15
- end
16
- json[:iTotalDisplayRecords] = payment_methods.pagination_total_nb_records
17
- json[:iTotalRecords] = payment_methods.pagination_max_nb_records
8
+ offset = params[:iDisplayStart] || 0
9
+ limit = params[:iDisplayLength] || 10
10
+
11
+ payment_methods = Kaui::PaymentMethod.list_or_search(search_key, offset, limit, options_for_klient)
12
+
13
+ json = {
14
+ :sEcho => params[:sEcho],
15
+ :iTotalRecords => payment_methods.pagination_max_nb_records,
16
+ :iTotalDisplayRecords => payment_methods.pagination_total_nb_records,
17
+ :aaData => []
18
+ }
18
19
 
19
20
  payment_methods.each do |payment_method|
20
21
  info_plugin = payment_method.plugin_info || OpenStruct.new
21
22
  json[:aaData] << [
22
- view_context.link_to(payment_method.account_id, view_context.url_for(:controller => :accounts, :action => :show, :id => payment_method.account_id)),
23
- info_plugin.external_payment_id,
24
- info_plugin.type,
25
- info_plugin.cc_name,
26
- info_plugin.cc_last4,
27
- ]
23
+ view_context.link_to(view_context.truncate_uuid(payment_method.payment_method_id), view_context.url_for(:controller => :payment_methods, :action => :show, :id => payment_method.payment_method_id)),
24
+ view_context.link_to(view_context.truncate_uuid(payment_method.account_id), view_context.url_for(:controller => :accounts, :action => :show, :id => payment_method.account_id)),
25
+ info_plugin.external_payment_id,
26
+ find_value_from_properties(info_plugin.properties, 'ccName'),
27
+ find_value_from_properties(info_plugin.properties, 'ccLast4'),
28
+ ]
28
29
  end
29
30
 
30
31
  respond_to do |format|
@@ -32,26 +33,81 @@ class Kaui::PaymentMethodsController < Kaui::EngineController
32
33
  end
33
34
  end
34
35
 
36
+ def new
37
+ @payment_method = Kaui::PaymentMethod.new(:account_id => params[:account_id],
38
+ :plugin_name => params[:plugin_name] || Kaui.creditcard_plugin_name.call)
39
+ end
40
+
41
+ def create
42
+ @payment_method = Kaui::PaymentMethod.new(params[:payment_method].delete_if { |key, value| value.blank? })
43
+ # Transform "1" into boolean
44
+ @payment_method.is_default = @payment_method.is_default == '1'
45
+ # Sensible default
46
+ @payment_method.plugin_name ||= Kaui.creditcard_plugin_name.call
47
+
48
+ # Instance variables needed in case of failure
49
+ @card_type = params[:card_type]
50
+ @card_holder_name = params[:card_holder_name]
51
+ @expiration_year = params[:expiration_year]
52
+ @expiration_month = params[:expiration_month]
53
+ @credit_card_number = params[:credit_card_number]
54
+ @address1 = params[:address1]
55
+ @address2 = params[:address2]
56
+ @city = params[:city]
57
+ @postal_code = params[:postal_code]
58
+ @state = params[:state]
59
+ @country = params[:country]
60
+
61
+ @payment_method.plugin_info = {
62
+ 'type' => 'CreditCard',
63
+ 'ccType' => @card_type,
64
+ 'ccName' => @card_holder_name,
65
+ 'ccExpirationMonth' => @expiration_month,
66
+ 'ccExpirationYear' => @expiration_year,
67
+ 'ccLast4' => @credit_card_number[-4, 4],
68
+ 'address1' => @address1,
69
+ 'address2' => @address2,
70
+ 'city' => @city,
71
+ 'country' => @country,
72
+ 'zip' => @postal_code,
73
+ 'state' => @state
74
+ }
75
+
76
+ begin
77
+ @payment_method = @payment_method.create(current_user.kb_username, @reason, @comment, options_for_klient)
78
+ redirect_to payment_method_path(@payment_method.payment_method_id), :notice => 'Payment method was successfully created'
79
+ rescue => e
80
+ flash.now[:error] = "Error while creating payment method: #{as_string(e)}"
81
+ render :action => :new
82
+ end
83
+ end
84
+
35
85
  def show
36
- @payment_methods = []
37
86
  begin
38
- @payment_methods << Kaui::KillbillHelper.get_payment_method(params[:id], options_for_klient)
87
+ @payment_methods = [Kaui::PaymentMethod.find_by_id(params[:id], true, options_for_klient)]
39
88
  rescue => e
40
89
  flash.now[:error] = "Error while retrieving payment method #{params[:id]}: #{as_string(e)}"
90
+ render :action => :index
41
91
  end
42
92
  end
43
93
 
44
94
  def destroy
45
95
  payment_method_id = params[:id]
46
- if payment_method_id.present?
47
- begin
48
- Kaui::KillbillHelper.delete_payment_method(payment_method_id, params[:set_auto_pay_off], current_user, params[:reason], params[:comment], options_for_klient)
49
- rescue => e
50
- flash[:error] = "Error while deleting payment method #{payment_method_id}: #{as_string(e)}"
51
- end
52
- else
53
- flash[:notice] = 'Did not get the payment method id'
96
+
97
+ begin
98
+ Kaui::PaymentMethod.destroy(payment_method_id, params[:set_auto_pay_off], current_user.kb_username, params[:reason], params[:comment], options_for_klient)
99
+ redirect_to payment_methods_path, :notice => "Payment method #{payment_method_id} successfully deleted"
100
+ rescue => e
101
+ flash.now[:error] = "Error while deleting payment method #{payment_method_id}: #{as_string(e)}"
102
+ render :action => :index
54
103
  end
55
- redirect_to :back
104
+ end
105
+
106
+ private
107
+
108
+ def find_value_from_properties(properties, key)
109
+ return nil if key.nil? or properties.nil?
110
+ prop = (properties.find { |kv| kv.key.to_s == key.to_s })
111
+ prop.nil? ? nil : prop.value
56
112
  end
57
113
  end
@@ -4,25 +4,33 @@ class Kaui::PaymentsController < Kaui::EngineController
4
4
  end
5
5
 
6
6
  def pagination
7
- json = { :sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => [] }
7
+ json = {:sEcho => params[:sEcho], :iTotalRecords => 0, :iTotalDisplayRecords => 0, :aaData => []}
8
8
 
9
9
  search_key = params[:sSearch]
10
10
  if search_key.present?
11
- payments = Kaui::KillbillHelper::search_payments(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
11
+ payments = Kaui::Payment::find_in_batches_by_search_key(search_key, params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
12
12
  else
13
- payments = Kaui::KillbillHelper::get_payments(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
13
+ payments = Kaui::Payment::find_in_batches(params[:iDisplayStart] || 0, params[:iDisplayLength] || 10, options_for_klient)
14
14
  end
15
15
  json[:iTotalDisplayRecords] = payments.pagination_total_nb_records
16
- json[:iTotalRecords] = payments.pagination_max_nb_records
16
+ json[:iTotalRecords] = payments.pagination_max_nb_records
17
17
 
18
18
  payments.each do |payment|
19
+ created_date = nil
20
+ payment.transactions.each do |transaction|
21
+ transaction_date = Date.parse(transaction.effective_date)
22
+ if created_date.nil? or transaction_date < created_date
23
+ created_date = transaction_date
24
+ end
25
+ end
26
+
19
27
  json[:aaData] << [
20
- view_context.link_to(payment.account_id, view_context.url_for(:controller => :accounts, :action => :show, :id => payment.account_id)),
21
- payment.payment_number,
22
- view_context.format_date(payment.effective_date),
23
- view_context.humanized_money_with_symbol(Kaui::Base.to_money(payment.amount, payment.currency)),
24
- payment.status
25
- ]
28
+ view_context.link_to(view_context.truncate_uuid(payment.account_id), view_context.url_for(:controller => :accounts, :action => :show, :id => payment.account_id)),
29
+ payment.payment_number,
30
+ view_context.format_date(created_date),
31
+ view_context.humanized_money_with_symbol(payment.paid_amount_to_money),
32
+ view_context.humanized_money_with_symbol(payment.returned_amount_to_money)
33
+ ]
26
34
  end
27
35
 
28
36
  respond_to do |format|
@@ -31,30 +39,60 @@ class Kaui::PaymentsController < Kaui::EngineController
31
39
  end
32
40
 
33
41
  def new
34
- @account_id = params[:account_id]
35
- @invoice_id = params[:invoice_id]
42
+ account_id = params[:account_id]
43
+ invoice_id = params[:invoice_id]
44
+ amount = 0
45
+
46
+ if invoice_id.nil?
47
+ flash[:error] = 'No invoice id specified'
48
+ render :action => :index and return
49
+ end
50
+
51
+ if account_id.nil?
52
+ flash[:error] = 'No account id specified'
53
+ render :action => :index and return
54
+ end
55
+
36
56
  begin
37
- @invoice = Kaui::KillbillHelper::get_invoice(@invoice_id, true, "NONE", options_for_klient)
38
- @account = Kaui::KillbillHelper::get_account(@account_id, false, false, options_for_klient)
57
+ @invoice = Kaui::Invoice.find_by_id_or_number(invoice_id, true, 'NONE', options_for_klient)
58
+ amount = @invoice.balance
39
59
  rescue => e
40
- flash[:error] = "Error while creating a new payment: #{as_string(e)}"
41
- redirect_to kaui_engine.account_timeline_path(:id => payment.account_id)
60
+ flash[:error] = "Unable to retrieve invoice: #{as_string(e)}"
61
+ render :action => :index and return
62
+ end
63
+
64
+ begin
65
+ @account = Kaui::Account.find_by_id(account_id, false, false, options_for_klient)
66
+ rescue => e
67
+ flash[:error] = "Unable to retrieve account: #{as_string(e)}"
68
+ render :action => :index and return
42
69
  end
43
70
 
44
- @payment = Kaui::Payment.new("accountId" => @account_id, "invoiceId" => @invoice_id, "amount" => @invoice.balance)
71
+ @payment = Kaui::InvoicePayment.new('accountId' => account_id, 'targetInvoiceId' => invoice_id, 'purchasedAmount' => amount)
45
72
  end
46
73
 
47
74
  def create
48
- payment = Kaui::Payment.new(params[:payment])
49
- if payment.present?
50
- payment.external = (payment.external == "1")
51
- begin
52
- Kaui::KillbillHelper::create_payment(payment, payment.external, current_user, params[:reason], params[:comment], options_for_klient)
53
- flash[:notice] = "Payment created"
54
- rescue => e
55
- flash[:error] = "Error while creating a new payment: #{as_string(e)}"
56
- end
75
+ payment = Kaui::InvoicePayment.new(params[:invoice_payment])
76
+
77
+ begin
78
+ payment = payment.create(params[:external] == '1', current_user.kb_username, params[:reason], params[:comment], options_for_klient)
79
+ flash[:notice] = 'Payment created'
80
+ rescue => e
81
+ flash[:error] = "Error while creating a new payment: #{as_string(e)}"
82
+ render :action => :index and return
57
83
  end
84
+
58
85
  redirect_to kaui_engine.account_timeline_path(:id => payment.account_id)
59
86
  end
87
+
88
+ def show
89
+ begin
90
+ @payments = [Kaui::InvoicePayment.find_by_id(params[:id], true, options_for_klient)]
91
+ @account = Kaui::Account.find_by_id(@payments.first.account_id, false, false, options_for_klient)
92
+ @payment_methods = Kaui::PaymentMethod.payment_methods_for_payments(@payments, options_for_klient)
93
+ rescue => e
94
+ flash.now[:error] = "Error while looking up payment: #{as_string(e)}"
95
+ render :action => :index
96
+ end
97
+ end
60
98
  end