Skip to content
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

update products when sales is disabled #8

Open
wants to merge 95 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
e2f51c2
Added a check for less than 0 msrp
ericsaupe Oct 6, 2014
f2ee480
Better shows sales if there is actually a percentage difference
ericsaupe Oct 6, 2014
9e52065
Improved visuals for non-sale items
ericsaupe Oct 6, 2014
69e9b85
Merge pull request #1 from deseretbook/msrp-fix
ericsaupe Oct 6, 2014
8e81c13
create 2-3-stable branch
thogg4 Oct 15, 2014
900b659
Adding sale price overrides
n00b2pr0 Oct 21, 2014
92f17b1
Made item_added override to work on variant level
n00b2pr0 Oct 22, 2014
9fd7834
Added a variant discount percentage
n00b2pr0 Oct 22, 2014
381d859
Added variant discount percentage; percent off to orders edit
n00b2pr0 Oct 22, 2014
3cb2794
Merge pull request #3 from deseretbook/kl0Ml4mh-spree-sale-pricing-si…
n00b2pr0 Oct 23, 2014
d4a8632
small changes for views
thogg4 Oct 24, 2014
3f2cba1
Merge branch '2-3-stable' of github.com:deseretbook/spree-sale-pricin…
thogg4 Oct 24, 2014
f702893
Merge pull request #2 from deseretbook/2-3-stable
thogg4 Oct 24, 2014
f780e3c
add display methods
thogg4 Oct 27, 2014
58d10eb
Merge branch '2-3-stable'
mike-bourgeous Oct 28, 2014
1f76e12
Big ole stinking hacky fix of a merge of open source work to improve …
Oct 28, 2014
4ca4948
Merge pull request #4 from deseretbook/dh_add_sale_price_to_item_total
thogg4 Oct 29, 2014
c30d618
Fix show percentage savings on cart page
MFRWDesign Oct 30, 2014
e8917e6
Merge pull request #5 from deseretbook/fix-percent-saving-cart
MFRWDesign Oct 30, 2014
bde4f1b
Add original price override to variant-listing; improvments to origin…
n00b2pr0 Nov 7, 2014
81e1105
Version bump
n00b2pr0 Nov 7, 2014
fc8b54b
Merge pull request #6 from deseretbook/RAGrgde9-improve-search-listin…
n00b2pr0 Nov 7, 2014
c36547c
Merge pull request #7 from deseretbook/2-3-stable
n00b2pr0 Nov 7, 2014
2c7ff37
rename in readme
thogg4 Nov 18, 2014
df8b999
change to put variants on sale instead of products
thogg4 Nov 19, 2014
3b0808d
up version
thogg4 Nov 19, 2014
943f576
Merge pull request #8 from deseretbook/put-variants-on-sale
thogg4 Nov 19, 2014
dc46602
Merge pull request #9 from deseretbook/2-3-stable
thogg4 Nov 19, 2014
33319c7
Add original price to variant options
n00b2pr0 Nov 25, 2014
70ef1bb
considerations for variant failing; version bump
n00b2pr0 Nov 25, 2014
83c680e
Removed duplicate check for variant
n00b2pr0 Nov 25, 2014
263337a
Merge pull request #10 from deseretbook/MKNOtnx7-add-price-and-shippi…
n00b2pr0 Nov 25, 2014
4e08c3b
Merge pull request #11 from deseretbook/2-3-stable
n00b2pr0 Nov 25, 2014
af04b07
remove puts statement and some spacing
thogg4 Jan 7, 2015
d42525f
Merge branch '2-3-stable' of github.com:deseretbook/spree-sale-pricin…
thogg4 Jan 7, 2015
acde792
up version
thogg4 Jan 7, 2015
201d477
get data from elasticsearch
thogg4 Feb 23, 2015
4d2126a
check to see if value is from elasticsearch index
thogg4 Feb 23, 2015
d41d25f
update version for es stuff
thogg4 Feb 23, 2015
b1450ae
Remove save percent, move list to right of price
n00b2pr0 Feb 25, 2015
e8796c8
Version bump to 0.3.4
n00b2pr0 Feb 25, 2015
eee8adb
Merge pull request #12 from deseretbook/Y3GklQwB-revert-search-listin…
n00b2pr0 Feb 25, 2015
f80f076
Merge pull request #13 from deseretbook/Y3GklQwB-revert-search-listin…
n00b2pr0 Feb 25, 2015
86fb691
Float fix
ericsaupe Mar 4, 2015
889bdaf
Merge pull request #14 from deseretbook/float-fix
ericsaupe Mar 4, 2015
fc07a49
Merge pull request #15 from deseretbook/2-3-stable
ericsaupe Mar 4, 2015
7b99ef4
Added PercentOnLineItemMsrp Calculator
ericsaupe May 7, 2015
6489e7c
Fixed initializer to add calculator correctly
ericsaupe May 7, 2015
7aa08a6
Moved translation into correct scope:
ericsaupe May 7, 2015
3a19fb0
Merge pull request #16 from deseretbook/msrp-calculator
ericsaupe May 7, 2015
a65c78e
Merge pull request #17 from deseretbook/2-3-stable
ericsaupe May 7, 2015
3e306a1
Removed % and added discounted method
n00b2pr0 May 21, 2015
1fbc696
Don't show red price if MSRP is greater than or equal to price.
mike-bourgeous Jul 8, 2015
4c3d0a9
Added a method directly on Spree::Variant to get a discount String.
mike-bourgeous Jul 10, 2015
211b4e3
Only mark variants as discounted if the rounded discount is >= 1%.
mike-bourgeous Jul 10, 2015
5bc00b1
Fix discount rounding check to mask inconsequential discount amounts.
mike-bourgeous Jul 10, 2015
1a62ff4
Fixed some missing percent symbols and superfluous discount texts.
mike-bourgeous Jul 10, 2015
2ddc0b0
Merge pull request #19 from deseretbook/7W2oirTl-add-styles-to-cart_form
mike-bourgeous Jul 13, 2015
e31c15d
Added hook after sale is created or deleted to update product
ericsaupe Sep 1, 2015
cc01c4f
Merge pull request #20 from deseretbook/save-hook
ericsaupe Sep 1, 2015
5274f49
move display_variant into nibley
thogg4 Sep 2, 2015
6277232
Merge branch '2-3-stable' of github.com:deseretbook/spree-sale-pricin…
thogg4 Sep 2, 2015
43bb990
up version
thogg4 Sep 2, 2015
9343caa
Merge branch 'master' of github.com:deseretbook/spree-sale-pricing in…
thogg4 Sep 2, 2015
6d55963
Readded original_price_display_variant
ericsaupe Sep 23, 2015
1bf0011
Merge pull request #21 from deseretbook/wishlists
ericsaupe Sep 23, 2015
26125c1
Add original price to variant pricing
n00b2pr0 Nov 13, 2015
0cf1404
Merge pull request #22 from deseretbook/jMIiHFFU-promo-banner
ericsaupe Nov 16, 2015
b0e53cb
Fixed Per-line-item action in Spree not taking quantity into account
ibagmet Nov 25, 2015
e4da7e2
Use lowest price rather than last created
ericsaupe Dec 2, 2015
c026c6d
Merge pull request #24 from deseretbook/lowest-sale
jeffreyfuchang Dec 2, 2015
f00ee39
Cleaned up commented code, indenting, whitespace, returns
n00b2pr0 Jan 11, 2016
5f2c6ae
Merge branch '2-3-stable' of github.com:deseretbook/spree-sale-pricin…
n00b2pr0 Jan 11, 2016
714d863
Merge pull request #25 from deseretbook/per_line_item_action
jeffreyfuchang Jan 28, 2016
6ca6fd8
version bump to 0.3.11
jeffreyfuchang Jan 29, 2016
b6f55cb
Merge pull request #26 from deseretbook/version_0-3-11
jeffreyfuchang Jan 29, 2016
8644a86
update ES documents when SalePrice is disabled
kascote Feb 2, 2016
c4c6a2e
fix product trigger update
kascote Feb 2, 2016
3086bbb
use parameters to lookup sales
kascote Feb 3, 2016
333ad6a
Revert back to original commit
jeffreyfuchang Feb 3, 2016
e64b734
spelling error
jeffreyfuchang Feb 3, 2016
bfe2f91
add date as param and make timezone aware
jeffreyfuchang Feb 5, 2016
0d688b7
correct time
jeffreyfuchang Feb 11, 2016
32b0010
disable SalePrice before update document
kascote Mar 8, 2016
bb403ef
fix merge
kascote Mar 8, 2016
01ca0dd
bump version to 0.3.8
jeffreyfuchang Mar 9, 2016
841b234
merge 2-3-stable branch and change version to 0-3-12
jeffreyfuchang Mar 9, 2016
a4f3d4f
update_product_documents expire anything before passed in date
jeffreyfuchang Mar 9, 2016
6406ddd
add log
jeffreyfuchang Mar 9, 2016
af1a4bd
remove disable of sale price in rake task update_product_documents
jeffreyfuchang Mar 9, 2016
9098592
use epoch for time
jeffreyfuchang Mar 10, 2016
935fea2
fix time
jeffreyfuchang Mar 10, 2016
c939111
add disable back
jeffreyfuchang Mar 10, 2016
b7ccd4c
change log to warn
jeffreyfuchang Mar 10, 2016
05cba64
add puts for info
jeffreyfuchang Mar 10, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ nbproject
*.swp
spec/dummy
.rvmrc
Gemfile.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ In your Gemfile add the following for the latest released version:

