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
@@ -0,0 +1,115 @@
1
+ module Kaui
2
+ module SubscriptionHelper
3
+
4
+ def humanized_product_category(sub)
5
+ if !sub.present? or !sub.product_category.present?
6
+ nil
7
+ elsif sub.product_category == 'BASE'
8
+ 'Base'
9
+ elsif sub.product_category == 'ADD_ON'
10
+ 'Add-on'
11
+ else
12
+ sub.product_category.downcase.capitalize
13
+ end
14
+ end
15
+
16
+ def humanized_product_name(sub)
17
+ if !sub.present? or !sub.product_name.present?
18
+ nil
19
+ else
20
+ sub.product_name.downcase.capitalize
21
+ end
22
+ end
23
+
24
+ def humanized_billing_period(sub)
25
+ if !sub.present? or !sub.billing_period.present?
26
+ nil
27
+ elsif sub.billing_period == 'NO_BILLING_PERIOD'
28
+ 'No billing period'
29
+ else
30
+ sub.billing_period.downcase.capitalize
31
+ end
32
+ end
33
+
34
+ def humanized_price_list(sub, show_default=true)
35
+ if !sub.present? or !sub.price_list.present? or (!show_default and sub.price_list.upcase == 'DEFAULT')
36
+ nil
37
+ else
38
+ sub.price_list.downcase.capitalize
39
+ end
40
+ end
41
+
42
+ def humanized_full_product_name(sub)
43
+ humanized_product_name = humanized_product_name(sub)
44
+ humanized_billing_period = humanized_billing_period(sub)
45
+ humanized_price_list = humanized_price_list(sub, false)
46
+
47
+ if humanized_billing_period.nil?
48
+ if humanized_price_list.nil?
49
+ humanized_product_name
50
+ else
51
+ humanized_product_name+ ' (' + humanized_price_list.downcase + ')'
52
+ end
53
+ else
54
+ if humanized_price_list.nil?
55
+ humanized_product_name + ' (' + humanized_billing_period.downcase + ')'
56
+ else
57
+ humanized_product_name+ ' (' + humanized_billing_period.downcase + ', ' + humanized_price_list.downcase + ')'
58
+ end
59
+ end
60
+ end
61
+
62
+ def humanized_start_date(sub, account)
63
+ if !sub.present? or !sub.start_date.present? or !account.present? or !account.time_zone.present?
64
+ nil
65
+ else
66
+ format_date(sub.start_date, account.time_zone).html_safe
67
+ end
68
+ end
69
+
70
+ def humanized_charged_through_date(sub, account)
71
+ if !sub.present? or !sub.charged_through_date.present? or !account.present? or !account.time_zone.present?
72
+ nil
73
+ else
74
+ format_date(sub.charged_through_date, account.time_zone).html_safe
75
+ end
76
+ end
77
+
78
+ def humanized_cancelled_date(sub, account)
79
+ if !sub.present? or !sub.cancelled_date.present? or !account.present? or !account.time_zone.present?
80
+ nil
81
+ else
82
+ cancelled_date = format_date(sub.cancelled_date, account.time_zone).html_safe
83
+ if Time.parse(sub.cancelled_date) > Time.now
84
+ 'Pending cancellation on ' + cancelled_date
85
+ else
86
+ 'Canceled on ' + cancelled_date
87
+ end
88
+ end
89
+ end
90
+
91
+ def humanized_billing_start_date(sub, account)
92
+ if !sub.present? or !sub.billing_start_date.present? or !account.present? or !account.time_zone.present?
93
+ nil
94
+ else
95
+ format_date(sub.billing_start_date, account.time_zone).html_safe
96
+ end
97
+ end
98
+
99
+ def humanized_billing_end_date(sub, account)
100
+ if !sub.present? or !sub.billing_end_date.present? or !account.present? or !account.time_zone.present?
101
+ nil
102
+ else
103
+ format_date(sub.billing_end_date, account.time_zone).html_safe
104
+ end
105
+ end
106
+
107
+ def is_future_cancelled?(sub)
108
+ sub.present? and sub.billing_end_date.present? and Time.parse(sub.billing_end_date) > Time.now
109
+ end
110
+
111
+ def is_cancelled?(sub)
112
+ sub.present? and !sub.billing_end_date.present?
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,9 @@
1
+ module Kaui
2
+ module UuidHelper
3
+
4
+ def truncate_uuid(uuid)
5
+ split = uuid.split('-')
6
+ split[0] + '-...-' + split[4]
7
+ end
8
+ end
9
+ end
@@ -14,9 +14,9 @@ module Kaui
14
14
  elsif model == '*' and action != '*'
