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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Extended Shipment model with helper methods (#12064)
Damian Legawiec5a8a8c9330db
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex f945de6696..ea995b8650 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -12,12 +12,16 @@ module Spreeif defined?(Spree::Security::Shipments)include Spree::Security::Shipmentsend+ if defined?(Spree::VendorConcern)+ include Spree::VendorConcern+ endwith_options inverse_of: :shipments dobelongs_to :address, class_name: 'Spree::Address'belongs_to :order, class_name: 'Spree::Order', touch: trueend- belongs_to :stock_location, class_name: 'Spree::StockLocation'+ belongs_to :stock_location, -> { with_deleted }, class_name: 'Spree::StockLocation'+ has_many :variants, through: :inventory_unitswith_options dependent: :delete_all dohas_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 Spreeself.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_totalalias 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 Spreemanifest.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 casesdef 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.rbindex 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 dolet!(: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: orderallow(shipment).to receive_messages shipping_method: shipping_methodend@@ -31,6 +31,47 @@ describe Spree::Shipment, type: :model doendend+ 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 #4063context 'number generation' dobefore do@@ -59,6 +100,11 @@ describe Spree::Shipment, type: :model doexpect(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?' doallow(order).to receive_messages can_ship?: falseexpect(shipment.determine_state(order)).to eq 'pending'