From 977ab26b00a76cb663f3a002a04516c81ad83b2e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 19 Aug 2020 12:50:17 +0100 Subject: [PATCH 1/2] Add failing spec for payment fee calculation in customer totals report --- .../customer_totals_report_spec.rb | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb index 6bae6da8a1..990f7e3cb7 100644 --- a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb +++ b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb @@ -1,5 +1,7 @@ require "spec_helper" +require 'open_food_network/orders_and_fulfillments_report' require 'open_food_network/orders_and_fulfillments_report/customer_totals_report' +require 'open_food_network/order_grouper' RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsReport do let!(:distributor) { create(:distributor_enterprise) } @@ -79,6 +81,28 @@ RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsRepor end end + context "displaying payment fees" do + context "with both failed and completed payments present" do + let!(:order) { + create(:order_ready_to_ship, user: customer.user, + customer: customer, distributor: distributor) + } + let(:completed_payment) { order.payments.completed.first } + let!(:failed_payment) { create(:payment, order: order, state: "failed") } + + before do + completed_payment.adjustment.update amount: 123.00 + failed_payment.adjustment.update amount: 456.00, eligible: false, state: "finalized" + end + + xit "shows the correct payment fee amount for the order" do + # Fails; the sum of adjustments for both failed and complete payments is shown + payment_fee_field = report_table.last[12] + expect(payment_fee_field).to eq completed_payment.adjustment.amount + end + end + end + context 'when a variant override applies' do let!(:order) do create(:completed_order_with_totals, line_items_count: 1, user: customer.user, From 3badaa07d20799b38d29f0309d3d9b922242f9d3 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 19 Aug 2020 12:52:40 +0100 Subject: [PATCH 2/2] Fix adjustment calculations; only "eligible" adjustments should be regarded as applied to an order. When an order is submitted and the payment fails, the failed payment's adjustments (payment fees) are set to `eligible: false` to indicate they do not apply. These should not be counted as being included in an order's adjustments. --- app/services/order_adjustments_fetcher.rb | 5 +++-- .../customer_totals_report_spec.rb | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/services/order_adjustments_fetcher.rb b/app/services/order_adjustments_fetcher.rb index 40d78cf6bc..560f04bdb1 100644 --- a/app/services/order_adjustments_fetcher.rb +++ b/app/services/order_adjustments_fetcher.rb @@ -57,11 +57,12 @@ class OrderAdjustmentsFetcher if adjustments_eager_loaded? adjustment_scope = public_send("#{scope}_scope") + # Adjustments are already loaded here, this block is using `Array#select` adjustments.select do |adjustment| - match_by_scope(adjustment, adjustment_scope) + match_by_scope(adjustment, adjustment_scope) && match_by_scope(adjustment, eligible_scope) end else - adjustments.where(nil).public_send scope + adjustments.where(nil).eligible.public_send scope end end diff --git a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb index 990f7e3cb7..4799c3e4ac 100644 --- a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb +++ b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb @@ -95,8 +95,7 @@ RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsRepor failed_payment.adjustment.update amount: 456.00, eligible: false, state: "finalized" end - xit "shows the correct payment fee amount for the order" do - # Fails; the sum of adjustments for both failed and complete payments is shown + it "shows the correct payment fee amount for the order" do payment_fee_field = report_table.last[12] expect(payment_fee_field).to eq completed_payment.adjustment.amount end