15
15
  # TODO
16
16
  elsif action == '*'
17
- can :all, ('Kaui::' + model.capitalize).constantize
17
+ can :all, ('Kaui::' + model.capitalize).constantize rescue nil
18
18
  else
19
- can action.to_sym, ('Kaui::' + model.capitalize).constantize
19
+ can action.to_sym, ('Kaui::' + model.capitalize).constantize rescue nil
20
20
  end
21
21
  end
22
22
  rescue KillBillClient::API::Unauthorized => e
@@ -1,53 +1,36 @@
1
- class Kaui::Account < Kaui::Base
2
- define_attr :account_id
3
- define_attr :external_key
4
- define_attr :name
5
- define_attr :first_name_length
6
- define_attr :email
7
- define_attr :currency
8
- define_attr :payment_method_id
9
- define_attr :timezone
10
- define_attr :address1
11
- define_attr :address2
12
- define_attr :company
13
- define_attr :state
14
- define_attr :country
15
- define_attr :phone
16
- define_attr :balance
17
- define_attr :cba
18
- define_attr :is_notified_for_invoices
19
- define_attr :bill_cycle_day
1
+ class Kaui::Account < KillBillClient::Model::Account
20
2
 
21
- def initialize(data = {})
22
- super(:account_id => data['accountId'],
23
- :external_key => data['externalKey'],
24
- :name => data['name'] || "#{data['firstName'] || ''}#{data.has_key?('firstName') ? ' ' : ''}#{data['lastName'] || ''}",
25
- :first_name_length => data['length'] || (data.has_key?('firstName') ? data['firstName'].length : 0),
26
- :email => data['email'],
27
- :currency => data['currency'],
28
- :payment_method_id => data['paymentMethodId'],
29
- :timezone => data['timeZone'] || data['time_zone'] || data['timezone'],
30
- :address1 => data['address1'],
31
- :address2 => data['address2'],
32
- :company => data['company'],
33
- :state => data['state'],
34
- :country => data['country'],
35
- :phone => data['phone'],
36
- :bill_cycle_day => data['billCycleDayLocal'],
37
- :balance => data['accountBalance'],
38
- :cba => data['accountCBA'],
39
- :is_notified_for_invoices => data['isNotifiedForInvoices'])
3
+ def self.find_by_id_or_key(account_id_or_key, with_balance = false, with_balance_and_cba = false, options = {})
4
+ if account_id_or_key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
5
+ begin
6
+ find_by_id(account_id_or_key, with_balance, with_balance_and_cba, options)
7
+ rescue => e
8
+ begin
9
+ # account_id_or_key looked like an id, but maybe it's an external key (this will happen in tests)?
10
+ find_by_external_key(account_id_or_key, with_balance, with_balance_and_cba, options)
11
+ rescue => f
12
+ # Nope - raise the initial exception
13
+ raise e
14
+ end
15
+ end
16
+ else
17
+ find_by_external_key(account_id_or_key, with_balance, with_balance_and_cba, options)
18
+ end
40
19
  end
41
20
 
42
- def to_param
43
- @account_id
21
+ def self.list_or_search(search_key = nil, offset = 0, limit = 10, options = {})
22
+ if search_key.present?
23
+ find_in_batches_by_search_key(search_key, offset, limit, false, false, options)
24
+ else
25
+ find_in_batches(offset, limit, false, false, options)
26
+ end
44
27
  end
