diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index 51ad5a519f..3ab665d9b5 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -65,13 +65,7 @@ module Admin def collection return Customer.where("1=0") unless json_request? && params[:enterprise_id].present? - Customer.of(managed_enterprise_id). - includes(:bill_address, :ship_address, user: :credit_cards). - joins(:orders). - merge(Spree::Order.complete.not_state(:canceled)). - group("customers.id"). - select("customers.*"). - select("SUM(total - payment_total) AS balance_value") + CustomersWithBalance.new(managed_enterprise_id).query end def managed_enterprise_id diff --git a/app/services/customers_with_balance.rb b/app/services/customers_with_balance.rb new file mode 100644 index 0000000000..0abd804ff2 --- /dev/null +++ b/app/services/customers_with_balance.rb @@ -0,0 +1,19 @@ +class CustomersWithBalance + def initialize(enterprise_id) + @enterprise_id = enterprise_id + end + + def query + Customer.of(enterprise_id). + includes(:bill_address, :ship_address, user: :credit_cards). + joins(:orders). + merge(Spree::Order.complete.not_state(:canceled)). + group("customers.id"). + select("customers.*"). + select("SUM(total - payment_total) AS balance_value") + end + + private + + attr_reader :enterprise_id +end