Class: Spree::StockLocation
- Inherits:
-
Object
- Object
- Spree::StockLocation
- Includes:
- Spree::Security::StockLocations, UniqueName, VendorConcern, Webhooks::HasWebhooks
- Defined in:
- app/models/spree/stock_location.rb
Instance Method Summary collapse
- #address ⇒ Object
- #backorderable?(variant) ⇒ Boolean
-
#count_on_hand(variant) ⇒ Integer
Returns the count on hand number for the variant.
- #display_name ⇒ Object
- #fill_status(variant, quantity) ⇒ Object
- #move(variant, quantity, originator = nil, persist: true) ⇒ Object
-
#propagate_variant(variant) ⇒ Object
Wrapper for creating a new stock item respecting the backorderable config.
-
#require_company? ⇒ Boolean
needed for address form.
-
#require_name? ⇒ Boolean
needed for address form.
- #require_phone? ⇒ Boolean
- #restock(variant, quantity, originator = nil, persist: true) ⇒ Object
- #restock_backordered(variant, quantity, _originator = nil) ⇒ Object
-
#set_up_stock_item(variant) ⇒ Object
Return either an existing stock item or create a new one.
- #show_company_address_field? ⇒ Boolean
- #state_text ⇒ Object
-
#stock_item(variant_id) ⇒ StockItem
Returns an instance of StockItem for the variant id.
-
#stock_item_or_create(variant_or_variant_id) ⇒ StockItem
Attempts to look up StockItem for the variant, and creates one if not found.
- #stocks?(variant) ⇒ Boolean
- #unstock(variant, quantity, originator = nil, persist: true) ⇒ Object
Instance Method Details
#address ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/spree/stock_location.rb', line 134 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
87 88 89 |
# File 'app/models/spree/stock_location.rb', line 87 def backorderable?(variant) stock_item(variant).try(:backorderable?) end |
#count_on_hand(variant) ⇒ Integer
Returns the count on hand number for the variant
83 84 85 |
# File 'app/models/spree/stock_location.rb', line 83 def count_on_hand(variant) stock_item(variant).try(:count_on_hand) end |
#display_name ⇒ Object
166 167 168 |
# File 'app/models/spree/stock_location.rb', line 166 def display_name @display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ') end |
#fill_status(variant, quantity) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/spree/stock_location.rb', line 117 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
107 108 109 110 111 112 113 114 115 |
# File 'app/models/spree/stock_location.rb', line 107 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
38 39 40 |
# File 'app/models/spree/stock_location.rb', line 38 def propagate_variant(variant) stock_items.create!(variant: variant, backorderable: backorderable_default) end |
#require_company? ⇒ Boolean
needed for address form
154 155 156 |
# File 'app/models/spree/stock_location.rb', line 154 def require_company? false end |
#require_name? ⇒ Boolean
needed for address form
149 150 151 |
# File 'app/models/spree/stock_location.rb', line 149 def require_name? false end |
#require_phone? ⇒ Boolean
158 159 160 |
# File 'app/models/spree/stock_location.rb', line 158 def require_phone? false end |
#restock(variant, quantity, originator = nil, persist: true) ⇒ Object
91 92 93 |
# File 'app/models/spree/stock_location.rb', line 91 def restock(variant, quantity, originator = nil, persist: true) move(variant, quantity, originator, persist: persist) end |
#restock_backordered(variant, quantity, _originator = nil) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/spree/stock_location.rb', line 95 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
45 46 47 |
# File 'app/models/spree/stock_location.rb', line 45 def set_up_stock_item(variant) stock_item(variant) || propagate_variant(variant) end |
#show_company_address_field? ⇒ Boolean
162 163 164 |
# File 'app/models/spree/stock_location.rb', line 162 def show_company_address_field? true end |
#state_text ⇒ Object
33 34 35 |
# File 'app/models/spree/stock_location.rb', line 33 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.
54 55 56 |
# File 'app/models/spree/stock_location.rb', line 54 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.
67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/spree/stock_location.rb', line 67 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
58 59 60 |
# File 'app/models/spree/stock_location.rb', line 58 def stocks?(variant) stock_items.exists?(variant: variant) end |
#unstock(variant, quantity, originator = nil, persist: true) ⇒ Object
103 104 105 |
# File 'app/models/spree/stock_location.rb', line 103 def unstock(variant, quantity, originator = nil, persist: true) move(variant, -quantity, originator, persist: persist) end |