45
28
 
46
29
  def balance_to_money
47
- Kaui::Base.to_money(balance.abs, currency)
30
+ Kaui::Base.to_money(account_balance.abs, currency)
48
31
  end
49
32
 
50
33
  def cba_to_money
51
- Kaui::Base.to_money(cba.abs, currency)
34
+ Kaui::Base.to_money(account_cba.abs, currency)
52
35
  end
53
36
  end
@@ -1,54 +1,17 @@
1
- class Kaui::AccountEmail < Kaui::Base
2
- define_attr :account_id
3
- define_attr :email
1
+ class Kaui::AccountEmail < KillBillClient::Model::AccountEmailAttributes
4
2
 
5
- def initialize(attributes = {})
6
- super(attributes)
7
-
8
- # We make Rails believe the id for the AccountEmail is actually the account_id
9
- # (this is handy for routes and form_for helpers). Make sure though to mark it as not persisted
10
- # (even though @id is not nil)
11
- @persisted = false
12
- end
13
-
14
- def self.where(conditions, options_for_klient = {})
15
- account_emails = Kaui::KillbillHelper.get_account_emails(conditions[:account_id], options_for_klient) || []
16
- return account_emails.sort unless conditions[:email].present?
17
-
18
- account_emails.each do |account_email|
19
- return account_email if account_email.email == conditions[:email]
20
- end
21
- []
22
- end
23
-
24
- def save(user = nil, reason = nil, comment = nil, options_for_klient = {})
25
- begin
26
- Kaui::KillbillHelper.add_account_email(self, user, reason, comment, options_for_klient)
27
- true
28
- rescue => e
29
- @errors.add(:save, "Error while trying to add an account email: #{e}")
30
- false
31
- end
32
- end
33
-
34
- def destroy(user = nil, reason = nil, comment = nil, options_for_klient = {})
35
- begin
36
- Kaui::KillbillHelper.remove_account_email(self, user, reason, comment, options_for_klient)
37
- true
38
- rescue => e
39
- @errors.add(:destroy, "Error while trying to delete an account email: #{e}")
40
- end
41
- end
42
-
43
- def id
44
- to_param
45
- end
46
-
47
- def to_param
48
- @account_id
3
+ def self.find_all_sorted_by_account_id(account_id, audit = 'NONE', options = {})
4
+ emails = Kaui::Account.new(:account_id => account_id).emails(audit, options)
5
+ emails.map { |email| Kaui::AccountEmail.new(email.to_hash) }.sort
49
6
  end
50
7
 
51
8
  def <=>(account_email)
52
- @email <=> account_email.email
9
+ if account_email.nil?
10
+ -1
11
+ elsif account_email.email.nil?
12
+ email.nil? ? 0 : 1
13
+ else
14
+ email.to_s <=> account_email.email.to_s
15
+ end
53
16
  end
54
17
  end
@@ -1,6 +1,2 @@
1
- class Kaui::AccountTimeline < Kaui::Base
2
- has_one :account, Kaui::Account
3
- has_many :payments, Kaui::Payment
4
- has_many :bundles, Kaui::Bundle
5
- has_many :invoices, Kaui::Invoice
1
+ class Kaui::AccountTimeline < KillBillClient::Model::AccountTimeline
6
2
  end
@@ -1,17 +1,4 @@
1
- class Kaui::AuditLog < Kaui::Base
2
- define_attr :change_date
3
- define_attr :change_type
4
- define_attr :changed_by
5
- define_attr :comments
6
- define_attr :reason_code
7
-
8
- def initialize(data = {})
9
- super(:change_date => data['changeDate'],
10
- :change_type => data['changeType'],
11
- :changed_by => data['changedBy'],
12
- :comments => data['comments'],
13
- :reason_code => data['reasonCode'])
14
- end
1
+ class Kaui::AuditLog
15
2
 
16
3
  def self.description(log)
