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.

Extended Shipment model with helper methods (#12064)

Damian Legawiec5a8a8c9330db
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index f945de6696..ea995b8650 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -12,12 +12,16 @@ module Spree
if defined?(Spree::Security::Shipments)
include Spree::Security::Shipments
end
+ if defined?(Spree::VendorConcern)
+ include Spree::VendorConcern
+ end
with_options inverse_of: :shipments do
belongs_to :address, class_name: 'Spree::Address'
belongs_to :order, class_name: 'Spree::Order', touch: true
end
- belongs_to :stock_location, class_name: 'Spree::StockLocation'
+ belongs_to :stock_location, -> { with_deleted }, class_name: 'Spree::StockLocation'
+ has_many :variants, through: :inventory_units
with_options dependent: :delete_all do
has_many :adjustments, as: :adjustable
@@ -47,8 +51,18 @@ module Spree
# sort by most recent shipped_at, falling back to created_at. add "id desc" to make specs that involve this scope more deterministic.
scope :reverse_chronological, -> { order(Arel.sql('coalesce(spree_shipments.shipped_at, spree_shipments.created_at) desc'), id: :desc) }
scope :valid, -> { where.not(state: :canceled) }
+ scope :canceled, -> { with_state('canceled') }
+ scope :not_canceled, -> { where.not(state: 'canceled') }
+ scope :shipped_but_canceled, -> { canceled.where.not(shipped_at: nil) }
+ # This scope will select the shipping_method_id from the shipments' selected shipping rate
+ scope :with_selected_shipping_method, lambda {
+ joins(:selected_shipping_rate).
+ where(Spree::ShippingRate.arel_table[:shipping_method_id].not_eq(nil)).
+ select(Spree::ShippingRate.arel_table[:shipping_method_id])
+ }
delegate :store, :currency, to: :order
+ delegate :amount_in_cents, to: :display_cost
# shipment state machine (see http://github.com/pluginaweek/state_machine/tree/master for details)
state_machine initial: :pending, use_transactions: false do
@@ -93,9 +107,20 @@ module Spree
self.whitelisted_ransackable_attributes = ['number']
extend DisplayMoney
- money_methods :cost, :discounted_cost, :final_price, :item_cost
+ money_methods :cost, :discounted_cost, :final_price, :item_cost, :additional_tax_total, :included_tax_total, :tax_total
alias display_amount display_cost
+ # Returns the shipment number and shipping method name
+ #
+ # @return [String]
+ def name
+ [number, shipping_method&.name].compact.join(' ').strip
+ end
+
+ def amount
+ cost
+ end
+
def add_shipping_method(shipping_method, selected = false)
shipping_rates.create(shipping_method: shipping_method, selected: selected, cost: cost)
end
@@ -108,17 +133,44 @@ module Spree
manifest.each { |item| manifest_unstock(item) }
end
+ # Returns true if the shipment has any backordered inventory units
+ #
+ # @return [Boolean]
def backordered?
inventory_units.any?(&:backordered?)
end
+ # Returns true if the shipment is tracked
+ #
+ # @return [Boolean]
+ def tracked?
+ tracking.present? || tracking_url.present?
+ end
+
+ # Returns true if the shipment is shippable
+ #
+ # @return [Boolean]
+ def shippable?
+ can_ship? && tracked?
+ end
+
+ # Returns true if not all of the shipment's line items are fully shipped
+ #
+ # @return [Boolean]
+ def partial?
+ manifest.any? do |manifest_item|
+ line_item = manifest_item.line_item
+ line_item.quantity > manifest_item.quantity
+ end
+ end
+
# Determines the appropriate +state+ according to the following logic:
#
# pending unless order is complete and +order.payment_state+ is +paid+
# shipped if already shipped (ie. does not change the state)
# ready all other cases
def determine_state(order)
- return 'canceled' if order.canceled?
+ return 'canceled' if canceled? || order.canceled?
return 'pending' unless order.can_ship?
return 'pending' if inventory_units.any? &:backordered?
return 'shipped' if shipped?
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 9ca81d2093..6e35bb4c27 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -6,7 +6,7 @@ describe Spree::Shipment, type: :model do
let!(:order) { create(:order, number: 'S12345') }
let(:shipping_method) { create(:shipping_method, name: 'UPS') }
let(:shipment) do
- create(:shipment, cost: 1, state: 'pending', stock_location: create(:stock_location)).tap do |shipment|
+ create(:shipment, number: 'H21265865494', cost: 1, state: 'pending', stock_location: create(:stock_location)).tap do |shipment|
allow(shipment).to receive_messages order: order
allow(shipment).to receive_messages shipping_method: shipping_method
end
@@ -31,6 +31,47 @@ describe Spree::Shipment, type: :model do
end
end
+ describe '#name' do
+ it 'returns the shipment number and shipping method name' do
+ expect(shipment.name).to eq('H21265865494 UPS')
+ end
+ end
+
+ describe '#tracked?' do
+ it 'returns true if the shipment is tracked' do
+ expect(shipment.tracked?).to eq(true)
+ end
+
+ context 'when the shipment is not tracked' do
+ let(:shipment) { build(:shipment, number: nil, tracking: nil) }
+
+ it 'returns false' do
+ expect(shipment.tracked?).to eq(false)
+ end
+ end
+ end
+
+ describe '#partial?' do
+ subject { shipment.partial? }
+
+ let(:shipment) { create(:shipment, order: order) }
+
+ let!(:line_item) { create(:line_item, quantity: 5, order: order) }
+ let(:order) { create(:order) }
+
+ context 'when all products are included in the shipment' do
+ it { is_expected.to be(false) }
+ end
+
+ context 'when not all products are included in the shipment' do
+ before do
+ shipment.inventory_units.first.update!(quantity: 3)
+ end
+
+ it { is_expected.to be(true) }
+ end
+ end
+
# Regression test for #4063
context 'number generation' do
before do
@@ -59,6 +100,11 @@ describe Spree::Shipment, type: :model do
expect(shipment.determine_state(order)).to eq 'canceled'
end
+ it 'returns canceled when shipment is canceled' do
+ allow(shipment).to receive_messages canceled?: true
+ expect(shipment.determine_state(order)).to eq 'canceled'
+ end
+
it 'returns pending unless order.can_ship?' do
allow(order).to receive_messages can_ship?: false
expect(shipment.determine_state(order)).to eq 'pending'