kaui 2.1.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +8 -7
  3. data/Rakefile +4 -2
  4. data/app/assets/config/kaui_manifest.js +45 -0
  5. data/app/assets/javascripts/jquery.spin.js +76 -0
  6. data/app/assets/javascripts/kaui/kaui.js +76 -0
  7. data/app/assets/javascripts/kaui_application.js +25 -0
  8. data/app/assets/javascripts/spin.js +76 -0
  9. data/app/assets/stylesheets/bootstrap_and_overrides.scss +240 -0
  10. data/app/assets/stylesheets/kaui/{account.less → account.scss} +4 -0
  11. data/app/assets/stylesheets/kaui/{audit.less → audit.scss} +5 -1
  12. data/app/assets/stylesheets/kaui/{common.less → common.scss} +17 -15
  13. data/app/assets/stylesheets/kaui/{datatable.less → datatable.scss} +6 -3
  14. data/app/assets/stylesheets/kaui/{header.less → header.scss} +16 -12
  15. data/app/assets/stylesheets/kaui/{home.less → home.scss} +7 -4
  16. data/app/assets/stylesheets/kaui/{invoice.less → invoice.scss} +7 -3
  17. data/app/assets/stylesheets/kaui/kaui.scss +265 -0
  18. data/app/assets/stylesheets/kaui/kaui_bootstrap.scss +3 -0
  19. data/app/assets/stylesheets/kaui/{overdue.less → overdue.scss} +3 -1
  20. data/app/assets/stylesheets/kaui/{payment.less → payment.scss} +3 -0
  21. data/app/assets/stylesheets/kaui/{subscription.less → subscription.scss} +4 -1
  22. data/app/assets/stylesheets/kaui/{tags.less → tags.scss} +9 -5
  23. data/app/assets/stylesheets/kaui/timeline.scss +7 -0
  24. data/app/assets/stylesheets/kaui/tooltip.scss +15 -0
  25. data/app/controllers/kaui/account_children_controller.rb +25 -28
  26. data/app/controllers/kaui/account_custom_fields_controller.rb +16 -15
  27. data/app/controllers/kaui/account_emails_controller.rb +28 -25
  28. data/app/controllers/kaui/account_tags_controller.rb +37 -34
  29. data/app/controllers/kaui/account_timelines_controller.rb +38 -33
  30. data/app/controllers/kaui/accounts_controller.rb +268 -250
  31. data/app/controllers/kaui/admin_allowed_users_controller.rb +105 -99
  32. data/app/controllers/kaui/admin_controller.rb +32 -31
  33. data/app/controllers/kaui/admin_tenants_controller.rb +434 -388
  34. data/app/controllers/kaui/audit_logs_controller.rb +86 -83
  35. data/app/controllers/kaui/bundle_tags_controller.rb +24 -20
  36. data/app/controllers/kaui/bundles_controller.rb +96 -82
  37. data/app/controllers/kaui/chargebacks_controller.rb +36 -33
  38. data/app/controllers/kaui/charges_controller.rb +23 -20
  39. data/app/controllers/kaui/credits_controller.rb +28 -25
  40. data/app/controllers/kaui/custom_fields_controller.rb +172 -38
  41. data/app/controllers/kaui/engine_controller.rb +2 -1
  42. data/app/controllers/kaui/engine_controller_util.rb +128 -117
  43. data/app/controllers/kaui/home_controller.rb +230 -217
  44. data/app/controllers/kaui/invoice_items_controller.rb +43 -39
  45. data/app/controllers/kaui/invoice_tags_controller.rb +24 -20
  46. data/app/controllers/kaui/invoices_controller.rb +132 -117
  47. data/app/controllers/kaui/login_proxy_controller.rb +2 -1
  48. data/app/controllers/kaui/payment_methods_controller.rb +102 -94
  49. data/app/controllers/kaui/payments_controller.rb +95 -92
  50. data/app/controllers/kaui/queues_controller.rb +47 -14
  51. data/app/controllers/kaui/refunds_controller.rb +36 -33
  52. data/app/controllers/kaui/registrations_controller.rb +7 -7
  53. data/app/controllers/kaui/role_definitions_controller.rb +17 -14
  54. data/app/controllers/kaui/sessions_controller.rb +7 -4
  55. data/app/controllers/kaui/subscriptions_controller.rb +197 -187
  56. data/app/controllers/kaui/tag_definitions_controller.rb +28 -27
  57. data/app/controllers/kaui/tags_controller.rb +25 -22
  58. data/app/controllers/kaui/tenants_controller.rb +30 -28
  59. data/app/controllers/kaui/transactions_controller.rb +43 -38
  60. data/app/helpers/kaui/account_helper.rb +16 -14
  61. data/app/helpers/kaui/application_helper.rb +8 -3
  62. data/app/helpers/kaui/date_helper.rb +4 -3
  63. data/app/helpers/kaui/home_helper.rb +2 -0
  64. data/app/helpers/kaui/locale_helper.rb +7 -6
  65. data/app/helpers/kaui/money_helper.rb +2 -1
  66. data/app/helpers/kaui/object_helper.rb +11 -10
  67. data/app/helpers/kaui/payment_helper.rb +7 -6
  68. data/app/helpers/kaui/payment_method_helper.rb +3 -2
  69. data/app/helpers/kaui/permissions_helper.rb +9 -9
  70. data/app/helpers/kaui/plugin_helper.rb +25 -23
  71. data/app/helpers/kaui/subscription_helper.rb +27 -29
  72. data/app/helpers/kaui/us_states_helper.rb +7 -6
  73. data/app/helpers/kaui/uuid_helper.rb +8 -6
  74. data/app/models/kaui/ability.rb +19 -12
  75. data/app/models/kaui/account.rb +45 -28
  76. data/app/models/kaui/account_email.rb +23 -20
  77. data/app/models/kaui/account_timeline.rb +5 -1
  78. data/app/models/kaui/admin.rb +65 -64
  79. data/app/models/kaui/admin_tenant.rb +81 -82
  80. data/app/models/kaui/allowed_user.rb +13 -9
  81. data/app/models/kaui/allowed_user_tenant.rb +4 -2
  82. data/app/models/kaui/application_record.rb +2 -0
  83. data/app/models/kaui/audit_log.rb +6 -3
  84. data/app/models/kaui/base.rb +13 -7
  85. data/app/models/kaui/bundle.rb +41 -24
  86. data/app/models/kaui/catalog.rb +118 -113
  87. data/app/models/kaui/chargeback.rb +5 -1
  88. data/app/models/kaui/credit.rb +4 -1
  89. data/app/models/kaui/custom_field.rb +9 -6
  90. data/app/models/kaui/invoice.rb +25 -22
  91. data/app/models/kaui/invoice_item.rb +9 -6
  92. data/app/models/kaui/invoice_payment.rb +49 -54
  93. data/app/models/kaui/killbill_authenticatable.rb +14 -8
  94. data/app/models/kaui/killbill_registerable.rb +7 -0
  95. data/app/models/kaui/overdue.rb +64 -65
  96. data/app/models/kaui/payment.rb +22 -19
  97. data/app/models/kaui/payment_method.rb +48 -39
  98. data/app/models/kaui/payment_state.rb +41 -38
  99. data/app/models/kaui/rails_methods.rb +9 -9
  100. data/app/models/kaui/refund.rb +7 -1
  101. data/app/models/kaui/role_definition.rb +5 -1
  102. data/app/models/kaui/simple_plan.rb +5 -3
  103. data/app/models/kaui/subscription.rb +10 -7
  104. data/app/models/kaui/tag.rb +25 -22
  105. data/app/models/kaui/tag_definition.rb +50 -45
  106. data/app/models/kaui/tenant.rb +6 -5
  107. data/app/models/kaui/transaction.rb +43 -31
  108. data/app/models/kaui/user.rb +10 -16
  109. data/app/models/kaui/user_role.rb +8 -5
  110. data/app/views/kaui/accounts/_billing_info.html.erb +1 -1
  111. data/app/views/kaui/accounts/_form.html.erb +2 -2
  112. data/app/views/kaui/accounts/index.html.erb +2 -0
  113. data/app/views/kaui/admin_allowed_users/_form.html.erb +3 -3
  114. data/app/views/kaui/admin_tenants/_form_catalog_translation.erb +1 -1
  115. data/app/views/kaui/admin_tenants/_form_invoice_template.erb +1 -1
  116. data/app/views/kaui/admin_tenants/_form_invoice_translation.erb +1 -1
  117. data/app/views/kaui/admin_tenants/_show_catalog_simple.erb +11 -5
  118. data/app/views/kaui/admin_tenants/new_catalog.html.erb +2 -2
  119. data/app/views/kaui/admin_tenants/new_overdue_config.html.erb +2 -2
  120. data/app/views/kaui/admin_tenants/new_plan_currency.html.erb +1 -1
  121. data/app/views/kaui/bundles/index.html.erb +1 -1
  122. data/app/views/kaui/bundles/transfer.html.erb +13 -5
  123. data/app/views/kaui/invoices/_invoice_table.html.erb +1 -1
  124. data/app/views/kaui/layouts/kaui_flash.html.erb +24 -0
  125. data/app/views/kaui/layouts/kaui_header.html.erb +19 -2
  126. data/app/views/kaui/payment_methods/_payment_methods_details_table.html.erb +13 -4
  127. data/app/views/kaui/payments/_payment_table.html.erb +21 -11
  128. data/app/views/kaui/payments/index.html.erb +9 -9
  129. data/app/views/kaui/queues/index.html.erb +1 -1
  130. data/app/views/kaui/subscriptions/_cancel_by_date_modal.html.erb +1 -4
  131. data/app/views/kaui/subscriptions/_edit_form.html.erb +1 -1
  132. data/app/views/kaui/subscriptions/_form.html.erb +1 -1
  133. data/app/views/kaui/subscriptions/_subscriptions_table.html.erb +3 -3
  134. data/app/views/kaui/subscriptions/edit_bcd.erb +1 -1
  135. data/app/views/kaui/tag_definitions/index.html.erb +2 -2
  136. data/app/views/kaui/tags/index.html.erb +1 -0
  137. data/config/initializers/bootstrap_datepicker_rails.rb +3 -0
  138. data/config/initializers/devise.rb +8 -6
  139. data/config/initializers/js_routes.rb +9 -0
  140. data/config/initializers/kilbill_client_init.rb +13 -0
  141. data/config/initializers/killbill_authenticatable.rb +71 -21
  142. data/config/initializers/killbill_registerable.rb +5 -3
  143. data/config/initializers/time_formats.rb +6 -4
  144. data/config/locales/en.yml +78 -1
  145. data/config/routes.rb +34 -32
  146. data/db/migrate/20130812155313_devise_create_kaui_users.rb +10 -8
  147. data/db/migrate/20150109214021_create_kaui_tenants.rb +10 -8
  148. data/db/migrate/20150112232813_create_kaui_allowed_users.rb +10 -8
  149. data/lib/generators/kaui/install/install_generator.rb +23 -20
  150. data/lib/generators/kaui/install/templates/config/initializers/kaui.rb +3 -1
  151. data/lib/kaui/engine.rb +17 -5
  152. data/lib/kaui/installer/installer.rb +27 -26
  153. data/lib/kaui/version.rb +3 -1
  154. data/lib/kaui.rb +28 -30
  155. data/lib/tasks/kaui_tasks.rake +1 -0
  156. metadata +136 -438
  157. data/app/assets/javascripts/application.js +0 -17
  158. data/app/assets/javascripts/kaui/bootstrap-tweaks.js +0 -172
  159. data/app/assets/stylesheets/bootstrap_and_overrides.css.less +0 -44
  160. data/app/assets/stylesheets/kaui/kaui.less +0 -21
  161. data/app/assets/stylesheets/kaui/timeline.less +0 -4
  162. data/config/initializers/asset.rb +0 -1
  163. data/config/initializers/kilbill-client-init.rb +0 -9
  164. data/db/schema.rb +0 -15
  165. data/test/dummy/README.rdoc +0 -261
  166. data/test/dummy/Rakefile +0 -7
  167. data/test/dummy/app/controllers/application_controller.rb +0 -9
  168. data/test/dummy/app/helpers/application_helper.rb +0 -2
  169. data/test/dummy/bin/bundle +0 -3
  170. data/test/dummy/bin/rails +0 -4
  171. data/test/dummy/bin/rake +0 -4
  172. data/test/dummy/bin/setup +0 -38
  173. data/test/dummy/bin/update +0 -29
  174. data/test/dummy/bin/yarn +0 -11
  175. data/test/dummy/config/application.rb +0 -18
  176. data/test/dummy/config/boot.rb +0 -3
  177. data/test/dummy/config/database.yml +0 -27
  178. data/test/dummy/config/environment.rb +0 -5
  179. data/test/dummy/config/environments/development.rb +0 -58
  180. data/test/dummy/config/environments/production.rb +0 -91
  181. data/test/dummy/config/environments/test.rb +0 -49
  182. data/test/dummy/config/initializers/application_controller_renderer.rb +0 -6
  183. data/test/dummy/config/initializers/assets.rb +0 -14
  184. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  185. data/test/dummy/config/initializers/cookies_serializer.rb +0 -5
  186. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  187. data/test/dummy/config/initializers/inflections.rb +0 -16
  188. data/test/dummy/config/initializers/killbill_client.rb +0 -1
  189. data/test/dummy/config/initializers/mime_types.rb +0 -4
  190. data/test/dummy/config/initializers/money.rb +0 -2
  191. data/test/dummy/config/initializers/new_framework_defaults_5_1.rb +0 -14
  192. data/test/dummy/config/initializers/secret_token.rb +0 -7
  193. data/test/dummy/config/initializers/session_store.rb +0 -8
  194. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  195. data/test/dummy/config/locales/en.yml +0 -33
  196. data/test/dummy/config/routes.rb +0 -3
  197. data/test/dummy/config/secrets.yml +0 -32
  198. data/test/dummy/config/symmetric-encryption.yml +0 -135
  199. data/test/dummy/config.ru +0 -4
  200. data/test/dummy/db/schema.rb +0 -52
  201. data/test/dummy/public/404.html +0 -26
  202. data/test/dummy/public/422.html +0 -26
  203. data/test/dummy/public/500.html +0 -25
  204. data/test/dummy/public/favicon.ico +0 -0
  205. data/test/dummy/script/rails +0 -6
  206. data/test/fixtures/SpyCarAdvanced.xml +0 -824
  207. data/test/fixtures/catalog-v1.xml +0 -73
  208. data/test/fixtures/catalog_translation_fr-v1.properties +0 -1
  209. data/test/fixtures/invoice_template-v1.html +0 -100
  210. data/test/fixtures/invoice_translation_fr-v1.properties +0 -23
  211. data/test/fixtures/overdue-v1.xml +0 -49
  212. data/test/fixtures/stripe.yml +0 -2
  213. data/test/functional/kaui/account_children_controller_test.rb +0 -32
  214. data/test/functional/kaui/account_custom_fields_controller_test.rb +0 -29
  215. data/test/functional/kaui/account_emails_controller_test.rb +0 -55
  216. data/test/functional/kaui/account_tags_controller_test.rb +0 -48
  217. data/test/functional/kaui/account_timelines_controller_test.rb +0 -20
  218. data/test/functional/kaui/accounts_controller_test.rb +0 -297
  219. data/test/functional/kaui/admin_allowed_users_controller_test.rb +0 -234
  220. data/test/functional/kaui/admin_controller_test.rb +0 -45
  221. data/test/functional/kaui/admin_tenants_controller_test.rb +0 -352
  222. data/test/functional/kaui/audit_logs_controller_test.rb +0 -71
  223. data/test/functional/kaui/bundle_tags_controller_test.rb +0 -23
  224. data/test/functional/kaui/bundles_controller_test.rb +0 -130
  225. data/test/functional/kaui/chargebacks_controller_test.rb +0 -42
  226. data/test/functional/kaui/charges_controller_test.rb +0 -60
  227. data/test/functional/kaui/credits_controller_test.rb +0 -54
  228. data/test/functional/kaui/custom_fields_controller_test.rb +0 -52
  229. data/test/functional/kaui/functional_test_helper.rb +0 -13
  230. data/test/functional/kaui/functional_test_helper_nosetup.rb +0 -120
  231. data/test/functional/kaui/home_controller_test.rb +0 -407
  232. data/test/functional/kaui/invoice_items_controller_test.rb +0 -82
  233. data/test/functional/kaui/invoices_controller_test.rb +0 -88
  234. data/test/functional/kaui/login_proxy_controller_test.rb +0 -12
  235. data/test/functional/kaui/payment_methods_controller_test.rb +0 -52
  236. data/test/functional/kaui/payments_controller_test.rb +0 -59
  237. data/test/functional/kaui/queues_controller_test.rb +0 -17
  238. data/test/functional/kaui/refunds_controller_test.rb +0 -63
  239. data/test/functional/kaui/registrations_controller_test.rb +0 -49
  240. data/test/functional/kaui/role_definitions_controller_test.rb +0 -47
  241. data/test/functional/kaui/subscriptions_controller_test.rb +0 -194
  242. data/test/functional/kaui/tag_definitions_controller_test.rb +0 -29
  243. data/test/functional/kaui/tags_controller_test.rb +0 -21
  244. data/test/functional/kaui/tenants_controller_test.rb +0 -60
  245. data/test/functional/kaui/transactions_controller_test.rb +0 -90
  246. data/test/integration/kaui/date_helper_integration_test.rb +0 -16
  247. data/test/integration/kaui/integration_test_helper.rb +0 -32
  248. data/test/integration/kaui/navigation_test.rb +0 -43
  249. data/test/killbill_test_helper.rb +0 -219
  250. data/test/test_helper.rb +0 -22
  251. data/test/unit/helpers/kaui/admin_allowed_users_helper_test.rb +0 -6
  252. data/test/unit/helpers/kaui/admin_tenants_helper_test.rb +0 -6
  253. data/test/unit/helpers/kaui/payment_method_helper_test.rb +0 -17
  254. data/test/unit/helpers/kaui/tenants_helper_test.rb +0 -6
  255. data/test/unit/helpers/kaui/uuid_helper_test.rb +0 -16
  256. data/test/unit/kaui/account_email_test.rb +0 -19
  257. data/test/unit/kaui/account_test.rb +0 -14
  258. data/test/unit/kaui/admin_tenant_test.rb +0 -132
  259. data/test/unit/kaui/allowed_user_test.rb +0 -34
  260. data/test/unit/kaui/base_test.rb +0 -23
  261. data/test/unit/kaui/date_helper_test.rb +0 -26
  262. data/test/unit/kaui/invoice_item_test.rb +0 -11
  263. data/test/unit/kaui/invoice_payment_test.rb +0 -42
  264. data/test/unit/kaui/invoice_test.rb +0 -20
  265. data/test/unit/kaui/money_helper_test.rb +0 -16
  266. data/test/unit/kaui/payment_test.rb +0 -42
  267. data/test/unit/kaui/tag_definition_test.rb +0 -11
  268. data/test/unit/kaui/tenant_test.rb +0 -19
  269. /data/app/assets/stylesheets/{application.css → kaui_application.css} +0 -0
