Sha256: 9618fc1a8bc89795facc0585c3332b52e655ee40fa2503c04d093cdf11d9d461

Contents?: true

Size: 1.57 KB

Versions: 102

Compression:

Stored size: 1.57 KB

Contents

# Used by Prioritizer to adjust item quantities
# see prioritizer_spec for use cases
module Spree
  module Stock
    class Adjuster
      attr_accessor :required_quantity, :received_quantity, :backorder_package,
                    :backorder_item

      def initialize(inventory_unit)
        self.required_quantity = inventory_unit.required_quantity
        self.backorder_package = nil
        self.backorder_item = nil
        self.received_quantity = 0
      end

      def adjust(package_to_adjust, item)
        if fulfilled?
          package_to_adjust.remove_item item
        elsif item.backordered?
          # We only use the first backorder item to fill backorders
          # as the items/packages are processed in priority order
          if backorder_package.nil?
            self.backorder_package = package_to_adjust
            self.backorder_item = item
          else
            package_to_adjust.remove_item item
          end
        else
          if item.quantity >= remaining_quantity
            item.quantity = remaining_quantity
          end
          self.received_quantity += item.quantity
          update_backorder
        end
      end

      def update_backorder
        return if backorder_package.nil?

        if fulfilled?
          backorder_package.remove_item backorder_item
        elsif backorder_item.present?
          backorder_item.quantity = remaining_quantity
        end
      end

      def fulfilled?
        remaining_quantity.zero?
      end

      def remaining_quantity
        required_quantity - received_quantity
      end
    end
  end
end

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/stock/adjuster.rb
spree_core-4.10.0 app/models/spree/stock/adjuster.rb
spree_core-4.9.0 app/models/spree/stock/adjuster.rb
spree_core-4.8.3 app/models/spree/stock/adjuster.rb
spree_core-4.8.2 app/models/spree/stock/adjuster.rb
spree_core-4.7.3 app/models/spree/stock/adjuster.rb
spree_core-4.6.6 app/models/spree/stock/adjuster.rb
spree_core-4.5.5 app/models/spree/stock/adjuster.rb
spree_core-4.0.9 app/models/spree/stock/adjuster.rb
spree_core-4.1.15 app/models/spree/stock/adjuster.rb
spree_core-4.2.7 app/models/spree/stock/adjuster.rb
spree_core-4.3.3 app/models/spree/stock/adjuster.rb
spree_core-4.4.1 app/models/spree/stock/adjuster.rb
spree_core-4.5.4 app/models/spree/stock/adjuster.rb
spree_core-4.6.5 app/models/spree/stock/adjuster.rb
spree_core-4.7.2 app/models/spree/stock/adjuster.rb
spree_core-4.7.1 app/models/spree/stock/adjuster.rb
spree_core-4.6.4 app/models/spree/stock/adjuster.rb
spree_core-4.7.0 app/models/spree/stock/adjuster.rb
spree_core-4.6.3 app/models/spree/stock/adjuster.rb