17
4
  if log.changed_by.present?
@@ -25,5 +12,4 @@ class Kaui::AuditLog < Kaui::Base
25
12
  end
26
13
  end
27
14
  end
28
-
29
15
  end
@@ -1,135 +1,10 @@
1
1
  class Kaui::Base
2
- include Kaui::RailsMethods
3
-
4
- attr_reader :errors
5
-
6
- @@attribute_names = {}
7
-
8
- def self.define_attr(*args)
9
- send("attr_accessor".to_sym, *args)
10
- args.each do |attr_name|
11
- @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
12
- @@attribute_names[self.name][attr_name.to_sym] = { :cardinality => :one }
13
- end
14
- end
15
- define_attr :audit_logs
16
-
17
- def self.has_one(attr_name, type = nil)
18
- send("attr_accessor".to_sym, attr_name)
19
- @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
20
- @@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :one }
21
- end
22
-
23
- def self.has_many(attr_name, type = nil)
24
- send("attr_accessor".to_sym, attr_name)
25
- @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
26
- @@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :many }
27
- end
28
-
29
- def self.from_json(json_text)
30
- json_hash = ActiveSupport::JSON.decode(json_text)
31
- return self.new(json_hash)
32
- end
33
-
34
- def initialize(attributes = {})
35
- # We can come here either from the Killbill API (attributes will be a JSON hash,
36
- # with camel cased keys) or from e.g. Rails forms (attributes will be a hash with
37
- # snake cased keys).
38
- # Either way, convert the keys to snake case as our attributes are snake cased.
39
- self.attributes = Kaui::Base.convert_hash_keys(attributes)
40
-
41
- # Make has_many associations return [] instead of nil by default
42
- @@attribute_names[self.class.name].each do |name, type_desc|
43
- val = send("#{name}")
44
- send("#{name}=", []) if val.nil? and !type_desc.nil? and type_desc[:cardinality] == :many
45
- end
46
-
47
- # Mark the record as persisted if we have an id
48
- @persisted = to_param.present?
49
-
50
- # Errors for form validations
51
- @errors = ActiveModel::Errors.new(self)
52
- end
53
-
54
- def attributes=(values)
55
- values.each do |name, value|
56
- type_desc = @@attribute_names[self.class.name][name.to_sym]
57
- unless type_desc.nil?
58
- type = type_desc[:type]
59
- if type_desc[:cardinality] == :many && !type.nil? && value.is_a?(Array)
60
- newValue = []
61
- value.each do |val|
62
- if val.is_a?(Hash)
63
- newValue << type.to_s.constantize.new(val)
64
- else
65
- newValue << val
66
- end
67
- end
68
- value = newValue
69
- elsif type_desc[:cardinality] == :one && !type.nil? && value.is_a?(Hash)
70
- value = type.to_s.constantize.new(value)
71
- end
72
- end
73
- begin
74
- send("#{name}=", value)
75
- rescue NoMethodError => e
76
- # Ignore new API fields
77
- end
78
- end
79
- end
80
-
81
- def to_hash
82
- result = {}
83
- @@attribute_names[self.class.name].each do |name, type_desc|
84
- val = send("#{name}")
85
- unless val.nil? || type_desc.nil?
86
- type = type_desc[:type]
87
- if type_desc[:cardinality] == :many && !type.nil? && val.is_a?(Array)
88
- newVal = []
89
- val.each do |curVal|
90
- newVal << curVal.as_json(:root => false)
91
- end
92
- val = newVal
93
- elsif type_desc[:cardinality] == :one && !type.nil?
94
- val = val.as_json(:root => false)
95
- end
96
- result[name.to_s] = val
97
- end
98
- end
99
- result
100
- end
101
2
 
102
3
  def self.to_money(amount, currency)
103
4
  begin
104
5
  return Money.new(amount.to_f * 100, currency)
105
6
  rescue => e
106
7
  end if currency.present?
107
- Money.new(amount.to_f * 100, "USD")
8
+ Money.new(amount.to_f * 100, 'USD')
108
9
  end
