diff --git a/app/services/customers_with_balance.rb b/app/services/customers_with_balance.rb index 3bf9c633ff..d17f1997cb 100644 --- a/app/services/customers_with_balance.rb +++ b/app/services/customers_with_balance.rb @@ -7,7 +7,7 @@ class CustomersWithBalance def query Customer.of(enterprise). - joins(left_join_non_cart_orders). + joins(left_join_complete_orders). group("customers.id"). select("customers.*"). select(outstanding_balance) @@ -29,10 +29,20 @@ class CustomersWithBalance SQL end - def left_join_non_cart_orders + def left_join_complete_orders <<-SQL.strip_heredoc LEFT JOIN spree_orders ON spree_orders.customer_id = customers.id - AND spree_orders.state != 'cart' + AND #{complete_orders.to_sql} SQL end + + def complete_orders + states_group = prior_to_completion_states.map { |state| Arel::Nodes.build_quoted(state) } + Arel::Nodes::NotIn.new(Spree::Order.arel_table[:state], 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 end diff --git a/spec/services/customers_with_balance_spec.rb b/spec/services/customers_with_balance_spec.rb index 0ad61fb8de..5ff95a0057 100644 --- a/spec/services/customers_with_balance_spec.rb +++ b/spec/services/customers_with_balance_spec.rb @@ -7,11 +7,10 @@ describe CustomersWithBalance do describe '#query' do let(:customer) { create(:customer) } + let(:total) { 200.00 } + let(:order_total) { 100.00 } context 'when orders are in cart state' do - let(:total) { 200.00 } - let(:order_total) { 100.00 } - before do create(:order, customer: customer, total: order_total, payment_total: 0, state: 'cart') create(:order, customer: customer, total: order_total, payment_total: 0, state: 'cart') @@ -23,10 +22,43 @@ describe CustomersWithBalance do end end - context 'when no orders where paid' do - let(:total) { 200.00 } - let(:order_total) { 100.00 } + context 'when orders are in address state' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0, state: 'address') + create(:order, customer: customer, total: order_total, payment_total: 50, state: 'address') + end + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(0) + end + end + + context 'when orders are in delivery state' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0, state: 'delivery') + create(:order, customer: customer, total: order_total, payment_total: 50, state: 'delivery') + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(0) + end + end + + context 'when orders are in payment state' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0, state: 'payment') + create(:order, customer: customer, total: order_total, payment_total: 50, state: 'payment') + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(0) + end + end + + context 'when no orders where paid' do before do order = create(:order, customer: customer, total: order_total, payment_total: 0) order.update_attribute(:state, 'checkout') @@ -41,8 +73,6 @@ describe CustomersWithBalance do end context 'when an order was paid' do - let(:total) { 200.00 } - let(:order_total) { 100.00 } let(:payment_total) { order_total } before do @@ -59,8 +89,6 @@ describe CustomersWithBalance do end context 'when an order is canceled' do - let(:total) { 200.00 } - let(:order_total) { 100.00 } let(:payment_total) { 100.00 } let(:non_canceled_orders_total) { order_total }