Class: Spree::StockLocation

Inherits:
Object
  • Object
show all
Includes:
Spree::Security::StockLocations, UniqueName, VendorConcern
Defined in:
app/models/spree/stock_location.rb

Constant Summary collapse

KINDS =

Categorizes the location. Open string — extensible by setting any value; KINDS lists the built-in options used by the admin UI dropdown.

%w[warehouse store fulfillment_center].freeze
PICKUP_STOCK_POLICIES =

Pickup stock policy: ‘local’ = only items physically at this location are collectable; ‘any’ = items can be transferred from other locations (ship-to-store). See docs/plans/6.0-fulfillment-and-delivery.md.

%w[local any].freeze

Instance Method Summary collapse

Instance Method Details

#addressObject



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/spree/stock_location.rb', line 169

def address
  Spree::Address.new(
    address1: address1,
    address2: address2,
    company: company,
    city: city,
    state: state,
    state_name: state_name,
    country: country,
    zipcode: zipcode,
    phone: phone
  )
end

#backorderable?(variant) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'app/models/spree/stock_location.rb', line 122

def backorderable?(variant)
  stock_item(variant).try(:backorderable?)
end

#count_on_hand(variant) ⇒ Integer

Returns the count on hand number for the variant

Parameters:

  • variant

    Variant instance

Returns:

  • (Integer)


118
119
120
# File 'app/models/spree/stock_location.rb', line 118

def count_on_hand(variant)
  stock_item(variant).try(:count_on_hand)
end

#country_iso=(value) ⇒ Object

Writer methods for API convenience — accept ISO/abbr codes instead of FK IDs. Mirrors Spree::Address: SDK clients use country_iso/state_abbr because Country/State don’t expose prefixed IDs (their ‘iso` is the public handle).



60
61
62
# File 'app/models/spree/stock_location.rb', line 60

def country_iso=(value)
  @country_iso_input = value
end

#display_nameObject



201
202
203
# File 'app/models/spree/stock_location.rb', line 201

def display_name
  @display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ')
end

#fill_status(variant, quantity) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/models/spree/stock_location.rb', line 152

def fill_status(variant, quantity)
  if item = stock_item_or_create(variant)
    if item.count_on_hand >= quantity
      on_hand = quantity
      backordered = 0
    else
      on_hand = item.count_on_hand
      on_hand = 0 if on_hand < 0
      backordered = item.backorderable? ? (quantity - on_hand) : 0
    end

    [on_hand, backordered]
  else
    [0, 0]
  end
end

#move(variant, quantity, originator = nil, persist: true) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'app/models/spree/stock_location.rb', line 142

def move(variant, quantity, originator = nil, persist: true)
  stock_item = stock_item_or_create(variant)

  if persist
    stock_item.stock_movements.create!(quantity: quantity, originator: originator)
  else
    originator.stock_movements << stock_item.stock_movements.build(quantity: quantity)
  end
end

#propagate_variant(variant) ⇒ Object

Wrapper for creating a new stock item respecting the backorderable config



73
74
75
# File 'app/models/spree/stock_location.rb', line 73

def propagate_variant(variant)
  stock_items.create!(variant: variant, backorderable: backorderable_default)
end

#require_company?Boolean

needed for address form

Returns:

  • (Boolean)


189
190
191
# File 'app/models/spree/stock_location.rb', line 189

def require_company?
  false
end

#require_name?Boolean

needed for address form

Returns:

  • (Boolean)


184
185
186
# File 'app/models/spree/stock_location.rb', line 184

def require_name?
  false
end

#require_phone?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'app/models/spree/stock_location.rb', line 193

def require_phone?
  false
end

#restock(variant, quantity, originator = nil, persist: true) ⇒ Object



126
127
128
# File 'app/models/spree/stock_location.rb', line 126

def restock(variant, quantity, originator = nil, persist: true)
  move(variant, quantity, originator, persist: persist)
end

#restock_backordered(variant, quantity, _originator = nil) ⇒ Object



130
131
132
133
134
135
136
# File 'app/models/spree/stock_location.rb', line 130

def restock_backordered(variant, quantity, _originator = nil)
  item = stock_item_or_create(variant)
  item.update_columns(
    count_on_hand: item.count_on_hand + quantity,
    updated_at: Time.current
  )
end

#set_up_stock_item(variant) ⇒ Object

Return either an existing stock item or create a new one. Useful in scenarios where the user might not know whether there is already a stock item for a given variant



80
81
82
# File 'app/models/spree/stock_location.rb', line 80

def set_up_stock_item(variant)
  stock_item(variant) || propagate_variant(variant)
end

#show_company_address_field?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'app/models/spree/stock_location.rb', line 197

def show_company_address_field?
  true
end

#state_abbr=(value) ⇒ Object



64
65
66
# File 'app/models/spree/stock_location.rb', line 64

def state_abbr=(value)
  @state_abbr_input = value
end

#state_textObject



68
69
70
# File 'app/models/spree/stock_location.rb', line 68

def state_text
  state.try(:abbr) || state.try(:name) || state_name
end

#stock_item(variant_id) ⇒ StockItem

Returns an instance of StockItem for the variant id.

Parameters:

  • variant_id (String)

    The id of a variant.

Returns:

  • (StockItem)

    Corresponding StockItem for the StockLocation’s variant.



89
90
91
# File 'app/models/spree/stock_location.rb', line 89

def stock_item(variant_id)
  stock_items.where(variant_id: variant_id).order(:id).first
end

#stock_item_or_create(variant_or_variant_id) ⇒ StockItem

Attempts to look up StockItem for the variant, and creates one if not found.

Parameters:

  • variant

    Variant instance or Variant ID

Returns:

  • (StockItem)

    Corresponding StockItem for the StockLocation’s variant.



102
103
104
105
106
107
108
109
110
111
# File 'app/models/spree/stock_location.rb', line 102

def stock_item_or_create(variant_or_variant_id)
  if variant_or_variant_id.is_a?(Spree::Variant)
    variant_id = variant_or_variant_id.id
    variant = variant_or_variant_id
  else
    variant_id = variant_or_variant_id
    variant = Spree::Variant.find(variant_or_variant_id)
  end
  stock_item(variant_id) || propagate_variant(variant)
end

#stocks?(variant) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/spree/stock_location.rb', line 93

def stocks?(variant)
  stock_items.exists?(variant: variant)
end

#unstock(variant, quantity, originator = nil, persist: true) ⇒ Object



138
139
140
# File 'app/models/spree/stock_location.rb', line 138

def unstock(variant, quantity, originator = nil, persist: true)
  move(variant, -quantity, originator, persist: persist)
end