-
-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Import count_on_hand methods for Spree 2 #2417
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,54 @@ def self.indexed | |
] | ||
end | ||
|
||
# Overriding `Spree::Variant.on_hand` in old Spree versions. | ||
# It doesn't exist in newer Spree versions. | ||
def on_hand | ||
if respond_to? :total_on_hand | ||
# This is Spree 2.0 | ||
total_on_hand | ||
elsif Spree::Config[:track_inventory_levels] && !on_demand | ||
count_on_hand | ||
else | ||
Float::INFINITY | ||
end | ||
end | ||
|
||
# Overriding `Spree::Variant.on_hand=` in old Spree versions. | ||
# It doesn't exist in newer Spree versions. | ||
def on_hand=(new_level) | ||
error = 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false' | ||
raise error unless Spree::Config[:track_inventory_levels] | ||
|
||
self.count_on_hand = new_level unless on_demand | ||
end | ||
|
||
# Overriding Spree::Variant.count_on_hand in old Spree versions. | ||
# It doesn't exist in newer Spree versions. | ||
def count_on_hand | ||
if respond_to? :total_on_hand | ||
# This is Spree 2.0 | ||
total_on_hand | ||
else | ||
# We assume old Spree and call ActiveRecord's method. | ||
# We can't call the same method on Variant, because we are overwriting it. | ||
super | ||
end | ||
end | ||
|
||
# Overriding `Spree::Variant.count_on_hand=` in old Spree versions. | ||
# It doesn't exist in newer Spree versions. | ||
def count_on_hand=(new_level) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these methods necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is this related to Variant Overrides? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Spree 1.3 the |
||
if respond_to? :total_on_hand | ||
# This is Spree 2.0 | ||
overwrite_stock_levels new_level | ||
else | ||
# We assume old Spree and call ActiveRecord's method. | ||
# We can't call the same method on Variant, because we are overwriting it. | ||
super unless on_demand | ||
end | ||
end | ||
|
||
def price_with_fees(distributor, order_cycle) | ||
price + fees_for(distributor, order_cycle) | ||
end | ||
|
@@ -119,6 +167,28 @@ def refresh_products_cache | |
|
||
private | ||
|
||
# Spree 2 creates this location in: | ||
# | ||
# core/db/migrate/20130213191427_create_default_stock.rb | ||
# | ||
# This is the only location we are using at the moment. So everything stays | ||
# the same, each variant has only one stock level (at the default location). | ||
def self.default_stock_location | ||
Spree::StockLocation.find_by_name("default") | ||
end | ||
|
||
# Temporary, backwards compatible setting of stock levels in Spree 2.0. | ||
# It would be better to use `Spree::StockItem.adjust_count_on_hand` which | ||
# takes a value to add to the current stock level and uses proper locking. | ||
def overwrite_stock_levels(new_level) | ||
stock_items.first.send :count_on_hand, new_level | ||
|
||
# There shouldn't be any other stock items, because we should have only one | ||
# stock location. But in case there are, the total should be new_level, | ||
# so all others need to be zero. | ||
stock_items[1..-1].send :count_on_hand, 0 | ||
end | ||
|
||
def update_weight_from_unit_value | ||
self.weight = weight_from_unit_value if self.product.variant_unit == 'weight' && unit_value.present? | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's where I saw this code before! Thank you. So this pull request would override Frank's change.