_OR_ to work from master:

gem 'spree_sale_pricing', :git => 'git://github.com/jpstokes/spree-sale-pricing.git'
gem 'spree_sale_pricing', :git => 'git://github.com/deseretbook/spree_sale_pricing.git'

Install the Gem:

Expand Down
43 changes: 21 additions & 22 deletions app/controllers/spree/admin/sale_prices_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module Spree
module Admin
class SalePricesController < BaseController

before_filter :load_product

before_filter :load_product_and_variant
after_filter :update_product, only: [:create, :destroy]
respond_to :js, :html

# Show all sale prices for a product
def index
@sale_prices = @product.sale_prices
@sale_price = Spree::SalePrice.new
@sale_prices = @variant.sale_prices
end

# Create a new sale price
def create
@sale_price = @product.put_on_sale params[:sale_price][:value], sale_price_params
@sale_price = @variant.put_on_sale(sale_price_params[:value], sale_price_params[:start_at], sale_price_params[:end_at])
logger.debug @sale_price.inspect
respond_with(@sale_price)
end

Expand All @@ -27,22 +27,21 @@ def destroy

private

# Load the product as a before filter. Redirect to the referer if no product is found
def load_product
@product = Spree::Product.find_by(slug: params[:product_id])
redirect_to request.referer unless @product.present?
end

