Skip to content

Commit

Permalink
fixup! Exclude #usage_count of cancelled order promotions
Browse files Browse the repository at this point in the history
  • Loading branch information
ikraamg committed Jul 5, 2021
1 parent b71e450 commit 62130e4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
11 changes: 6 additions & 5 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ class Adjustment < Spree::Base
#
# @param excluded_orders [Array<Spree::Order>] Orders to exclude from query
# @return [ActiveRecord::Relation] Scoped Adjustments
def self.in_completed_orders(excluded_orders: [])
joins(:order).
merge(Spree::Order.complete).
where.not(spree_orders: { id: excluded_orders }).
distinct
def self.in_completed_orders(excluded_orders: [], exclude_cancelled: false)
result = joins(:order)
.merge(Spree::Order.complete)
.where.not(spree_orders: { id: excluded_orders })
.distinct
exclude_cancelled ? result.merge(Spree::Order.not_canceled) : result
end

def finalize!
Expand Down
3 changes: 1 addition & 2 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ def usage_limit_exceeded?(excluded_orders: [])
def usage_count(excluded_orders: [])
Spree::Adjustment.promotion.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
where(spree_orders: { id: Spree::Order.not_canceled }).
in_completed_orders(excluded_orders: excluded_orders, exclude_cancelled: true).
where(source_id: actions).
count(:order_id)
end
Expand Down
3 changes: 1 addition & 2 deletions core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def usage_limit_exceeded?(excluded_orders: [])
def usage_count(excluded_orders: [])
adjustments.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
where(spree_orders: { id: Spree::Order.not_canceled }).
in_completed_orders(excluded_orders: excluded_orders, exclude_cancelled: true).
count(:order_id)
end

Expand Down

0 comments on commit 62130e4

Please sign in to comment.