Sha256: 73692e56bbc8382893a7783c9016e949d9b147b253e5ab4f447da970ee246cd7

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 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, inventory_units.count, shipment
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_core-2.8.0 app/models/spree/stock/inventory_units_finalizer.rb