109
-
110
- def self.camelize(value)
111
- case value
112
- when Array
113
- value.map {|v| camelize(v) }
114
- when Hash
115
- value.inject({}) {|result, (k, v)| result.merge!(k.to_s.camelize(:lower).to_sym => camelize(v)) }
116
- else
117
- value
118
- end
119
- end
120
-
121
- # Convert a hash with camel cased keys into a hash with snake cased keys:
122
- #
123
- # { :accountId => 12 } becomes { :account_id => 12 }
124
- #
125
- def self.convert_hash_keys(value)
126
- case value
127
- when Hash
128
- Hash[value.map { |k, v| [k.to_s.underscore.to_sym, convert_hash_keys(v)] }]
129
- else
130
- value
131
- end
132
- end
133
-
134
-
135
10
  end
@@ -1,16 +1,49 @@
1
- class Kaui::Bundle < Kaui::Base
2
- define_attr :account_id
3
- define_attr :external_key
4
- define_attr :bundle_id
5
-
6
- has_many :subscriptions, Kaui::Subscription
7
- has_many :audit_logs, Kaui::AuditLog
8
-
9
- def initialize(data = {})
10
- super(:external_key => data['externalKey'],
11
- :bundle_id => data['bundleId'],
12
- :account_id => data['accountId'],
13
- :subscriptions => data['subscriptions'],
14
- :audit_logs => data['auditLogs'])
1
+ class Kaui::Bundle < KillBillClient::Model::Bundle
2
+
3
+ def self.find_by_id_or_key(bundle_id_or_key, account_id = nil, options = {})
4
+ if bundle_id_or_key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
5
+ find_by_id(bundle_id_or_key, options)
6
+ else
7
+ if account_id.blank?
8
+ # Return the active one
9
+ find_by_external_key(bundle_id_or_key, options)
10
+ else
11
+ # Return active and inactive ones
12
+ bundles = find_all_by_account_id_and_external_key(account_id, bundle_id_or_key, options)
13
+ get_active_bundle_or_latest_created(bundles)
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.list_or_search(search_key = nil, offset = 0, limit = 10, options = {})
19
+ if search_key.present?
20
+ find_in_batches_by_search_key(search_key, offset, limit, options)
21
+ else
22
+ find_in_batches(offset, limit, options)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def self.get_active_bundle_or_latest_created(bundles)
29
+ return nil if bundles.empty?
30
+
31
+ latest_start_date = nil
32
+ latest_bundle = nil
33
+
34
+ bundles.each do |b|
35
+ b.subscriptions.each do |s|
36
+ if s.product_category != 'ADD_ON'
37
+ if latest_start_date.nil? || latest_start_date < s.start_date
38
+ latest_start_date = s.start_date
39
+ latest_bundle = b
40
+ end
41
+
42
+ return b if s.cancelled_date.nil? || s.cancelled_date > Time.now
43
+ end
44
+ end
45
+ end
46
+
47
+ latest_bundle
15
48
  end
16
49
  end
@@ -0,0 +1,2 @@
1
+ class Kaui::Catalog < KillBillClient::Model::Catalog
2
+ end
@@ -1,37 +1,25 @@
1
- class Kaui::Chargeback < Kaui::Base
2
- SAMPLE_REASON_CODES = [ "400 - Canceled Recurring Transaction",
3
- "401 - Cardholder Disputes Quality of Goods or Services",
4
- "402 - Cardholder Does Not Recognize Transaction",
5
- "403 - Cardholder Request Due to Dispute",
6
- "404 - Credit Not Processed",
7
- "405 - Duplicate Processing",
8
- "406 - Fraud Investigation",
9
- "407 - Fraudulent Transaction - Card Absent Environment",
10
- "408 - Incorrect Transaction Amount or Account Number",
11
- "409 - No Cardholder Authorization",
12
- "410 - Non receipt of Merchandise",
13
- "411 - Not as Described or Defective Merchandise",
14
- "412 - Recurring Payment",
15
- "413 - Request for Copy Bearing Signature",
16
- "414 - Requested Transaction Data Not Received",
17
- "415 - Services Not Provided or Merchandise not Received",
18
- "416 - Transaction Amount Differs",
19
- "417 - Validity Challenged",
20
- "418 - Unauthorized Payment",
21
- "419 - Unauthorized Claim",
22
- "420 - Not as Described",
23
- "499 - OTHER" ]
1
+ class Kaui::Chargeback < KillBillClient::Model::Transaction
24
2
 
