Sha256: c76fa673a2d933ee8f88dcdc6019f454d5c362d3c5537d18c6254c1a3d0e0cf2

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Spree
  class StockMovement < Spree::Base
    QUANTITY_LIMITS = {
      max: 2**31 - 1,
      min: -2**31
    }.freeze

    if defined?(Spree::Webhooks::HasWebhooks)
      include Spree::Webhooks::HasWebhooks
    end

    belongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movements
    belongs_to :originator, polymorphic: true

    after_create :update_stock_item_quantity

    with_options presence: true do
      validates :stock_item
      validates :quantity, numericality: {
        greater_than_or_equal_to: QUANTITY_LIMITS[:min],
        less_than_or_equal_to: QUANTITY_LIMITS[:max],
        only_integer: true
      }
    end

    scope :recent, -> { order(created_at: :desc) }

    delegate :variant, to: :stock_item
    delegate :product, to: :variant

    self.whitelisted_ransackable_attributes = ['quantity']

    def readonly?
      persisted?
    end

    private

    def update_stock_item_quantity
      return unless stock_item.should_track_inventory?

      stock_item.adjust_count_on_hand quantity
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_core-4.8.3 app/models/spree/stock_movement.rb
spree_core-4.8.2 app/models/spree/stock_movement.rb