mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Don't consider order total when it is canceled
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CustomersWithBalance
|
||||
def initialize(enterprise_id)
|
||||
@enterprise_id = enterprise_id
|
||||
@@ -6,14 +8,22 @@ class CustomersWithBalance
|
||||
def query
|
||||
Customer.of(enterprise_id).
|
||||
includes(:bill_address, :ship_address, user: :credit_cards).
|
||||
joins(:orders).
|
||||
merge(Spree::Order.complete.not_state(:canceled)).
|
||||
joins("LEFT JOIN spree_orders ON spree_orders.customer_id = customers.id").
|
||||
group("customers.id").
|
||||
select("customers.*").
|
||||
select("SUM(total - payment_total) AS balance_value")
|
||||
select(outstanding_balance)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :enterprise_id
|
||||
|
||||
def outstanding_balance
|
||||
<<-SQL.strip_heredoc
|
||||
SUM(
|
||||
CASE WHEN state = 'canceled' THEN payment_total
|
||||
ELSE payment_total - total END
|
||||
) AS balance_value
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
65
spec/services/customers_with_balance_spec.rb
Normal file
65
spec/services/customers_with_balance_spec.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe CustomersWithBalance do
|
||||
subject(:customers_with_balance) { described_class.new(customer.enterprise.id) }
|
||||
|
||||
describe '#query' do
|
||||
let(:customer) { create(:customer) }
|
||||
|
||||
context 'when no orders where paid' do
|
||||
let(:total) { 200.00 }
|
||||
let(:order_total) { 100.00 }
|
||||
|
||||
before do
|
||||
create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
customer = customers_with_balance.query.first
|
||||
expect(customer.balance_value).to eq(-total)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when an order was paid' do
|
||||
let(:total) { 200.00 }
|
||||
let(:order_total) { 100.00 }
|
||||
let(:payment_total) { order_total }
|
||||
|
||||
before do
|
||||
create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
create(:order, customer: customer, total: order_total, payment_total: payment_total)
|
||||
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 canceled' do
|
||||
let(:total) { 200.00 }
|
||||
let(:order_total) { 100.00 }
|
||||
let(:payment_total) { 100.00 }
|
||||
let(:complete_orders_total) { order_total }
|
||||
|
||||
before do
|
||||
create(:order, customer: customer, total: order_total, payment_total: 0)
|
||||
create(
|
||||
:order,
|
||||
customer: customer,
|
||||
total: order_total,
|
||||
payment_total: order_total,
|
||||
state: 'canceled'
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns the customer balance' do
|
||||
customer = customers_with_balance.query.first
|
||||
expect(customer.balance_value).to eq(payment_total - complete_orders_total)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user