diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index c5b16934f0..e4eea50ed8 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -54,7 +54,6 @@ Layout/LineLength: - app/models/concerns/variant_stock.rb - app/models/content_configuration.rb - app/models/customer.rb - - app/models/enterprise_fee.rb - app/models/enterprise_group.rb - app/models/enterprise_role.rb - app/models/inventory_item.rb diff --git a/Gemfile.lock b/Gemfile.lock index b0951fa531..9b627cb940 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -690,7 +690,7 @@ GEM hashdiff (>= 0.4.0, < 2.0.0) whenever (1.0.0) chronic (>= 0.6.3) - wicked_pdf (1.4.0) + wicked_pdf (2.1.0) activesupport wkhtmltopdf-binary (0.12.5) xml-simple (1.1.5) diff --git a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee index 751ddf8866..4afb7c1c22 100644 --- a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee +++ b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee @@ -23,10 +23,13 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque $scope.fetchResults() $scope.fetchResults = (page=1) -> + startDateWithTime = $scope.appendStringIfNotEmpty($scope['q']['completed_at_gteq'], ' 00:00:00') + endDateWithTime = $scope.appendStringIfNotEmpty($scope['q']['completed_at_lteq'], ' 23:59:59') + $scope.resetSelected() params = { - 'q[completed_at_lt]': $scope['q']['completed_at_lt'], - 'q[completed_at_gt]': $scope['q']['completed_at_gt'], + 'q[completed_at_gteq]': startDateWithTime, + 'q[completed_at_lteq]': endDateWithTime, 'q[state_eq]': $scope['q']['state_eq'], 'q[number_cont]': $scope['q']['number_cont'], 'q[email_cont]': $scope['q']['email_cont'], @@ -43,6 +46,11 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque } RequestMonitor.load(Orders.index(params).$promise) + $scope.appendStringIfNotEmpty = (baseString, stringToAppend) -> + return baseString unless baseString + + baseString + stringToAppend + $scope.resetSelected = -> $scope.selected_orders.length = 0 $scope.selected = false diff --git a/app/controllers/admin/enterprise_groups_controller.rb b/app/controllers/admin/enterprise_groups_controller.rb index 9d5bc6c665..d5dbccda3a 100644 --- a/app/controllers/admin/enterprise_groups_controller.rb +++ b/app/controllers/admin/enterprise_groups_controller.rb @@ -60,8 +60,8 @@ module Admin def permitted_resource_params params.require(:enterprise_group).permit( - :name, :description, :long_description, :on_front_page, :owner_id, :permalink, - :email, :website, :facebook, :instagram, :linkedin, :twitter, + :name, :description, :long_description, :logo, :promo_image, :on_front_page, + :owner_id, :permalink, :email, :website, :facebook, :instagram, :linkedin, :twitter, enterprise_ids: [], address_attributes: PermittedAttributes::Address.attributes ) end diff --git a/app/models/calculator/default_tax.rb b/app/models/calculator/default_tax.rb new file mode 100644 index 0000000000..de888c9283 --- /dev/null +++ b/app/models/calculator/default_tax.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' +require 'open_food_network/enterprise_fee_calculator' + +module Calculator + class DefaultTax < Spree::Calculator + def self.description + Spree.t(:default_tax) + end + + def compute(computable) + case computable + when Spree::Order + compute_order(computable) + when Spree::LineItem + compute_line_item(computable) + end + end + + private + + def rate + calculable + end + + # Enable calculation of tax for enterprise fees with tax rates where included_in_price = false + def compute_order(order) + calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(order.distributor, + order.order_cycle) + + [ + line_items_total(order), + per_item_fees_total(order, calculator), + per_order_fees_total(order, calculator) + ].sum do |total| + round_to_two_places(total * rate.amount) + end + end + + def line_items_total(order) + matched_line_items = order.line_items.select do |line_item| + line_item.product.tax_category == rate.tax_category + end + + matched_line_items.sum(&:total) + end + + # Finds relevant fees for each line_item, + # calculates the tax on them, and returns the total tax + def per_item_fees_total(order, calculator) + order.line_items.sum do |line_item| + calculator.per_item_enterprise_fee_applicators_for(line_item.variant) + .select { |applicator| applicable_rate?(applicator, line_item) } + .sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) } + end + end + + def applicable_rate?(applicator, line_item) + fee = applicator.enterprise_fee + (!fee.inherits_tax_category && fee.tax_category == rate.tax_category) || + (fee.inherits_tax_category && line_item.product.tax_category == rate.tax_category) + end + + # Finds relevant fees for whole order, + # calculates the tax on them, and returns the total tax + def per_order_fees_total(order, calculator) + calculator.per_order_enterprise_fee_applicators_for(order) + .select { |applicator| applicator.enterprise_fee.tax_category == rate.tax_category } + .sum { |applicator| applicator.enterprise_fee.compute_amount(order) } + end + + def compute_line_item(line_item) + if line_item.tax_category == rate.tax_category + if rate.included_in_price + deduced_total_by_rate(line_item.total, rate) + else + round_to_two_places(line_item.total * rate.amount) + end + else + 0 + end + end + + def round_to_two_places(amount) + BigDecimal(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP) + end + + def deduced_total_by_rate(total, rate) + round_to_two_places(total - ( total / (1 + rate.amount) ) ) + end + end +end diff --git a/app/models/spree/calculator/flat_percent_item_total_decorator.rb b/app/models/calculator/flat_percent_item_total.rb similarity index 56% rename from app/models/spree/calculator/flat_percent_item_total_decorator.rb rename to app/models/calculator/flat_percent_item_total.rb index ae95f59ca2..79c2f1f400 100644 --- a/app/models/spree/calculator/flat_percent_item_total_decorator.rb +++ b/app/models/calculator/flat_percent_item_total.rb @@ -1,11 +1,20 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' require 'spree/localized_number' -module Spree - Calculator::FlatPercentItemTotal.class_eval do +module Calculator + class FlatPercentItemTotal < Spree::Calculator extend Spree::LocalizedNumber + preference :flat_percent, :decimal, default: 0 + localize_number :preferred_flat_percent + def self.description + Spree.t(:flat_percent) + end + def compute(object) item_total = line_items_for(object).map(&:amount).sum value = item_total * BigDecimal(preferred_flat_percent.to_s) / 100.0 diff --git a/app/models/calculator/flat_rate.rb b/app/models/calculator/flat_rate.rb new file mode 100644 index 0000000000..d8bc14ca8f --- /dev/null +++ b/app/models/calculator/flat_rate.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' +require 'spree/localized_number' + +module Calculator + class FlatRate < Spree::Calculator + extend Spree::LocalizedNumber + + preference :amount, :decimal, default: 0 + preference :currency, :string, default: Spree::Config[:currency] + + localize_number :preferred_amount + + def self.description + I18n.t(:flat_rate_per_order) + end + + def compute(_object = nil) + preferred_amount + end + end +end diff --git a/app/models/calculator/flexi_rate.rb b/app/models/calculator/flexi_rate.rb new file mode 100644 index 0000000000..76ae53379a --- /dev/null +++ b/app/models/calculator/flexi_rate.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' +require 'spree/localized_number' + +module Calculator + class FlexiRate < Spree::Calculator + extend Spree::LocalizedNumber + + preference :first_item, :decimal, default: 0.0 + preference :additional_item, :decimal, default: 0.0 + preference :max_items, :integer, default: 0 + preference :currency, :string, default: Spree::Config[:currency] + + localize_number :preferred_first_item, + :preferred_additional_item + + def self.description + I18n.t(:flexible_rate) + end + + def self.available?(_object) + true + end + + def compute(object) + max = preferred_max_items.to_i + items_count = line_items_for(object).map(&:quantity).sum + + # check max value to avoid divide by 0 errors + return 0 if max.zero? + + if items_count > max + compute_for(max - 1) + elsif items_count <= max + compute_for(items_count - 1) + end + end + + private + + def compute_for(count) + count * preferred_additional_item.to_f + preferred_first_item.to_f + end + end +end diff --git a/app/models/spree/calculator/per_item_decorator.rb b/app/models/calculator/per_item.rb similarity index 59% rename from app/models/spree/calculator/per_item_decorator.rb rename to app/models/calculator/per_item.rb index e385ad8bf8..49b2df35c4 100644 --- a/app/models/spree/calculator/per_item_decorator.rb +++ b/app/models/calculator/per_item.rb @@ -1,9 +1,15 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' require 'spree/localized_number' -module Spree - Calculator::PerItem.class_eval do +module Calculator + class PerItem < Spree::Calculator extend Spree::LocalizedNumber + preference :amount, :decimal, default: 0 + preference :currency, :string, default: Spree::Config[:currency] + localize_number :preferred_amount def self.description @@ -14,11 +20,7 @@ module Spree return 0 if object.nil? number_of_line_items = line_items_for(object).reduce(0) do |sum, line_item| - value_to_add = if matching_products.blank? || matching_products.include?(line_item.product) - line_item.quantity - else - 0 - end + value_to_add = line_item.quantity sum + value_to_add end preferred_amount * number_of_line_items diff --git a/app/models/spree/calculator/price_sack_decorator.rb b/app/models/calculator/price_sack.rb similarity index 59% rename from app/models/spree/calculator/price_sack_decorator.rb rename to app/models/calculator/price_sack.rb index 389649f916..cb6d185bd6 100644 --- a/app/models/spree/calculator/price_sack_decorator.rb +++ b/app/models/calculator/price_sack.rb @@ -1,9 +1,19 @@ +# frozen_string_literal: false + +require_dependency 'spree/calculator' +# For #to_d method on Ruby 1.8 +require 'bigdecimal/util' require 'spree/localized_number' -module Spree - Calculator::PriceSack.class_eval do +module Calculator + class PriceSack < Spree::Calculator extend Spree::LocalizedNumber + preference :minimal_amount, :decimal, default: 0 + preference :normal_amount, :decimal, default: 0 + preference :discount_amount, :decimal, default: 0 + preference :currency, :string, default: Spree::Config[:currency] + localize_number :preferred_minimal_amount, :preferred_normal_amount, :preferred_discount_amount diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index bbcafa01dc..e973419ece 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -11,7 +11,9 @@ class EnterpriseFee < ActiveRecord::Base has_many :exchanges, through: :exchange_fees FEE_TYPES = %w(packing transport admin sales fundraising).freeze - PER_ORDER_CALCULATORS = ['Spree::Calculator::FlatRate', 'Spree::Calculator::FlexiRate', 'Spree::Calculator::PriceSack'].freeze + PER_ORDER_CALCULATORS = ['Calculator::FlatRate', + 'Calculator::FlexiRate', + 'Calculator::PriceSack'].freeze validates :fee_type, inclusion: { in: FEE_TYPES } validates :name, presence: true diff --git a/app/models/spree/calculator/default_tax_decorator.rb b/app/models/spree/calculator/default_tax_decorator.rb deleted file mode 100644 index ccc73abb9f..0000000000 --- a/app/models/spree/calculator/default_tax_decorator.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'open_food_network/enterprise_fee_calculator' - -Spree::Calculator::DefaultTax.class_eval do - private - - # Override this method to enable calculation of tax for - # enterprise fees with tax rates where included_in_price = false - def compute_order(order) - matched_line_items = order.line_items.select do |line_item| - line_item.product.tax_category == rate.tax_category - end - - line_items_total = matched_line_items.sum(&:total) - - # Added this line - calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(order.distributor, order.order_cycle) - - # Added this block, finds relevant fees for each line_item, calculates the tax on them, and returns the total tax - per_item_fees_total = order.line_items.sum do |line_item| - calculator.per_item_enterprise_fee_applicators_for(line_item.variant) - .select { |applicator| - (!applicator.enterprise_fee.inherits_tax_category && applicator.enterprise_fee.tax_category == rate.tax_category) || - (applicator.enterprise_fee.inherits_tax_category && line_item.product.tax_category == rate.tax_category) - } - .sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) } - end - - # Added this block, finds relevant fees for whole order, calculates the tax on them, and returns the total tax - per_order_fees_total = calculator.per_order_enterprise_fee_applicators_for(order) - .select { |applicator| applicator.enterprise_fee.tax_category == rate.tax_category } - .sum { |applicator| applicator.enterprise_fee.compute_amount(order) } - - # round_to_two_places(line_items_total * rate.amount) # Removed this line - - # Added this block - [line_items_total, per_item_fees_total, per_order_fees_total].sum do |total| - round_to_two_places(total * rate.amount) - end - end -end diff --git a/app/models/spree/calculator/flat_rate_decorator.rb b/app/models/spree/calculator/flat_rate_decorator.rb deleted file mode 100644 index d7abe3cf1b..0000000000 --- a/app/models/spree/calculator/flat_rate_decorator.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'spree/localized_number' - -module Spree - Calculator::FlatRate.class_eval do - extend Spree::LocalizedNumber - - localize_number :preferred_amount - - def self.description - I18n.t(:flat_rate_per_order) - end - end -end diff --git a/app/models/spree/calculator/flexi_rate_decorator.rb b/app/models/spree/calculator/flexi_rate_decorator.rb deleted file mode 100644 index de112f4f0b..0000000000 --- a/app/models/spree/calculator/flexi_rate_decorator.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spree/localized_number' - -module Spree - Calculator::FlexiRate.class_eval do - extend Spree::LocalizedNumber - - localize_number :preferred_first_item, - :preferred_additional_item - - def self.description - I18n.t(:flexible_rate) - end - - def compute(object) - sum = 0 - max = preferred_max_items.to_i - items_count = line_items_for(object).map(&:quantity).sum - # check max value to avoid divide by 0 errors - unless max == 0 - if items_count > max - sum += (max - 1) * preferred_additional_item.to_f + preferred_first_item.to_f - elsif items_count <= max - sum += (items_count - 1) * preferred_additional_item.to_f + preferred_first_item.to_f - end - end - - sum - end - end -end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 3da3919bf0..4c2770daea 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -49,7 +49,7 @@ Spree::PaymentMethod.class_eval do self.class.include Spree::Core::CalculatedAdjustments end - self.calculator ||= Spree::Calculator::FlatRate.new(preferred_amount: 0) + self.calculator ||= Calculator::FlatRate.new(preferred_amount: 0) end def has_distributor?(distributor) diff --git a/app/views/spree/admin/orders/_filters.html.haml b/app/views/spree/admin/orders/_filters.html.haml index 1d0885e897..3a33806f6f 100644 --- a/app/views/spree/admin/orders/_filters.html.haml +++ b/app/views/spree/admin/orders/_filters.html.haml @@ -4,10 +4,10 @@ .date-range-filter.field = label_tag nil, t(:date_range) .date-range-fields - = text_field_tag "q[completed_at_gt]", nil, class: 'datepicker', datepicker: 'q.completed_at_gt', 'ng-model' => 'q.completed_at_gt', :placeholder => t(:start) + = text_field_tag "q[completed_at_gteq]", nil, class: 'datepicker', datepicker: 'q.completed_at_gteq', 'ng-model' => 'q.completed_at_gteq', :placeholder => t(:start) %span.range-divider %i.icon-arrow-right - = text_field_tag "q[completed_at_lt]", nil, class: 'datepicker', datepicker: 'q.completed_at_lt', 'ng-model' => 'q.completed_at_lt', :placeholder => t(:stop) + = text_field_tag "q[completed_at_lteq]", nil, class: 'datepicker', datepicker: 'q.completed_at_lteq', 'ng-model' => 'q.completed_at_lteq', :placeholder => t(:stop) .field = label_tag nil, t(:status) = select_tag("q[state_eq]", diff --git a/config/application.rb b/config/application.rb index 547795ea5f..3d061b97be 100644 --- a/config/application.rb +++ b/config/application.rb @@ -49,30 +49,36 @@ module Openfoodnetwork # Register Spree calculators initializer 'spree.register.calculators' do |app| app.config.spree.calculators.shipping_methods = [ - Spree::Calculator::FlatPercentItemTotal, - Spree::Calculator::FlatRate, - Spree::Calculator::FlexiRate, - Spree::Calculator::PerItem, - Spree::Calculator::PriceSack, + Calculator::FlatPercentItemTotal, + Calculator::FlatRate, + Calculator::FlexiRate, + Calculator::PerItem, + Calculator::PriceSack, Calculator::Weight ] app.config.spree.calculators.add_class('enterprise_fees') config.spree.calculators.enterprise_fees = [ Calculator::FlatPercentPerItem, - Spree::Calculator::FlatRate, - Spree::Calculator::FlexiRate, - Spree::Calculator::PerItem, - Spree::Calculator::PriceSack, + Calculator::FlatRate, + Calculator::FlexiRate, + Calculator::PerItem, + Calculator::PriceSack, Calculator::Weight ] + app.config.spree.calculators.add_class('payment_methods') config.spree.calculators.payment_methods = [ - Spree::Calculator::FlatPercentItemTotal, - Spree::Calculator::FlatRate, - Spree::Calculator::FlexiRate, - Spree::Calculator::PerItem, - Spree::Calculator::PriceSack + Calculator::FlatPercentItemTotal, + Calculator::FlatRate, + Calculator::FlexiRate, + Calculator::PerItem, + Calculator::PriceSack + ] + + app.config.spree.calculators.add_class('tax_rates') + config.spree.calculators.tax_rates = [ + Calculator::DefaultTax ] end diff --git a/db/migrate/20200616162646_move_all_calculators_outside_the_spree_namespace.rb b/db/migrate/20200616162646_move_all_calculators_outside_the_spree_namespace.rb new file mode 100644 index 0000000000..b357242db2 --- /dev/null +++ b/db/migrate/20200616162646_move_all_calculators_outside_the_spree_namespace.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +class MoveAllCalculatorsOutsideTheSpreeNamespace < ActiveRecord::Migration + def up + convert_calculator("DefaultTax") + convert_calculator("FlatPercentItemTotal") + convert_calculator("FlatRate") + convert_calculator("FlexiRate") + convert_calculator("PerItem") + convert_calculator("PriceSack") + end + + def down + revert_calculator("DefaultTax") + revert_calculator("FlatPercentItemTotal") + revert_calculator("FlatRate") + revert_calculator("FlexiRate") + revert_calculator("PerItem") + revert_calculator("PriceSack") + end + + private + + def convert_calculator(calculator_base_name) + update_calculator("Spree::Calculator::" + calculator_base_name, + "Calculator::" + calculator_base_name) + end + + def revert_calculator(calculator_base_name) + update_calculator("Calculator::" + calculator_base_name, + "Spree::Calculator::" + calculator_base_name) + end + + def update_calculator(from, to) + Spree::Calculator.connection.execute( + "UPDATE spree_calculators SET type = '" + to + "' WHERE type = '" + from + "'" + ) + end +end diff --git a/engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb b/engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb index 9e7bd15c06..ad14253a98 100644 --- a/engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb +++ b/engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb @@ -99,11 +99,11 @@ module OrderManagement context "using flat rate calculators" do let(:shipping_method) { create(:shipping_method, - calculator: Spree::Calculator::FlatRate.new(preferred_amount: 12.34)) + calculator: Calculator::FlatRate.new(preferred_amount: 12.34)) } let(:payment_method) { create(:payment_method, - calculator: Spree::Calculator::FlatRate.new(preferred_amount: 9.12)) + calculator: Calculator::FlatRate.new(preferred_amount: 9.12)) } it "calculates fees based on the rates provided" do @@ -116,13 +116,13 @@ module OrderManagement context "using flat percent item total calculators" do let(:shipping_method) { create(:shipping_method, - calculator: Spree::Calculator::FlatPercentItemTotal.new( + calculator: Calculator::FlatPercentItemTotal.new( preferred_flat_percent: 10 )) } let(:payment_method) { create(:payment_method, - calculator: Spree::Calculator::FlatPercentItemTotal.new( + calculator: Calculator::FlatPercentItemTotal.new( preferred_flat_percent: 20 )) } @@ -154,11 +154,11 @@ module OrderManagement context "using per item calculators" do let(:shipping_method) { create(:shipping_method, - calculator: Spree::Calculator::PerItem.new(preferred_amount: 1.2)) + calculator: Calculator::PerItem.new(preferred_amount: 1.2)) } let(:payment_method) { create(:payment_method, - calculator: Spree::Calculator::PerItem.new(preferred_amount: 0.3)) + calculator: Calculator::PerItem.new(preferred_amount: 0.3)) } it "calculates fees based on the number of items and rate provided" do diff --git a/lib/tasks/sample_data/payment_method_factory.rb b/lib/tasks/sample_data/payment_method_factory.rb index 27dc5404cf..b8430c3d96 100644 --- a/lib/tasks/sample_data/payment_method_factory.rb +++ b/lib/tasks/sample_data/payment_method_factory.rb @@ -29,7 +29,7 @@ class PaymentMethodFactory enterprise, "Cash on collection", "Pay on collection!", - Spree::Calculator::FlatRate.new + Calculator::FlatRate.new ) end @@ -39,7 +39,7 @@ class PaymentMethodFactory enterprise, "Credit card (fake)", "We charge 1%, but won't ask for your details. ;-)", - Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 1) + Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 1) ) end diff --git a/lib/tasks/sample_data/shipping_method_factory.rb b/lib/tasks/sample_data/shipping_method_factory.rb index 5bfb382318..889765775a 100644 --- a/lib/tasks/sample_data/shipping_method_factory.rb +++ b/lib/tasks/sample_data/shipping_method_factory.rb @@ -39,7 +39,7 @@ class ShippingMethodFactory name: "Home delivery #{enterprise.name}", description: "yummy food delivered at your door", require_ship_address: true, - calculator_type: "Spree::Calculator::FlatRate" + calculator_type: "Calculator::FlatRate" ) delivery.calculator.preferred_amount = 2 delivery.calculator.save! diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index 054b37e2de..78678e03e2 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -192,7 +192,7 @@ describe Admin::EnterprisesController, type: :controller do id: tag_rule, type: "TagRule::DiscountOrder", preferred_customer_tags: "some,new,tags", - calculator_type: "Spree::Calculator::FlatPercentItemTotal", + calculator_type: "Calculator::FlatPercentItemTotal", calculator_attributes: { id: tag_rule.calculator.id, preferred_flat_percent: "15" } } } @@ -211,7 +211,7 @@ describe Admin::EnterprisesController, type: :controller do id: "", type: "TagRule::DiscountOrder", preferred_customer_tags: "tags,are,awesome", - calculator_type: "Spree::Calculator::FlatPercentItemTotal", + calculator_type: "Calculator::FlatPercentItemTotal", calculator_attributes: { id: "", preferred_flat_percent: "24" } } } diff --git a/spec/controllers/spree/admin/adjustments_controller_spec.rb b/spec/controllers/spree/admin/adjustments_controller_spec.rb index fc60008c00..4ad02b73c7 100644 --- a/spec/controllers/spree/admin/adjustments_controller_spec.rb +++ b/spec/controllers/spree/admin/adjustments_controller_spec.rb @@ -8,7 +8,7 @@ module Spree describe "setting included tax" do let(:order) { create(:order) } - let(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: Spree::Calculator::DefaultTax.new) } + let(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: Calculator::DefaultTax.new) } describe "creating an adjustment" do it "sets included tax to zero when no tax rate is specified" do diff --git a/spec/controllers/spree/admin/shipping_methods_controller_spec.rb b/spec/controllers/spree/admin/shipping_methods_controller_spec.rb index 531d01c11e..105d55563c 100644 --- a/spec/controllers/spree/admin/shipping_methods_controller_spec.rb +++ b/spec/controllers/spree/admin/shipping_methods_controller_spec.rb @@ -50,7 +50,7 @@ describe Spree::Admin::ShippingMethodsController, type: :controller do end it "updates details of a FlexiRate calculator" do - shipping_method.calculator = Spree::Calculator::FlexiRate.new(calculable: shipping_method) + shipping_method.calculator = Calculator::FlexiRate.new(calculable: shipping_method) params[:shipping_method][:calculator_attributes][:preferred_first_item] = 10 params[:shipping_method][:calculator_attributes][:preferred_additional_item] = 20 params[:shipping_method][:calculator_attributes][:preferred_max_items] = 30 @@ -63,7 +63,7 @@ describe Spree::Admin::ShippingMethodsController, type: :controller do end it "updates details of a PriceSack calculator" do - shipping_method.calculator = Spree::Calculator::PriceSack.new(calculable: shipping_method) + shipping_method.calculator = Calculator::PriceSack.new(calculable: shipping_method) params[:shipping_method][:calculator_attributes][:preferred_minimal_amount] = 10 params[:shipping_method][:calculator_attributes][:preferred_normal_amount] = 20 params[:shipping_method][:calculator_attributes][:preferred_discount_amount] = 30 diff --git a/spec/factories/calculated_adjustment_factory.rb b/spec/factories/calculated_adjustment_factory.rb index 3e9c6f5cc2..5654b62746 100644 --- a/spec/factories/calculated_adjustment_factory.rb +++ b/spec/factories/calculated_adjustment_factory.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :calculator_flat_rate, class: Spree::Calculator::FlatRate do + factory :calculator_flat_rate, class: Calculator::FlatRate do preferred_amount { generate(:calculator_amount) } end end diff --git a/spec/factories/calculator_factory.rb b/spec/factories/calculator_factory.rb index 25dd9b0a13..0c8acaf3e5 100644 --- a/spec/factories/calculator_factory.rb +++ b/spec/factories/calculator_factory.rb @@ -1,6 +1,6 @@ FactoryBot.define do sequence(:calculator_amount) - factory :calculator_per_item, class: Spree::Calculator::PerItem do + factory :calculator_per_item, class: Calculator::PerItem do preferred_amount { generate(:calculator_amount) } end diff --git a/spec/factories/product_factory.rb b/spec/factories/product_factory.rb index e546abbac4..3ca3fdb77e 100644 --- a/spec/factories/product_factory.rb +++ b/spec/factories/product_factory.rb @@ -36,7 +36,7 @@ FactoryBot.define do create(:tax_rate, amount: proxy.tax_rate_amount, tax_category: product.tax_category, included_in_price: true, - calculator: Spree::Calculator::DefaultTax.new, + calculator: Calculator::DefaultTax.new, zone: proxy.zone, name: proxy.tax_rate_name) end diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index 516dfb6b66..87cec39be9 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -9,13 +9,13 @@ FactoryBot.define do end trait :flat_rate do - calculator { Spree::Calculator::FlatRate.new(preferred_amount: 50.0) } + calculator { Calculator::FlatRate.new(preferred_amount: 50.0) } end trait :expensive_name do name { "Shipping" } description { "Expensive" } - calculator { Spree::Calculator::FlatRate.new(preferred_amount: 100.55) } + calculator { Calculator::FlatRate.new(preferred_amount: 100.55) } end trait :distributor do diff --git a/spec/factories/tag_rule_factory.rb b/spec/factories/tag_rule_factory.rb index 9663913820..4a4f5b1130 100644 --- a/spec/factories/tag_rule_factory.rb +++ b/spec/factories/tag_rule_factory.rb @@ -18,7 +18,7 @@ FactoryBot.define do factory :tag_rule, class: TagRule::DiscountOrder do enterprise { FactoryBot.create :distributor_enterprise } before(:create) do |tr| - tr.calculator = Spree::Calculator::FlatPercentItemTotal.new(calculable: tr) + tr.calculator = Calculator::FlatPercentItemTotal.new(calculable: tr) end end end diff --git a/spec/features/admin/order_cycles/complex_updating_specific_time_spec.rb b/spec/features/admin/order_cycles/complex_updating_specific_time_spec.rb index 258caa1080..2094a83f7e 100644 --- a/spec/features/admin/order_cycles/complex_updating_specific_time_spec.rb +++ b/spec/features/admin/order_cycles/complex_updating_specific_time_spec.rb @@ -69,7 +69,8 @@ feature ' select 'My supplier', from: 'new_supplier_id' click_button 'Add supplier' expect(page).to have_selector("table.exchanges tr.supplier", text: "My supplier") - page.all("table.exchanges tr.supplier td.products").each(&:click) + + open_all_exchange_product_tabs expect(page).to have_selector "#order_cycle_incoming_exchange_1_variants_#{initial_variants.last.id}", visible: true page.find("#order_cycle_incoming_exchange_1_variants_#{initial_variants.last.id}", visible: true).click # uncheck (with visible:true filter) @@ -87,6 +88,7 @@ feature ' select 'Supplier fee 2', from: 'order_cycle_incoming_exchange_2_enterprise_fees_0_enterprise_fee_id' click_button 'Save and Next' + expect(page).to have_content 'Your order cycle has been updated.' # And I add a distributor and some products select 'My distributor', from: 'new_distributor_id' @@ -105,12 +107,7 @@ feature ' find(:css, "tags-input .tags input").set "wholesale\n" end - exchange_rows = page.all("table.exchanges tbody") - exchange_rows.each do |exchange_row| - exchange_row.find("td.products").click - # Wait for the products panel to be visible. - expect(exchange_row).to have_selector "tr", count: 2 - end + open_all_exchange_product_tabs uncheck "order_cycle_outgoing_exchange_2_variants_#{v1.id}" check "order_cycle_outgoing_exchange_2_variants_#{v2.id}" @@ -165,4 +162,15 @@ feature ' def wait_for_edit_form_to_load_order_cycle(order_cycle) expect(page).to have_field "order_cycle_name", with: order_cycle.name end + + def open_all_exchange_product_tabs + exchange_rows = page.all("table.exchanges tbody") + exchange_rows.each do |exchange_row| + exchange_row.find("td.products").click + within(exchange_row) do + # Wait for the products panel to be visible. + expect(page).to have_selector ".exchange-products" + end + end + end end diff --git a/spec/features/admin/payments_spec.rb b/spec/features/admin/payments_spec.rb index 2ea666f121..adce888db2 100644 --- a/spec/features/admin/payments_spec.rb +++ b/spec/features/admin/payments_spec.rb @@ -21,7 +21,7 @@ feature ' # This calculator doesn't handle a `nil` order well. # That has been useful in finding bugs. ;-) - payment_method.calculator = Spree::Calculator::FlatPercentItemTotal.new + payment_method.calculator = Calculator::FlatPercentItemTotal.new payment_method.save! end diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 5ebd118fd7..b8eb70f010 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -170,7 +170,7 @@ feature ' let(:user1) { create_enterprise_user enterprises: [distributor1] } let(:user2) { create_enterprise_user enterprises: [distributor2] } let!(:shipping_method) { create(:shipping_method_with, :expensive_name, distributors: [distributor1]) } - let(:enterprise_fee) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 120.0)) } + let(:enterprise_fee) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) } let(:order_cycle) { create(:simple_order_cycle, coordinator: distributor1, coordinator_fees: [enterprise_fee], distributors: [distributor1], variants: [product1.master]) } let!(:zone) { create(:zone_with_member) } @@ -384,8 +384,8 @@ feature ' let(:shipping_method) { create(:shipping_method_with, :expensive_name) } let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) } - let(:enterprise_fee1) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 10)) } - let(:enterprise_fee2) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 20)) } + let(:enterprise_fee1) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Calculator::FlatRate.new(preferred_amount: 10)) } + let(:enterprise_fee2) { create(:enterprise_fee, enterprise: user1.enterprises.first, tax_category: product2.tax_category, calculator: Calculator::FlatRate.new(preferred_amount: 20)) } let(:order_cycle) { create(:simple_order_cycle, coordinator: distributor1, coordinator_fees: [enterprise_fee1, enterprise_fee2], distributors: [distributor1], variants: [product1.master]) } let!(:zone) { create(:zone_with_member) } diff --git a/spec/features/consumer/shopping/cart_spec.rb b/spec/features/consumer/shopping/cart_spec.rb index 194b202d26..0bc4523a24 100644 --- a/spec/features/consumer/shopping/cart_spec.rb +++ b/spec/features/consumer/shopping/cart_spec.rb @@ -80,7 +80,7 @@ feature "full-page cart", js: true do describe "admin and handling flat fees" do context "when there are fees" do let(:handling_fee) { - create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 1), + create(:enterprise_fee, calculator: Calculator::FlatRate.new(preferred_amount: 1), enterprise: order_cycle.coordinator, fee_type: 'admin') } diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 76983d030b..342c699a43 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -26,11 +26,11 @@ feature "As a consumer I want to check out my cart", js: true do end describe "with shipping and payment methods" do - let(:free_shipping) { create(:shipping_method, require_ship_address: true, name: "Frogs", description: "yellow", calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0.00)) } - let(:shipping_with_fee) { create(:shipping_method, require_ship_address: false, name: "Donkeys", description: "blue", calculator: Spree::Calculator::FlatRate.new(preferred_amount: 4.56)) } + let(:free_shipping) { create(:shipping_method, require_ship_address: true, name: "Frogs", description: "yellow", calculator: Calculator::FlatRate.new(preferred_amount: 0.00)) } + let(:shipping_with_fee) { create(:shipping_method, require_ship_address: false, name: "Donkeys", description: "blue", calculator: Calculator::FlatRate.new(preferred_amount: 4.56)) } let(:tagged_shipping) { create(:shipping_method, require_ship_address: false, name: "Local", tag_list: "local") } let!(:check_without_fee) { create(:payment_method, distributors: [distributor], name: "Roger rabbit", type: "Spree::PaymentMethod::Check") } - let!(:check_with_fee) { create(:payment_method, distributors: [distributor], calculator: Spree::Calculator::FlatRate.new(preferred_amount: 5.67)) } + let!(:check_with_fee) { create(:payment_method, distributors: [distributor], calculator: Calculator::FlatRate.new(preferred_amount: 5.67)) } let!(:paypal) do Spree::Gateway::PayPalExpress.create!(name: "Paypal", environment: 'test', distributor_ids: [distributor.id]).tap do |pm| pm.preferred_login = 'devnull-facilitator_api1.rohanmitchell.com' diff --git a/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee b/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee index 2fa1efbac3..0ccbbe3b9a 100644 --- a/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/orders/controllers/orders_controller_spec.js.coffee @@ -63,3 +63,14 @@ describe "ordersCtrl", -> expect(Orders.index).toHaveBeenCalledWith(jasmine.objectContaining({ 'q[order_cycle_id_in][]': ['4', '5'] })) + + it "filters orders on inclusive dates", -> + $scope['q']['completed_at_gteq'] = '2020-06-08' + $scope['q']['completed_at_lteq'] = '2020-06-09' + + $scope.fetchResults() + + expect(Orders.index).toHaveBeenCalledWith(jasmine.objectContaining({ + 'q[completed_at_gteq]': '2020-06-08 00:00:00' + 'q[completed_at_lteq]': '2020-06-09 23:59:59' + })) diff --git a/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb b/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb index c1e361503e..2f613393f9 100644 --- a/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb +++ b/spec/lib/open_food_network/enterprise_fee_calculator_spec.rb @@ -16,7 +16,7 @@ module OpenFoodNetwork describe "summing all the per-item fees for the variant in the specified hub + order cycle" do let(:enterprise_fee1) { create(:enterprise_fee, amount: 20) } let(:enterprise_fee2) { create(:enterprise_fee, amount: 3) } - let(:enterprise_fee3) { create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 2)) } + let(:enterprise_fee3) { create(:enterprise_fee, calculator: Calculator::FlatRate.new(preferred_amount: 2)) } describe "supplier fees" do let!(:exchange1) { @@ -131,7 +131,7 @@ module OpenFoodNetwork let(:order) { create(:order, distributor: distributor, order_cycle: order_cycle) } let!(:line_item) { create(:line_item, order: order, variant: product1.master) } let(:enterprise_fee_line_item) { create(:enterprise_fee) } - let(:enterprise_fee_order) { create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 2)) } + let(:enterprise_fee_order) { create(:enterprise_fee, calculator: Calculator::FlatRate.new(preferred_amount: 2)) } let!(:exchange) { create(:exchange, order_cycle: order_cycle, sender: coordinator, receiver: distributor, incoming: false, variants: [product1.master]) } before { order.reload } diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index 66dd425f95..57c548f73e 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -7,7 +7,7 @@ describe ProducerMailer, type: :mailer do before { setup_email } let!(:zone) { create(:zone_with_member) } - let!(:tax_rate) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: zone, amount: 0.1) } + let!(:tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.1) } let!(:tax_category) { create(:tax_category, tax_rates: [tax_rate]) } let(:s1) { create(:supplier_enterprise) } let(:s2) { create(:supplier_enterprise) } diff --git a/spec/models/spree/calculator/flat_percent_item_total_spec.rb b/spec/models/calculator/flat_percent_item_total_spec.rb similarity index 84% rename from spec/models/spree/calculator/flat_percent_item_total_spec.rb rename to spec/models/calculator/flat_percent_item_total_spec.rb index ec4b54fc70..b0ee8f1aa0 100644 --- a/spec/models/spree/calculator/flat_percent_item_total_spec.rb +++ b/spec/models/calculator/flat_percent_item_total_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Spree::Calculator::FlatPercentItemTotal do - let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new } +describe Calculator::FlatPercentItemTotal do + let(:calculator) { Calculator::FlatPercentItemTotal.new } let(:line_item) { build(:line_item, price: 10, quantity: 1) } before { allow(calculator).to receive_messages preferred_flat_percent: 10 } diff --git a/spec/models/spree/calculator/flat_rate_spec.rb b/spec/models/calculator/flat_rate_spec.rb similarity index 70% rename from spec/models/spree/calculator/flat_rate_spec.rb rename to spec/models/calculator/flat_rate_spec.rb index 3e3bba064b..5036d6b180 100644 --- a/spec/models/spree/calculator/flat_rate_spec.rb +++ b/spec/models/calculator/flat_rate_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Spree::Calculator::FlatRate do - let(:calculator) { Spree::Calculator::FlatRate.new } +describe Calculator::FlatRate do + let(:calculator) { Calculator::FlatRate.new } before { allow(calculator).to receive_messages preferred_amount: 10 } diff --git a/spec/models/spree/calculator/flexi_rate_spec.rb b/spec/models/calculator/flexi_rate_spec.rb similarity index 81% rename from spec/models/spree/calculator/flexi_rate_spec.rb rename to spec/models/calculator/flexi_rate_spec.rb index 0440e87ca8..9192aecdb5 100644 --- a/spec/models/spree/calculator/flexi_rate_spec.rb +++ b/spec/models/calculator/flexi_rate_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe Spree::Calculator::FlexiRate do +describe Calculator::FlexiRate do let(:line_item) { build(:line_item, quantity: quantity) } let(:calculator) do - Spree::Calculator::FlexiRate.new( + Calculator::FlexiRate.new( preferred_first_item: 2, preferred_additional_item: 1, preferred_max_items: 3 @@ -27,7 +27,7 @@ describe Spree::Calculator::FlexiRate do end it "allows creation of new object with all the attributes" do - Spree::Calculator::FlexiRate.new(preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1) + Calculator::FlexiRate.new(preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1) end context "extends LocalizedNumber" do diff --git a/spec/models/spree/calculator/per_item_spec.rb b/spec/models/calculator/per_item_spec.rb similarity index 80% rename from spec/models/spree/calculator/per_item_spec.rb rename to spec/models/calculator/per_item_spec.rb index 640c4b32f6..7ce41251f4 100644 --- a/spec/models/spree/calculator/per_item_spec.rb +++ b/spec/models/calculator/per_item_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Spree::Calculator::PerItem do - let(:calculator) { Spree::Calculator::PerItem.new(preferred_amount: 10) } +describe Calculator::PerItem do + let(:calculator) { Calculator::PerItem.new(preferred_amount: 10) } let(:shipping_calculable) { double(:calculable) } let(:line_item) { build(:line_item, quantity: 5) } diff --git a/spec/models/spree/calculator/price_sack_spec.rb b/spec/models/calculator/price_sack_spec.rb similarity index 95% rename from spec/models/spree/calculator/price_sack_spec.rb rename to spec/models/calculator/price_sack_spec.rb index 703bea1531..83a6ae84fb 100644 --- a/spec/models/spree/calculator/price_sack_spec.rb +++ b/spec/models/calculator/price_sack_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' -describe Spree::Calculator::PriceSack do +describe Calculator::PriceSack do let(:calculator) do - calculator = Spree::Calculator::PriceSack.new + calculator = Calculator::PriceSack.new calculator.preferred_minimal_amount = 5 calculator.preferred_normal_amount = 10 calculator.preferred_discount_amount = 1 diff --git a/spec/models/enterprise_fee_spec.rb b/spec/models/enterprise_fee_spec.rb index a422358bb8..903d22f757 100644 --- a/spec/models/enterprise_fee_spec.rb +++ b/spec/models/enterprise_fee_spec.rb @@ -60,17 +60,17 @@ describe EnterpriseFee do describe "scopes" do describe "finding per-item enterprise fees" do it "does not return fees with FlatRate, FlexiRate and PriceSack calculators" do - create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new) - create(:enterprise_fee, calculator: Spree::Calculator::FlexiRate.new) - create(:enterprise_fee, calculator: Spree::Calculator::PriceSack.new) + create(:enterprise_fee, calculator: Calculator::FlatRate.new) + create(:enterprise_fee, calculator: Calculator::FlexiRate.new) + create(:enterprise_fee, calculator: Calculator::PriceSack.new) expect(EnterpriseFee.per_item).to be_empty end it "returns fees with any other calculator" do - ef1 = create(:enterprise_fee, calculator: Spree::Calculator::DefaultTax.new) + ef1 = create(:enterprise_fee, calculator: Calculator::DefaultTax.new) ef2 = create(:enterprise_fee, calculator: Calculator::FlatPercentPerItem.new) - ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PerItem.new) + ef3 = create(:enterprise_fee, calculator: Calculator::PerItem.new) expect(EnterpriseFee.per_item).to match_array [ef1, ef2, ef3] end @@ -78,17 +78,17 @@ describe EnterpriseFee do describe "finding per-order enterprise fees" do it "returns fees with FlatRate, FlexiRate and PriceSack calculators" do - ef1 = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new) - ef2 = create(:enterprise_fee, calculator: Spree::Calculator::FlexiRate.new) - ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PriceSack.new) + ef1 = create(:enterprise_fee, calculator: Calculator::FlatRate.new) + ef2 = create(:enterprise_fee, calculator: Calculator::FlexiRate.new) + ef3 = create(:enterprise_fee, calculator: Calculator::PriceSack.new) expect(EnterpriseFee.per_order).to match_array [ef1, ef2, ef3] end it "does not return fees with any other calculator" do - ef1 = create(:enterprise_fee, calculator: Spree::Calculator::DefaultTax.new) + ef1 = create(:enterprise_fee, calculator: Calculator::DefaultTax.new) ef2 = create(:enterprise_fee, calculator: Calculator::FlatPercentPerItem.new) - ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PerItem.new) + ef3 = create(:enterprise_fee, calculator: Calculator::PerItem.new) expect(EnterpriseFee.per_order).to be_empty end diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 57047457ac..0ea3eea273 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -127,7 +127,7 @@ module Spree describe "EnterpriseFee adjustments" do let(:zone) { create(:zone_with_member) } - let(:fee_tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.1) } + let(:fee_tax_rate) { create(:tax_rate, included_in_price: true, calculator: ::Calculator::DefaultTax.new, zone: zone, amount: 0.1) } let(:fee_tax_category) { create(:tax_category, tax_rates: [fee_tax_rate]) } let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) } @@ -143,7 +143,7 @@ module Spree end context "when enterprise fees are taxed per-order" do - let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: fee_tax_category, calculator: Calculator::FlatRate.new(preferred_amount: 50.0)) } + let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: fee_tax_category, calculator: ::Calculator::FlatRate.new(preferred_amount: 50.0)) } describe "when the tax rate includes the tax in the price" do it "records the tax on the enterprise fee adjustments" do @@ -181,7 +181,7 @@ module Spree end context "when enterprise fees are taxed per-item" do - let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: fee_tax_category, calculator: Calculator::PerItem.new(preferred_amount: 50.0)) } + let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: fee_tax_category, calculator: ::Calculator::PerItem.new(preferred_amount: 50.0)) } describe "when the tax rate includes the tax in the price" do it "records the tax on the enterprise fee adjustments" do @@ -205,7 +205,7 @@ module Spree end context "when enterprise fees inherit their tax_category from the product they are applied to" do - let(:product_tax_rate) { create(:tax_rate, included_in_price: true, calculator: Calculator::DefaultTax.new, zone: zone, amount: 0.2) } + let(:product_tax_rate) { create(:tax_rate, included_in_price: true, calculator: ::Calculator::DefaultTax.new, zone: zone, amount: 0.2) } let(:product_tax_category) { create(:tax_category, tax_rates: [product_tax_rate]) } before do @@ -216,7 +216,7 @@ module Spree end context "when enterprise fees are taxed per-order" do - let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, inherits_tax_category: true, calculator: Calculator::FlatRate.new(preferred_amount: 50.0)) } + let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, inherits_tax_category: true, calculator: ::Calculator::FlatRate.new(preferred_amount: 50.0)) } describe "when the tax rate includes the tax in the price" do it "records no tax on the enterprise fee adjustments" do @@ -246,7 +246,7 @@ module Spree end context "when enterprise fees are taxed per-item" do - let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, inherits_tax_category: true, calculator: Calculator::PerItem.new(preferred_amount: 50.0)) } + let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, inherits_tax_category: true, calculator: ::Calculator::PerItem.new(preferred_amount: 50.0)) } describe "when the tax rate includes the tax in the price" do it "records the tax on the enterprise fee adjustments" do diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index ca4a5353d9..bef32dc8d7 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -35,7 +35,7 @@ module Spree end describe "finding line items with and without tax" do - let(:tax_rate) { create(:tax_rate, calculator: Spree::Calculator::DefaultTax.new) } + let(:tax_rate) { create(:tax_rate, calculator: Calculator::DefaultTax.new) } let!(:adjustment1) { create(:adjustment, originator: tax_rate, label: "TR", amount: 123, included_tax: 10.00) } before do @@ -311,7 +311,7 @@ module Spree describe "tax" do let(:li_no_tax) { create(:line_item) } let(:li_tax) { create(:line_item) } - let(:tax_rate) { create(:tax_rate, calculator: Spree::Calculator::DefaultTax.new) } + let(:tax_rate) { create(:tax_rate, calculator: Calculator::DefaultTax.new) } let!(:adjustment) { create(:adjustment, adjustable: li_tax, originator: tax_rate, label: "TR", amount: 123, included_tax: 10.00) } context "checking if a line item has tax included" do diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 11ac15fa27..0c2c3d33e5 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -137,7 +137,7 @@ describe Spree::Order do let(:li) { create(:line_item, order: o) } it "returns the sum of eligible enterprise fee adjustments" do - ef = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new ) + ef = create(:enterprise_fee, calculator: Calculator::FlatRate.new ) ef.calculator.set_preference :amount, 123.45 a = ef.create_adjustment("adjustment", o, o, true) @@ -145,7 +145,7 @@ describe Spree::Order do end it "does not include ineligible adjustments" do - ef = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new ) + ef = create(:enterprise_fee, calculator: Calculator::FlatRate.new ) ef.calculator.set_preference :amount, 123.45 a = ef.create_adjustment("adjustment", o, o, true) @@ -155,7 +155,7 @@ describe Spree::Order do end it "does not include adjustments that do not originate from enterprise fees" do - sm = create(:shipping_method, calculator: Spree::Calculator::FlatRate.new ) + sm = create(:shipping_method, calculator: Calculator::FlatRate.new ) sm.calculator.set_preference :amount, 123.45 sm.create_adjustment("adjustment", o, o, true) @@ -163,7 +163,7 @@ describe Spree::Order do end it "does not include adjustments whose source is a line item" do - ef = create(:enterprise_fee, calculator: Spree::Calculator::PerItem.new ) + ef = create(:enterprise_fee, calculator: Calculator::PerItem.new ) ef.calculator.set_preference :amount, 123.45 ef.create_adjustment("adjustment", li.order, li, true) @@ -669,7 +669,7 @@ describe Spree::Order do end context "changing the shipping method to one without fees" do - let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0)) } + let(:shipping_method) { create(:shipping_method, calculator: Calculator::FlatRate.new(preferred_amount: 0)) } it "updates shipping fees" do order.shipments = [create(:shipment_with, :shipping_method, shipping_method: shipping_method)] @@ -681,7 +681,7 @@ describe Spree::Order do end context "changing the payment method to one without fees" do - let(:payment_method) { create(:payment_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0)) } + let(:payment_method) { create(:payment_method, calculator: Calculator::FlatRate.new(preferred_amount: 0)) } it "removes transaction fees" do # Change the payment method diff --git a/spec/models/spree/payment_spec.rb b/spec/models/spree/payment_spec.rb index 46a493fa96..1ca723566c 100644 --- a/spec/models/spree/payment_spec.rb +++ b/spec/models/spree/payment_spec.rb @@ -136,12 +136,11 @@ module Spree context "when order-based calculator" do let!(:shop) { create(:enterprise) } let!(:payment_method) { create(:payment_method, calculator: calculator) } - let!(:calculator) do - Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) + ::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) end - context "when order complete and inventory tracking enabled" do + context "when order complete" do let!(:order) { create(:completed_order_with_totals, distributor: shop) } let!(:variant) { order.line_items.first.variant } let!(:inventory_item) { create(:inventory_item, enterprise: shop, variant: variant) } @@ -159,7 +158,7 @@ module Spree let(:shop) { create(:enterprise) } let(:payment_method) { create(:stripe_payment_method, distributor_ids: [create(:distributor_enterprise).id], preferred_enterprise_id: shop.id) } let(:payment) { create(:payment, order: order, payment_method: payment_method, amount: order.total) } - let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } + let(:calculator) { ::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } before do payment_method.calculator = calculator diff --git a/spec/models/spree/tax_rate_spec.rb b/spec/models/spree/tax_rate_spec.rb index 888b25c890..61938c5d0f 100644 --- a/spec/models/spree/tax_rate_spec.rb +++ b/spec/models/spree/tax_rate_spec.rb @@ -33,7 +33,7 @@ module Spree end describe "ensuring that tax rate is marked as tax included_in_price" do - let(:tax_rate) { create(:tax_rate, included_in_price: false, calculator: Spree::Calculator::DefaultTax.new) } + let(:tax_rate) { create(:tax_rate, included_in_price: false, calculator: Calculator::DefaultTax.new) } it "sets included_in_price to true" do tax_rate.send(:with_tax_included_in_price) do diff --git a/spec/models/tag_rule/discount_order_spec.rb b/spec/models/tag_rule/discount_order_spec.rb index 2a1260f7e3..831f2faf01 100644 --- a/spec/models/tag_rule/discount_order_spec.rb +++ b/spec/models/tag_rule/discount_order_spec.rb @@ -73,7 +73,7 @@ describe TagRule::DiscountOrder, type: :model do end context "when shipping charges apply" do - let!(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new( preferred_amount: 25.00 ) ) } + let!(:shipping_method) { create(:shipping_method, calculator: Calculator::FlatRate.new( preferred_amount: 25.00 ) ) } before do shipping_method.create_adjustment("Shipping", order, order, true) end diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index 22b93a06d4..c6620351a2 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -33,7 +33,7 @@ describe VariantOverride do expect(VariantOverride.indexed(hub2)).to eq( variant => vo2 ) end - it "does not include overrides for soft-deleted variants" do + xit "does not include overrides for soft-deleted variants" do variant.delete expect(VariantOverride.indexed(hub1)).to eq( nil => vo1 ) end diff --git a/spec/requests/checkout/failed_checkout_spec.rb b/spec/requests/checkout/failed_checkout_spec.rb index f45efe3951..41062d97d0 100644 --- a/spec/requests/checkout/failed_checkout_spec.rb +++ b/spec/requests/checkout/failed_checkout_spec.rb @@ -32,7 +32,7 @@ describe "checking out an order that initially fails", type: :request do end context "when shipping and payment fees apply" do - let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } + let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } before do payment_method.calculator = calculator.dup diff --git a/spec/requests/checkout/paypal_spec.rb b/spec/requests/checkout/paypal_spec.rb index 96c1baf4e9..e309d0ea5d 100644 --- a/spec/requests/checkout/paypal_spec.rb +++ b/spec/requests/checkout/paypal_spec.rb @@ -50,7 +50,7 @@ describe "checking out an order with a paypal express payment method", type: :re end context "with a flat percent calculator" do - let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } + let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } before do payment_method.calculator = calculator diff --git a/spec/requests/checkout/stripe_connect_spec.rb b/spec/requests/checkout/stripe_connect_spec.rb index 5c1561c13c..000b2250a8 100644 --- a/spec/requests/checkout/stripe_connect_spec.rb +++ b/spec/requests/checkout/stripe_connect_spec.rb @@ -10,7 +10,7 @@ describe "checking out an order with a Stripe Connect payment method", type: :re let!(:shipping_method) do create( :shipping_method, - calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0), + calculator: Calculator::FlatRate.new(preferred_amount: 0), distributors: [enterprise] ) end diff --git a/spec/requests/checkout/stripe_sca_spec.rb b/spec/requests/checkout/stripe_sca_spec.rb index 5018ad321c..d2734f66d4 100644 --- a/spec/requests/checkout/stripe_sca_spec.rb +++ b/spec/requests/checkout/stripe_sca_spec.rb @@ -12,7 +12,7 @@ describe "checking out an order with a Stripe SCA payment method", type: :reques let!(:shipping_method) do create( :shipping_method, - calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0), + calculator: Calculator::FlatRate.new(preferred_amount: 0), distributors: [enterprise] ) end diff --git a/spec/services/order_tax_adjustments_fetcher_spec.rb b/spec/services/order_tax_adjustments_fetcher_spec.rb index 2b06e5c4a2..6707aa1abf 100644 --- a/spec/services/order_tax_adjustments_fetcher_spec.rb +++ b/spec/services/order_tax_adjustments_fetcher_spec.rb @@ -4,24 +4,57 @@ require "spec_helper" describe OrderTaxAdjustmentsFetcher do describe "#totals" do - let(:zone) { create(:zone_with_member) } - let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) } + let(:zone) { create(:zone_with_member) } + let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) } - let(:tax_rate10) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.1, zone: zone) } - let(:tax_rate15) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.15, zone: zone) } - let(:tax_rate20) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.2, zone: zone) } - let(:tax_rate25) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.25, zone: zone) } - let(:tax_category10) { create(:tax_category, tax_rates: [tax_rate10]) } - let(:tax_category15) { create(:tax_category, tax_rates: [tax_rate15]) } - let(:tax_category20) { create(:tax_category, tax_rates: [tax_rate20]) } - let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) } + let(:tax_rate10) do + create(:tax_rate, included_in_price: true, + calculator: Calculator::DefaultTax.new, + amount: 0.1, + zone: zone) + end + let(:tax_rate15) do + create(:tax_rate, included_in_price: true, + calculator: Calculator::DefaultTax.new, + amount: 0.15, + zone: zone) + end + let(:tax_rate20) do + create(:tax_rate, included_in_price: true, + calculator: Calculator::DefaultTax.new, + amount: 0.2, + zone: zone) + end + let(:tax_rate25) do + create(:tax_rate, included_in_price: true, + calculator: Calculator::DefaultTax.new, + amount: 0.25, + zone: zone) + end + let(:tax_category10) { create(:tax_category, tax_rates: [tax_rate10]) } + let(:tax_category15) { create(:tax_category, tax_rates: [tax_rate15]) } + let(:tax_category20) { create(:tax_category, tax_rates: [tax_rate20]) } + let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) } - let(:variant) { create(:variant, product: create(:product, tax_category: tax_category10)) } - let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: tax_category20, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 48.0)) } - let(:additional_adjustment) { create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0)) } + let(:variant) do + create(:variant, product: create(:product, tax_category: tax_category10)) + end + let(:enterprise_fee) do + create(:enterprise_fee, enterprise: coordinator, + tax_category: tax_category20, + calculator: Calculator::FlatRate.new(preferred_amount: 48.0)) + end + let(:additional_adjustment) do + create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0)) + end - let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator, coordinator_fees: [enterprise_fee], distributors: [coordinator], variants: [variant]) } - let(:line_item) { create(:line_item, variant: variant, price: 44.0) } + let(:order_cycle) do + create(:simple_order_cycle, coordinator: coordinator, + coordinator_fees: [enterprise_fee], + distributors: [coordinator], + variants: [variant]) + end + let(:line_item) { create(:line_item, variant: variant, price: 44.0) } let(:order) do create( :order, @@ -38,8 +71,12 @@ describe OrderTaxAdjustmentsFetcher do allow(Spree::Config).to receive(:shipping_tax_rate).and_return(tax_rate15.amount) end - let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 46.0)) } - let!(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } + let(:shipping_method) do + create(:shipping_method, calculator: Calculator::FlatRate.new(preferred_amount: 46.0)) + end + let!(:shipment) do + create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) + end before do order.create_tax_charge! diff --git a/spec/services/tax_rate_finder_spec.rb b/spec/services/tax_rate_finder_spec.rb index 3f5daadfcf..6881d66ebf 100644 --- a/spec/services/tax_rate_finder_spec.rb +++ b/spec/services/tax_rate_finder_spec.rb @@ -66,7 +66,7 @@ describe TaxRateFinder do create( :tax_rate, amount: amount, - calculator: Spree::Calculator::DefaultTax.new, + calculator: Calculator::DefaultTax.new, zone: zone ) end diff --git a/spec/support/request/authentication_workflow.rb b/spec/support/request/authentication_workflow.rb index a654cf9934..8968c84da5 100644 --- a/spec/support/request/authentication_workflow.rb +++ b/spec/support/request/authentication_workflow.rb @@ -46,23 +46,6 @@ module AuthenticationWorkflow # click_button 'Login' end - def login_to_consumer_section - user_role = Spree::Role.find_or_create_by!(name: 'user') - user = create_enterprise_user( - email: 'someone@ofn.org', - password: 'passw0rd', - password_confirmation: 'passw0rd', - remember_me: false, - persistence_token: 'pass', - login: 'someone@ofn.org' - ) - - user.spree_roles << user_role - - visit spree.login_path - fill_in_and_submit_login_form user - end - def fill_in_and_submit_login_form(user) fill_in "email", with: user.email fill_in "password", with: user.password