effective_orders 2.2.4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +124 -84
  4. data/app/assets/javascripts/effective_orders/customers.js.coffee +39 -0
  5. data/app/assets/javascripts/effective_orders/providers/{stripe_charges.js.coffee → stripe.js.coffee} +15 -13
  6. data/app/assets/javascripts/effective_orders/subscriptions.js.coffee +73 -0
  7. data/app/assets/stylesheets/effective_orders.scss +2 -1
  8. data/app/assets/stylesheets/effective_orders/_order.scss +16 -8
  9. data/app/assets/stylesheets/effective_orders/_subscriptions.scss +14 -0
  10. data/app/controllers/admin/customers_controller.rb +11 -8
  11. data/app/controllers/admin/order_items_controller.rb +4 -8
  12. data/app/controllers/admin/orders_controller.rb +133 -87
  13. data/app/controllers/effective/carts_controller.rb +18 -8
  14. data/app/controllers/effective/concerns/purchase.rb +39 -0
  15. data/app/controllers/effective/customers_controller.rb +43 -0
  16. data/app/controllers/effective/orders_controller.rb +73 -119
  17. data/app/controllers/effective/providers/app_checkout.rb +3 -1
  18. data/app/controllers/effective/providers/ccbill.rb +4 -6
  19. data/app/controllers/effective/providers/cheque.rb +20 -11
  20. data/app/controllers/effective/providers/free.rb +33 -0
  21. data/app/controllers/effective/providers/mark_as_paid.rb +33 -0
  22. data/app/controllers/effective/providers/moneris.rb +9 -17
  23. data/app/controllers/effective/providers/paypal.rb +4 -6
  24. data/app/controllers/effective/providers/pretend.rb +4 -4
  25. data/app/controllers/effective/providers/refund.rb +39 -0
  26. data/app/controllers/effective/providers/stripe.rb +19 -40
  27. data/app/controllers/effective/providers/stripe_connect.rb +2 -6
  28. data/app/controllers/effective/webhooks_controller.rb +44 -95
  29. data/app/datatables/effective_customers_datatable.rb +21 -29
  30. data/app/datatables/effective_order_items_datatable.rb +77 -79
  31. data/app/datatables/effective_orders_datatable.rb +67 -57
  32. data/app/helpers/effective_carts_helper.rb +17 -14
  33. data/app/helpers/effective_orders_helper.rb +40 -56
  34. data/app/helpers/effective_paypal_helper.rb +3 -3
  35. data/app/helpers/effective_stripe_helper.rb +47 -18
  36. data/app/helpers/effective_subscriptions_helper.rb +79 -0
  37. data/app/mailers/effective/orders_mailer.rb +125 -2
  38. data/app/models/concerns/acts_as_purchasable.rb +23 -33
  39. data/app/models/concerns/acts_as_subscribable.rb +68 -0
  40. data/app/models/concerns/acts_as_subscribable_buyer.rb +22 -0
  41. data/app/models/effective/cart.rb +53 -24
  42. data/app/models/effective/cart_item.rb +6 -12
  43. data/app/models/effective/customer.rb +51 -54
  44. data/app/models/effective/order.rb +160 -147
  45. data/app/models/effective/order_item.rb +18 -21
  46. data/app/models/effective/product.rb +7 -7
  47. data/app/models/effective/providers/ccbill_postback.rb +1 -1
  48. data/app/models/effective/providers/stripe_charge.rb +8 -19
  49. data/app/models/effective/subscripter.rb +230 -0
  50. data/app/models/effective/subscription.rb +27 -76
  51. data/app/models/effective/tax_rate_calculator.rb +10 -7
  52. data/app/views/admin/customers/_actions.html.haml +1 -2
  53. data/app/views/admin/customers/index.html.haml +1 -1
  54. data/app/views/admin/customers/show.html.haml +6 -0
  55. data/app/views/admin/orders/_actions.html.haml +9 -7
  56. data/app/views/admin/orders/_form.html.haml +11 -7
  57. data/app/views/admin/orders/_order_actions.html.haml +2 -1
  58. data/app/views/admin/orders/_order_item_fields.html.haml +1 -1
  59. data/app/views/admin/orders/edit.html.haml +4 -0
  60. data/app/views/admin/orders/index.html.haml +1 -4
  61. data/app/views/admin/orders/new.html.haml +1 -1
  62. data/app/views/admin/orders/show.html.haml +5 -6
  63. data/app/views/effective/carts/_cart.html.haml +2 -2
  64. data/app/views/effective/carts/show.html.haml +2 -2
  65. data/app/views/effective/customers/_customer.html.haml +152 -0
  66. data/app/views/effective/customers/_fields.html.haml +12 -0
  67. data/app/views/effective/customers/_form.html.haml +13 -0
  68. data/app/views/effective/customers/edit.html.haml +3 -0
  69. data/app/views/effective/orders/_checkout_step1.html.haml +8 -15
  70. data/app/views/effective/orders/_checkout_step2.html.haml +34 -21
  71. data/app/views/effective/orders/_order.html.haml +8 -9
  72. data/app/views/effective/orders/_order_actions.html.haml +7 -8
  73. data/app/views/effective/orders/_order_header.html.haml +1 -1
  74. data/app/views/effective/orders/_order_items.html.haml +11 -5
  75. data/app/views/effective/orders/_order_note.html.haml +4 -7
  76. data/app/views/effective/orders/_orders_table.html.haml +26 -26
  77. data/app/views/effective/orders/app_checkout/_form.html.haml +2 -2
  78. data/app/views/effective/orders/ccbill/_form.html.haml +1 -1
  79. data/app/views/effective/orders/cheque/_form.html.haml +3 -1
  80. data/app/views/effective/orders/declined.html.haml +1 -1
  81. data/app/views/effective/orders/{checkout_step1.html.haml → edit.html.haml} +0 -0
  82. data/app/views/effective/orders/free/_form.html.haml +4 -0
  83. data/app/views/effective/orders/index.html.haml +2 -4
  84. data/app/views/effective/orders/mark_as_paid/_form.html.haml +32 -0
  85. data/app/views/effective/orders/moneris/_form.html.haml +6 -6
  86. data/app/views/effective/orders/{checkout_step2.html.haml → new.html.haml} +1 -1
  87. data/app/views/effective/orders/paypal/_form.html.haml +2 -2
  88. data/app/views/effective/orders/pretend/_form.html.haml +2 -2
  89. data/app/views/effective/orders/purchased.html.haml +3 -0
  90. data/app/views/effective/orders/refund/_form.html.haml +32 -0
  91. data/app/views/effective/orders/show.html.haml +4 -1
  92. data/app/views/effective/orders/stripe/_form.html.haml +5 -5
  93. data/app/views/effective/orders_mailer/subscription_canceled.html.haml +9 -0
  94. data/app/views/effective/orders_mailer/subscription_payment_failed.html.haml +9 -0
  95. data/app/views/effective/orders_mailer/subscription_payment_succeeded.html.haml +9 -0
  96. data/app/views/effective/orders_mailer/subscription_trial_expired.html.haml +5 -0
  97. data/app/views/effective/orders_mailer/subscription_trial_expiring.html.haml +7 -0
  98. data/app/views/effective/subscriptions/_fields.html.haml +16 -0
  99. data/app/views/effective/subscriptions/_plan.html.haml +21 -0
  100. data/app/views/layouts/effective_orders_mailer_layout.html.haml +6 -8
  101. data/config/effective_orders.rb +41 -20
  102. data/config/routes.rb +48 -48
  103. data/db/migrate/01_create_effective_orders.rb.erb +19 -5
  104. data/lib/effective_orders.rb +78 -42
  105. data/lib/effective_orders/engine.rb +36 -82
  106. data/lib/effective_orders/version.rb +1 -1
  107. data/lib/generators/effective_orders/install_generator.rb +2 -2
  108. data/lib/generators/templates/effective_orders_mailer_preview.rb +39 -4
  109. data/lib/tasks/effective_orders_tasks.rake +42 -0
  110. data/spec/controllers/carts_controller_spec.rb +1 -1
  111. data/spec/controllers/moneris_orders_controller_spec.rb +4 -4
  112. data/spec/controllers/orders_controller_spec.rb +4 -4
  113. data/spec/controllers/stripe_orders_controller_spec.rb +2 -2
  114. data/spec/controllers/webhooks_controller_spec.rb +1 -1
  115. data/spec/dummy/config/initializers/effective_orders.rb +1 -7
  116. data/spec/dummy/db/schema.rb +1 -0
  117. data/spec/dummy/db/test.sqlite3 +0 -0
  118. data/spec/dummy/log/test.log +3 -0
  119. data/spec/models/acts_as_purchasable_spec.rb +0 -56
  120. data/spec/models/customer_spec.rb +3 -3
  121. data/spec/models/order_spec.rb +2 -2
  122. data/spec/spec_helper.rb +1 -1
  123. data/spec/support/factories.rb +2 -1
  124. metadata +37 -49
  125. data/active_admin/effective_carts.rb +0 -14
  126. data/active_admin/effective_orders.rb +0 -112
  127. data/app/assets/javascripts/effective_orders/providers/stripe_subscriptions.js.coffee +0 -28
  128. data/app/controllers/concerns/acts_as_active_admin_controller.rb +0 -69
  129. data/app/controllers/effective/subscriptions_controller.rb +0 -126
  130. data/app/models/effective/datatables/customers.rb +0 -40
  131. data/app/models/effective/datatables/order_items.rb +0 -101
  132. data/app/models/effective/datatables/orders.rb +0 -91
  133. data/app/models/inputs/price_field.rb +0 -63
  134. data/app/models/inputs/price_form_input.rb +0 -7
  135. data/app/models/inputs/price_formtastic_input.rb +0 -9
  136. data/app/models/inputs/price_input.rb +0 -19
  137. data/app/models/inputs/price_simple_form_input.rb +0 -8
  138. data/app/views/admin/orders/_form_mark_as_paid.html.haml +0 -33
  139. data/app/views/admin/orders/_order_payment_details.html.haml +0 -5
  140. data/app/views/admin/orders/mark_as_paid.html.haml +0 -7
  141. data/app/views/effective/orders/stripe/_subscription_fields.html.haml +0 -7
  142. data/app/views/effective/subscriptions/index.html.haml +0 -22
  143. data/app/views/effective/subscriptions/new.html.haml +0 -9
  144. data/app/views/effective/subscriptions/show.html.haml +0 -49
  145. data/db/upgrade/02_upgrade_effective_orders_from03x.rb.erb +0 -29
  146. data/db/upgrade/03_upgrade_effective_orders_from1x.rb.erb +0 -98
  147. data/db/upgrade/upgrade_price_column_on_table.rb.erb +0 -17
  148. data/lib/generators/effective_orders/upgrade_from03x_generator.rb +0 -31
  149. data/lib/generators/effective_orders/upgrade_from1x_generator.rb +0 -27
  150. data/lib/generators/effective_orders/upgrade_price_column_generator.rb +0 -30
