From dfed20d6342a342a58c4d212584e5096a20d6eaa Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Dec 2020 19:02:07 +0100 Subject: [PATCH] Consider awaiting_return, returned in balance These states are reached when the order is complete and shipped. An admin can create a new return authorization, which will set the order in `awaiting_return` state. It's only after, when we call `return_authorization#receive` that the return authorization moves to `received` state and the order to `returned`. You can do so from the UI by editing the return authorization and clicking on Receive. However, we didn't find any order in such state in UK, FR and AU. The UX is quite obfuscated. That must be why no users clicked on it. The `returned` state cannot count for the balance as is, since some of the products are returned to the shop. That's enough for now. --- app/services/customers_with_balance.rb | 13 +++++++- spec/services/customers_with_balance_spec.rb | 33 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) 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