Sha256: 1016ded85e4bebd2c765ba2b93c0f4e299f32fd49a5a013b326aa1fe57cb4464

Contents?: true

Size: 1.89 KB

Versions: 26

Compression:

Stored size: 1.89 KB

Contents

# removes an item (variant) from shipment
# quantity can be passed
# if there is no quantity left for this item for this shipent, shipment itself will be removed
module Spree
  module Shipments
    class RemoveItem
      prepend Spree::ServiceModule::Base
      include Helper

      def call(shipment:, variant_id:, quantity: nil)
        ActiveRecord::Base.transaction do
          run :prepare_arguments
          run :remove_item
        end
      end

      protected

      def prepare_arguments(shipment:, variant_id:, quantity: nil)
        variant = Spree::Variant.find_by(id: variant_id)
        return failure(nil, :variant_not_found) if variant.nil? || !shipment.include?(variant)

        # if quantity isn't passed let's remove all added qty of this variant
        quantity = quantity&.to_i || shipment.inventory_units_for(variant).sum(:quantity)

        success(shipment: shipment, variant: variant, quantity: quantity)
      end

      # if the removed variant was the only one shipped via this shipment
      # we need to remove the shipment itself
      def remove_item(shipment:, variant:, quantity:)
        result = remove_item_service.call(order: shipment.order,
                                          variant: variant,
                                          quantity: quantity,
                                          options: { shipment: shipment })

        if result.success?
          line_item = result.value
          line_item.destroy if line_item.quantity.zero?

          # `OrderInventory#remove_from_shipment` is called in `remove_item_service`
          # which will delete the shipment if all inventory units were removed
          if shipment.inventory_units.any?
            success(shipment)
          else
            shipment.destroy!
            success(:shipment_deleted)
          end
        else
          failure(result.value, result.error)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/services/spree/shipments/remove_item.rb
spree_core-4.10.0 app/services/spree/shipments/remove_item.rb
spree_core-4.9.0 app/services/spree/shipments/remove_item.rb
spree_core-4.8.3 app/services/spree/shipments/remove_item.rb
spree_core-4.8.2 app/services/spree/shipments/remove_item.rb
spree_core-4.7.3 app/services/spree/shipments/remove_item.rb
spree_core-4.6.6 app/services/spree/shipments/remove_item.rb
spree_core-4.5.5 app/services/spree/shipments/remove_item.rb
spree_core-4.4.1 app/services/spree/shipments/remove_item.rb
spree_core-4.5.4 app/services/spree/shipments/remove_item.rb
spree_core-4.6.5 app/services/spree/shipments/remove_item.rb
spree_core-4.7.2 app/services/spree/shipments/remove_item.rb
spree_core-4.7.1 app/services/spree/shipments/remove_item.rb
spree_core-4.6.4 app/services/spree/shipments/remove_item.rb
spree_core-4.7.0 app/services/spree/shipments/remove_item.rb
spree_core-4.6.3 app/services/spree/shipments/remove_item.rb
spree_core-4.6.2 app/services/spree/shipments/remove_item.rb
spree_core-4.6.1 app/services/spree/shipments/remove_item.rb
spree_core-4.6.0 app/services/spree/shipments/remove_item.rb
spree_core-4.5.3 app/services/spree/shipments/remove_item.rb