# Sale price params
def sale_price_params
params.require(:sale_price).permit(
:id,
:value,
:start_at,
:end_at,
:enabled
)
end
# Load the variant as a before filter. Redirect to the referer if no product is found
def load_product_and_variant
@product = Spree::Product.find_by(slug: params[:product_id])
@variant = @product.variants_including_master.find(params[:variant_id])
redirect_to request.referer unless @variant
end

# Sale price params
def sale_price_params
params.require(:sale_price).permit(:id, :value, :start_at, :end_at, :enabled)
end

def update_product
@product.touch
end

end
end
Expand Down
10 changes: 8 additions & 2 deletions app/helpers/spree/products_helper_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
def discount_percentage(product)
if product.master.msrp.present?
"#{((1 - product.price.to_f / product.master.msrp.to_f) * 100).round}%"
if product.master.msrp.present? && (product.master.msrp.to_f > product.price.to_f)
"#{((1 - product.price.to_f / product.master.msrp.to_f) * 100).round}"
else
'0'
end
end

def discount_percentage_variant(variant)
variant.display_discount
end
26 changes: 26 additions & 0 deletions app/models/spree/calculator/percent_on_line_item_msrp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Spree
class Calculator
class PercentOnLineItemMsrp < Calculator
preference :percent, :decimal, default: 0

def self.description
Spree.t(:percent_per_item_msrp)
end

def compute(object)
variant = object.variant
sale_percent = 0

if variant.msrp.present? && (variant.msrp.to_f > 0)
sale_percent = ((1 - ((variant.price.instance_of?(BigDecimal) || variant.price.instance_of?(Float)) ? variant.price.to_f : variant.price.amount.to_f) / variant.msrp.to_f) * 100).round
end

