Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added Order#pre_tax_total and Order#display_pre_tax_total

Damian Legawiec81ca4eb54b53
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index ad6ef9dfca..56f9337fb6 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -22,7 +22,7 @@ module Spree
money_methods :outstanding_balance, :item_total, :adjustment_total,
:included_tax_total, :additional_tax_total, :tax_total,
:shipment_total, :promo_total, :total,
- :cart_promo_total, :pre_tax_item_amount
+ :cart_promo_total, :pre_tax_item_amount, :pre_tax_total
alias display_ship_total display_shipment_total
alias_attribute :ship_total, :shipment_total
@@ -177,6 +177,11 @@ module Spree
line_items.sum(:pre_tax_amount)
end
+ # Sum of all line item and shipment pre-tax
+ def pre_tax_total
+ pre_tax_item_amount + shipments.sum(:pre_tax_amount)
+ end
+
def shipping_discount
shipment_adjustments.non_tax.eligible.sum(:amount) * - 1
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 61e815b57f..5a37e4ac77 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -861,6 +861,29 @@ describe Spree::Order, type: :model do
end
end
+ describe '#pre_tax_total' do
+ let(:order) { create(:order) }
+
+ before do
+ line_item = create(:line_item, order: order, price: 10, quantity: 2)
+ shipment = create(:shipment, order: order, cost: 5)
+
+ line_item.update(pre_tax_amount: 8.0)
+ shipment.update(pre_tax_amount: 4.0)
+ end
+
+ it "sums all of the line items' and shipments pre tax amounts" do
+ expect(order.pre_tax_total).to eq BigDecimal(12)
+ end
+ end
+
+ describe '#display_pre_tax_total' do
+ it 'returns the value as a spree money' do
+ allow(order).to receive(:pre_tax_total).and_return(10.55)
+ expect(order.display_pre_tax_total).to eq(Spree::Money.new(10.55))
+ end
+ end
+
describe '#quantity' do
# Uses a persisted record, as the quantity is retrieved via a DB count
let(:order) { create :order_with_line_items, line_items_count: 3 }