Sha256: 06193daa1738ba6342c3138990e51160d6ea17b17fa7ea5efaf6b5e7de48a23d

Contents?: true

Size: 1.4 KB

Versions: 20

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Spree
  module Stock
    # Service class to finalize inventory units, it means unstock the desired
    # quantity from related stock item and updates the given inventory units to
    # not be pending.
    class InventoryUnitsFinalizer
      # @attr_reader [Spree::InventoryUnit] inventory_units to be finalized
      attr_reader :inventory_units

      # @param [Spree::InventoryUnit] inventory_units to be finalized
      def initialize(inventory_units)
        @inventory_units = inventory_units
      end

      # Finalize the inventory units, unstock and mark them as not pending.
      def run!
        Spree::InventoryUnit.transaction do
          unstock_inventory_units
          finalize_inventory_units
        end
      end

      private

      def finalize_inventory_units
        inventory_units.map do |iu|
          iu.update_columns(
            pending: false,
            updated_at: Time.current
          )
        end
      end

      def unstock_inventory_units
        inventory_units.group_by(&:shipment_id).each_value do |inventory_units_for_shipment|
          inventory_units_for_shipment.group_by(&:line_item_id).each_value do |units|
            shipment = units.first.shipment
            line_item = units.first.line_item
            shipment.stock_location.unstock line_item.variant, units.count, shipment
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
solidus_core-2.10.5 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.10.3 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.10.2 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.6 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.6 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.10.1 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.5 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.10.0 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.4 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.5 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.3 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.2 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.10.0.beta1 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.1 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.0 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.9.0.rc.1 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.4 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.3 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.2 app/models/spree/stock/inventory_units_finalizer.rb
solidus_core-2.8.1 app/models/spree/stock/inventory_units_finalizer.rb