Sha256: a13ae28b6bcd7a7924e878011c949f63e24a1cf9aac409087b96c4b22fa8ca2e

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Spree
  class StockItem < ActiveRecord::Base
    belongs_to :stock_location, class_name: 'Spree::StockLocation'
    belongs_to :variant, class_name: 'Spree::Variant'
    has_many :stock_movements, dependent: :destroy

    validates_presence_of :stock_location, :variant
    validates_uniqueness_of :variant_id, scope: :stock_location_id

    attr_accessible :count_on_hand, :variant, :stock_location, :backorderable, :variant_id

    delegate :weight, to: :variant

    def backordered_inventory_units
      Spree::InventoryUnit.backordered_for_stock_item(self)
    end

    def variant_name
      variant.name
    end

    def adjust_count_on_hand(value)
      self.with_lock do
        self.count_on_hand = self.count_on_hand + value
        process_backorders if in_stock?

        self.save!
      end
    end

    def in_stock?
      self.count_on_hand > 0
    end

    # Tells whether it's available to be included in a shipment
    def available?
      self.in_stock? || self.backorderable?
    end

    private
      def count_on_hand=(value)
        write_attribute(:count_on_hand, value)
      end

      def process_backorders
        backordered_inventory_units.each do |unit|
          return unless in_stock?
          unit.fill_backorder
        end
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_core-2.0.4 app/models/spree/stock_item.rb
spree_core-2.0.3 app/models/spree/stock_item.rb
spree_core-2.0.2 app/models/spree/stock_item.rb
spree_core-2.0.1 app/models/spree/stock_item.rb