solidus_core 2.0.3 → 2.1.0.beta1
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.
- checksums.yaml +4 -4
- data/README.md +62 -3
- data/app/assets/javascripts/spree.js.coffee.erb +4 -1
- data/app/helpers/spree/base_helper.rb +7 -48
- data/app/models/spree/address.rb +5 -1
- data/app/models/spree/adjustment.rb +3 -3
- data/app/models/spree/app_configuration.rb +13 -0
- data/app/models/spree/calculator.rb +3 -2
- data/app/models/spree/calculator/default_tax.rb +6 -10
- data/app/models/spree/calculator/flat_percent_item_total.rb +0 -4
- data/app/models/spree/calculator/flat_rate.rb +0 -4
- data/app/models/spree/calculator/flexi_rate.rb +0 -4
- data/app/models/spree/calculator/free_shipping.rb +0 -3
- data/app/models/spree/calculator/percent_on_line_item.rb +0 -4
- data/app/models/spree/calculator/percent_per_item.rb +0 -4
- data/app/models/spree/calculator/price_sack.rb +0 -4
- data/app/models/spree/calculator/returns/default_refund_amount.rb +0 -3
- data/app/models/spree/calculator/shipping/flat_percent_item_total.rb +0 -4
- data/app/models/spree/calculator/shipping/flat_rate.rb +0 -4
- data/app/models/spree/calculator/shipping/flexi_rate.rb +0 -4
- data/app/models/spree/calculator/shipping/per_item.rb +0 -4
- data/app/models/spree/calculator/shipping/price_sack.rb +0 -4
- data/app/models/spree/calculator/tiered_flat_rate.rb +0 -4
- data/app/models/spree/calculator/tiered_percent.rb +0 -4
- data/app/models/spree/credit_card.rb +27 -14
- data/app/models/spree/gateway.rb +4 -0
- data/app/models/spree/inventory_unit.rb +2 -0
- data/app/models/spree/line_item.rb +31 -26
- data/app/models/spree/option_type.rb +0 -3
- data/app/models/spree/order.rb +28 -31
- data/app/models/spree/order/checkout.rb +0 -2
- data/app/models/spree/order_contents.rb +0 -45
- data/app/models/spree/order_merger.rb +6 -6
- data/app/models/spree/order_update_attributes.rb +0 -2
- data/app/models/spree/order_updater.rb +91 -13
- data/app/models/spree/payment.rb +9 -2
- data/app/models/spree/payment/processing.rb +15 -9
- data/app/models/spree/payment_method.rb +48 -5
- data/app/models/spree/price.rb +7 -9
- data/app/models/spree/product.rb +1 -25
- data/app/models/spree/promotion.rb +22 -14
- data/app/models/spree/promotion/actions/create_adjustment.rb +12 -1
- data/app/models/spree/promotion/actions/create_item_adjustments.rb +15 -1
- data/app/models/spree/promotion/actions/create_quantity_adjustments.rb +5 -3
- data/app/models/spree/promotion/actions/free_shipping.rb +14 -0
- data/app/models/spree/promotion/rules/taxon.rb +7 -2
- data/app/models/spree/promotion/rules/user_role.rb +43 -0
- data/app/models/spree/promotion_action.rb +19 -2
- data/app/models/spree/promotion_handler/coupon.rb +1 -4
- data/app/models/spree/promotion_handler/free_shipping.rb +22 -17
- data/app/models/spree/promotion_rule_role.rb +6 -0
- data/app/models/spree/property.rb +0 -3
- data/app/models/spree/return_authorization.rb +2 -0
- data/app/models/spree/shipment.rb +5 -21
- data/app/models/spree/shipping_method.rb +23 -2
- data/app/models/spree/shipping_rate.rb +3 -0
- data/app/models/spree/stock/estimator.rb +1 -1
- data/app/models/spree/stock_location.rb +3 -0
- data/app/models/spree/store.rb +7 -0
- data/app/models/spree/tax/item_adjuster.rb +27 -12
- data/app/models/spree/tax/order_adjuster.rb +2 -5
- data/app/models/spree/tax/tax_helpers.rb +4 -8
- data/app/models/spree/tax_rate.rb +1 -15
- data/app/models/spree/taxon.rb +0 -3
- data/app/models/spree/transfer_item.rb +1 -1
- data/app/models/spree/user_class_handle.rb +14 -9
- data/app/models/spree/variant/pricing_options.rb +1 -1
- data/app/models/spree/wallet/add_payment_sources_to_wallet.rb +1 -1
- data/app/models/spree/zone.rb +20 -13
- data/config/locales/en.yml +144 -62
- data/db/migrate/20120831092320_spree_one_two.rb +0 -7
- data/db/migrate/20150723224133_remove_unnecessary_indexes.rb +0 -2
- data/db/migrate/20160924135758_remove_is_default_from_prices.rb +5 -0
- data/db/migrate/20161009141333_remove_currency_from_line_items.rb +5 -0
- data/db/migrate/20161014221052_add_available_to_columns_and_remove_display_on_from_payment_methods.rb +28 -0
- data/db/migrate/20161123154034_add_available_to_users_and_remove_display_on_from_shipping_methods.rb +20 -0
- data/lib/generators/spree/custom_user/templates/authentication_helpers.rb.tt +4 -0
- data/lib/generators/spree/dummy/dummy_generator.rb +0 -2
- data/lib/spree/core.rb +0 -5
- data/lib/spree/core/controller_helpers/pricing.rb +2 -1
- data/lib/spree/core/engine.rb +14 -0
- data/lib/spree/core/version.rb +1 -1
- data/lib/spree/deprecation.rb +1 -1
- data/lib/spree/localized_number.rb +3 -2
- data/lib/spree/permission_sets/configuration_display.rb +0 -1
- data/lib/spree/permission_sets/configuration_management.rb +0 -1
- data/lib/spree/permission_sets/product_display.rb +0 -1
- data/lib/spree/permission_sets/product_management.rb +0 -1
- data/lib/spree/permission_sets/user_management.rb +2 -4
- data/lib/spree/permitted_attributes.rb +3 -2
- data/lib/spree/testing_support/capybara_ext.rb +0 -12
- data/lib/spree/testing_support/factories/address_factory.rb +1 -1
- data/lib/spree/testing_support/factories/line_item_factory.rb +0 -1
- data/lib/spree/testing_support/factories/payment_factory.rb +4 -0
- data/lib/spree/testing_support/factories/payment_method_factory.rb +8 -1
- data/lib/spree/testing_support/factories/user_factory.rb +2 -2
- data/solidus_core.gemspec +4 -3
- data/spec/helpers/base_helper_spec.rb +0 -40
- data/spec/lib/spree/core/controller_helpers/pricing_spec.rb +16 -0
- data/spec/lib/spree/core/importer/order_spec.rb +27 -18
- data/spec/lib/spree/core/price_migrator_spec.rb +3 -1
- data/spec/lib/spree/core/testing_support/factories/order_factory_spec.rb +16 -0
- data/spec/lib/spree/core/unreturned_item_charger_spec.rb +0 -2
- data/spec/lib/tasks/exchanges_spec.rb +4 -2
- data/spec/lib/tasks/migrations/create_vat_prices_spec.rb +5 -3
- data/spec/models/spree/adjustment_spec.rb +136 -0
- data/spec/models/spree/calculator/default_tax_spec.rb +13 -7
- data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +3 -0
- data/spec/models/spree/calculator/flat_rate_spec.rb +3 -0
- data/spec/models/spree/calculator/flexi_rate_spec.rb +3 -0
- data/spec/models/spree/calculator/free_shipping_spec.rb +6 -0
- data/spec/models/spree/calculator/percent_on_line_item_spec.rb +9 -4
- data/spec/models/spree/calculator/percent_per_item_spec.rb +10 -0
- data/spec/models/spree/calculator/price_sack_spec.rb +3 -0
- data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +3 -0
- data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +3 -0
- data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +3 -0
- data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +3 -0
- data/spec/models/spree/calculator/shipping/per_item_spec.rb +3 -0
- data/spec/models/spree/calculator/shipping/price_sack_spec.rb +4 -1
- data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +3 -0
- data/spec/models/spree/calculator/tiered_percent_spec.rb +3 -0
- data/spec/models/spree/credit_card_spec.rb +27 -1
- data/spec/models/spree/line_item_spec.rb +58 -65
- data/spec/models/spree/order/checkout_spec.rb +2 -1
- data/spec/models/spree/order/payment_spec.rb +9 -10
- data/spec/models/spree/order/tax_spec.rb +22 -7
- data/spec/models/spree/order/updating_spec.rb +1 -3
- data/spec/models/spree/order_cancellations_spec.rb +6 -4
- data/spec/models/spree/order_contents_spec.rb +34 -50
- data/spec/models/spree/order_inventory_spec.rb +3 -5
- data/spec/models/spree/order_merger_spec.rb +20 -0
- data/spec/models/spree/order_spec.rb +28 -64
- data/spec/models/spree/order_update_attributes_spec.rb +1 -5
- data/spec/models/spree/order_updater_spec.rb +251 -0
- data/spec/models/spree/payment_method_spec.rb +178 -28
- data/spec/models/spree/payment_spec.rb +35 -19
- data/spec/models/spree/permission_sets/configuration_display.rb +0 -4
- data/spec/models/spree/permission_sets/configuration_management_spec.rb +0 -2
- data/spec/models/spree/permission_sets/product_display_spec.rb +0 -4
- data/spec/models/spree/permission_sets/product_management_spec.rb +0 -2
- data/spec/models/spree/permission_sets/user_management_spec.rb +9 -2
- data/spec/models/spree/price_spec.rb +16 -1
- data/spec/models/spree/product_spec.rb +0 -75
- data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +20 -0
- data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +39 -15
- data/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb +203 -22
- data/spec/models/spree/promotion/actions/free_shipping_spec.rb +22 -3
- data/spec/models/spree/promotion/rules/taxon_spec.rb +26 -0
- data/spec/models/spree/promotion/rules/user_role_spec.rb +86 -0
- data/spec/models/spree/promotion_action_spec.rb +38 -0
- data/spec/models/spree/promotion_handler/coupon_spec.rb +36 -33
- data/spec/models/spree/promotion_handler/free_shipping_spec.rb +21 -22
- data/spec/models/spree/promotion_spec.rb +46 -6
- data/spec/models/spree/reimbursement_spec.rb +1 -1
- data/spec/models/spree/reimbursement_tax_calculator_spec.rb +2 -2
- data/spec/models/spree/shipment_spec.rb +68 -50
- data/spec/models/spree/shipping_method_spec.rb +41 -0
- data/spec/models/spree/shipping_rate_spec.rb +9 -3
- data/spec/models/spree/stock/estimator_spec.rb +4 -2
- data/spec/models/spree/store_credit_spec.rb +3 -3
- data/spec/models/spree/tax/item_adjuster_spec.rb +31 -21
- data/spec/models/spree/tax/order_adjuster_spec.rb +6 -10
- data/spec/models/spree/tax/taxation_integration_spec.rb +19 -0
- data/spec/models/spree/tax_rate_spec.rb +5 -26
- data/spec/models/spree/transfer_item_spec.rb +11 -0
- data/spec/models/spree/variant/pricing_options_spec.rb +7 -17
- data/spec/models/spree/variant_spec.rb +2 -4
- data/spec/models/spree/zone_spec.rb +60 -20
- data/spec/shared_examples/calculator_shared_examples.rb +8 -0
- metadata +19 -24
- data/app/models/spree/item_adjustments.rb +0 -89
- data/app/models/spree/option_type_prototype.rb +0 -6
- data/app/models/spree/property_prototype.rb +0 -6
- data/app/models/spree/prototype.rb +0 -14
- data/app/models/spree/prototype_taxon.rb +0 -6
- data/app/models/spree/tracker.rb +0 -8
- data/db/migrate/20150128032538_remove_environment_from_tracker.rb +0 -6
- data/lib/generators/spree/dummy/templates/initializers/custom_user.rb +0 -1
- data/lib/spree/core/delegate_belongs_to.rb +0 -94
- data/lib/spree/testing_support/factories/prototype_factory.rb +0 -8
- data/lib/spree/testing_support/factories/tracker_factory.rb +0 -6
- data/spec/lib/spree/core/delegate_belongs_to_spec.rb +0 -24
- data/spec/lib/spree/core/testing_support/factories/prototype_factory_spec.rb +0 -12
- data/spec/lib/spree/core/testing_support/factories/tracker_factory_spec.rb +0 -12
- data/spec/models/spree/item_adjustments_spec.rb +0 -306
- data/spec/models/spree/tracker_spec.rb +0 -21
|
@@ -27,7 +27,7 @@ module Spree
|
|
|
27
27
|
order: order,
|
|
28
28
|
source: self,
|
|
29
29
|
promotion_code: options[:promotion_code],
|
|
30
|
-
label:
|
|
30
|
+
label: Spree.t('adjustment_labels.order', promotion: Spree::Promotion.model_name.human, promotion_name: promotion.name)
|
|
31
31
|
)
|
|
32
32
|
true
|
|
33
33
|
end
|
|
@@ -39,6 +39,17 @@ module Spree
|
|
|
39
39
|
[(calculable.item_total + calculable.ship_total), amount].min * -1
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# Removes any adjustments generated by this action from the order.
|
|
43
|
+
# @param order [Spree::Order] the order to remove the action from.
|
|
44
|
+
# @return [void]
|
|
45
|
+
def remove_from(order)
|
|
46
|
+
order.adjustments.each do |adjustment|
|
|
47
|
+
if adjustment.source == self
|
|
48
|
+
order.adjustments.destroy(adjustment)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
42
53
|
private
|
|
43
54
|
|
|
44
55
|
# Tells us if there if the specified promotion is already associated with the line item
|
|
@@ -35,6 +35,20 @@ module Spree
|
|
|
35
35
|
[adjustable.amount, promotion_amount].min * -1
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
# Removes any adjustments generated by this action from the order's
|
|
39
|
+
# line items.
|
|
40
|
+
# @param order [Spree::Order] the order to remove the action from.
|
|
41
|
+
# @return [void]
|
|
42
|
+
def remove_from(order)
|
|
43
|
+
order.line_items.each do |line_item|
|
|
44
|
+
line_item.adjustments.each do |adjustment|
|
|
45
|
+
if adjustment.source == self
|
|
46
|
+
line_item.adjustments.destroy(adjustment)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
38
52
|
private
|
|
39
53
|
|
|
40
54
|
def create_adjustment(adjustable, order, promotion_code)
|
|
@@ -45,7 +59,7 @@ module Spree
|
|
|
45
59
|
amount: amount,
|
|
46
60
|
order: order,
|
|
47
61
|
promotion_code: promotion_code,
|
|
48
|
-
label:
|
|
62
|
+
label: Spree.t('adjustment_labels.line_item', promotion: Spree::Promotion.model_name.human, promotion_name: promotion.name)
|
|
49
63
|
)
|
|
50
64
|
true
|
|
51
65
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
module Spree::Promotion::Actions
|
|
2
2
|
class CreateQuantityAdjustments < CreateItemAdjustments
|
|
3
|
-
include Spree::CalculatedAdjustments
|
|
4
|
-
|
|
5
3
|
preference :group_size, :integer, default: 1
|
|
6
4
|
|
|
7
5
|
has_many :line_item_actions, foreign_key: :action_id, dependent: :destroy
|
|
@@ -80,7 +78,7 @@ module Spree::Promotion::Actions
|
|
|
80
78
|
private
|
|
81
79
|
|
|
82
80
|
def actionable_line_items(order)
|
|
83
|
-
order.line_items.
|
|
81
|
+
order.line_items.select do |item|
|
|
84
82
|
promotion.line_item_actionable? order, item
|
|
85
83
|
end
|
|
86
84
|
end
|
|
@@ -121,6 +119,10 @@ module Spree::Promotion::Actions
|
|
|
121
119
|
@line_item.price
|
|
122
120
|
end
|
|
123
121
|
|
|
122
|
+
def order
|
|
123
|
+
@line_item.order
|
|
124
|
+
end
|
|
125
|
+
|
|
124
126
|
def currency
|
|
125
127
|
@line_item.currency
|
|
126
128
|
end
|
|
@@ -30,6 +30,20 @@ module Spree
|
|
|
30
30
|
shipment.cost * -1
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# Removes any adjustments generated by this action from the order's
|
|
34
|
+
# shipments.
|
|
35
|
+
# @param order [Spree::Order] the order to remove the action from.
|
|
36
|
+
# @return [void]
|
|
37
|
+
def remove_from(order)
|
|
38
|
+
order.shipments.each do |shipment|
|
|
39
|
+
shipment.adjustments.each do |adjustment|
|
|
40
|
+
if adjustment.source == self
|
|
41
|
+
shipment.adjustments.destroy(adjustment)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
33
47
|
private
|
|
34
48
|
|
|
35
49
|
def promotion_credit_exists?(shipment)
|
|
@@ -6,12 +6,11 @@ module Spree
|
|
|
6
6
|
dependent: :destroy
|
|
7
7
|
has_many :taxons, through: :promotion_rule_taxons, class_name: 'Spree::Taxon'
|
|
8
8
|
|
|
9
|
-
MATCH_POLICIES = %w(any all)
|
|
9
|
+
MATCH_POLICIES = %w(any all none)
|
|
10
10
|
|
|
11
11
|
validates_inclusion_of :preferred_match_policy, in: MATCH_POLICIES
|
|
12
12
|
|
|
13
13
|
preference :match_policy, :string, default: MATCH_POLICIES.first
|
|
14
|
-
|
|
15
14
|
def applicable?(promotable)
|
|
16
15
|
promotable.is_a?(Spree::Order)
|
|
17
16
|
end
|
|
@@ -28,6 +27,10 @@ module Spree
|
|
|
28
27
|
unless taxons.any?{ |taxon| order_taxons.include? taxon }
|
|
29
28
|
eligibility_errors.add(:base, eligibility_error_message(:no_matching_taxons))
|
|
30
29
|
end
|
|
30
|
+
when 'none'
|
|
31
|
+
unless taxons.none?{ |taxon| order_taxons.include? taxon }
|
|
32
|
+
eligibility_errors.add(:base, eligibility_error_message(:has_excluded_taxon))
|
|
33
|
+
end
|
|
31
34
|
else
|
|
32
35
|
# Change this to an exception in a future version of Solidus
|
|
33
36
|
warn_invalid_match_policy(assume: 'any')
|
|
@@ -43,6 +46,8 @@ module Spree
|
|
|
43
46
|
case preferred_match_policy
|
|
44
47
|
when 'any', 'all'
|
|
45
48
|
taxon_product_ids.include?(line_item.variant.product_id)
|
|
49
|
+
when 'none'
|
|
50
|
+
taxon_product_ids.exclude? line_item.variant.product_id
|
|
46
51
|
else
|
|
47
52
|
# Change this to an exception in a future version of Solidus
|
|
48
53
|
warn_invalid_match_policy(assume: 'any')
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
class Promotion
|
|
3
|
+
module Rules
|
|
4
|
+
class UserRole < PromotionRule
|
|
5
|
+
preference :role_ids, :array, default: []
|
|
6
|
+
|
|
7
|
+
MATCH_POLICIES = %w(any all)
|
|
8
|
+
preference :match_policy, default: MATCH_POLICIES.first
|
|
9
|
+
|
|
10
|
+
def applicable?(promotable)
|
|
11
|
+
promotable.is_a?(Spree::Order)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def eligible?(order, _options = {})
|
|
15
|
+
return false unless order.user
|
|
16
|
+
if all_match_policy?
|
|
17
|
+
match_all_roles?(order)
|
|
18
|
+
else
|
|
19
|
+
match_any_roles?(order)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def all_match_policy?
|
|
26
|
+
preferred_match_policy == 'all' && preferred_role_ids.present?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def user_roles(order)
|
|
30
|
+
order.user.spree_roles.where(id: preferred_role_ids)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def match_all_roles?(order)
|
|
34
|
+
user_roles(order).count == preferred_role_ids.count
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def match_any_roles?(order)
|
|
38
|
+
user_roles(order).exists?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -6,9 +6,9 @@ module Spree
|
|
|
6
6
|
class PromotionAction < Spree::Base
|
|
7
7
|
acts_as_paranoid
|
|
8
8
|
|
|
9
|
-
belongs_to :promotion, class_name: 'Spree::Promotion'
|
|
9
|
+
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions
|
|
10
10
|
|
|
11
|
-
scope :of_type, ->(t) { where(type: t) }
|
|
11
|
+
scope :of_type, ->(t) { where(type: t.to_s) }
|
|
12
12
|
|
|
13
13
|
# Updates the state of the order or performs some other action depending on
|
|
14
14
|
# the subclass options will contain the payload from the event that
|
|
@@ -19,5 +19,22 @@ module Spree
|
|
|
19
19
|
def perform(_options = {})
|
|
20
20
|
raise 'perform should be implemented in a sub-class of PromotionAction'
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
# Removes the action from an order
|
|
24
|
+
#
|
|
25
|
+
# @note This method should be overriden in subclassses.
|
|
26
|
+
#
|
|
27
|
+
# @param order [Spree::Order] the order to remove the action from
|
|
28
|
+
# @return [void]
|
|
29
|
+
def remove_from(order)
|
|
30
|
+
Spree::Deprecation.warn("#{self.class.name.inspect} does not define #remove_from. The default behavior may be incorrect and will be removed in a future version of Solidus.", caller)
|
|
31
|
+
[order, *order.line_items, *order.shipments].each do |item|
|
|
32
|
+
item.adjustments.each do |adjustment|
|
|
33
|
+
if adjustment.source == self
|
|
34
|
+
item.adjustments.destroy(adjustment)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
22
39
|
end
|
|
23
40
|
end
|
|
@@ -94,10 +94,7 @@ module Spree
|
|
|
94
94
|
discount ||= order.shipment_adjustments.promotion.detect(&detector)
|
|
95
95
|
discount ||= order.adjustments.promotion.detect(&detector)
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
created_line_items = promotion.actions.detect { |a| a.type == 'Spree::Promotion::Actions::CreateLineItems' }
|
|
99
|
-
|
|
100
|
-
if (discount && discount.eligible) || created_line_items
|
|
97
|
+
if discount && discount.eligible
|
|
101
98
|
order.update_totals
|
|
102
99
|
order.persist_totals
|
|
103
100
|
set_success_code :coupon_code_applied
|
|
@@ -2,19 +2,21 @@ module Spree
|
|
|
2
2
|
module PromotionHandler
|
|
3
3
|
# Used for activating promotions with shipping rules
|
|
4
4
|
class FreeShipping
|
|
5
|
-
attr_reader :order
|
|
5
|
+
attr_reader :order
|
|
6
6
|
attr_accessor :error, :success
|
|
7
7
|
|
|
8
8
|
def initialize(order)
|
|
9
9
|
@order = order
|
|
10
|
-
@order_promo_ids = order.promotions.pluck(:id)
|
|
11
10
|
end
|
|
12
11
|
|
|
13
12
|
def activate
|
|
14
13
|
promotions.each do |promotion|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if order_promotion = existing_order_promotion(promotion)
|
|
15
|
+
promotion.activate(
|
|
16
|
+
order: order,
|
|
17
|
+
promotion_code: order_promotion.promotion_code,
|
|
18
|
+
)
|
|
19
|
+
elsif promotion.apply_automatically?
|
|
18
20
|
promotion.activate(order: order)
|
|
19
21
|
end
|
|
20
22
|
end
|
|
@@ -23,20 +25,23 @@ module Spree
|
|
|
23
25
|
private
|
|
24
26
|
|
|
25
27
|
def promotions
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
Spree::Promotion.
|
|
29
|
+
active.
|
|
30
|
+
joins(:promotion_actions).
|
|
31
|
+
merge(
|
|
32
|
+
Spree::PromotionAction.of_type(
|
|
33
|
+
Spree::Promotion::Actions::FreeShipping
|
|
34
|
+
)
|
|
35
|
+
).
|
|
36
|
+
distinct
|
|
37
|
+
end
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
def existing_order_promotion(promotion)
|
|
40
|
+
@lookup ||= order.order_promotions.map do |order_promotion|
|
|
41
|
+
[order_promotion.promotion_id, order_promotion]
|
|
42
|
+
end.to_h
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
joins(promotion_code_join).
|
|
35
|
-
where({
|
|
36
|
-
id: Spree::Promotion::Actions::FreeShipping.pluck(:promotion_id), # This would probably be more efficient by joining instead
|
|
37
|
-
spree_promotion_codes: { id: nil },
|
|
38
|
-
path: nil
|
|
39
|
-
})
|
|
44
|
+
@lookup[promotion.id]
|
|
40
45
|
end
|
|
41
46
|
end
|
|
42
47
|
end
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
module Spree
|
|
2
|
+
# An order's planned shipments including tracking and cost.
|
|
3
|
+
#
|
|
2
4
|
class Shipment < Spree::Base
|
|
3
5
|
belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :shipments
|
|
4
6
|
belongs_to :stock_location, class_name: 'Spree::StockLocation'
|
|
5
7
|
|
|
6
8
|
has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :delete_all
|
|
7
9
|
has_many :inventory_units, dependent: :destroy, inverse_of: :shipment
|
|
8
|
-
has_many :shipping_rates, -> { order(:cost) }, dependent: :
|
|
10
|
+
has_many :shipping_rates, -> { order(:cost) }, dependent: :destroy
|
|
9
11
|
has_many :shipping_methods, through: :shipping_rates
|
|
10
12
|
has_many :state_changes, as: :stateful
|
|
11
13
|
has_many :cartons, -> { uniq }, through: :inventory_units
|
|
@@ -280,11 +282,9 @@ module Spree
|
|
|
280
282
|
def update_amounts
|
|
281
283
|
if selected_shipping_rate
|
|
282
284
|
self.cost = selected_shipping_rate.cost
|
|
283
|
-
self.adjustment_total = adjustments.additional.map(&:update!).compact.sum
|
|
284
285
|
if changed?
|
|
285
286
|
update_columns(
|
|
286
287
|
cost: cost,
|
|
287
|
-
adjustment_total: adjustment_total,
|
|
288
288
|
updated_at: Time.current
|
|
289
289
|
)
|
|
290
290
|
end
|
|
@@ -296,25 +296,9 @@ module Spree
|
|
|
296
296
|
if update_attributes params
|
|
297
297
|
if params.key? :selected_shipping_rate_id
|
|
298
298
|
# Changing the selected Shipping Rate won't update the cost (for now)
|
|
299
|
-
# so we persist the Shipment#cost before
|
|
300
|
-
# total and updating payment state (given a change in shipment cost
|
|
301
|
-
# might change the Order#payment_state)
|
|
299
|
+
# so we persist the Shipment#cost before running `order.update!`
|
|
302
300
|
update_amounts
|
|
303
|
-
|
|
304
|
-
order.updater.update_shipment_total
|
|
305
|
-
order.updater.update_payment_state
|
|
306
|
-
|
|
307
|
-
# Update shipment state only after order total is updated because it
|
|
308
|
-
# (via Order#paid?) affects the shipment state (YAY)
|
|
309
|
-
update_columns(
|
|
310
|
-
state: determine_state(order),
|
|
311
|
-
updated_at: Time.current
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
# And then it's time to update shipment states and finally persist
|
|
315
|
-
# order changes
|
|
316
|
-
order.updater.update_shipment_state
|
|
317
|
-
order.updater.persist_totals
|
|
301
|
+
order.update!
|
|
318
302
|
end
|
|
319
303
|
|
|
320
304
|
true
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
module Spree
|
|
2
|
+
# Represents a means of having a shipment delivered, such as FedEx or UPS.
|
|
3
|
+
#
|
|
2
4
|
class ShippingMethod < Spree::Base
|
|
3
5
|
acts_as_paranoid
|
|
4
6
|
include Spree::CalculatedAdjustments
|
|
5
|
-
DISPLAY =
|
|
7
|
+
DISPLAY = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
|
|
8
|
+
[:both, :front_end, :back_end],
|
|
9
|
+
"Spree::ShippingMethod::DISPLAY is deprecated",
|
|
10
|
+
Spree::Deprecation
|
|
11
|
+
)
|
|
6
12
|
|
|
7
13
|
has_many :shipping_method_categories, dependent: :destroy
|
|
8
14
|
has_many :shipping_categories, through: :shipping_method_categories
|
|
@@ -77,10 +83,25 @@ module Spree
|
|
|
77
83
|
tracking_url.gsub(/:tracking/, ERB::Util.url_encode(tracking)) # :url_encode exists in 1.8.7 through 2.1.0
|
|
78
84
|
end
|
|
79
85
|
|
|
86
|
+
def display_on
|
|
87
|
+
if available_to_users?
|
|
88
|
+
"both"
|
|
89
|
+
else
|
|
90
|
+
"back_end"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
deprecate display_on: :available_to_users?, deprecator: Spree::Deprecation
|
|
94
|
+
|
|
95
|
+
def display_on=(value)
|
|
96
|
+
self.available_to_users = (value != "back_end")
|
|
97
|
+
end
|
|
98
|
+
deprecate 'display_on=': :available_to_users=, deprecator: Spree::Deprecation
|
|
99
|
+
|
|
80
100
|
# Some shipping methods are only meant to be set via backend
|
|
81
101
|
def frontend?
|
|
82
|
-
|
|
102
|
+
available_to_users?
|
|
83
103
|
end
|
|
104
|
+
deprecate frontend?: :available_to_users?, deprecator: Spree::Deprecation
|
|
84
105
|
|
|
85
106
|
private
|
|
86
107
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
module Spree
|
|
2
|
+
# Records the costs of different shipping methods for a shipment and which
|
|
3
|
+
# method has been selected to deliver the shipment.
|
|
4
|
+
#
|
|
2
5
|
class ShippingRate < Spree::Base
|
|
3
6
|
belongs_to :shipment, class_name: 'Spree::Shipment'
|
|
4
7
|
belongs_to :shipping_method, -> { with_deleted }, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates
|
|
@@ -16,7 +16,7 @@ module Spree
|
|
|
16
16
|
raise OrderRequired if package.shipment.order.nil?
|
|
17
17
|
|
|
18
18
|
rates = calculate_shipping_rates(package)
|
|
19
|
-
rates.select! { |rate| rate.shipping_method.
|
|
19
|
+
rates.select! { |rate| rate.shipping_method.available_to_users? } if frontend_only
|
|
20
20
|
choose_default_shipping_rate(rates)
|
|
21
21
|
Spree::Config.shipping_rate_sorter_class.new(rates).sort
|
|
22
22
|
end
|