From 62130e45ca8c5427060184ed5d673dbfa0c45631 Mon Sep 17 00:00:00 2001 From: Ikraam Ghoor Date: Mon, 5 Jul 2021 11:13:11 +0200 Subject: [PATCH] fixup! Exclude #usage_count of cancelled order promotions --- core/app/models/spree/adjustment.rb | 11 ++++++----- core/app/models/spree/promotion.rb | 3 +-- core/app/models/spree/promotion_code.rb | 3 +-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb index 3d9362c022b..418bad932ad 100644 --- a/core/app/models/spree/adjustment.rb +++ b/core/app/models/spree/adjustment.rb @@ -55,11 +55,12 @@ class Adjustment < Spree::Base # # @param excluded_orders [Array] 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! diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index 77408609fc5..3945905944c 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -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 diff --git a/core/app/models/spree/promotion_code.rb b/core/app/models/spree/promotion_code.rb index 124d0599909..ec7b87f3995 100644 --- a/core/app/models/spree/promotion_code.rb +++ b/core/app/models/spree/promotion_code.rb @@ -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