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
80
81
82
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 80
def active?
inventory_date.nil? || inventory_date >= Time.zone.today
end
|
#adjust_quantity!(quantity) ⇒ Object
This method is only used when admin update stock
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 34
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 59
def adjust_quantity_in_redis(quantity)
SpreeCmCommissioner.redis_pool.with do |redis|
script = " local key = KEYS[1]\n local increment = tonumber(ARGV[1])\n local expiry = tonumber(ARGV[2])\n local current = tonumber(redis.call('GET', key) or 0)\n local new_value = current + increment\n if new_value < 0 then\n new_value = 0\n end\n redis.call('SET', key, new_value, 'EX', expiry)\n return new_value\n LUA\n\n redis.eval(script, keys: [redis_key], argv: [quantity, redis_expired_in])\n end\nend\n"
|
#price_in(currency) ⇒ Object
29
30
31
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 29
def price_in(currency)
prices.detect { |price| price.currency == currency } || prices.build(currency: currency)
end
|
#public_quantity_available ⇒ Object
25
26
27
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 25
def public_quantity_available
[quantity_available, MAX_DISPLAY_STOCK].min
end
|
#redis_expired_in ⇒ Object
84
85
86
87
88
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 84
def redis_expired_in
expired_in = 31_536_000
expired_in = Time.parse(inventory_date.to_s).end_of_day.to_i - Time.zone.now.to_i if inventory_date.present?
[expired_in, 0].max
end
|
#redis_key ⇒ Object
55
56
57
|
# File 'app/models/spree_cm_commissioner/inventory_item.rb', line 55
def redis_key
"inventory:#{id}"
end
|