diff --git a/app/services/customers_with_balance.rb b/app/services/customers_with_balance.rb index f24c06d408..56a3c2d171 100644 --- a/app/services/customers_with_balance.rb +++ b/app/services/customers_with_balance.rb @@ -22,7 +22,7 @@ class CustomersWithBalance def outstanding_balance <<-SQL.strip_heredoc SUM( - CASE WHEN state = 'canceled' THEN payment_total + CASE WHEN state IN #{non_fulfilled_states_group.to_sql} THEN payment_total WHEN state IS NOT NULL THEN payment_total - total ELSE 0 END ) AS balance_value @@ -43,8 +43,19 @@ class CustomersWithBalance Arel::Nodes::NotIn.new(Spree::Order.arel_table[:state], states_group) end + def non_fulfilled_states_group + states_group = non_fulfilled_states.map { |state| Arel::Nodes.build_quoted(state) } + Arel::Nodes::Grouping.new(states_group) + end + # All the states an order can be in before completing the checkout def prior_to_completion_states %w(cart address delivery payment) end + + # All the states of a complete order but that shouldn't count towards the balance. Those that the + # customer won't enjoy. + def non_fulfilled_states + %w(canceled returned) + end end diff --git a/spec/services/customers_with_balance_spec.rb b/spec/services/customers_with_balance_spec.rb index 5bac659d23..fefd1d9090 100644 --- a/spec/services/customers_with_balance_spec.rb +++ b/spec/services/customers_with_balance_spec.rb @@ -142,6 +142,39 @@ describe CustomersWithBalance do end end + context 'when an order is awaiting_return' do + let(:payment_total) { order_total } + + before do + order = create(:order, customer: customer, total: order_total, payment_total: 0) + order.update_attribute(:state, 'checkout') + order = create(:order, customer: customer, total: order_total, payment_total: payment_total) + order.update_attribute(:state, 'awaiting_return') + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - total) + end + end + + context 'when an order is returned' do + let(:payment_total) { order_total } + let(:non_returned_orders_total) { order_total } + + before do + order = create(:order, customer: customer, total: order_total, payment_total: 0) + order.update_attribute(:state, 'checkout') + order = create(:order, customer: customer, total: order_total, payment_total: payment_total) + order.update_attribute(:state, 'returned') + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - non_returned_orders_total) + end + end + context 'when there are no orders' do it 'returns the customer balance' do customer = customers_with_balance.query.first