Class: Spree::StockLocation
- Inherits:
-
Object
- Object
- Spree::StockLocation
- Includes:
- Spree::Security::StockLocations, UniqueName, VendorConcern
- 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
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/spree/stock_location.rb', line 131 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
84 85 86 |
# File 'app/models/spree/stock_location.rb', line 84 def backorderable?(variant) stock_item(variant).try(:backorderable?) end |
#count_on_hand(variant) ⇒ Integer
Returns the count on hand number for the variant
80 81 82 |
# File 'app/models/spree/stock_location.rb', line 80 def count_on_hand(variant) stock_item(variant).try(:count_on_hand) end |
#display_name ⇒ Object
163 164 165 |
# File 'app/models/spree/stock_location.rb', line 163 def display_name @display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ') end |
#fill_status(variant, quantity) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/spree/stock_location.rb', line 114 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
104 105 106 107 108 109 110 111 112 |
# File 'app/models/spree/stock_location.rb', line 104 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
35 36 37 |
# File 'app/models/spree/stock_location.rb', line 35 def propagate_variant(variant) stock_items.create!(variant: variant, backorderable: backorderable_default) end |
#require_company? ⇒ Boolean
needed for address form
151 152 153 |
# File 'app/models/spree/stock_location.rb', line 151 def require_company? false end |
#require_name? ⇒ Boolean
needed for address form
146 147 148 |
# File 'app/models/spree/stock_location.rb', line 146 def require_name? false end |
#require_phone? ⇒ Boolean
155 156 157 |
# File 'app/models/spree/stock_location.rb', line 155 def require_phone? false end |
#restock(variant, quantity, originator = nil, persist: true) ⇒ Object
88 89 90 |
# File 'app/models/spree/stock_location.rb', line 88 def restock(variant, quantity, originator = nil, persist: true) move(variant, quantity, originator, persist: persist) end |
#restock_backordered(variant, quantity, _originator = nil) ⇒ Object
92 93 94 95 96 97 98 |
# File 'app/models/spree/stock_location.rb', line 92 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
42 43 44 |
# File 'app/models/spree/stock_location.rb', line 42 def set_up_stock_item(variant) stock_item(variant) || propagate_variant(variant) end |
#show_company_address_field? ⇒ Boolean
159 160 161 |
# File 'app/models/spree/stock_location.rb', line 159 def show_company_address_field? true end |
#state_text ⇒ Object
30 31 32 |
# File 'app/models/spree/stock_location.rb', line 30 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.
51 52 53 |
# File 'app/models/spree/stock_location.rb', line 51 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.
64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/spree/stock_location.rb', line 64 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
55 56 57 |
# File 'app/models/spree/stock_location.rb', line 55 def stocks?(variant) stock_items.exists?(variant: variant) end |
#unstock(variant, quantity, originator = nil, persist: true) ⇒ Object
100 101 102 |
# File 'app/models/spree/stock_location.rb', line 100 def unstock(variant, quantity, originator = nil, persist: true) move(variant, -quantity, originator, persist: persist) end |