mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Move all calculators outside the spree namespace
This commit is contained in:
@@ -5,7 +5,7 @@ require 'open_food_network/enterprise_fee_calculator'
|
||||
|
||||
module Spree
|
||||
module Calculator
|
||||
class DefaultTax < Calculator
|
||||
class DefaultTax < Spree::Calculator
|
||||
def self.description
|
||||
Spree.t(:default_tax)
|
||||
end
|
||||
24
app/models/calculator/flat_percent_item_total.rb
Normal file
24
app/models/calculator/flat_percent_item_total.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
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
|
||||
(value * 100).round.to_f / 100
|
||||
end
|
||||
end
|
||||
end
|
||||
23
app/models/calculator/flat_rate.rb
Normal file
23
app/models/calculator/flat_rate.rb
Normal file
@@ -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
|
||||
43
app/models/calculator/flexi_rate.rb
Normal file
43
app/models/calculator/flexi_rate.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
# 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)
|
||||
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.zero?
|
||||
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
|
||||
29
app/models/calculator/per_item.rb
Normal file
29
app/models/calculator/per_item.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
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
|
||||
I18n.t(:flat_rate_per_item)
|
||||
end
|
||||
|
||||
def compute(object = nil)
|
||||
return 0 if object.nil?
|
||||
|
||||
number_of_line_items = line_items_for(object).reduce(0) do |sum, line_item|
|
||||
value_to_add = line_item.quantity
|
||||
sum + value_to_add
|
||||
end
|
||||
preferred_amount * number_of_line_items
|
||||
end
|
||||
end
|
||||
end
|
||||
38
app/models/calculator/price_sack.rb
Normal file
38
app/models/calculator/price_sack.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
# For #to_d method on Ruby 1.8
|
||||
require 'bigdecimal/util'
|
||||
require 'spree/localized_number'
|
||||
|
||||
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
|
||||
|
||||
def self.description
|
||||
I18n.t(:price_sack)
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
min = preferred_minimal_amount.to_f
|
||||
order_amount = line_items_for(object).map { |x| x.price * x.quantity }.sum
|
||||
|
||||
if order_amount < min
|
||||
cost = preferred_normal_amount.to_f
|
||||
elsif order_amount >= min
|
||||
cost = preferred_discount_amount.to_f
|
||||
end
|
||||
|
||||
cost
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -11,7 +11,7 @@ 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
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
module Spree
|
||||
module Calculator
|
||||
class FlatPercentItemTotal < 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
|
||||
(value * 100).round.to_f / 100
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,25 +0,0 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
module Spree
|
||||
module Calculator
|
||||
class FlatRate < 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
|
||||
end
|
||||
@@ -1,45 +0,0 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
module Spree
|
||||
module Calculator
|
||||
class FlexiRate < 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)
|
||||
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.zero?
|
||||
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
|
||||
end
|
||||
@@ -1,31 +0,0 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require_dependency 'spree/calculator'
|
||||
require 'spree/localized_number'
|
||||
|
||||
module Spree
|
||||
module Calculator
|
||||
class PerItem < 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_item)
|
||||
end
|
||||
|
||||
def compute(object = nil)
|
||||
return 0 if object.nil?
|
||||
|
||||
number_of_line_items = line_items_for(object).reduce(0) do |sum, line_item|
|
||||
value_to_add = line_item.quantity
|
||||
sum + value_to_add
|
||||
end
|
||||
preferred_amount * number_of_line_items
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,40 +0,0 @@
|
||||
# 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
|
||||
module Calculator
|
||||
class PriceSack < 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
|
||||
|
||||
def self.description
|
||||
I18n.t(:price_sack)
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
min = preferred_minimal_amount.to_f
|
||||
order_amount = line_items_for(object).map { |x| x.price * x.quantity }.sum
|
||||
|
||||
if order_amount < min
|
||||
cost = preferred_normal_amount.to_f
|
||||
elsif order_amount >= min
|
||||
cost = preferred_discount_amount.to_f
|
||||
end
|
||||
|
||||
cost
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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)
|
||||
|
||||
@@ -49,30 +49,30 @@ 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
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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!
|
||||
|
||||
@@ -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" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) }
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -138,7 +138,7 @@ module Spree
|
||||
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
|
||||
@@ -159,7 +159,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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -66,7 +66,7 @@ describe TaxRateFinder do
|
||||
create(
|
||||
:tax_rate,
|
||||
amount: amount,
|
||||
calculator: Spree::Calculator::DefaultTax.new,
|
||||
calculator: Calculator::DefaultTax.new,
|
||||
zone: zone
|
||||
)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user