Skip to content

Commit

Permalink
MAke more code just use the single shipment of the order even if the …
Browse files Browse the repository at this point in the history
…DB still supports multiple shipments per order
  • Loading branch information
luisramos0 committed Nov 30, 2020
1 parent 3c71b7f commit cf8dada
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 63 deletions.
44 changes: 0 additions & 44 deletions app/models/concerns/order_shipment.rb

This file was deleted.

66 changes: 47 additions & 19 deletions app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def item_count
end

def backordered?
shipments.any?(&:backordered?)
shipment&.backordered?
end

# Returns the relevant zone (if any) to be used for taxation purposes.
Expand Down Expand Up @@ -443,10 +443,8 @@ def finalize!

# update payment and shipment(s) states, and save
updater.update_payment_state
shipments.each do |shipment|
shipment.update!(self)
shipment.finalize!
end
shipment&.update!(self)
shipment&.finalize!

updater.update_shipment_state
updater.before_save_hook
Expand Down Expand Up @@ -561,20 +559,52 @@ def state_changed(name)
)
end

# The Spree 2 db model we use supports multiple shipments per order,
# in OFN we have only one shipment per order.
# A shipment is associated to a shipping_method through a selected shipping_rate.
def shipment
return if shipments.blank?

shipments.first
end

# The shipping method of the shipment in the order
def shipping_method
return if shipment.blank?

shipment.shipping_method
end

# Finds the shipment's shipping_rate for the given shipping_method_id and selects that shipping_rate.
# If the selection is successful, it persists it in the database by saving the shipment.
# If it fails, it does not clear the current shipping_method selection.
#
# @return [ShippingMethod] the selected shipping method, or nil if the given shipping_method_id is
# empty or if it cannot find the given shipping_method_id in the order
def select_shipping_method(shipping_method_id)
return if shipping_method_id.blank? || shipment.blank?

shipment = shipments.first

shipping_rate = shipment.shipping_rates.find_by(shipping_method_id: shipping_method_id)
return unless shipping_rate

shipment.selected_shipping_rate_id = shipping_rate.id
shipment.shipping_method
end

def shipped?
%w(partial shipped).include?(shipment_state)
end

# Does this order have shipments that can be shipped?
def ready_to_ship?
shipments.any?(&:can_ship?)
shipment&.can_ship?
end

# Ship all pending orders
def ship
shipments.each do |s|
s.ship if s.can_ship?
end
shipment.ship if ready_to_ship?
end

def line_item_variants
Expand Down Expand Up @@ -610,14 +640,14 @@ def create_proposed_shipments
# to delivery again so that proper updated shipments are created.
# e.g. customer goes back from payment step and changes order items
def ensure_updated_shipments
return unless shipments.any?
return if shipment.blank?

shipments.destroy_all
update_column(:state, "address")
end

def refresh_shipment_rates
shipments.map(&:refresh_rates)
shipment.refresh_rates
end

# Check that line_items in the current order are available from a newly selected distribution
Expand All @@ -636,12 +666,10 @@ def disallow_guest_order

# After changing line items of a completed order
def update_shipping_fees!
shipments.each do |shipment|
next if shipment.shipped?
return if shipment.blank? || shipment.shipped?

update_adjustment! shipment.adjustment if shipment.adjustment
save_or_rescue_shipment(shipment)
end
update_adjustment!(shipment.adjustment) if shipment.adjustment
save_or_rescue_shipment(shipment)
end

def save_or_rescue_shipment(shipment)
Expand Down Expand Up @@ -800,7 +828,7 @@ def has_available_shipment
end

def ensure_available_shipping_rates
return unless shipments.empty? || shipments.any? { |shipment| shipment.shipping_rates.blank? }
return unless shipment.blank? || shipment.shipping_rates.blank?

errors.add(:base, Spree.t(:items_cannot_be_shipped)) && (return false)
end
Expand All @@ -811,14 +839,14 @@ def has_available_payment
end

def after_cancel
shipments.each(&:cancel!)
shipment&.cancel!

OrderMailer.cancel_email(id).deliver
self.payment_state = 'credit_owed' unless shipped?
end

def after_resume
shipments.each(&:resume!)
shipment&.resume!
end

def use_billing?
Expand Down

0 comments on commit cf8dada

Please sign in to comment.