@@ -1,328 +1,346 @@
1
- class Kaui::AccountsController < Kaui::EngineController
2
-
3
- def index
4
- @search_query = params[:q]
5
-
6
- if params[:fast] == '1' && !@search_query.blank?
7
- account = Kaui::Account.list_or_search(@search_query, -1, 1, options_for_klient).first
8
- if account.nil?
9
- flash[:error] = "No account matches \"#{@search_query}\""
10
- redirect_to kaui_engine.home_path and return
11
- else
12
- redirect_to kaui_engine.account_path(account.account_id) and return
1
+ # frozen_string_literal: true
2
+
3
+ module Kaui
4
+ class AccountsController < Kaui::EngineController
5
+ def index
6
+ @search_query = params[:q]
7
+
8
+ if params[:fast] == '1' && !@search_query.blank?
9
+ account = Kaui::Account.list_or_search(@search_query, -1, 1, options_for_klient).first
10
+ if account.nil?
11
+ flash[:error] = "No account matches \"#{@search_query}\""
12
+ redirect_to kaui_engine.home_path
13
+ else
14
+ redirect_to kaui_engine.account_path(account.account_id)
15
+ end
16
+ return
13
17
  end
14
- end
15
18
 
16
- @ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
17
- @offset = params[:offset] || 0
18
- @limit = params[:limit] || 50
19
+ @ordering = params[:ordering] || (@search_query.blank? ? 'desc' : 'asc')
20
+ @offset = params[:offset] || 0
21
+ @limit = params[:limit] || 50
19
22
 