@@ -1,63 +0,0 @@
1
- module Inputs
2
- class PriceField
3
- delegate :content_tag, :text_field_tag, :hidden_field_tag, :to => :@template
4
-
5
- def initialize(object, object_name, template, method, opts)
6
- @object = object
7
- @object_name = object_name
8
- @template = template
9
- @method = method
10
- @opts = opts
11
- end
12
-
13
- def to_html
14
- content_tag(:div, :class => 'input-group') do
15
- content_tag(:span, '$', :class => 'input-group-addon') +
16
- text_field_tag(field_name, value, options) +
17
- hidden_field_tag(field_name, price, :id => price_field)
18
- end
19
- end
20
-
21
- private
22
-
23
- # These two are for the text field
24
-
25
- def field_name
26
- @object_name + "[#{@method}]"
27
- end
28
-
29
- def value
30
- val = @object.send(@method) || 0
31
- val.kind_of?(Integer) ? ('%.2f' % (val / 100.0)) : ('%.2f' % val)
32
- end
33
-
34
- # These two are for the hidden input
35
- def price_field
36
- "#{field_name.parameterize.gsub('-', '_')}_value_as_integer"
37
- end
38
-
39
- def price
40
- val = @object.send(@method) || 0
41
- val.kind_of?(Integer) ? val : (val * 100.0).to_i
42
- end
43
-
44
- def options
45
- (@opts || {}).tap do |options|
46
- if options[:class].blank?
47
- options[:class] = 'numeric'
48
- elsif options[:class].kind_of?(Array)
49
- options[:class] << :numeric
50
- elsif options[:class].kind_of?(String)
51
- options[:class] << ' numeric'
52
- end
53
-
54
- options[:pattern] = "[0-9]+(\\.[0-9][0-9]){1}"
55
- options[:maxlength] = 14
56
- options[:title] = 'a price like 19.99, 10000.99 or 0.00'
57
- options[:onchange] = "console.log(this.value);"
58
- options[:onchange] = "document.querySelectorAll('##{price_field}')[0].setAttribute('value', ((parseFloat(this.value) || 0.00) * 100.0).toFixed(0));"
59
- end
60
- end
61
- end
62
- end
63
-
@@ -1,7 +0,0 @@
1
- module Inputs
2
- module PriceFormInput
3
- def price_field(method, opts = {})
4
- Inputs::PriceField.new(@object, @object_name, @template, method, opts).to_html
5
- end
6
- end
7
- end
@@ -1,9 +0,0 @@
1
- if defined?(Formtastic)
2
- class PriceFormtasticInput < Formtastic::Inputs::NumberInput
3
- def to_html
4
- input_wrapping do
5
- label_html << Inputs::PriceField.new(@object, @object_name, @template, @method, @options).to_html
6
- end
7
- end
8
- end
9
- end
@@ -1,19 +0,0 @@
1
- # This allows the app to call f.input :something, :as => :price
2
- # in either Formtastic or SimpleForm, but not both at the same time
3
-
4
- if defined?(SimpleForm)
5
- class PriceInput < SimpleForm::Inputs::NumericInput
6
- def input(wrapper_options = nil)
7
- options = merge_wrapper_options(input_html_options, wrapper_options)
8
- Inputs::PriceField.new(object, object_name, template, attribute_name, options).to_html
9
- end
10
- end
11
- elsif defined?(Formtastic)
12
- class PriceInput < Formtastic::Inputs::NumberInput
13
- def to_html
14
- input_wrapping do
15
- label_html << Inputs::PriceField.new(@object, @object_name, @template, @method, @options).to_html
16
- end
17
- end
18
- end
19
- end
@@ -1,8 +0,0 @@
1
- if defined?(SimpleForm)
2
- class PriceSimpleFormInput < SimpleForm::Inputs::NumericInput
3
- def input(wrapper_options = nil)
4
- options = merge_wrapper_options(input_html_options, wrapper_options)
5
- Inputs::PriceField.new(object, object_name, template, attribute_name, options).to_html
6
- end
7
- end
8
- end
@@ -1,33 +0,0 @@
1
- = simple_form_for [:admin, order], (EffectiveOrders.admin_simple_form_options || {}).merge(url: effective_orders.mark_as_paid_admin_order_path(order), method: :post) do |f|
2
- %h2 Payment Details
3
-
4
- - if order.purchased?
5
- .alert.alert-warning Warning: This order has already been purchased
6
- %br
7
-
8
- = f.input :payment_provider,
9
- as: (defined?(EffectiveFormInputs) ? :effective_select : :select),
10
- collection: (EffectiveOrders.payment_providers + EffectiveOrders.other_payment_providers).sort,
11
- required: true
12
-
13
- = f.input :payment_card,
14
- label: 'Payment card type, cheque or transaction number',
15
- placeholder: 'visa',
16
- hint: 'Full credit card numbers should not be entered here, or anywhere.'
17
-
18
- = f.input :payment,
19
- as: :text,
20
- label: 'Additional details',
21
- input_html: { value: f.object.payment.kind_of?(Hash) ? f.object.payment[:details] : f.object.payment.presence }
22
-
23
- = f.input :send_mark_as_paid_email_to_buyer,
24
- as: :boolean,
25
- label: 'Yes, send an email receipt to the buyer.',
26
- value: (f.object.send_mark_as_paid_email_to_buyer.nil? ? EffectiveOrders.mailer[:send_order_receipts_when_mark_as_paid_by_admin] : f.object.send_mark_as_paid_email_to_buyer?)
27
-
28
- .effective-order-note-to-buyer
29
- %h2 Note to Buyer
30
- = f.input :note_to_buyer, label: false, hint: 'This is displayed to the buyer on all order receipts.'
31
-
32
- = render partial: '/admin/orders/order_actions', locals: { order: @order, form: f, f: f }
33
-
@@ -1,5 +0,0 @@
1
- - if order.payment.present?
2
- .effective-order-payment-details
3
- %h2 Payment Provider Details
4
- = tableize_order_payment(order.payment[:details] || order.payment, class: 'table')
5
-
@@ -1,7 +0,0 @@
1
- %h1.effective-admin-heading= @page_title
2
-
3
- %p Mark the following order as paid:
4
-
5
- = render partial: 'effective/orders/order', locals: {order: @order, no_order_actions: true}
6
-
7
- = render partial: 'admin/orders/form_mark_as_paid', locals: { order: @order }
@@ -1,7 +0,0 @@
1
- .effective-stripe-subscription
2
- = form.simple_fields_for :order_items, subscription do |fa|
3
- = fa.input_field :class, :as => :hidden
4
- - if subscription.stripe_coupon_id.blank? || subscription.errors[:stripe_coupon_id].present?
5
- = fa.input :stripe_coupon_id, :label => 'Coupon&nbsp;Code'.html_safe, :placeholder => 'Enter Coupon Code'
6
- - else
7
- = fa.input :stripe_coupon_id, :as => :hidden, :label => 'Coupon&nbsp;Code'.html_safe, :placeholder => 'Enter Coupon Code'
@@ -1,22 +0,0 @@
1
- %h1.effective-heading= @page_title
2
-
3
- %p.text-right.effective-actions
4
- = link_to 'New Subscription', effective_orders.new_subscription_path, class: 'btn btn-primary'
5
-
6
- - if @subscriptions.present?
7
- %p You have the following subscriptions:
8
- %ul
9
- - @subscriptions.each do |subscription|
10
- %li
11
- = subscription.title.html_safe
12
- = '('
13
- = link_to 'details', effective_orders.subscription_path(subscription.stripe_plan_id)
14
- = '-'
15
- = link_to 'unsubscribe', effective_orders.subscription_path(subscription.stripe_plan_id), :data => {:method => :delete, :confirm => 'This will cancel your subscription. Are you sure? This cannot be undone.'}
16
- = ')'
17
-
18
- - if @active_stripe_subscription.present?
19
- %p Your current subscription will be renewed automatically at #{ Time.zone.at(@active_stripe_subscription.current_period_end).strftime('%d-%b-%Y %H:%M') }.
20
-
21
- - else
22
- %p You have no subscriptions
@@ -1,9 +0,0 @@
1
- %h1.effective-heading= @page_title
2
-
3
- - if @plans.present?
4
- = simple_form_for(@subscription, (EffectiveOrders.simple_form_options || {}).merge(:url => effective_orders.subscriptions_path, :method => :post)) do |f|
5
- = f.input :stripe_plan_id, :as => :select, :collection => stripe_plans_collection(@plans), :include_blank => false, :label => 'Plan'
6
- = f.input :stripe_coupon_id, :placeholder => 'Coupon Code', :label => 'Coupon'
7
- = f.submit 'Add to Cart', :class => 'btn btn-primary'
8
- - else
9
- %p No available Subscriptions
@@ -1,49 +0,0 @@
1
- %h1.effective-heading
2
- = @plan.name
3
- Subscription
4
- details
5
-
6
- %p= stripe_plan_description(@plan)
7
-
8
- %table.table
9
- %tbody
10
- %tr
11
- %th Started
12
- %td= Time.zone.at(@stripe_subscription.start).strftime("%d-%b-%Y")
13
- - if @stripe_subscription.discount.present?
14
- %tr
15
- %th Coupon
16
- %td= stripe_coupon_description(@stripe_subscription.discount.coupon)
17
- %tr
18
- %th Status
19
- %td= @stripe_subscription.status
20
- %tr
21
- %th Current Period Start
22
- %td= Time.zone.at(@stripe_subscription.current_period_start).strftime("%d-%b-%Y")
23
- %tr
24
- %th Current Period End
25
- %td= Time.zone.at(@stripe_subscription.current_period_end).strftime("%d-%b-%Y")
26
-
27
- %h2= "#{@plan.interval.chomp('ly')}ly Invoices"
28
-
29
- %table.table
30
- %thead
31
- %tr
32
- %th Date
33
- %th Id
34
- %th Period
35
- %th Charge
36
- %tbody
37
- - @invoices.each do |invoice|
38
- %tr
39
- %td= Time.zone.at(invoice.date).strftime("%d-%b-%Y")
40
- %td= invoice.id
41
- %td
42
- = Time.zone.at(invoice.lines.first.period.start).strftime("%d-%b-%Y")
43
- to
44
- = Time.zone.at(invoice.lines.first.period.end).strftime("%d-%b-%Y")
45
- %td= price_to_currency(invoice.total)
46
-
47
- %h2 Cancel Subscription
48
-
49
- = link_to 'Unsubscribe', effective_orders.subscription_path(@plan.id), :data => {:method => :delete, :confirm => 'This will cancel your subscription. Are you sure? This cannot be undone.', :disable_with => 'Unsubscribing...'}
@@ -1,29 +0,0 @@
1
- class UpgradeEffectiveOrdersFrom03x < ActiveRecord::Migration
2
- def self.up
3
- prices = ActiveRecord::Base.connection.execute("SELECT price from <%= @order_items_table_name.gsub(':', '') %> LIMIT 10").values.flatten
4
- if prices.blank? || prices.any? { |price| price.to_s.include?('.') }
5
- ActiveRecord::Base.connection.execute("UPDATE <%= @order_items_table_name.gsub(':', '') %> O SET price = (O.price * 100.0)")
6
- change_column <%= @order_items_table_name %>, :price, :integer, :default => 0
7
- end
8
-
9
- prices = ActiveRecord::Base.connection.execute("SELECT price from <%= @subscriptions_table_name.gsub(':', '') %> LIMIT 10").values.flatten
10
- if prices.blank? || prices.any? { |price| price.to_s.include?('.') }
11
- ActiveRecord::Base.connection.execute("UPDATE <%= @subscriptions_table_name.gsub(':', '') %> O SET price = (O.price * 100.0)")
12
- change_column <%= @subscriptions_table_name %>, :price, :integer, :default => 0
13
- end
14
- end
15
-
16
- def self.down
17
- prices = ActiveRecord::Base.connection.execute("SELECT price from <%= @order_items_table_name.gsub(':', '') %> LIMIT 10").values.flatten
18
- if prices.none? { |price| price.to_s.include?('.') }
19
- change_column <%= @order_items_table_name %>, :price, :decimal, :precision => 8, :scale => 2, :default => 0.0
20
- ActiveRecord::Base.connection.execute("UPDATE <%= @order_items_table_name.gsub(':', '') %> O SET price = (O.price / 100.0)")
21
- end
22
-
23
- prices = ActiveRecord::Base.connection.execute("SELECT price from <%= @subscriptions_table_name.gsub(':', '') %> LIMIT 10").values.flatten
24
- if prices.none? { |price| price.to_s.include?('.') }
25
- change_column <%= @subscriptions_table_name %>, :price, :decimal, :precision => 8, :scale => 2, :default => 0.0
26
- ActiveRecord::Base.connection.execute("UPDATE <%= @subscriptions_table_name.gsub(':', '') %> O SET price = (O.price / 100.0)")
27
- end
28
- end
29
- end
@@ -1,98 +0,0 @@
1
- class UpgradeEffectiveOrdersFrom1x < ActiveRecord::Migration
2
- def self.up
3
- create_table <%= @products_table_name %> do |t|
4
- t.string :title
5
- t.integer :price, :default => 0
6
- t.boolean :tax_exempt, :default => false
7
-
8
- t.timestamps
9
- end
10
-
11
- add_column <%= @orders_table_name %>, :note, :text
12
- add_column <%= @orders_table_name %>, :note_to_buyer, :text
13
- add_column <%= @orders_table_name %>, :note_internal, :text
14
-
15
- add_column <%= @orders_table_name %>, :payment_provider, :string
16
- add_column <%= @orders_table_name %>, :payment_card, :string
17
-
18
- add_column <%= @orders_table_name %>, :tax_rate, :decimal, :precision => 6, :scale => 3
19
- add_column <%= @orders_table_name %>, :subtotal, :integer
20
- add_column <%= @orders_table_name %>, :tax, :integer
21
- add_column <%= @orders_table_name %>, :total, :integer
22
-
23
- puts 'Resaving all Effective::Order objects to assign new database column values.'
24
-
25
- Effective::Order.reset_column_information
26
-
27
- Effective::Order.find_each do |order|
28
- order.tax_rate = ((order.order_items.first.try(:tax_rate) || 0.0) * 100.0)
29
- update_payment_provider_and_card(order)
30
- order.save(validate: false)
31
- print '.'
32
- end
33
- print "\n" if Effective::Order.any?
34
-
35
- remove_column <%= @order_items_table_name %>, :tax_rate
36
-
37
- puts 'Done'
38
- end
39
-
40
- def self.down
41
- add_column <%= @order_items_table_name %>, :tax_rate, :decimal, :precision => 5, :scale => 3
42
- Effective::OrderItem.reset_column_information
43
- Effective::OrderItem.find_each { |order_item| order_item.update_column(:tax_rate, (order_item.try(:order).tax_rate || 0.0) / 100.0) }
44
-
45
- remove_column <%= @orders_table_name %>, :note
46
- remove_column <%= @orders_table_name %>, :payment_provider
47
- remove_column <%= @orders_table_name %>, :payment_card
48
-
49
- remove_column <%= @orders_table_name %>, :tax_rate
50
- remove_column <%= @orders_table_name %>, :subtotal
51
- remove_column <%= @orders_table_name %>, :tax
52
- remove_column <%= @orders_table_name %>, :total
53
- end
54
-
55
- def update_payment_provider_and_card(order)
56
- return unless order.payment.present?
57
-
58
- payment = HashWithIndifferentAccess.new(order.payment)
59
- charge = HashWithIndifferentAccess.new(payment[:charge])
60
-
61
- # Try to figure out the payment provider
62
- provider =
63
- if payment[:response_code] && payment[:transactionKey]
64
- 'moneris'
65
- elsif charge[:id] && charge[:customer] && charge[:application_fee].present?
66
- 'stripe_connect'
67
- elsif charge[:id] && charge[:customer]
68
- 'stripe'
69
- elsif payment[:payer_email]
70
- 'paypal'
71
- elsif (payment[:details] || '').include?('free')
72
- 'free'
73
- else
74
- 'admin'
75
- end
76
-
77
- card =
78
- case provider
79
- when 'moneris'
80
- payment[:card]
81
- when 'stripe_connect'
82
- (charge['card']['brand'] rescue nil)
83
- when 'stripe'
84
- (charge['card']['brand'] rescue nil)
85
- when 'paypal'
86
- payment[:payment_type]
87
- when 'free'
88
- 'none'
89
- when 'admin'
90
- 'none'
91
- end
92
-
93
- order.payment_provider = provider.presence || 'admin'
94
- order.payment_card = card.presence || 'unknown'
95
- end
96
-
97
-
98
- end
@@ -1,17 +0,0 @@
1
- class UpgradePriceColumnOn<%= @table.capitalize %> < ActiveRecord::Migration
2
- def self.up
3
- prices = ActiveRecord::Base.connection.execute("SELECT <%= @column %> FROM <%= @table %> LIMIT 10").values.flatten
4
- if prices.blank? || prices.any? { |<%= @column %>| <%= @column %>.to_s.include?('.') }
5
- ActiveRecord::Base.connection.execute("UPDATE <%= @table %> O SET <%= @column %> = (O.<%= @column %> * 100.0)")
6
- change_column :<%= @table %>, :<%= @column %>, :integer, :default => 0
7
- end
8
- end
9
-
10
- def self.down
11
- prices = ActiveRecord::Base.connection.execute("SELECT price FROM <%= @table %> LIMIT 10").values.flatten
12
- if prices.none? { |<%= @column %>| <%= @column %>.to_s.include?('.') }
13
- change_column :<%= @table %>, :<%= @column %>, :decimal, :precision => 8, :scale => 2, :default => 0.0
14
- ActiveRecord::Base.connection.execute("UPDATE <%= @table %> O SET <%= @column %> = (O.<%= @column %> / 100.0)")
15
- end
16
- end
17
- end
@@ -1,31 +0,0 @@
1
- module EffectiveOrders
2
- module Generators
3
- class UpgradeFrom03xGenerator < Rails::Generators::Base
4
- include Rails::Generators::Migration
5
-
6
- desc "Upgrade effective_orders from the 0.3.x branch"
7
-
8
- source_root File.expand_path("../../templates", __FILE__)
9
-
10
- def self.next_migration_number(dirname)
11
- if not ActiveRecord::Base.timestamped_migrations
12
- Time.new.utc.strftime("%Y%m%d%H%M%S")
13
- else
14
- "%.3d" % (current_migration_number(dirname) + 1)
15
- end
16
- end
17
-
18
- def create_migration_file
19
- @orders_table_name = ':' + EffectiveOrders.orders_table_name.to_s
20
- @order_items_table_name = ':' + EffectiveOrders.order_items_table_name.to_s
21
- @carts_table_name = ':' + EffectiveOrders.carts_table_name.to_s
22
- @cart_items_table_name = ':' + EffectiveOrders.cart_items_table_name.to_s
23
- @customers_table_name = ':' + EffectiveOrders.customers_table_name.to_s
24
- @subscriptions_table_name = ':' + EffectiveOrders.subscriptions_table_name.to_s
25
- @products_table_name = ':' + EffectiveOrders.products_table_name.to_s
26
-
27
- migration_template ('../' * 3) + 'db/upgrade/02_upgrade_effective_orders_from03x.rb.erb', 'db/migrate/upgrade_effective_orders_from03x.rb'
28
- end
29
- end
30
- end
31
- end
@@ -1,27 +0,0 @@
1
- module EffectiveOrders
2
- module Generators
3
- class UpgradeFrom1xGenerator < Rails::Generators::Base
4
- include Rails::Generators::Migration
5
-
6
- desc 'Upgrade effective_orders from the 1.x branch'
7
-
8
- source_root File.expand_path('../../templates', __FILE__)
9
-
10
- def self.next_migration_number(dirname)
11
- ActiveRecord::Migration.new.next_migration_number(1) #=> “20160114171807"
12
- end
13
-
14
- def create_migration_file
15
- @orders_table_name = ':' + EffectiveOrders.orders_table_name.to_s
16
- @order_items_table_name = ':' + EffectiveOrders.order_items_table_name.to_s
17
- @carts_table_name = ':' + EffectiveOrders.carts_table_name.to_s
18
- @cart_items_table_name = ':' + EffectiveOrders.cart_items_table_name.to_s
19
- @customers_table_name = ':' + EffectiveOrders.customers_table_name.to_s
20
- @subscriptions_table_name = ':' + EffectiveOrders.subscriptions_table_name.to_s
21
- @products_table_name = ':' + EffectiveOrders.products_table_name.to_s
22
-
23
- migration_template ('../' * 3) + 'db/upgrade/03_upgrade_effective_orders_from1x.rb.erb', 'db/migrate/upgrade_effective_orders_from1x.rb'
24
- end
25
- end
26
- end
27
- end