From baf1ecb4366159f063d69fdc77b54c493799406d Mon Sep 17 00:00:00 2001 From: lin-d-hop Date: Wed, 29 Jan 2020 12:51:45 +0000 Subject: [PATCH] Fixing incorrect payment and balance calculation Corrections and added specs --- .../order_cycle_management_report.rb | 4 +- .../user_balance_calculator.rb | 14 +---- .../user_balance_calculator_spec.rb | 58 ++++++++++++++++++- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb index 49fb2e7365..0188391551 100644 --- a/lib/open_food_network/order_cycle_management_report.rb +++ b/lib/open_food_network/order_cycle_management_report.rb @@ -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] diff --git a/lib/open_food_network/user_balance_calculator.rb b/lib/open_food_network/user_balance_calculator.rb index ba9d089452..5ce083e10f 100644 --- a/lib/open_food_network/user_balance_calculator.rb +++ b/lib/open_food_network/user_balance_calculator.rb @@ -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 diff --git a/spec/lib/open_food_network/user_balance_calculator_spec.rb b/spec/lib/open_food_network/user_balance_calculator_spec.rb index 021e0376ea..ebc1ee1568 100644 --- a/spec/lib/open_food_network/user_balance_calculator_spec.rb +++ b/spec/lib/open_food_network/user_balance_calculator_spec.rb @@ -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