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,8 @@
|
|
1
|
+
import { application } from "solidus_friendly_promotions/controllers/application";
|
2
|
+
|
3
|
+
// Eager load all controllers defined in the import map under controllers/**/*_controller
|
4
|
+
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading";
|
5
|
+
eagerLoadControllersFrom(
|
6
|
+
"solidus_friendly_promotions/controllers",
|
7
|
+
application
|
8
|
+
);
|
data/app/javascript/solidus_friendly_promotions/controllers/product_option_values_controller.js
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
import { Controller } from "@hotwired/stimulus";
|
2
|
+
|
3
|
+
export default class extends Controller {
|
4
|
+
static targets = ["links", "template"];
|
5
|
+
|
6
|
+
connect() {
|
7
|
+
this.wrapperClass =
|
8
|
+
this.data.get("wrapperClass") || "promo-rule-option-value";
|
9
|
+
|
10
|
+
this.element.querySelectorAll("." + this.wrapperClass).forEach((element) => this.buildSelects(element))
|
11
|
+
}
|
12
|
+
|
13
|
+
add_row(event) {
|
14
|
+
event.preventDefault();
|
15
|
+
|
16
|
+
var content = this.templateTarget.innerHTML;
|
17
|
+
this.linksTarget.insertAdjacentHTML("beforebegin", content);
|
18
|
+
this.buildSelects(this.linksTarget.previousElementSibling)
|
19
|
+
}
|
20
|
+
|
21
|
+
propagate_product_id_to_value_input(event) {
|
22
|
+
event.preventDefault();
|
23
|
+
// targets the content of the last pair of square brackets
|
24
|
+
// we first need to greedily match all other square brackets
|
25
|
+
const regEx = /(\[.*\])\[.*?\]$/;
|
26
|
+
let wrapper = event.target.closest("." + this.wrapperClass);
|
27
|
+
let optionValuesInput = wrapper.querySelector(".option-values-select[type='hidden']");
|
28
|
+
optionValuesInput.name = optionValuesInput.name.replace(
|
29
|
+
regEx,
|
30
|
+
`$1[${event.target.value}]`
|
31
|
+
);
|
32
|
+
}
|
33
|
+
|
34
|
+
remove_row(event) {
|
35
|
+
event.preventDefault();
|
36
|
+
|
37
|
+
let wrapper = event.target.closest("." + this.wrapperClass);
|
38
|
+
wrapper.remove();
|
39
|
+
}
|
40
|
+
|
41
|
+
// helper functions
|
42
|
+
|
43
|
+
buildSelects(wrapper) {
|
44
|
+
let productSelect = wrapper.querySelector(".product-select")
|
45
|
+
let optionValueSelect = wrapper.querySelector(".option-values-select[type='hidden']")
|
46
|
+
this.buildProductSelect(productSelect)
|
47
|
+
$(optionValueSelect).optionValueAutocomplete({ productSelect });
|
48
|
+
}
|
49
|
+
|
50
|
+
buildProductSelect(productSelect) {
|
51
|
+
var jQueryProductSelect = $(productSelect)
|
52
|
+
jQueryProductSelect.productAutocomplete({
|
53
|
+
multiple: false,
|
54
|
+
})
|
55
|
+
// capture the jQuery "change" event and re-emit it as DOM event "select2Change"
|
56
|
+
// so that Stimulus can capture it
|
57
|
+
jQueryProductSelect.on('change', function () {
|
58
|
+
let event = new Event('select2Change', { bubbles: true }) // fire a native event
|
59
|
+
productSelect.dispatchEvent(event);
|
60
|
+
});
|
61
|
+
}
|
62
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
$.fn.optionValueAutocomplete = function (options) {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
// Default options
|
5
|
+
options = options || {}
|
6
|
+
var multiple = typeof(options['multiple']) !== 'undefined' ? options['multiple'] : true;
|
7
|
+
var productSelect = options['productSelect'];
|
8
|
+
|
9
|
+
function formatOptionValue(option_value) {
|
10
|
+
return Select2.util.escapeMarkup(option_value.name);
|
11
|
+
}
|
12
|
+
|
13
|
+
this.select2({
|
14
|
+
minimumInputLength: 3,
|
15
|
+
multiple: multiple,
|
16
|
+
initSelection: function (element, callback) {
|
17
|
+
$.get(Spree.pathFor('api/option_values'), {
|
18
|
+
ids: element.val().split(','),
|
19
|
+
token: Spree.api_key
|
20
|
+
}, function (data) {
|
21
|
+
callback(multiple ? data : data[0]);
|
22
|
+
});
|
23
|
+
},
|
24
|
+
ajax: {
|
25
|
+
url: Spree.pathFor('api/option_values'),
|
26
|
+
datatype: 'json',
|
27
|
+
data: function (term, page) {
|
28
|
+
var productId = typeof(productSelect) !== 'undefined' ? $(productSelect).select2('val') : null;
|
29
|
+
return {
|
30
|
+
q: {
|
31
|
+
name_cont: term,
|
32
|
+
variants_product_id_eq: productId
|
33
|
+
},
|
34
|
+
token: Spree.api_key
|
35
|
+
};
|
36
|
+
},
|
37
|
+
results: function (data, page) {
|
38
|
+
return { results: data };
|
39
|
+
}
|
40
|
+
},
|
41
|
+
formatResult: formatOptionValue,
|
42
|
+
formatSelection: formatOptionValue
|
43
|
+
});
|
44
|
+
};
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import "@hotwired/turbo-rails";
|
2
|
+
import "solidus_friendly_promotions/controllers";
|
3
|
+
import "solidus_friendly_promotions/jquery/option_value_picker"
|
4
|
+
|
5
|
+
Turbo.session.drive = false;
|
6
|
+
|
7
|
+
document.addEventListener("turbo:frame-load", ({ _target }) => {
|
8
|
+
Spree.initNumberWithCurrency();
|
9
|
+
$(".product_picker").productAutocomplete();
|
10
|
+
$(".user_picker").userAutocomplete();
|
11
|
+
$(".taxon_picker").taxonAutocomplete();
|
12
|
+
});
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionCodeBatchJob < ApplicationJob
|
5
|
+
queue_as :default
|
6
|
+
|
7
|
+
def perform(promotion_code_batch)
|
8
|
+
PromotionCode::BatchBuilder.new(
|
9
|
+
promotion_code_batch
|
10
|
+
).build_promotion_codes
|
11
|
+
|
12
|
+
if promotion_code_batch.email?
|
13
|
+
SolidusFriendlyPromotions.config.promotion_code_batch_mailer_class
|
14
|
+
.promotion_code_batch_finished(promotion_code_batch)
|
15
|
+
.deliver_now
|
16
|
+
end
|
17
|
+
rescue => e
|
18
|
+
if promotion_code_batch.email?
|
19
|
+
SolidusFriendlyPromotions.config.promotion_code_batch_mailer_class
|
20
|
+
.promotion_code_batch_errored(promotion_code_batch)
|
21
|
+
.deliver_now
|
22
|
+
end
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class PromotionCodeBatchMailer < Spree::BaseMailer
|
5
|
+
def promotion_code_batch_finished(promotion_code_batch)
|
6
|
+
@promotion_code_batch = promotion_code_batch
|
7
|
+
mail(to: promotion_code_batch.email)
|
8
|
+
end
|
9
|
+
|
10
|
+
def promotion_code_batch_errored(promotion_code_batch)
|
11
|
+
@promotion_code_batch = promotion_code_batch
|
12
|
+
mail(to: promotion_code_batch.email)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module DiscountableAmount
|
5
|
+
def discountable_amount
|
6
|
+
amount + current_discounts.sum(&:amount)
|
7
|
+
end
|
8
|
+
|
9
|
+
def current_discounts
|
10
|
+
@current_discounts ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_discounts=(args)
|
14
|
+
@current_discounts = args
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_current_discounts
|
18
|
+
@current_discounts = []
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
module LineItemApplicableOrderRule
|
6
|
+
def self.included(klass)
|
7
|
+
klass.preference :line_item_applicable, :boolean, default: true
|
8
|
+
end
|
9
|
+
|
10
|
+
def applicable?(promotable)
|
11
|
+
promotable.is_a?(Spree::Order) || preferred_line_item_applicable && promotable.is_a?(Spree::LineItem)
|
12
|
+
end
|
13
|
+
|
14
|
+
def eligible?(promotable)
|
15
|
+
send("#{promotable.class.name.demodulize.underscore}_eligible?", promotable)
|
16
|
+
end
|
17
|
+
|
18
|
+
def level
|
19
|
+
:order
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Actions
|
5
|
+
class AdjustLineItemQuantityGroups < AdjustLineItem
|
6
|
+
preference :group_size, :integer, default: 1
|
7
|
+
|
8
|
+
##
|
9
|
+
# Computes the amount for the adjustment based on the line item and any
|
10
|
+
# other applicable items in the order. The rules for this specific
|
11
|
+
# adjustment are as follows:
|
12
|
+
#
|
13
|
+
# = Setup
|
14
|
+
#
|
15
|
+
# We have a quantity group promotion on t-shirts. If a user orders 3
|
16
|
+
# t-shirts, they get $5 off of each. The shirts come in one size and three
|
17
|
+
# colours: red, blue, and white.
|
18
|
+
#
|
19
|
+
# == Scenario 1
|
20
|
+
#
|
21
|
+
# User has 2 red shirts, 1 white shirt, and 1 blue shirt in their
|
22
|
+
# order. We want to compute the adjustment amount for the white shirt.
|
23
|
+
#
|
24
|
+
# *Result:* -$5
|
25
|
+
#
|
26
|
+
# *Reasoning:* There are a total of 4 items that are eligible for the
|
27
|
+
# promotion. Since that is greater than 3, we can discount the items. The
|
28
|
+
# white shirt has a quantity of 1, therefore it will get discounted by
|
29
|
+
# +adjustment_amount * 1+ or $5.
|
30
|
+
#
|
31
|
+
# === Scenario 1-1
|
32
|
+
#
|
33
|
+
# What about the blue shirt? How much does it get discounted?
|
34
|
+
#
|
35
|
+
# *Result:* $0
|
36
|
+
#
|
37
|
+
# *Reasoning:* We have a total quantity of 4. However, we only apply the
|
38
|
+
# adjustment to groups of 3. Assuming the white and red shirts have already
|
39
|
+
# had their adjustment calculated, that means 3 units have been discounted.
|
40
|
+
# Leaving us with a lonely blue shirt that isn't part of a group of 3.
|
41
|
+
# Therefore, it does not receive the discount.
|
42
|
+
#
|
43
|
+
# == Scenario 2
|
44
|
+
#
|
45
|
+
# User has 4 red shirts in their order. What is the amount?
|
46
|
+
#
|
47
|
+
# *Result:* -$15
|
48
|
+
#
|
49
|
+
# *Reasoning:* The total quantity of eligible items is 4, so we the
|
50
|
+
# adjustment will be non-zero. However, we only apply it to groups of 3,
|
51
|
+
# therefore there is one extra item that is not eligible for the
|
52
|
+
# adjustment. +adjustment_amount * 3+ or $15.
|
53
|
+
#
|
54
|
+
def compute_amount(line_item)
|
55
|
+
adjustment_amount = calculator.compute(Item.new(line_item))
|
56
|
+
return BigDecimal("0") if adjustment_amount.nil? || adjustment_amount.zero?
|
57
|
+
|
58
|
+
adjustment_amount = adjustment_amount.abs
|
59
|
+
|
60
|
+
order = line_item.order
|
61
|
+
line_items = actionable_line_items(order)
|
62
|
+
|
63
|
+
item_units = line_items.sort_by do |line_item|
|
64
|
+
[-line_item.quantity, line_item.id]
|
65
|
+
end.flat_map do |line_item|
|
66
|
+
Array.new(line_item.quantity) do
|
67
|
+
Item.new(line_item)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
item_units_in_groups = item_units.in_groups_of(preferred_group_size, false)
|
72
|
+
item_units_in_groups.select! { |group| group.length == preferred_group_size }
|
73
|
+
usable_quantity = item_units_in_groups.flatten.count { |item_unit| item_unit.line_item == line_item }
|
74
|
+
|
75
|
+
amount = adjustment_amount * usable_quantity
|
76
|
+
[line_item.discountable_amount, amount].min * -1
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def actionable_line_items(order)
|
82
|
+
order.line_items.select do |item|
|
83
|
+
promotion.rules.select do |rule|
|
84
|
+
rule.applicable?(item)
|
85
|
+
end.all? { |rule| rule.eligible?(item) }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
##
|
90
|
+
# Used specifically for PercentOnLineItem calculator. That calculator uses
|
91
|
+
# `line_item.amount`, however we might not necessarily want to discount the
|
92
|
+
# entire amount. This class allows us to determine the discount per
|
93
|
+
# quantity and then calculate the adjustment amount the way we normally do
|
94
|
+
# for flat rate adjustments.
|
95
|
+
class Item
|
96
|
+
attr_reader :line_item
|
97
|
+
def initialize(line_item)
|
98
|
+
@line_item = line_item
|
99
|
+
end
|
100
|
+
|
101
|
+
def discountable_amount
|
102
|
+
@line_item.discountable_amount / @line_item.quantity.to_d
|
103
|
+
end
|
104
|
+
alias_method :amount, :discountable_amount
|
105
|
+
|
106
|
+
def order
|
107
|
+
@line_item.order
|
108
|
+
end
|
109
|
+
|
110
|
+
def currency
|
111
|
+
@line_item.currency
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Actions
|
5
|
+
class AdjustShipment < PromotionAction
|
6
|
+
def can_discount?(object)
|
7
|
+
object.is_a?(Spree::Shipment) || object.is_a?(Spree::ShippingRate)
|
8
|
+
end
|
9
|
+
|
10
|
+
def level
|
11
|
+
:shipment
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
# This is a calculator for line item adjustment actions. It accepts a line item
|
6
|
+
# and calculates its weighted adjustment amount based on the value of the
|
7
|
+
# preferred amount and the price of the other line items. More expensive line
|
8
|
+
# items will receive a greater share of the preferred amount.
|
9
|
+
module SolidusFriendlyPromotions
|
10
|
+
module Calculators
|
11
|
+
class DistributedAmount < Spree::Calculator
|
12
|
+
preference :amount, :decimal, default: 0
|
13
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
14
|
+
|
15
|
+
def compute_line_item(line_item)
|
16
|
+
return 0 unless line_item
|
17
|
+
return 0 unless preferred_currency.casecmp(line_item.currency).zero?
|
18
|
+
|
19
|
+
distributable_line_items = eligible_line_items(line_item.order)
|
20
|
+
return 0 unless line_item.in?(distributable_line_items)
|
21
|
+
|
22
|
+
DistributedAmountsHandler.new(
|
23
|
+
distributable_line_items,
|
24
|
+
preferred_amount
|
25
|
+
).amount(line_item)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def eligible_line_items(order)
|
31
|
+
order.line_items.select do |line_item|
|
32
|
+
calculable.promotion.eligible_by_applicable_rules?(line_item)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
module Calculators
|
7
|
+
class FlatRate < Spree::Calculator
|
8
|
+
preference :amount, :decimal, default: 0
|
9
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
10
|
+
|
11
|
+
def compute(object = nil)
|
12
|
+
currency = object.order.currency
|
13
|
+
if object && preferred_currency.casecmp(currency).zero?
|
14
|
+
preferred_amount
|
15
|
+
else
|
16
|
+
0
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
module Calculators
|
7
|
+
class FlexiRate < Spree::Calculator
|
8
|
+
preference :first_item, :decimal, default: 0
|
9
|
+
preference :additional_item, :decimal, default: 0
|
10
|
+
preference :max_items, :integer, default: 0
|
11
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
12
|
+
|
13
|
+
def compute(object)
|
14
|
+
items_count = object.quantity
|
15
|
+
items_count = [items_count, preferred_max_items].min unless preferred_max_items.zero?
|
16
|
+
|
17
|
+
return BigDecimal("0") if items_count == 0
|
18
|
+
|
19
|
+
additional_items_count = items_count - 1
|
20
|
+
preferred_first_item + preferred_additional_item * additional_items_count
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
module Calculators
|
7
|
+
class Percent < Spree::Calculator
|
8
|
+
preference :percent, :decimal, default: 0
|
9
|
+
|
10
|
+
def compute(object)
|
11
|
+
preferred_currency = object.order.currency
|
12
|
+
currency_exponent = ::Money::Currency.find(preferred_currency).exponent
|
13
|
+
(object.discountable_amount * preferred_percent / 100).round(currency_exponent)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
module Calculators
|
7
|
+
class TieredFlatRate < Spree::Calculator
|
8
|
+
preference :base_amount, :decimal, default: 0
|
9
|
+
preference :tiers, :hash, default: {}
|
10
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
11
|
+
|
12
|
+
before_validation do
|
13
|
+
# Convert tier values to decimals. Strings don't do us much good.
|
14
|
+
if preferred_tiers.is_a?(Hash)
|
15
|
+
self.preferred_tiers = preferred_tiers.map do |key, value|
|
16
|
+
[cast_to_d(key.to_s), cast_to_d(value.to_s)]
|
17
|
+
end.to_h
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
validate :preferred_tiers_content
|
22
|
+
|
23
|
+
def compute_item(object)
|
24
|
+
_base, amount = preferred_tiers.sort.reverse.detect do |value, _|
|
25
|
+
object.discountable_amount >= value
|
26
|
+
end
|
27
|
+
|
28
|
+
if preferred_currency.casecmp(object.currency).zero?
|
29
|
+
amount || preferred_base_amount
|
30
|
+
else
|
31
|
+
0
|
32
|
+
end
|
33
|
+
end
|
34
|
+
alias_method :compute_shipment, :compute_item
|
35
|
+
alias_method :compute_line_item, :compute_item
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def cast_to_d(value)
|
40
|
+
value.to_s.to_d
|
41
|
+
rescue ArgumentError
|
42
|
+
BigDecimal("0")
|
43
|
+
end
|
44
|
+
|
45
|
+
def preferred_tiers_content
|
46
|
+
if preferred_tiers.is_a? Hash
|
47
|
+
unless preferred_tiers.keys.all? { |key| key.is_a?(Numeric) && key > 0 }
|
48
|
+
errors.add(:base, :keys_should_be_positive_number)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
errors.add(:preferred_tiers, :should_be_hash)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_dependency "spree/calculator"
|
4
|
+
|
5
|
+
module SolidusFriendlyPromotions
|
6
|
+
module Calculators
|
7
|
+
class TieredPercent < Spree::Calculator
|
8
|
+
preference :base_percent, :decimal, default: 0
|
9
|
+
preference :tiers, :hash, default: {}
|
10
|
+
preference :currency, :string, default: -> { Spree::Config[:currency] }
|
11
|
+
|
12
|
+
before_validation do
|
13
|
+
# Convert tier values to decimals. Strings don't do us much good.
|
14
|
+
if preferred_tiers.is_a?(Hash)
|
15
|
+
self.preferred_tiers = preferred_tiers.map do |key, value|
|
16
|
+
[cast_to_d(key.to_s), cast_to_d(value.to_s)]
|
17
|
+
end.to_h
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
validates :preferred_base_percent, numericality: {
|
22
|
+
greater_than_or_equal_to: 0,
|
23
|
+
less_than_or_equal_to: 100
|
24
|
+
}
|
25
|
+
validate :preferred_tiers_content
|
26
|
+
|
27
|
+
def compute_item(object)
|
28
|
+
order = object.order
|
29
|
+
|
30
|
+
_base, percent = preferred_tiers.sort.reverse.detect do |value, _|
|
31
|
+
order.item_total >= value
|
32
|
+
end
|
33
|
+
|
34
|
+
if preferred_currency.casecmp(order.currency).zero?
|
35
|
+
currency_exponent = ::Money::Currency.find(preferred_currency).exponent
|
36
|
+
(object.amount * (percent || preferred_base_percent) / 100).round(currency_exponent)
|
37
|
+
else
|
38
|
+
0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
alias_method :compute_shipment, :compute_item
|
42
|
+
alias_method :compute_line_item, :compute_item
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def cast_to_d(value)
|
47
|
+
value.to_s.to_d
|
48
|
+
rescue ArgumentError
|
49
|
+
BigDecimal("0")
|
50
|
+
end
|
51
|
+
|
52
|
+
def preferred_tiers_content
|
53
|
+
if preferred_tiers.is_a? Hash
|
54
|
+
unless preferred_tiers.keys.all? { |key| key.is_a?(Numeric) && key > 0 }
|
55
|
+
errors.add(:base, :keys_should_be_positive_number)
|
56
|
+
end
|
57
|
+
unless preferred_tiers.values.all? { |key| key.is_a?(Numeric) && key >= 0 && key <= 100 }
|
58
|
+
errors.add(:base, :values_should_be_percent)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
errors.add(:preferred_tiers, :should_be_hash)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|