25
- define_attr :payment_id
26
- define_attr :chargeback_amount
27
- define_attr :requested_date
28
- define_attr :effective_date
29
- define_attr :reason
30
-
31
- has_many :audit_logs, Kaui::AuditLog
32
-
33
- # TODO - fix chargeback json to return the currency
34
- def chargeback_amount_to_money(currency)
35
- Kaui::Base.to_money(chargeback_amount, currency)
36
- end
37
- end
3
+ SAMPLE_REASON_CODES = ['400 - Canceled Recurring Transaction',
4
+ '401 - Cardholder Disputes Quality of Goods or Services',
5
+ '402 - Cardholder Does Not Recognize Transaction',
6
+ '403 - Cardholder Request Due to Dispute',
7
+ '404 - Credit Not Processed',
8
+ '405 - Duplicate Processing',
9
+ '406 - Fraud Investigation',
10
+ '407 - Fraudulent Transaction - Card Absent Environment',
11
+ '408 - Incorrect Transaction Amount or Account Number',
12
+ '409 - No Cardholder Authorization',
13
+ '410 - Non receipt of Merchandise',
14
+ '411 - Not as Described or Defective Merchandise',
15
+ '412 - Recurring Payment',
16
+ '413 - Request for Copy Bearing Signature',
17
+ '414 - Requested Transaction Data Not Received',
18
+ '415 - Services Not Provided or Merchandise not Received',
19
+ '416 - Transaction Amount Differs',
20
+ '417 - Validity Challenged',
21
+ '418 - Unauthorized Payment',
22
+ '419 - Unauthorized Claim',
23
+ '420 - Not as Described',
24
+ '499 - OTHER']
25
+ end
@@ -1,21 +1,6 @@
1
- class Kaui::Credit < Kaui::Base
2
- SAMPLE_REASON_CODES = [ "100 - Courtesy",
3
- "101 - Billing Error",
4
- "199 - OTHER" ]
1
+ class Kaui::Credit < KillBillClient::Model::Credit
5
2
 
6
- define_attr :account_id
7
- define_attr :invoice_id
8
- define_attr :credit_amount
9
- define_attr :requested_date
10
- define_attr :effective_date
11
- define_attr :comment
12
- define_attr :reason
13
-
14
- def initialize(data = {})
15
- super(:account_id => data['accountId'] || data['account_id'],
16
- :invoice_id => data['invoiceId'] || data['invoice_id'],
17
- :credit_amount => data['creditAmount'] || data['credit_amount'],
18
- :requested_date => data['requestedDate'] || data['requested_date'],
19
- :effective_date => data['effectiveDate'] || data['effective_date'])
20
- end
21
- end
3
+ SAMPLE_REASON_CODES = ['100 - Courtesy',
4
+ '101 - Billing Error',
5
+ '199 - OTHER']
6
+ end
@@ -1,6 +1,10 @@
1
- class Kaui::CustomField < Kaui::Base
1
+ class Kaui::CustomField < KillBillClient::Model::CustomField
2
2
 
3
- define_attr :name
4
- define_attr :value
5
-
6
- end
3
+ def self.list_or_search(search_key = nil, offset = 0, limit = 10, options = {})
4
+ if search_key.present?
5
+ find_in_batches_by_search_key(search_key, offset, limit, options)
6
+ else
7
+ find_in_batches(offset, limit, options)
8
+ end
9
+ end
10
+ end