20
- @max_nb_records = @search_query.blank? ? Kaui::Account.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
21
- end
22
-
23
- def pagination
24
- cached_options_for_klient = options_for_klient
25
- searcher = lambda do |search_key, offset, limit|
26
- Kaui::Account.list_or_search(search_key, offset, limit, cached_options_for_klient)
23
+ @max_nb_records = @search_query.blank? ? Kaui::Account.list_or_search(nil, 0, 0, options_for_klient).pagination_max_nb_records : 0
27
24
  end
28
25
 
29
- data_extractor = lambda do |account, column|
30
- [
26
+ def pagination
27
+ cached_options_for_klient = options_for_klient
28
+ searcher = lambda do |search_key, offset, limit|
29
+ Kaui::Account.list_or_search(search_key, offset, limit, cached_options_for_klient)
30
+ end
31
+
32
+ data_extractor = lambda do |account, column|
33
+ [
31
34
  account.parent_account_id,
32
35
  account.account_id,
33
36
  account.external_key,
34
37
  account.account_balance
35
- ][column]
36
- end
38
+ ][column]
39
+ end
40
+
41
+ formatter = lambda do |account|
42
+ child_label = ''
43
+ unless account.parent_account_id.nil?
44
+ child_label = account.parent_account_id.nil? ? '' : view_context.content_tag(:span, 'Child', class: %w[label label-info account-child-label])
45
+ end
37
46
 
