mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Fixing incorrect payment and balance calculation
Corrections and added specs
This commit is contained in:
@@ -77,7 +77,7 @@ module OpenFoodNetwork
|
||||
ba.phone,
|
||||
order.shipping_method.andand.name,
|
||||
order.payments.first.andand.payment_method.andand.name,
|
||||
order.payments.first.amount,
|
||||
order.payment_total,
|
||||
OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance]
|
||||
end
|
||||
|
||||
@@ -92,7 +92,7 @@ module OpenFoodNetwork
|
||||
sa.phone,
|
||||
order.shipping_method.andand.name,
|
||||
order.payments.first.andand.payment_method.andand.name,
|
||||
order.payments.first.amount,
|
||||
order.payment_total,
|
||||
OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance,
|
||||
has_temperature_controlled_items?(order),
|
||||
order.special_instructions]
|
||||
|
||||
@@ -6,25 +6,13 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
def balance
|
||||
payment_total - completed_order_total
|
||||
-completed_orders.sum(&:outstanding_balance)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def completed_order_total
|
||||
completed_orders.sum(&:total)
|
||||
end
|
||||
|
||||
def payment_total
|
||||
payments.sum(&:amount)
|
||||
end
|
||||
|
||||
def completed_orders
|
||||
Spree::Order.where(distributor_id: @distributor, email: @email).complete.not_state(:canceled)
|
||||
end
|
||||
|
||||
def payments
|
||||
Spree::Payment.where(order_id: completed_orders, state: "completed")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -69,7 +69,7 @@ module OpenFoodNetwork
|
||||
create(:order_with_totals_and_distribution,
|
||||
user: user1, distributor: hub1,
|
||||
completed_at: 1.day.ago, state: "canceled")
|
||||
} # total=10
|
||||
} # total=13 (10 + 3 shipping fee)
|
||||
let!(:p4) {
|
||||
create(:payment, order: o4, amount: 20.00,
|
||||
state: "completed")
|
||||
@@ -79,6 +79,62 @@ module OpenFoodNetwork
|
||||
expect(UserBalanceCalculator.new(o4.email, hub1).balance).to eq(-9) # = 15 + 2 - 13 - 13
|
||||
end
|
||||
end
|
||||
|
||||
context "with void payments" do
|
||||
let!(:o4) {
|
||||
create(:order_with_totals_and_distribution,
|
||||
user: user1, distributor: hub1,
|
||||
completed_at: 1.day.ago)
|
||||
} # total=13 (10 + 3 shipping fee)
|
||||
let!(:p4) {
|
||||
create(:payment, order: o4, amount: 20.00,
|
||||
state: "void")
|
||||
}
|
||||
|
||||
it "does not include void in the balance" do
|
||||
expect(UserBalanceCalculator.new(o4.email, hub1).balance).to eq(-22) # = 15 + 2 - 13 - 13 - 10
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid payments" do
|
||||
let!(:o4) {
|
||||
create(:order_with_totals_and_distribution,
|
||||
user: user1, distributor: hub1,
|
||||
completed_at: 1.day.ago)
|
||||
} # total=13 (10 + 3 shipping fee)
|
||||
let!(:p4) {
|
||||
create(:payment, order: o4, amount: 20.00,
|
||||
state: "invalid")
|
||||
}
|
||||
|
||||
it "does not include invalid payments in the balance" do
|
||||
expect(UserBalanceCalculator.new(o4.email, hub1).balance).to eq(-22) # = 15 + 2 - 13 - 13 - 10
|
||||
end
|
||||
end
|
||||
|
||||
context "with multiple payments on single order" do
|
||||
let!(:o4) {
|
||||
create(:order_with_totals_and_distribution,
|
||||
user: user1, distributor: hub1,
|
||||
completed_at: 1.day.ago)
|
||||
} # total=13 (10 + 3 shipping fee)
|
||||
let!(:p4) {
|
||||
create(:payment, order: o4, amount: 4.00,
|
||||
state: "completed")
|
||||
}
|
||||
let!(:p5) {
|
||||
create(:payment, order: o4, amount: 5.00,
|
||||
state: "completed")
|
||||
}
|
||||
let!(:p6) {
|
||||
create(:payment, order: o4, amount: 6.00,
|
||||
state: "completed")
|
||||
}
|
||||
|
||||
it "includes orders with multiple payments in the balance" do
|
||||
expect(UserBalanceCalculator.new(o4.email, hub1).balance).to eq(-7) # = 15 + 2 + 4 + 5 + 6 - 13 - 13 - 10
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user