Class: SpreeCmCommissioner::InventoryItem
- Inherits:
-
Base
- Object
- Spree::Base
- Base
- SpreeCmCommissioner::InventoryItem
show all
- Includes:
- ProductType
- Defined in:
- app/models/spree_cm_commissioner/inventory_item.rb
Constant Summary
collapse
- MAX_DISPLAY_STOCK =
20
Constants included
from ProductType
ProductType::PERMANENT_STOCK_PRODUCT_TYPES, ProductType::PRE_INVENTORY_DAYS, ProductType::PRODUCT_TYPES
Instance Method Summary
collapse
#permanent_stock?, #pre_inventory_days
Instance Method Details
#active? ⇒ Boolean
89
90
91
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 89
def active?
inventory_date.nil? || inventory_date >= Time.zone.today
end
|
#adjust_quantity!(quantity) ⇒ Object
This method is only used when admin update stock
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 35
def adjust_quantity!(quantity)
with_lock do
self.max_capacity = max_capacity + quantity
self.quantity_available = quantity_available + quantity
save!
adjust_quantity_in_redis(quantity)
end
end
|
#adjust_quantity_in_redis(quantity) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 68
def adjust_quantity_in_redis(quantity)
SpreeCmCommissioner.inventory_redis_pool.with do |redis|
script = <<~LUA
local key = KEYS[1]
local increment = tonumber(ARGV[1])
local expiry = tonumber(ARGV[2])
local current = tonumber(redis.call('GET', key) or 0)
local new_value = current + increment
if new_value < 0 then
new_value = 0
end
redis.call('SET', key, new_value, 'EX', expiry)
return new_value
LUA
redis.eval(script, keys: [redis_key], argv: [quantity, redis_expired_in])
end
end
|
#price_in(currency) ⇒ Object
30
31
32
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 30
def price_in(currency)
prices.detect { |price| price.currency == currency } || prices.build(currency: currency)
end
|
#public_quantity_available ⇒ Object
26
27
28
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 26
def public_quantity_available
[quantity_available, MAX_DISPLAY_STOCK].min
end
|
#quantity_in_redis ⇒ Object
#redis_expired_in ⇒ Object
1 year expiry, whether the inventory item is permanent stock or not.
Why even for permanent stock? Because if we expire it too soon (e.g. right after inventory_date), admin usually still need to look up past/old inventory items or adjust stock when needed.
97
98
99
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 97
def redis_expired_in
31_536_000
end
|
#redis_hold_key ⇒ Object
60
61
62
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 60
def redis_hold_key
"inventory:on_hold:#{id}"
end
|
#redis_key ⇒ Object
56
57
58
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 56
def redis_key
"inventory:#{id}"
end
|