38
- formatter = lambda do |account|
39
- child_label = ''
40
- unless account.parent_account_id.nil?
41
- child_label = account.parent_account_id.nil? ? '' : view_context.content_tag(:span, 'Child', class: ['label', 'label-info', 'account-child-label'])
47
+ row = [child_label, view_context.link_to(account.account_id, view_context.url_for(action: :show, account_id: account.account_id))]
48
+ row += Kaui.account_search_columns.call(account, view_context)[1]
49
+ row
42
50
  end
43
51
 
44
- row = [child_label, view_context.link_to(account.account_id, view_context.url_for(:action => :show, :account_id => account.account_id))]
45
- row += Kaui.account_search_columns.call(account, view_context)[1]
46
- row
52
+ paginate searcher, data_extractor, formatter
47
53
  end
48
54
 
49
- paginate searcher, data_extractor, formatter
50
- end
55
+ def new
56
+ @account = Kaui::Account.new
57
+ end
51
58
 
52
- def new
53
- @account = Kaui::Account.new
54
- end
59
+ def create
60
+ @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
55
61
 
56
- def create
57
- @account = Kaui::Account.new(params.require(:account).delete_if { |key, value| value.blank? })
62
+ @account.errors.add(:phone, :invalid_phone) if !@account.phone.nil? && !@account.check_account_details_phone
58
63
 
