solidus_friendly_promotions 1.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +61 -0
- data/.gem_release.yml +5 -0
- data/.github/stale.yml +1 -0
- data/.github_changelog_generator +2 -0
- data/.gitignore +21 -0
- data/.rspec +2 -0
- data/.rubocop.yml +29 -0
- data/.rubocop_todo.yml +157 -0
- data/.standard.yml +4 -0
- data/CHANGELOG.md +1 -0
- data/Gemfile +42 -0
- data/LICENSE +26 -0
- data/MIGRATING.md +134 -0
- data/README.md +144 -0
- data/Rakefile +7 -0
- data/app/assets/config/solidus_friendly_promotions/manifest.js +7 -0
- data/app/controllers/solidus_friendly_promotions/admin/base_controller.rb +54 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_actions_controller.rb +103 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_categories_controller.rb +13 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_code_batches_controller.rb +30 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_codes_controller.rb +58 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_rules_controller.rb +105 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotions_controller.rb +64 -0
- data/app/decorators/models/solidus_friendly_promotions/adjustment_decorator.rb +26 -0
- data/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb +7 -0
- data/app/decorators/models/solidus_friendly_promotions/order_decorator.rb +42 -0
- data/app/decorators/models/solidus_friendly_promotions/order_recalculator_decorator.rb +13 -0
- data/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb +14 -0
- data/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb +29 -0
- data/app/helpers/solidus_friendly_promotions/admin/promotion_actions_helper.rb +23 -0
- data/app/helpers/solidus_friendly_promotions/admin/promotion_rules_helper.rb +25 -0
- data/app/javascript/solidus_friendly_promotions/controllers/application.js +9 -0
- data/app/javascript/solidus_friendly_promotions/controllers/calculator_tiers_controller.js +37 -0
- data/app/javascript/solidus_friendly_promotions/controllers/flash_controller.js +10 -0
- data/app/javascript/solidus_friendly_promotions/controllers/index.js +8 -0
- data/app/javascript/solidus_friendly_promotions/controllers/product_option_values_controller.js +62 -0
- data/app/javascript/solidus_friendly_promotions/jquery/option_value_picker.js +44 -0
- data/app/javascript/solidus_friendly_promotions.js +12 -0
- data/app/jobs/solidus_friendly_promotions/promotion_code_batch_job.rb +26 -0
- data/app/mailers/solidus_friendly_promotions/promotion_code_batch_mailer.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/discountable_amount.rb +21 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/line_item_applicable_order_rule.rb +23 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/line_item_level_rule.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/order_level_rule.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/shipment_level_rule.rb +15 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb +15 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_line_item_quantity_groups.rb +116 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb +15 -0
- data/app/models/solidus_friendly_promotions/calculators/distributed_amount.rb +37 -0
- data/app/models/solidus_friendly_promotions/calculators/flat_rate.rb +21 -0
- data/app/models/solidus_friendly_promotions/calculators/flexi_rate.rb +24 -0
- data/app/models/solidus_friendly_promotions/calculators/percent.rb +17 -0
- data/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb +56 -0
- data/app/models/solidus_friendly_promotions/calculators/tiered_percent.rb +66 -0
- data/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb +43 -0
- data/app/models/solidus_friendly_promotions/eligibility_result.rb +5 -0
- data/app/models/solidus_friendly_promotions/eligibility_results.rb +48 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb +21 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb +76 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/load_promotions.rb +54 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/persist_discounted_order.rb +81 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb +25 -0
- data/app/models/solidus_friendly_promotions/item_discount.rb +21 -0
- data/app/models/solidus_friendly_promotions/migration_support/order_promotion_syncer.rb +54 -0
- data/app/models/solidus_friendly_promotions/order_promotion.rb +23 -0
- data/app/models/solidus_friendly_promotions/permission_sets/friendly_promotion_management.rb +15 -0
- data/app/models/solidus_friendly_promotions/products_promotion_rule.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion.rb +166 -0
- data/app/models/solidus_friendly_promotions/promotion_action.rb +73 -0
- data/app/models/solidus_friendly_promotions/promotion_category.rb +9 -0
- data/app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb +72 -0
- data/app/models/solidus_friendly_promotions/promotion_code.rb +55 -0
- data/app/models/solidus_friendly_promotions/promotion_code_batch.rb +25 -0
- data/app/models/solidus_friendly_promotions/promotion_handler/coupon.rb +113 -0
- data/app/models/solidus_friendly_promotions/promotion_handler/page.rb +30 -0
- data/app/models/solidus_friendly_promotions/promotion_rule.rb +55 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_store.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_taxon.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_user.rb +8 -0
- data/app/models/solidus_friendly_promotions/rules/discounted_item_total.rb +22 -0
- data/app/models/solidus_friendly_promotions/rules/first_order.rb +31 -0
- data/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb +31 -0
- data/app/models/solidus_friendly_promotions/rules/item_total.rb +86 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb +37 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_product.rb +52 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb +55 -0
- data/app/models/solidus_friendly_promotions/rules/minimum_quantity.rb +48 -0
- data/app/models/solidus_friendly_promotions/rules/nth_order.rb +40 -0
- data/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb +25 -0
- data/app/models/solidus_friendly_promotions/rules/option_value.rb +28 -0
- data/app/models/solidus_friendly_promotions/rules/product.rb +85 -0
- data/app/models/solidus_friendly_promotions/rules/shipping_method.rb +19 -0
- data/app/models/solidus_friendly_promotions/rules/store.rb +26 -0
- data/app/models/solidus_friendly_promotions/rules/taxon.rb +98 -0
- data/app/models/solidus_friendly_promotions/rules/user.rb +35 -0
- data/app/models/solidus_friendly_promotions/rules/user_logged_in.rb +16 -0
- data/app/models/solidus_friendly_promotions/rules/user_role.rb +42 -0
- data/app/models/solidus_friendly_promotions/shipping_rate_discount.rb +11 -0
- data/app/models/solidus_friendly_promotions/simple_order_contents.rb +27 -0
- data/app/models/solidus_friendly_promotions.rb +7 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_calculator_select.html.erb +16 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_form.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_promotion_action.html.erb +29 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_type_select.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_line_item.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_line_item_quantity_groups.html.erb +13 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_shipment.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_calculator_fields.erb +8 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/_default_fields.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/distributed_amount/_fields.html.erb +56 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/flat_rate/_fields.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_flat_rate/_fields.html.erb +34 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_flat_rate/_tier_fields.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_percent/_fields.html.erb +34 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_percent/_tier_fields.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/edit.html.erb +23 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/new.html.erb +26 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/_form.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/edit.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/index.html.erb +47 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/new.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/index.csv.ruby +8 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/index.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/new.html.erb +31 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/_promotion_rule.html.erb +22 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/_type_select.html.erb +20 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/new.html.erb +24 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_first_order.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_first_repeat_purchase_since.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_item_total.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_option_value.html.erb +25 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_product.html.erb +21 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_taxon.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_minimum_quantity.html.erb +5 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_nth_order.html.erb +15 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_one_use_per_user.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_option_value.html.erb +63 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_product.html.erb +44 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_shipping_method.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_store.html.erb +9 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_taxon.html.erb +44 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user.html.erb +7 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user_logged_in.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user_role.html.erb +15 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/line_item_option_value/_option_value_fields.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_activations_edit.html.erb +22 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_activations_new.html.erb +43 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_form.html.erb +96 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/edit.html.erb +73 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/index.html.erb +124 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/new.html.erb +9 -0
- data/app/views/solidus_friendly_promotions/admin/shared/_promotion_sub_menu.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/promotion_code_batch_mailer/promotion_code_batch_errored.text.erb +2 -0
- data/app/views/solidus_friendly_promotions/promotion_code_batch_mailer/promotion_code_batch_finished.text.erb +2 -0
- data/bin/console +17 -0
- data/bin/importmap +4 -0
- data/bin/rails +7 -0
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +75 -0
- data/bin/setup +8 -0
- data/config/importmap.rb +12 -0
- data/config/initializers/solidus_friendly_promotions.rb +3 -0
- data/config/locales/en.yml +255 -0
- data/config/routes.rb +18 -0
- data/db/migrate/20230703101637_create_promotions.rb +16 -0
- data/db/migrate/20230703141116_create_promotion_categories.rb +14 -0
- data/db/migrate/20230703143943_create_promotion_rules.rb +12 -0
- data/db/migrate/20230704083830_add_rule_tables.rb +31 -0
- data/db/migrate/20230704093625_create_promotion_actions.rb +14 -0
- data/db/migrate/20230704102444_create_promotion_codes.rb +11 -0
- data/db/migrate/20230704102656_create_promotion_code_batches.rb +33 -0
- data/db/migrate/20230705171556_create_friendly_order_promotions.rb +11 -0
- data/db/migrate/20230725074235_create_shipping_rate_discounts.rb +12 -0
- data/db/migrate/20230928093138_add_lane_to_solidus_friendly_promotions_promotions.rb +5 -0
- data/db/migrate/20231006134042_add_customer_label_to_promotions.rb +7 -0
- data/db/migrate/20231011100059_add_db_comments_to_friendly_order_promotions.rb +61 -0
- data/db/migrate/20231011120928_add_db_comments_to_friendly_promotion_rules_taxons.rb +54 -0
- data/db/migrate/20231011131324_add_db_comments_to_friendly_promotion_rules.rb +60 -0
- data/db/migrate/20231011142040_add_db_comments_to_friendly_promotion_rules_users.rb +53 -0
- data/db/migrate/20231011154553_allow_null_promotion_ids.rb +9 -0
- data/db/migrate/20231011155822_add_db_comments_to_friendly_promotions.rb +123 -0
- data/db/migrate/20231011163030_add_db_comments_to_friendly_promotion_codes.rb +60 -0
- data/db/migrate/20231011173312_add_db_comments_to_friendly_promotion_code_batches.rb +91 -0
- data/db/migrate/20231011184205_add_db_comments_to_friendly_promotion_rules_stores.rb +53 -0
- data/db/migrate/20231011190222_add_db_comments_to_friendly_promotion_actions.rb +68 -0
- data/db/migrate/20231012020928_add_db_comments_to_friendly_products_promotion_rules.rb +52 -0
- data/db/migrate/20231012120928_add_db_comments_to_friendly_promotion_categories.rb +52 -0
- data/db/migrate/20231013181921_add_original_promotion_ids.rb +6 -0
- data/lib/generators/solidus_friendly_promotions/install/install_generator.rb +38 -0
- data/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb +118 -0
- data/lib/solidus_friendly_promotions/configuration.rb +52 -0
- data/lib/solidus_friendly_promotions/engine.rb +37 -0
- data/lib/solidus_friendly_promotions/migrate_adjustments.rb +62 -0
- data/lib/solidus_friendly_promotions/migrate_order_promotions.rb +73 -0
- data/lib/solidus_friendly_promotions/nested_class_set.rb +24 -0
- data/lib/solidus_friendly_promotions/promotion_map.rb +96 -0
- data/lib/solidus_friendly_promotions/promotion_migrator.rb +103 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_order_factory.rb +20 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_order_promotion_factory.rb +8 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_category_factory.rb +7 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_code_factory.rb +8 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_factory.rb +91 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_shipping_rate_discount_factory.rb +14 -0
- data/lib/solidus_friendly_promotions/testing_support.rb +9 -0
- data/lib/solidus_friendly_promotions/version.rb +5 -0
- data/lib/solidus_friendly_promotions.rb +18 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake +17 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_existing_promotions.rake +12 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_order_promotions.rake +17 -0
- data/solidus_friendly_promotions.gemspec +40 -0
- metadata +392 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spree/preferences/persistable"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
# Base class for all types of promotion action.
|
7
|
+
#
|
8
|
+
# PromotionActions perform the necessary tasks when a promotion is activated
|
9
|
+
# by an event and determined to be eligible.
|
10
|
+
class PromotionAction < Spree::Base
|
11
|
+
include Spree::Preferences::Persistable
|
12
|
+
include Spree::SoftDeletable
|
13
|
+
include Spree::CalculatedAdjustments
|
14
|
+
include Spree::AdjustmentSource
|
15
|
+
|
16
|
+
belongs_to :promotion, inverse_of: :actions
|
17
|
+
belongs_to :original_promotion_action, class_name: "Spree::PromotionAction", optional: true
|
18
|
+
has_many :adjustments, class_name: "Spree::Adjustment", as: :source
|
19
|
+
|
20
|
+
scope :of_type, ->(type) { where(type: Array.wrap(type).map(&:to_s)) }
|
21
|
+
|
22
|
+
def preload_relations
|
23
|
+
[:calculator]
|
24
|
+
end
|
25
|
+
|
26
|
+
def can_discount?(object)
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
29
|
+
|
30
|
+
def discount(adjustable)
|
31
|
+
amount = compute_amount(adjustable)
|
32
|
+
return if amount.zero?
|
33
|
+
ItemDiscount.new(
|
34
|
+
item: adjustable,
|
35
|
+
label: adjustment_label(adjustable),
|
36
|
+
amount: amount,
|
37
|
+
source: self
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Ensure a negative amount which does not exceed the object's amount
|
42
|
+
def compute_amount(adjustable)
|
43
|
+
promotion_amount = calculator.compute(adjustable) || BigDecimal("0")
|
44
|
+
[adjustable.discountable_amount, promotion_amount.abs].min * -1
|
45
|
+
end
|
46
|
+
|
47
|
+
def adjustment_label(adjustable)
|
48
|
+
I18n.t(
|
49
|
+
"solidus_friendly_promotions.adjustment_labels.#{adjustable.class.name.demodulize.underscore}",
|
50
|
+
promotion: SolidusFriendlyPromotions::Promotion.model_name.human,
|
51
|
+
promotion_customer_label: promotion.customer_label
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_partial_path
|
56
|
+
"solidus_friendly_promotions/admin/promotion_actions/actions/#{model_name.element}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def level
|
60
|
+
raise NotImplementedError
|
61
|
+
end
|
62
|
+
|
63
|
+
def relevant_rules
|
64
|
+
promotion.rules.select do |rule|
|
65
|
+
rule.level.in?([:order, level].uniq)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def available_calculators
|
70
|
+
SolidusFriendlyPromotions.config.promotion_calculators[self.class] || (raise NotImplementedError)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionCode < Spree::Base
|
5
|
+
class BatchBuilder
|
6
|
+
attr_reader :promotion_code_batch, :options
|
7
|
+
|
8
|
+
delegate :promotion, :number_of_codes, :base_code, to: :promotion_code_batch
|
9
|
+
|
10
|
+
DEFAULT_OPTIONS = {
|
11
|
+
random_code_length: 6,
|
12
|
+
batch_size: 1000,
|
13
|
+
sample_characters: ("a".."z").to_a + (2..9).to_a.map(&:to_s)
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
def initialize(promotion_code_batch, options = {})
|
17
|
+
@promotion_code_batch = promotion_code_batch
|
18
|
+
options.assert_valid_keys(*DEFAULT_OPTIONS.keys)
|
19
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def build_promotion_codes
|
23
|
+
generate_random_codes
|
24
|
+
promotion_code_batch.update!(state: "completed")
|
25
|
+
rescue => e
|
26
|
+
promotion_code_batch.update!(
|
27
|
+
error: e.inspect,
|
28
|
+
state: "failed"
|
29
|
+
)
|
30
|
+
raise e
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def generate_random_codes
|
36
|
+
created_codes = promotion_code_batch.promotion_codes.count
|
37
|
+
|
38
|
+
batch_size = @options[:batch_size]
|
39
|
+
|
40
|
+
while created_codes < number_of_codes
|
41
|
+
max_codes_to_generate = [batch_size, number_of_codes - created_codes].min
|
42
|
+
|
43
|
+
new_codes = Array.new(max_codes_to_generate) { generate_random_code }.uniq
|
44
|
+
codes_for_current_batch = get_unique_codes(new_codes)
|
45
|
+
|
46
|
+
codes_for_current_batch = codes_for_current_batch.map do |value|
|
47
|
+
SolidusFriendlyPromotions::PromotionCode.create!(
|
48
|
+
value: value,
|
49
|
+
promotion: promotion,
|
50
|
+
promotion_code_batch: promotion_code_batch
|
51
|
+
)
|
52
|
+
rescue ActiveRecord::RecordInvalid
|
53
|
+
nil
|
54
|
+
end.compact
|
55
|
+
created_codes += codes_for_current_batch.size
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def generate_random_code
|
60
|
+
suffix = Array.new(@options[:random_code_length]) do
|
61
|
+
@options[:sample_characters].sample
|
62
|
+
end.join
|
63
|
+
|
64
|
+
"#{base_code}#{@promotion_code_batch.join_characters}#{suffix}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_unique_codes(code_set)
|
68
|
+
code_set - PromotionCode.where(value: code_set.to_a).pluck(:value)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionCode < Spree::Base
|
5
|
+
belongs_to :promotion, inverse_of: :codes
|
6
|
+
belongs_to :promotion_code_batch, inverse_of: :promotion_codes, optional: true
|
7
|
+
has_many :adjustments, class_name: "Spree::Adjustment"
|
8
|
+
|
9
|
+
before_validation :normalize_code
|
10
|
+
|
11
|
+
validates :value, presence: true, uniqueness: {allow_blank: true, case_sensitive: true}
|
12
|
+
validate :promotion_not_apply_automatically, on: :create
|
13
|
+
|
14
|
+
self.allowed_ransackable_attributes = ["value"]
|
15
|
+
|
16
|
+
# Whether the promotion code has exceeded its usage restrictions
|
17
|
+
#
|
18
|
+
# @param excluded_orders [Array<Spree::Order>] Orders to exclude from usage limit
|
19
|
+
# @return true or false
|
20
|
+
def usage_limit_exceeded?(excluded_orders: [])
|
21
|
+
return unless usage_limit
|
22
|
+
|
23
|
+
usage_count(excluded_orders: excluded_orders) >= usage_limit
|
24
|
+
end
|
25
|
+
|
26
|
+
# Number of times the code has been used overall
|
27
|
+
#
|
28
|
+
# @param excluded_orders [Array<Spree::Order>] Orders to exclude from usage count
|
29
|
+
# @return [Integer] usage count
|
30
|
+
def usage_count(excluded_orders: [])
|
31
|
+
promotion
|
32
|
+
.discounted_orders
|
33
|
+
.complete
|
34
|
+
.where.not(spree_orders: {state: :canceled})
|
35
|
+
.joins(:friendly_order_promotions)
|
36
|
+
.where(friendly_order_promotions: {promotion_code_id: id})
|
37
|
+
.where.not(id: excluded_orders.map(&:id))
|
38
|
+
.count
|
39
|
+
end
|
40
|
+
|
41
|
+
def usage_limit
|
42
|
+
promotion.per_code_usage_limit
|
43
|
+
end
|
44
|
+
|
45
|
+
def promotion_not_apply_automatically
|
46
|
+
errors.add(:base, :disallowed_with_apply_automatically) if promotion.apply_automatically
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def normalize_code
|
52
|
+
self.value = value.downcase.strip
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionCodeBatch < Spree::Base
|
5
|
+
class CantProcessStartedBatch < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
belongs_to :promotion
|
9
|
+
has_many :promotion_codes, dependent: :destroy
|
10
|
+
|
11
|
+
validates :number_of_codes, numericality: {greater_than: 0}
|
12
|
+
validates :base_code, :number_of_codes, presence: true
|
13
|
+
|
14
|
+
def finished?
|
15
|
+
state == "completed"
|
16
|
+
end
|
17
|
+
|
18
|
+
def process
|
19
|
+
raise CantProcessStartedBatch, "Batch #{id} already started" unless state == "pending"
|
20
|
+
|
21
|
+
update!(state: "processing")
|
22
|
+
PromotionCodeBatchJob.perform_later(self)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module PromotionHandler
|
5
|
+
class Coupon
|
6
|
+
attr_reader :order, :coupon_code, :errors
|
7
|
+
attr_accessor :error, :success, :status_code
|
8
|
+
|
9
|
+
def initialize(order)
|
10
|
+
@order = order
|
11
|
+
@errors = []
|
12
|
+
@coupon_code = order&.coupon_code&.downcase
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply
|
16
|
+
if coupon_code.present?
|
17
|
+
if promotion.present? && promotion.active?
|
18
|
+
handle_present_promotion
|
19
|
+
elsif promotion_code&.promotion&.expired?
|
20
|
+
set_error_code :coupon_code_expired
|
21
|
+
else
|
22
|
+
set_error_code :coupon_code_not_found
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def remove
|
30
|
+
if promotion.blank?
|
31
|
+
set_error_code :coupon_code_not_found
|
32
|
+
elsif !promotion_exists_on_order?(order, promotion)
|
33
|
+
set_error_code :coupon_code_not_present
|
34
|
+
else
|
35
|
+
order.friendly_order_promotions.destroy_by(
|
36
|
+
promotion: promotion
|
37
|
+
)
|
38
|
+
order.recalculate
|
39
|
+
set_success_code :coupon_code_removed
|
40
|
+
end
|
41
|
+
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def successful?
|
46
|
+
success.present? && error.blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
def promotion
|
50
|
+
@promotion ||= if promotion_code&.promotion&.active?
|
51
|
+
promotion_code.promotion
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def set_success_code(status_code)
|
58
|
+
@status_code = status_code
|
59
|
+
@success = I18n.t(status_code, scope: "solidus_friendly_promotions.eligibility_results")
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_error_code(status_code, options = {})
|
63
|
+
@status_code = status_code
|
64
|
+
@error = options[:error] || I18n.t(status_code, scope: "solidus_friendly_promotions.eligibility_errors")
|
65
|
+
@errors = options[:errors] || [@error]
|
66
|
+
end
|
67
|
+
|
68
|
+
def promotion_code
|
69
|
+
@promotion_code ||= SolidusFriendlyPromotions::PromotionCode.where(value: coupon_code).first
|
70
|
+
end
|
71
|
+
|
72
|
+
def handle_present_promotion
|
73
|
+
return promotion_usage_limit_exceeded if promotion.usage_limit_exceeded? || promotion_code.usage_limit_exceeded?
|
74
|
+
return promotion_applied if promotion_exists_on_order?(order, promotion)
|
75
|
+
|
76
|
+
# Try applying this promotion, with no effects
|
77
|
+
Spree::Config.promotion_adjuster_class.new(order, dry_run_promotion: promotion).call
|
78
|
+
|
79
|
+
if promotion.eligibility_results.success?
|
80
|
+
order.friendly_order_promotions.create!(
|
81
|
+
promotion: promotion,
|
82
|
+
promotion_code: promotion_code
|
83
|
+
)
|
84
|
+
order.recalculate
|
85
|
+
set_success_code :coupon_code_applied
|
86
|
+
else
|
87
|
+
set_promotion_eligibility_error(promotion)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_promotion_eligibility_error(promotion)
|
92
|
+
eligibility_error = promotion.eligibility_results.detect { |result| !result.success }
|
93
|
+
set_error_code(eligibility_error.code, error: eligibility_error.message, errors: promotion.eligibility_results.error_messages)
|
94
|
+
end
|
95
|
+
|
96
|
+
def promotion_usage_limit_exceeded
|
97
|
+
set_error_code :coupon_code_max_usage
|
98
|
+
end
|
99
|
+
|
100
|
+
def ineligible_for_this_order
|
101
|
+
set_error_code :coupon_code_not_eligible
|
102
|
+
end
|
103
|
+
|
104
|
+
def promotion_applied
|
105
|
+
set_error_code :coupon_code_already_applied
|
106
|
+
end
|
107
|
+
|
108
|
+
def promotion_exists_on_order?(order, promotion)
|
109
|
+
order.friendly_promotions.include? promotion
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module PromotionHandler
|
5
|
+
class Page
|
6
|
+
attr_reader :order, :path
|
7
|
+
|
8
|
+
def initialize(order, path)
|
9
|
+
@order = order
|
10
|
+
@path = path.gsub(/\A\//, "")
|
11
|
+
end
|
12
|
+
|
13
|
+
def activate
|
14
|
+
if promotion
|
15
|
+
Spree::Config.promotion_adjuster_class.new(order, dry_run_promotion: promotion).call
|
16
|
+
if promotion.eligibility_results.success?
|
17
|
+
order.friendly_promotions << promotion
|
18
|
+
order.recalculate
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def promotion
|
26
|
+
@promotion ||= SolidusFriendlyPromotions::Promotion.active.find_by(path: path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spree/preferences/persistable"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
class PromotionRule < Spree::Base
|
7
|
+
include Spree::Preferences::Persistable
|
8
|
+
|
9
|
+
belongs_to :promotion
|
10
|
+
|
11
|
+
scope :of_type, ->(type) { where(type: type) }
|
12
|
+
|
13
|
+
validate :unique_per_promotion, on: :create
|
14
|
+
|
15
|
+
def preload_relations
|
16
|
+
[]
|
17
|
+
end
|
18
|
+
|
19
|
+
def applicable?(_promotable)
|
20
|
+
raise NotImplementedError, "applicable? should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
|
21
|
+
end
|
22
|
+
|
23
|
+
def eligible?(_promotable, _options = {})
|
24
|
+
raise NotImplementedError, "eligible? should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
|
25
|
+
end
|
26
|
+
|
27
|
+
def level
|
28
|
+
raise NotImplementedError, "level should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
|
29
|
+
end
|
30
|
+
|
31
|
+
def eligibility_errors
|
32
|
+
@eligibility_errors ||= ActiveModel::Errors.new(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_partial_path
|
36
|
+
"solidus_friendly_promotions/admin/promotion_rules/rules/#{model_name.element}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def updateable?
|
40
|
+
preferences.any?
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def unique_per_promotion
|
46
|
+
return unless self.class.exists?(promotion_id: promotion_id, type: self.class.name)
|
47
|
+
|
48
|
+
errors[:base] << "Promotion already contains this rule type"
|
49
|
+
end
|
50
|
+
|
51
|
+
def eligibility_error_message(key, options = {})
|
52
|
+
I18n.t(key, **{scope: [:solidus_friendly_promotions, :eligibility_errors, self.class.name.underscore]}.merge(options))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionRulesStore < Spree::Base
|
5
|
+
belongs_to :promotion_rule, class_name: "SolidusFriendlyPromotions::PromotionRule", optional: true
|
6
|
+
belongs_to :store, class_name: "Spree::Store", optional: true
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionRulesTaxon < Spree::Base
|
5
|
+
belongs_to :promotion_rule, class_name: "SolidusFriendlyPromotions::PromotionRule", optional: true
|
6
|
+
belongs_to :taxon, class_name: "Spree::Taxon", optional: true
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionRulesUser < Spree::Base
|
5
|
+
belongs_to :promotion_rule, class_name: "SolidusFriendlyPromotions::PromotionRule", optional: true
|
6
|
+
belongs_to :user, class_name: Spree::UserClassHandle.new, optional: true
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
# A rule to apply to an order greater than (or greater than or equal to)
|
6
|
+
# a specific amount after previous promotions have applied
|
7
|
+
#
|
8
|
+
# To add extra operators please override `self.operators_map` or any other helper method.
|
9
|
+
# To customize the error message you can also override `ineligible_message`.
|
10
|
+
class DiscountedItemTotal < ItemTotal
|
11
|
+
def to_partial_path
|
12
|
+
"solidus_friendly_promotions/admin/promotion_rules/rules/item_total"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def total_for_order(order)
|
18
|
+
order.discountable_item_total
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class FirstOrder < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
attr_reader :user, :email
|
8
|
+
|
9
|
+
def eligible?(order, options = {})
|
10
|
+
@user = order.try(:user) || options[:user]
|
11
|
+
@email = order.email
|
12
|
+
|
13
|
+
if (user || email) && (completed_orders.present? && completed_orders.first != order)
|
14
|
+
eligibility_errors.add(:base, eligibility_error_message(:not_first_order), error_code: :not_first_order)
|
15
|
+
end
|
16
|
+
|
17
|
+
eligibility_errors.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def completed_orders
|
23
|
+
user ? user.orders.complete : orders_by_email
|
24
|
+
end
|
25
|
+
|
26
|
+
def orders_by_email
|
27
|
+
Spree::Order.where(email: email).complete
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class FirstRepeatPurchaseSince < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
preference :days_ago, :integer, default: 365
|
9
|
+
validates :preferred_days_ago, numericality: {only_integer: true, greater_than: 0}
|
10
|
+
|
11
|
+
# This is never eligible if the order does not have a user, and that user does not have any previous completed orders.
|
12
|
+
#
|
13
|
+
# This is eligible if the user's most recently completed order is more than the preferred days ago
|
14
|
+
# @param order [Spree::Order]
|
15
|
+
def eligible?(order, _options = {})
|
16
|
+
return false unless order.user
|
17
|
+
|
18
|
+
last_order = last_completed_order(order.user)
|
19
|
+
return false unless last_order
|
20
|
+
|
21
|
+
last_order.completed_at < preferred_days_ago.days.ago
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def last_completed_order(user)
|
27
|
+
user.orders.complete.order(:completed_at).last
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
# A rule to apply to an order greater than (or greater than or equal to)
|
6
|
+
# a specific amount
|
7
|
+
#
|
8
|
+
# To add extra operators please override `self.operators_map` or any other helper method.
|
9
|
+
# To customize the error message you can also override `ineligible_message`.
|
10
|
+
class ItemTotal < PromotionRule
|
11
|
+
include OrderLevelRule
|
12
|
+
|
13
|
+
preference :amount, :decimal, default: 100.00
|
14
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
15
|
+
preference :operator, :string, default: "gt"
|
16
|
+
|
17
|
+
# The list of allowed operators names mapped to their symbols.
|
18
|
+
def self.operators_map
|
19
|
+
{
|
20
|
+
gte: :>=,
|
21
|
+
gt: :>
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.operator_options
|
26
|
+
operators_map.map do |name, _method|
|
27
|
+
[I18n.t(name, scope: "spree.item_total_rule.operators"), name]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def eligible?(order, _options = {})
|
32
|
+
return false unless order.currency == preferred_currency
|
33
|
+
|
34
|
+
unless total_for_order(order).send(operator, threshold)
|
35
|
+
eligibility_errors.add(:base, ineligible_message, error_code: ineligible_error_code)
|
36
|
+
end
|
37
|
+
|
38
|
+
eligibility_errors.empty?
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def operator
|
44
|
+
self.class.operators_map.fetch(
|
45
|
+
preferred_operator.to_sym,
|
46
|
+
preferred_operator_default
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
def total_for_order(order)
|
51
|
+
order.item_total
|
52
|
+
end
|
53
|
+
|
54
|
+
def threshold
|
55
|
+
BigDecimal(preferred_amount.to_s)
|
56
|
+
end
|
57
|
+
|
58
|
+
def formatted_amount
|
59
|
+
Spree::Money.new(preferred_amount, currency: preferred_currency).to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
def ineligible_message
|
63
|
+
case preferred_operator.to_s
|
64
|
+
when "gte"
|
65
|
+
eligibility_error_message(:item_total_less_than, amount: formatted_amount)
|
66
|
+
when "gt"
|
67
|
+
eligibility_error_message(:item_total_less_than_or_equal, amount: formatted_amount)
|
68
|
+
else
|
69
|
+
eligibility_error_message(
|
70
|
+
:item_total_doesnt_match_with_operator,
|
71
|
+
amount: formatted_amount,
|
72
|
+
operator: preferred_operator
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def ineligible_error_code
|
78
|
+
if preferred_operator == "gte"
|
79
|
+
:item_total_less_than
|
80
|
+
else
|
81
|
+
:item_total_less_than_or_equal
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class LineItemOptionValue < PromotionRule
|
6
|
+
include LineItemLevelRule
|
7
|
+
|
8
|
+
preference :eligible_values, :hash
|
9
|
+
|
10
|
+
def eligible?(line_item, _options = {})
|
11
|
+
pid = line_item.product.id
|
12
|
+
ovids = line_item.variant.option_values.pluck(:id)
|
13
|
+
|
14
|
+
product_ids.include?(pid) && (value_ids(pid) & ovids).present?
|
15
|
+
end
|
16
|
+
|
17
|
+
def preferred_eligible_values
|
18
|
+
values = preferences[:eligible_values] || {}
|
19
|
+
values.keys.map(&:to_i).zip(
|
20
|
+
values.values.map do |value|
|
21
|
+
(value.is_a?(Array) ? value : value.split(",")).map(&:to_i)
|
22
|
+
end
|
23
|
+
).to_h
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def product_ids
|
29
|
+
preferred_eligible_values.keys
|
30
|
+
end
|
31
|
+
|
32
|
+
def value_ids(product_id)
|
33
|
+
preferred_eligible_values[product_id]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|