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
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/models/spree/stock_location.rb', line 133 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
86 87 88 |
# File 'app/models/spree/stock_location.rb', line 86 def backorderable?(variant) stock_item(variant).try(:backorderable?) end |
#count_on_hand(variant) ⇒ Integer
Returns the count on hand number for the variant
82 83 84 |
# File 'app/models/spree/stock_location.rb', line 82 def count_on_hand(variant) stock_item(variant).try(:count_on_hand) end |
#display_name ⇒ Object
165 166 167 |
# File 'app/models/spree/stock_location.rb', line 165 def display_name @display_name ||= [admin_name, name].delete_if(&:blank?).join(' / ') end |
#fill_status(variant, quantity) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/spree/stock_location.rb', line 116 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
106 107 108 109 110 111 112 113 114 |
# File 'app/models/spree/stock_location.rb', line 106 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
37 38 39 |
# File 'app/models/spree/stock_location.rb', line 37 def propagate_variant(variant) stock_items.create!(variant: variant, backorderable: backorderable_default) end |
#require_company? ⇒ Boolean
needed for address form
153 154 155 |
# File 'app/models/spree/stock_location.rb', line 153 def require_company? false end |
#require_name? ⇒ Boolean
needed for address form
148 149 150 |
# File 'app/models/spree/stock_location.rb', line 148 def require_name? false end |
#require_phone? ⇒ Boolean
157 158 159 |
# File 'app/models/spree/stock_location.rb', line 157 def require_phone? false end |
#restock(variant, quantity, originator = nil, persist: true) ⇒ Object
90 91 92 |
# File 'app/models/spree/stock_location.rb', line 90 def restock(variant, quantity, originator = nil, persist: true) move(variant, quantity, originator, persist: persist) end |
#restock_backordered(variant, quantity, _originator = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/models/spree/stock_location.rb', line 94 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
44 45 46 |
# File 'app/models/spree/stock_location.rb', line 44 def set_up_stock_item(variant) stock_item(variant) || propagate_variant(variant) end |
#show_company_address_field? ⇒ Boolean
161 162 163 |
# File 'app/models/spree/stock_location.rb', line 161 def show_company_address_field? true end |
#state_text ⇒ Object
32 33 34 |
# File 'app/models/spree/stock_location.rb', line 32 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.
53 54 55 |
# File 'app/models/spree/stock_location.rb', line 53 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.
66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/spree/stock_location.rb', line 66 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
57 58 59 |
# File 'app/models/spree/stock_location.rb', line 57 def stocks?(variant) stock_items.exists?(variant: variant) end |
#unstock(variant, quantity, originator = nil, persist: true) ⇒ Object
102 103 104 |
# File 'app/models/spree/stock_location.rb', line 102 def unstock(variant, quantity, originator = nil, persist: true) move(variant, -quantity, originator, persist: persist) end |