59
- # Transform "1" into boolean
60
- @account.is_migrated = @account.is_migrated == '1'
64
+ @account.errors.add(:check_account_details_bill_cycle_day_local, :invalid_bill_cycle_day_local) if !@account.bill_cycle_day_local.nil? && !@account.check_account_details_bill_cycle_day_local
61
65
 
62
- begin
63
- @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
66
+ unless @account.errors.empty?
67
+ flash.now[:errors] = @account.errors.messages.values.flatten
68
+ render action: :new and return
69
+ end
64
70
 
65
- redirect_to account_path(@account.account_id), :notice => 'Account was successfully created'
66
- rescue => e
67
- flash.now[:error] = "Error while creating account: #{as_string(e)}"
68
- render :action => :new
69
- end
70
- end
71
+ # Transform "1" into boolean
72
+ @account.is_migrated = @account.is_migrated == '1'
71
73
 
72
- def show
73
- # Go to the database once
74
- cached_options_for_klient = options_for_klient
75
-
76
- # Re-fetch the account with balance and CBA
77
- @account = Kaui::Account::find_by_id_or_key(params.require(:account_id), true, true, cached_options_for_klient)
78
-
79
- fetch_children = promise { @account.children(false, false, 'NONE',cached_options_for_klient)}
80
- fetch_parent = @account.parent_account_id.nil? ? nil : promise { Kaui::Account::find_by_id(@account.parent_account_id,false,false,cached_options_for_klient) }
81
- fetch_overdue_state = promise { @account.overdue(cached_options_for_klient) }
82
- fetch_account_tags = promise { @account.tags(false, 'NONE', cached_options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b } }
83
- fetch_account_fields = promise { @account.custom_fields('NONE', cached_options_for_klient).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } }
84
- fetch_account_emails = promise { Kaui::AccountEmail.find_all_sorted_by_account_id(@account.account_id, 'NONE', cached_options_for_klient) }
85
- fetch_payments = promise { @account.payments(cached_options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) } }
86
- fetch_payment_methods = promise { Kaui::PaymentMethod.find_all_by_account_id(@account.account_id, false, cached_options_for_klient) }
87
-
88
- # is email notification plugin available
89
- is_email_notifications_plugin_available = Kenui::EmailNotificationService.email_notification_plugin_available?(cached_options_for_klient).first
90
- fetch_email_notification_configuration = !is_email_notifications_plugin_available ? nil : promise do
91
- Kenui::EmailNotificationService.get_configuration_per_account(params.require(:account_id),cached_options_for_klient)
92
- end.then do |configuration|
93
- if configuration.first.is_a?(FalseClass)
94
- Rails.logger.warn(configuration[1])
95
- configuration = []
74
+ begin
75
+ @account = @account.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
76
+
77
+ redirect_to account_path(@account.account_id), notice: 'Account was successfully created'
78
+ rescue StandardError => e
79
+ flash.now[:error] = "Error while creating account: #{as_string(e)}"
80
+ render action: :new
96
81
  end
97
- configuration
98
82
  end
99
83
 
100
- fetch_payment_methods_with_details = fetch_payment_methods.then do |pms|
101
- ops = []
102
- pms.each do |pm|
103
- ops << promise {
104
- begin
84
+ # rubocop:disable Style/MultilineBlockChain
85
+ def show
86
+ # Go to the database once
87
+ cached_options_for_klient = options_for_klient
88
+
89
+ # Re-fetch the account with balance and CBA
90
+ @account = Kaui::Account.find_by_id_or_key(params.require(:account_id), true, true, cached_options_for_klient)
91
+
92
+ fetch_children = promise { @account.children(false, false, 'NONE', cached_options_for_klient) }
93
+ fetch_parent = @account.parent_account_id.nil? ? nil : promise { Kaui::Account.find_by_id(@account.parent_account_id, false, false, cached_options_for_klient) }
94
+ fetch_overdue_state = promise { @account.overdue(cached_options_for_klient) }
95
+ fetch_account_tags = promise { @account.tags(false, 'NONE', cached_options_for_klient).sort { |tag_a, tag_b| tag_a <=> tag_b } }
96
+ fetch_account_fields = promise { @account.custom_fields('NONE', cached_options_for_klient).sort { |cf_a, cf_b| cf_a.name.downcase <=> cf_b.name.downcase } }
97
+ fetch_account_emails = promise { Kaui::AccountEmail.find_all_sorted_by_account_id(@account.account_id, 'NONE', cached_options_for_klient) }
98
+ fetch_payments = promise { @account.payments(cached_options_for_klient).map! { |payment| Kaui::Payment.build_from_raw_payment(payment) } }
99
+ fetch_payment_methods = promise { Kaui::PaymentMethod.find_all_by_account_id(@account.account_id, false, cached_options_for_klient) }
100
+
101
+ # is email notification plugin available
102
+ is_email_notifications_plugin_available = Kenui::EmailNotificationService.email_notification_plugin_available?(cached_options_for_klient).first
103
+ fetch_email_notification_configuration = if is_email_notifications_plugin_available
104
+ promise do
105
+ Kenui::EmailNotificationService.get_configuration_per_account(params.require(:account_id), cached_options_for_klient)
106
+ end.then do |configuration|
107
+ if configuration.first.is_a?(FalseClass)
108
+ Rails.logger.warn(configuration[1])
109
+ configuration = []
110
+ end
111
+ configuration
112
+ end
113
+ else
114
+ nil
115
+ end
116
+
117
+ fetch_payment_methods_with_details = fetch_payment_methods.then do |pms|
118
+ ops = []
119
+ pms.each do |pm|
120
+ ops << promise do
105
121
  Kaui::PaymentMethod.find_by_id(pm.payment_method_id, true, cached_options_for_klient)
106
- rescue => e
122
+ rescue StandardError => e
107
123
  # Maybe the plugin is not registered or the plugin threw an exception
108
124
  Rails.logger.warn(e)
109
125
  nil
110
126
  end
111
- }
127
+ end
128
+ ops
112
129
  end
