Skip to content

Commit

Permalink
Change calculators to use #discountable_amount
Browse files Browse the repository at this point in the history
This change allows use to actually use the "lanes" feature in our
calculators.
  • Loading branch information
mamhoff committed Oct 2, 2023
1 parent ef76937 commit aa36625
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def line_item
__getobj__
end

def discounted_amount
def discountable_amount
amount + discounts.sum(&:amount)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def shipment
__getobj__
end

def discounted_amount
def discountable_amount
amount + discounts.sum(&:amount)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def shipping_rate
__getobj__
end

def discounted_amount
def discountable_amount
amount + discounts.sum(&:amount)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def line_item_ids
end

def elligible_amounts
line_items.map(&:amount)
line_items.map(&:discountable_amount)
end

def subtotal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit aa36625

Please sign in to comment.