if preferred_percent < sale_percent
return 0
end

((variant.msrp * object.quantity) * (preferred_percent - sale_percent)) / 100
end
end
end
end
8 changes: 8 additions & 0 deletions app/models/spree/order_updater_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Spree::OrderUpdater.class_eval do

def update_item_total
order.item_total = line_items.sum('price * quantity')
update_order_total
end

end
55 changes: 31 additions & 24 deletions app/models/spree/price_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
Spree::Price.class_eval do

has_many :sale_prices

def put_on_sale value, params={}
new_sale(value, params).save
end

def new_sale value, params={}
calculator_type = params[:calculator_type] || Spree::Calculator::DollarAmountSalePriceCalculator.new
start_at = params[:start_at] || Time.now
end_at = params[:end_at] || nil
enabled = params[:enabled] || true
# TODO also accept a class reference for calculator type instead of only a string
def put_on_sale(value, calculator_type = "Spree::Calculator::DollarAmountSalePriceCalculator", start_at = Time.now, end_at = nil, enabled = true)
new_sale(value, calculator_type, start_at, end_at, enabled).save
end
alias :create_sale :put_on_sale

sale_price = sale_prices.new({ value: value, start_at: start_at, end_at: end_at, enabled: enabled, calculator: calculator_type })
def new_sale(value, calculator_type = "Spree::Calculator::DollarAmountSalePriceCalculator", start_at = Time.now, end_at = nil, enabled = true)
sale_price = sale_prices.new({ value: value, start_at: start_at, end_at: end_at, enabled: enabled })
sale_price.calculator_type = calculator_type
sale_price
end

# TODO make update_sale method

def active_sale
on_sale? ? first_sale(sale_prices.active) : nil
on_sale? ? lowest_sale(sale_prices.active) : nil
end
alias :current_sale :active_sale

def next_active_sale
sale_prices.present? ? first_sale(sale_prices) : nil
sale_prices.present? ? lowest_sale(sale_prices) : nil
end
alias :next_current_sale :next_active_sale

def sale_price
on_sale? ? active_sale.price : nil
end

def sale_price=(value)
on_sale? ? active_sale.update_attribute(:value, value) : put_on_sale(value)
end
Expand All @@ -41,25 +38,35 @@ def discount_percent
end

def on_sale?
sale_prices.active.present? && first_sale(sale_prices.active).value != original_price
sale_prices.active.present? && lowest_sale(sale_prices.active).value != original_price
end

def original_price
self[:amount]
end

def original_price=(value)
self.price = value
end

def price
on_sale? ? sale_price : original_price
end

def amount
price
end



def display_original_price
Spree::Money.new(self[:amount] || 0, { currency: currency })
end
def display_sale_price
Spree::Money.new(amount || 0, { currency: currency })
end


def enable_sale
return nil unless next_active_sale.present?
next_active_sale.enable
Expand All @@ -79,10 +86,10 @@ def stop_sale
return nil unless active_sale.present?
active_sale.stop
end
private
def first_sale(scope)
scope.order("created_at DESC").first

private

def lowest_sale(scope)
scope.order("value ASC").first
end
end
12 changes: 3 additions & 9 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
Spree::Product.class_eval do

has_many :sale_prices, through: :prices

# Essentially all read values here are delegated to reading the value on the Master variant
# All write values will write to all variants (including the Master) unless that method's all_variants parameter is set to false, in which case it will only write to the Master variant

delegate_belongs_to :master, :active_sale_in, :current_sale_in, :next_active_sale_in, :next_current_sale_in,
:sale_price_in, :on_sale_in?, :original_price_in, :discount_percent_in, :sale_price,
:original_price, :on_sale?

:original_price, :on_sale?, :display_original_price, :display_sale_price


# TODO also accept a class reference for calculator type instead of only a string
def put_on_sale value, params={}
all_variants = params[:all_variants] || true

run_on_variants(all_variants) { |v| v.put_on_sale(value, params) }
def put_on_sale(value, calculator_type = "Spree::Calculator::DollarAmountSalePriceCalculator", all_variants = true, start_at = Time.now, end_at = nil, enabled = true)
run_on_variants(all_variants) { |v| v.put_on_sale(value, calculator_type, true, start_at, end_at, enabled) }
end
alias :create_sale :put_on_sale