113
- ops
114
- end
115
- fetch_available_tags = promise { Kaui::TagDefinition.all_for_account(cached_options_for_klient) }
116
-
117
- @overdue_state = wait(fetch_overdue_state)
118
- @tags = wait(fetch_account_tags)
119
- @custom_fields = wait(fetch_account_fields)
120
- @account_emails = wait(fetch_account_emails)
121
- wait(fetch_payment_methods)
122
- @payment_methods = wait(fetch_payment_methods_with_details).map{ |pm_f| wait(pm_f) }.reject { |pm| pm.nil? }
123
- @available_tags = wait(fetch_available_tags)
124
- @children = wait(fetch_children)
125
- @account_parent = @account.parent_account_id.nil? ? nil : wait(fetch_parent)
126
- @email_notification_configuration = wait(fetch_email_notification_configuration) if is_email_notifications_plugin_available
127
-
128
- @last_transaction_by_payment_method_id = {}
129
- wait(fetch_payments).each do |payment|
130
- transaction = payment.transactions.last
131
- transaction_date = Date.parse(transaction.effective_date)
132
-
133
- last_seen_transaction_date = @last_transaction_by_payment_method_id[payment.payment_method_id]
134
- if last_seen_transaction_date.nil? || Date.parse(last_seen_transaction_date.effective_date) < transaction_date
135
- @last_transaction_by_payment_method_id[payment.payment_method_id] = transaction
130
+ fetch_available_tags = promise { Kaui::TagDefinition.all_for_account(cached_options_for_klient) }
131
+
132
+ @overdue_state = wait(fetch_overdue_state)
133
+ @tags = wait(fetch_account_tags)
134
+ @custom_fields = wait(fetch_account_fields)
135
+ @account_emails = wait(fetch_account_emails)
136
+ wait(fetch_payment_methods)
137
+ @payment_methods = wait(fetch_payment_methods_with_details).map { |pm_f| wait(pm_f) }.compact
138
+ @available_tags = wait(fetch_available_tags)
139
+ @children = wait(fetch_children)
140
+ @account_parent = @account.parent_account_id.nil? ? nil : wait(fetch_parent)
141
+ @email_notification_configuration = wait(fetch_email_notification_configuration) if is_email_notifications_plugin_available
142
+
143
+ @last_transaction_by_payment_method_id = {}
144
+ wait(fetch_payments).each do |payment|
145
+ transaction = payment.transactions.last
146
+ transaction_date = Date.parse(transaction.effective_date)
147
+
148
+ last_seen_transaction_date = @last_transaction_by_payment_method_id[payment.payment_method_id]
149
+ @last_transaction_by_payment_method_id[payment.payment_method_id] = transaction if last_seen_transaction_date.nil? || Date.parse(last_seen_transaction_date.effective_date) < transaction_date
136
150
  end
151
+
152
+ params.permit!
137
153
  end
154
+ # rubocop:enable Style/MultilineBlockChain
138
155
 
139
- params.permit!
140
- end
156
+ def destroy
157
+ account_id = params.require(:account_id)
158
+ options = params[:options] || []
141
159
 
142
- def destroy
143
- account_id = params.require(:account_id)
144
- options = params[:options] || []
160
+ cancel_subscriptions = options.include?('cancel_all_subscriptions')
161
+ writeoff_unpaid_invoices = options.include?('writeoff_unpaid_invoices')
162
+ item_adjust_unpaid_invoices = options.include?('item_adjust_unpaid_invoices')
163
+ cached_options_for_klient = options_for_klient
145
164
 
146
- cancel_subscriptions = options.include?('cancel_all_subscriptions')
147
- writeoff_unpaid_invoices = options.include?('writeoff_unpaid_invoices')
148
- item_adjust_unpaid_invoices = options.include?('item_adjust_unpaid_invoices')
149
- cached_options_for_klient = options_for_klient
165
+ begin
166
+ @account = Kaui::Account.find_by_id_or_key(account_id, false, false, cached_options_for_klient)
167
+ @account.close(cancel_subscriptions, writeoff_unpaid_invoices, item_adjust_unpaid_invoices, current_user.kb_username, nil, nil, cached_options_for_klient)
150
168
 
151
- begin
152
- @account = Kaui::Account::find_by_id_or_key(account_id, false, false, cached_options_for_klient)
153
- @account.close(cancel_subscriptions, writeoff_unpaid_invoices, item_adjust_unpaid_invoices, current_user.kb_username, nil, nil, cached_options_for_klient );
169
+ flash[:notice] = "Account #{account_id} successfully closed"
170
+ rescue StandardError => e
171
+ flash[:error] = "Error while closing account: #{as_string(e)}"
172
+ end
154
173
 
155
- flash[:notice] = "Account #{account_id} successfully closed"
156
- rescue => e
157
- flash[:error] = "Error while closing account: #{as_string(e)}"
174
+ redirect_to account_path(account_id)
158
175
  end
159
176
 
160
- redirect_to account_path(account_id)
161
- end
177
+ def trigger_invoice
178
+ account_id = params.require(:account_id)
179
+ target_date = params[:target_date].presence
180
+ dry_run = params[:dry_run].nil? ? false : params[:dry_run] == '1'
162
181
 
