kaui 2.0.4 → 2.2.1

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