Expand Down
24 changes: 17 additions & 7 deletions app/models/spree/sale_price.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module Spree
class SalePrice < ActiveRecord::Base

# TODO validations
belongs_to :price, :class_name => "Spree::Price"
has_one :calculator, :class_name => "Spree::Calculator", :as => :calculable, :dependent => :destroy

accepts_nested_attributes_for :calculator

validates :calculator, :presence => true

scope :active, -> { where(enabled: true).where('(start_at <= ? OR start_at IS NULL) AND (end_at >= ? OR end_at IS NULL)', Time.now, Time.now) }
after_save :refresh_product_document

#attr_accessible :value, :start_at, :end_at, :enabled

scope :active, lambda {
where("enabled = 't' AND (start_at <= ? OR start_at IS NULL) AND (end_at >= ? OR end_at IS NULL)", Time.now, Time.now)
}

# TODO make this work or remove it
#def self.calculators
Expand All @@ -20,6 +23,11 @@ def calculator_type
calculator.class.to_s if calculator
end

def calculator_type=(calculator_type)
clazz = calculator_type.constantize if calculator_type
self.calculator = clazz.new if clazz and not self.calculator.is_a? clazz
end

def price
calculator.compute self
end
Expand All @@ -43,9 +51,11 @@ def stop
update_attributes({ end_at: Time.now, enabled: false })
end

# Convenience method for displaying the price of a given sale_price in the table
def display_price
Spree::Money.new(value, {currency: Spree::Config[:currency]})

def refresh_product_document
_price = Spree::Price.where(id: self.price_id).first
_price.variant.product.save unless _price.nil?
end

end
end
42 changes: 32 additions & 10 deletions app/models/spree/variant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

has_many :sale_prices, through: :prices

delegate_belongs_to :default_price, :sale_price, :original_price, :on_sale?
delegate_belongs_to :default_price, :sale_price, :original_price, :on_sale?,
:display_original_price, :display_sale_price

def put_on_sale value, params={}
all_currencies = params[:all_variants] || true
run_on_prices(all_currencies) { |p| p.put_on_sale value, params }
# TODO also accept a class reference for calculator type instead of only a string
def put_on_sale(value, start_at = Time.now, end_at = nil, enabled = true, all_currencies = true, calculator_type = "Spree::Calculator::DollarAmountSalePriceCalculator")
run_on_prices(all_currencies) { |p| p.put_on_sale value, calculator_type, start_at, end_at, enabled }
end
alias :create_sale :put_on_sale

Expand All @@ -25,11 +26,32 @@ def next_active_sale_in(currency)
def sale_price_in(currency)
Spree::Price.new variant_id: self.id, currency: currency, amount: price_in(currency).sale_price
end


# Returns true if the product is discounted by an amount that rounds to at least 1%
def discounted?
begin
price = self.price.respond_to?(:to_f) ? self.price.to_f : self.price.amount.to_f
msrp = self.msrp.try(:to_f)

msrp && self.price && ((1 - price / msrp) * 100).round > 0
rescue
false
end
end

def discount_percent_in(currency)
price_in(currency).discount_percent
end


# Returns a String with the discounted percentage between #price and #msrp.
def display_discount
if discounted?
"#{((1 - price / msrp) * 100).round}"
else
'0'
end
end

def on_sale_in?(currency)
price_in(currency).on_sale?
end
Expand All @@ -53,14 +75,14 @@ def start_sale(end_time = nil, all_currencies = true)
def stop_sale(all_currencies=true)
run_on_prices(all_currencies) { |p| p.stop_sale }
end

private

def run_on_prices(all_currencies, &block)
if all_currencies && prices.present?
prices.each { |p| block.call p }
else
block.call default_price
block.call default_price
end
end
end
end
5 changes: 0 additions & 5 deletions app/overrides/add_msrp_to_new_product.rb

This file was deleted.

5 changes: 5 additions & 0 deletions app/overrides/add_original_price_to_orders_item_added.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deface::Override.new(virtual_path: "spree/orders/item_added",
name: "add_original_price_to_orders_item_added",
replace_contents: "p.listing-price",
partial: "spree/shared/original_price_display_item_added",
disabled: false)
Loading