163
- def trigger_invoice
164
- account_id = params.require(:account_id)
165
- target_date = params[:target_date].presence
166
- dry_run = params[:dry_run].nil? ? false : params[:dry_run] == '1'
167
-
168
- invoice = nil
169
- begin
170
- invoice = dry_run ? Kaui::Invoice.trigger_invoice_dry_run(account_id, target_date, false, options_for_klient) :
171
- Kaui::Invoice.trigger_invoice(account_id, target_date, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
172
- rescue KillBillClient::API::NotFound
173
- # Null invoice
174
- end
182
+ invoice = nil
183
+ begin
184
+ invoice = if dry_run
185
+ Kaui::Invoice.trigger_invoice_dry_run(account_id, target_date, false, options_for_klient)
186
+ else
187
+ Kaui::Invoice.trigger_invoice(account_id, target_date, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
188
+ end
189
+ rescue KillBillClient::API::NotFound
190
+ # Null invoice
191
+ end
175
192
 
176
- if invoice.nil?
177
- redirect_to account_path(account_id), :notice => "Nothing to generate for target date #{target_date.nil? ? 'today' : target_date}"
178
- elsif dry_run
179
- @invoice = Kaui::Invoice.build_from_raw_invoice(invoice)
180
- @invoice_tags = []
181
- @available_invoice_tags = []
182
- @payments = []
183
- @payment_methods = nil
184
- @account = Kaui::Account.find_by_id(account_id, false, false, options_for_klient)
185
- render :template => 'kaui/invoices/show'
186
- else
187
- # Redirect to fetch payments, etc.
188
- redirect_to invoice_path(invoice.invoice_id, :account_id => account_id), :notice => "Generated invoice #{invoice.invoice_number} for target date #{invoice.target_date}"
193
+ if invoice.nil?
194
+ redirect_to account_path(account_id), notice: "Nothing to generate for target date #{target_date.nil? ? 'today' : target_date}"
195
+ elsif dry_run
196
+ @invoice = Kaui::Invoice.build_from_raw_invoice(invoice)
197
+ @invoice_tags = []
198
+ @available_invoice_tags = []
199
+ @payments = []
200
+ @payment_methods = nil
201
+ @account = Kaui::Account.find_by_id(account_id, false, false, options_for_klient)
202
+ render template: 'kaui/invoices/show'
203
+ else
204
+ # Redirect to fetch payments, etc.
205
+ redirect_to invoice_path(invoice.invoice_id, account_id:), notice: "Generated invoice #{invoice.invoice_number} for target date #{invoice.target_date}"
206
+ end
189
207
  end
190
- end
191
208
 
192
- # Fetched asynchronously, as it takes time. This also helps with enforcing permissions.
193
- def next_invoice_date
194
- json_response do
195
- next_invoice = Kaui::Invoice.trigger_invoice_dry_run(params.require(:account_id), nil, true, options_for_klient)
196
- next_invoice ? next_invoice.target_date.to_json : nil
209
+ # Fetched asynchronously, as it takes time. This also helps with enforcing permissions.
210
+ def next_invoice_date
211
+ json_response do
212
+ next_invoice = Kaui::Invoice.trigger_invoice_dry_run(params.require(:account_id), nil, true, options_for_klient)
213
+ next_invoice ? next_invoice.target_date.to_json : nil
214
+ end
197
215
  end
198
- end
199
216
 
200
- def edit
201
- end
217
+ def edit; end
202
218
 
203
- def update
204
- @account = Kaui::Account.new(params.require(:account).delete_if { |key, value| value.blank? })
205
- @account.account_id = params.require(:account_id)
219
+ def update
220
+ @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
221
+ @account.account_id = params.require(:account_id)
206
222
 
207
- # Transform "1" into boolean
208
- @account.is_migrated = @account.is_migrated == '1'
223
+ # Transform "1" into boolean
224
+ @account.is_migrated = @account.is_migrated == '1'
209
225
 
210
- @account.update(true, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
226
+ @account.update(true, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
211
227
 
212
- redirect_to account_path(@account.account_id), :notice => 'Account successfully updated'
213
- rescue => e
214
- flash.now[:error] = "Error while updating account: #{as_string(e)}"
215
- render :action => :edit
216
- end
228
+ redirect_to account_path(@account.account_id), notice: 'Account successfully updated'
229
+ rescue StandardError => e
230
+ flash.now[:error] = "Error while updating account: #{as_string(e)}"
231
+ render action: :edit
232
+ end
217
233
 
218
- def set_default_payment_method
219
- account_id = params.require(:account_id)
220
- payment_method_id = params.require(:payment_method_id)
234
+ def set_default_payment_method
235
+ account_id = params.require(:account_id)
236
+ payment_method_id = params.require(:payment_method_id)
221
237
 
222
- Kaui::PaymentMethod.set_default(payment_method_id, account_id, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
238
+ Kaui::PaymentMethod.set_default(payment_method_id, account_id, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
223
239
 
224
- redirect_to account_path(account_id), :notice => "Successfully set #{payment_method_id} as default"
225
- end
240
+ redirect_to account_path(account_id), notice: "Successfully set #{payment_method_id} as default"
241
+ end
226
242
 
227
- def pay_all_invoices
228
- payment = Kaui::InvoicePayment.new(:account_id => params.require(:account_id))
243
+ def pay_all_invoices
244
+ payment = Kaui::InvoicePayment.new(account_id: params.require(:account_id))
229
245
 
230
- payment.bulk_create(params[:is_external_payment] == 'true', nil, nil, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
246
+ payment.bulk_create(params[:is_external_payment] == 'true', nil, nil, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
231
247
 
232
- redirect_to account_path(payment.account_id), :notice => 'Successfully triggered a payment for all unpaid invoices'
233
- end
248
+ redirect_to account_path(payment.account_id), notice: 'Successfully triggered a payment for all unpaid invoices'
249
+ end
234
250
 
235
- def validate_external_key
236
- json_response do
237
- external_key = params.require(:external_key)
251
+ def validate_external_key
252
+ json_response do
253
+ external_key = params.require(:external_key)
238
254
 
239
- begin
240
- account = Kaui::Account::find_by_external_key(external_key, false, false, options_for_klient)
241
- rescue KillBillClient::API::NotFound
242
- account = nil
255
+ begin
256
+ account = Kaui::Account.find_by_external_key(external_key, false, false, options_for_klient)
257
+ rescue KillBillClient::API::NotFound
258
+ account = nil
259
+ end
260
+ { is_found: !account.nil? }
243
261
  end
244
- {:is_found => !account.nil?}
245
262
  end
246
- end
247
263
 
248
- def link_to_parent
249
- @account = Kaui::Account.new(params.require(:account).delete_if { |key, value| value.blank? })
250
- @account.account_id = params.require(:account_id)
251
- @account.is_payment_delegated_to_parent = @account.is_payment_delegated_to_parent == '1'
264
+ def link_to_parent
265
+ @account = Kaui::Account.new(params.require(:account).delete_if { |_key, value| value.blank? })
266
+ @account.account_id = params.require(:account_id)
267
+ @account.is_payment_delegated_to_parent = @account.is_payment_delegated_to_parent == '1'
252
268
 
253
- raise('Account id and account parent id cannot be equal.') if @account.account_id == @account.parent_account_id
269
+ raise('Account id and account parent id cannot be equal.') if @account.account_id == @account.parent_account_id
254
270
 
255
- cached_options_for_klient = options_for_klient
271
+ cached_options_for_klient = options_for_klient
256
272
 
257
- # check if parent id is valid
258
- Kaui::Account.find_by_id(@account.parent_account_id, false, false, cached_options_for_klient)
273
+ # check if parent id is valid
274
+ Kaui::Account.find_by_id(@account.parent_account_id, false, false, cached_options_for_klient)
259
275
 
260
- @account.update(false, current_user.kb_username, params[:reason], params[:comment], cached_options_for_klient)
276
+ @account.update(false, current_user.kb_username, params[:reason], params[:comment], cached_options_for_klient)
261
277
 
262
- redirect_to account_path(@account.account_id), :notice => 'Account successfully updated'
263
- rescue => e
264
- if e.is_a?(KillBillClient::API::NotFound)
265
- flash[:error] = "Parent account id not found: #{@account.parent_account_id}"
266
- else
267
- flash[:error] = "Error while linking parent account: #{as_string(e)}"
278
+ redirect_to account_path(@account.account_id), notice: 'Account successfully updated'
279
+ rescue StandardError => e
280
+ flash[:error] = if e.is_a?(KillBillClient::API::NotFound)
281
+ "Parent account id not found: #{@account.parent_account_id}"
282
+ else
283
+ "Error while linking parent account: #{as_string(e)}"
284
+ end
285
+ redirect_to account_path(@account.account_id)
268
286
  end
269
- redirect_to account_path(@account.account_id)
270
- end
271
287
 
272
- def unlink_to_parent
273
- account_id = params.require(:account_id)
274
- cached_options_for_klient = options_for_klient
275
-
276
- # search for the account and remove the parent account id
277
- # check if parent id is valid
278
- account = Kaui::Account.find_by_id(account_id, false, false, cached_options_for_klient)
279
- account.is_payment_delegated_to_parent = false
280
- account.parent_account_id = nil
281
- account.update(true, current_user.kb_username, params[:reason], params[:comment], cached_options_for_klient)
282
-
283
- redirect_to account_path(@account.account_id), :notice => 'Account successfully updated'
284
- rescue => e
285
- flash[:error] = "Error while un-linking parent account: #{as_string(e)}"
286
- redirect_to account_path(@account.account_id)
287
- end
288
+ def unlink_to_parent
289
+ account_id = params.require(:account_id)
290
+ cached_options_for_klient = options_for_klient
291
+
292
+ # search for the account and remove the parent account id
293
+ # check if parent id is valid
294
+ account = Kaui::Account.find_by_id(account_id, false, false, cached_options_for_klient)
295
+ account.is_payment_delegated_to_parent = false
296
+ account.parent_account_id = nil
297
+ account.update(true, current_user.kb_username, params[:reason], params[:comment], cached_options_for_klient)
298
+
299
+ redirect_to account_path(@account.account_id), notice: 'Account successfully updated'
300
+ rescue StandardError => e
301
+ flash[:error] = "Error while un-linking parent account: #{as_string(e)}"
302
+ redirect_to account_path(@account.account_id)
303
+ end
288
304
 
289
- def set_email_notifications_configuration
290
- configuration = params.require(:configuration)
291
- account_id = configuration[:account_id]
292
- event_types = configuration[:event_types]
293
- cached_options_for_klient = options_for_klient
294
-
295
- is_success, message = email_notification_plugin_available?(cached_options_for_klient)
296
-
297
- is_success, message = Kenui::EmailNotificationService.set_configuration_per_account(account_id,
298
- event_types,
299
- current_user.kb_username,
300
- params[:reason],
301
- params[:comment],
302
- cached_options_for_klient) if is_success
303
- if is_success
304
- flash[:notice] = message
305
- else
306
- flash[:error] = message
305
+ def set_email_notifications_configuration
306
+ configuration = params.require(:configuration)
307
+ account_id = configuration[:account_id]
308
+ event_types = configuration[:event_types]
309
+ cached_options_for_klient = options_for_klient
310
+
311
+ is_success, message = email_notification_plugin_available?(cached_options_for_klient)
312
+
313
+ if is_success
314
+ is_success, message = Kenui::EmailNotificationService.set_configuration_per_account(account_id,
315
+ event_types,
316
+ current_user.kb_username,
317
+ params[:reason],
318
+ params[:comment],
319
+ cached_options_for_klient)
320
+ end
321
+ if is_success
322
+ flash[:notice] = message
323
+ else
324
+ flash[:error] = message
325
+ end
326
+ redirect_to account_path(account_id)
307
327
  end
308
- redirect_to account_path(account_id)
309
- end
310
328
 
311
- def events_to_consider
312
- json_response do
313
- { data: Kenui::EmailNotificationService.get_events_to_consider(options_for_klient) }
329
+ def events_to_consider
330
+ json_response do
331
+ { data: Kenui::EmailNotificationService.get_events_to_consider(options_for_klient) }
332
+ end
314
333
  end
315
- end
316
334
 
317
- private
335
+ private
318
336
 
319
337
  def email_notification_plugin_available?(options_for_klient)
320
338
  error_message = 'Email notification plugin is not installed'
321
339
 
322
340
  is_available = Kenui::EmailNotificationService.email_notification_plugin_available?(options_for_klient).first
323
- return is_available, is_available ? nil : error_message
324
- rescue
325
- return false, error_message
341
+ [is_available, is_available ? nil : error_message]
342
+ rescue StandardError
343
+ [false, error_message]
326
344
  end
327
-
345
+ end
328
346
  end