Class: Spree::StockLevels::Correct

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree/stock_levels/correct.rb

Overview

Corrects one shelf to a counted figure, or by a counted difference.

This is what a merchant does after counting: they know what is on the shelf ("set it to 40"), or what changed ("two fewer, damaged"). Either way the change is written as an adjusted movement carrying the delta, never onto the column, so the stock history explains every figure it shows.

Shared by the single-level endpoint and BulkUpsert, so a feed's row and an admin's edit obey one set of rules.

Instance Method Summary collapse

Methods included from Spree::ServiceModule::Base

prepended

Instance Method Details

#call(stock_level:, count_on_hand: nil, adjustment: nil, reason: nil) ⇒ Spree::ServiceModule::Result

Returns the corrected stock level.

Parameters:

  • stock_level (Spree::StockLevel)
  • count_on_hand (Integer, String, nil) (defaults to: nil) —

    the figure the shelf should end at

  • adjustment (Integer, String, nil) (defaults to: nil) —

    a signed change instead, for a caller who knows the difference but not the current count

  • reason (String, Symbol, nil) (defaults to: nil) —

    why, for the stock history. A key from Spree::StockMovement::ADJUSTMENT_REASONS is resolved to its English text; anything else is stored as given

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/spree/stock_levels/correct.rb', line 25

def call(stock_level:, count_on_hand: nil, adjustment: nil, reason: nil)
  if count_on_hand.present? && adjustment.present?
    return failure(stock_level, I18n.t('spree.stock_level.errors.adjustment_exclusive_with_count_on_hand'))
  end

  target = parse(count_on_hand)
  delta = parse(adjustment)
  return failure(stock_level, I18n.t('spree.stock_level.errors.count_on_hand_not_an_integer')) if count_on_hand.present? && target.nil?
  return failure(stock_level, I18n.t('spree.stock_level.errors.adjustment_not_an_integer')) if adjustment.present? && delta.nil?

  # Read before locking, which reloads the level and with it drops the
  # variant and location a bulk caller loaded for every row at once.
  stock_location = stock_level.stock_location
  variant = stock_level.variant

  # Locked around the read: the delta is worked out from the count this
  # caller first saw, so two admins correcting the same level at once
  # would otherwise each measure against a shelf the other had already
  # moved, and both edits would land.
  stock_level.with_lock do
    move = delta || (target && target - stock_level.count_on_hand)
    next if move.nil? || move.zero?

    # Through the location's own mover rather than building a movement
    # by hand: it types the row as an adjustment and records the reason,
    # so a correction reads like every other change in the history. The
    # movement writes through this locked instance, which leaves it
    # current without another reload.
    stock_location.adjust(variant, move, reason: Spree::StockMovement.adjustment_reason_text(reason),
                                         stock_level: stock_level)
  end

  success(stock_level)
end