Skip to content

Commit

Permalink
Merge pull request #4123 from nebulab/exclude-canceled-orders-from-pr…
Browse files Browse the repository at this point in the history
…omotioncount

Exclude canceled orders in the #usage_count of promotions and promotion codes
  • Loading branch information
spaghetticode authored Jul 14, 2021
2 parents 7118543 + 843df0b commit abdd013
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 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_canceled: false)
result = joins(:order)
.merge(Spree::Order.complete)
.where.not(spree_orders: { id: excluded_orders })
.distinct
exclude_canceled ? result.merge(Spree::Order.not_canceled) : result
end

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

Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/promotion_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@
before { order.adjustments.promotion.update_all(eligible: false) }
it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@
before { order.adjustments.promotion.update_all(eligible: false) }
it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end

Expand Down

0 comments on commit abdd013

Please sign in to comment.