diff --git a/promotions/app/models/solidus_friendly_promotions/calculators/percent.rb b/promotions/app/models/solidus_friendly_promotions/calculators/percent.rb index 8864904f..50d51225 100644 --- a/promotions/app/models/solidus_friendly_promotions/calculators/percent.rb +++ b/promotions/app/models/solidus_friendly_promotions/calculators/percent.rb @@ -10,7 +10,7 @@ class Percent < Spree::Calculator def compute(object) preferred_currency = object.order.currency currency_exponent = ::Money::Currency.find(preferred_currency).exponent - (object.amount * preferred_percent / 100).round(currency_exponent) + (object.discountable_amount * preferred_percent / 100).round(currency_exponent) end end end diff --git a/promotions/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb b/promotions/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb index 1fda3e91..f6cda6cc 100644 --- a/promotions/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb +++ b/promotions/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb @@ -22,7 +22,7 @@ class TieredFlatRate < Spree::Calculator def compute_item(object) _base, amount = preferred_tiers.sort.reverse.detect do |value, _| - object.amount >= value + object.discountable_amount >= value end if preferred_currency.casecmp(object.currency).zero? diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb b/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb index d196c553..f732710d 100644 --- a/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb +++ b/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb @@ -15,7 +15,7 @@ def line_item __getobj__ end - def discounted_amount + def discountable_amount amount + discounts.sum(&:amount) end end diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb b/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb index e84a22a7..e16c5a96 100644 --- a/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb +++ b/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb @@ -16,7 +16,7 @@ def shipment __getobj__ end - def discounted_amount + def discountable_amount amount + discounts.sum(&:amount) end end diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb b/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb index bc938e69..77a6ca46 100644 --- a/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb +++ b/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb @@ -15,7 +15,7 @@ def shipping_rate __getobj__ end - def discounted_amount + def discountable_amount amount + discounts.sum(&:amount) end end diff --git a/promotions/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb b/promotions/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb index 6e0c0630..485b8b22 100644 --- a/promotions/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb +++ b/promotions/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb @@ -29,7 +29,7 @@ def line_item_ids end def elligible_amounts - line_items.map(&:amount) + line_items.map(&:discountable_amount) end def subtotal diff --git a/promotions/app/models/solidus_friendly_promotions/promotion_action.rb b/promotions/app/models/solidus_friendly_promotions/promotion_action.rb index 27eb152b..9ce0c9df 100644 --- a/promotions/app/models/solidus_friendly_promotions/promotion_action.rb +++ b/promotions/app/models/solidus_friendly_promotions/promotion_action.rb @@ -38,7 +38,7 @@ def discount(adjustable) # Ensure a negative amount which does not exceed the object's amount def compute_amount(adjustable) promotion_amount = calculator.compute(adjustable) || BigDecimal("0") - [adjustable.amount, promotion_amount.abs].min * -1 + [adjustable.discountable_amount, promotion_amount.abs].min * -1 end def adjustment_label(adjustable) diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb index 65aca311..1f4cc920 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb @@ -10,7 +10,8 @@ end let(:rules) { [] } let(:action) { SolidusFriendlyPromotions::Actions::AdjustLineItem.create(calculator: calculator) } - let(:order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) } + let(:spree_order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) } + let(:order) { SolidusFriendlyPromotions::Discountable::Order.new(spree_order) } let(:currency) { "USD" } context "applied to an order" do diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb index f7173495..fe98125d 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb @@ -7,7 +7,7 @@ context "compute" do let(:currency) { "USD" } let(:order) { double(currency: currency) } - let(:line_item) { double("LineItem", amount: 100, order: order) } + let(:line_item) { double("SolidusFriendlyPromotions::Discountable::LineItem", discountable_amount: 100, order: order) } before { subject.preferred_percent = 15 } diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb index ee438e6c..490dac04 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb @@ -75,7 +75,7 @@ line_items_price: amount ) end - let(:line_item) { order.line_items.first } + let(:line_item) { SolidusFriendlyPromotions::Discountable::LineItem.new(order.line_items.first, order: order) } let(:preferred_currency) { "USD" } before do @@ -109,7 +109,8 @@ context "with a shipment" do subject { calculator.compute(shipment) } - let(:shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) } + let(:spree_shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) } + let(:shipment) { SolidusFriendlyPromotions::Discountable::Shipment.new(spree_shipment, order: order) } let(:line_item_count) { 1 } let(:amount) { 10 } diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb index 2778f522..c107b821 100644 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb @@ -14,8 +14,8 @@ it { is_expected.to eq(discountable_order) } end - describe "#discounted_amount" do - subject(:discounted_amount) { discountable_line_item.discounted_amount } + describe "#discountable_amount" do + subject(:discountable_amount) { discountable_line_item.discountable_amount } context "with no discounts" do it { is_expected.to eq(20) } diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb index 3c963f7d..96e58f1f 100644 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb @@ -15,8 +15,8 @@ it { is_expected.to eq(discountable_order) } end - describe "#discounted_amount" do - subject(:discounted_amount) { discountable_shipment.discounted_amount } + describe "#discountable_amount" do + subject(:discountable_amount) { discountable_shipment.discountable_amount } context "with no discounts" do it { is_expected.to eq(20) } diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb index 25cbaf6a..f6c6d778 100644 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb @@ -15,8 +15,8 @@ it { is_expected.to eq(discountable_shipment) } end - describe "#discounted_amount" do - subject(:discounted_amount) { discountable_shipping_rate.discounted_amount } + describe "#discountable_amount" do + subject(:discountable_amount) { discountable_shipping_rate.discountable_amount } context "with no discounts" do it { is_expected.to eq(20) } diff --git a/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb b/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb index f3d24490..ee267f90 100644 --- a/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb @@ -3,13 +3,17 @@ require "spec_helper" RSpec.describe SolidusFriendlyPromotions::DistributedAmountsHandler, type: :model do - let(:order) do + let(:spree_order) do FactoryBot.create( :order_with_line_items, line_items_attributes: line_items_attributes ) end + let(:order) do + SolidusFriendlyPromotions::Discountable::Order.new(spree_order) + end + let(:handler) { described_class.new(order.line_items, total_amount) }