Sum balances in Payments report implementing #+

This avoids consumers of `OrderBalance` having to couple with the inner
details of this abstraction, which makes the code more changeable.
This commit is contained in:
Pau Perez
2021-03-22 11:36:23 +01:00
parent 1a1552a6ed
commit c7b85a3591
4 changed files with 115 additions and 27 deletions

View File

@@ -23,6 +23,10 @@ class OrderBalance
end
end
def +(other)
to_f + other.to_f
end
private
attr_reader :order

View File

@@ -100,7 +100,7 @@ module OpenFoodNetwork
proc { |orders| orders.first.distributor.name },
proc { |orders| orders.to_a.sum(&:item_total) },
proc { |orders| orders.sum(&:ship_total) },
proc { |orders| orders.sum { |order| order.outstanding_balance.to_f } },
proc { |orders| orders.sum(&:outstanding_balance) },
proc { |orders| orders.map(&:total).sum }]
when "payment_totals"
[proc { |orders| orders.first.payment_state },
@@ -124,7 +124,7 @@ module OpenFoodNetwork
}.sum(&:amount)
}
},
proc { |orders| orders.sum { |order| order.outstanding_balance.to_f } }]
proc { |orders| orders.sum(&:outstanding_balance) }]
else
[proc { |payments| payments.first.order.payment_state },
proc { |payments| payments.first.order.distributor.name },

View File

@@ -5,7 +5,7 @@ require 'spec_helper'
describe "Payments Reports" do
include AuthenticationHelper
let!(:order) do
let(:order) do
create(
:order_with_distributor,
state: 'complete',
@@ -13,37 +13,87 @@ describe "Payments Reports" do
order_cycle: order_cycle
)
end
let(:order_cycle) { create(:simple_order_cycle) }
let!(:line_item) do
create(:line_item_with_shipment, order: order, product: product)
let(:other_order) do
create(
:order_with_distributor,
state: 'complete',
completed_at: Time.zone.now,
order_cycle: order_cycle,
distributor: order.distributor
)
end
let(:order_cycle) { create(:simple_order_cycle) }
let(:product) { create(:product, supplier: supplier) }
let(:supplier) { create(:supplier_enterprise) }
before { login_as_admin }
before do
create(:line_item_with_shipment, order: order, product: product)
create(:line_item_with_shipment, order: other_order, product: product)
it 'shows orders with payment state, their balance and totals' do
visit spree.payments_admin_reports_path
login_as_admin
end
select I18n.t(:report_itemised_payment), from: "report_type"
find("[type='submit']").click
context "when choosing itemised payments report type" do
it "shows orders with payment state, their balance and totals" do
visit spree.payments_admin_reports_path
expect(page.find("#listing_orders thead tr").text).to eq([
I18n.t(:report_header_payment_state),
I18n.t(:report_header_distributor),
I18n.t(:report_header_product_total_price, currency: currency_symbol),
I18n.t(:report_header_shipping_total_price, currency: currency_symbol),
I18n.t(:report_header_outstanding_balance_price, currency: currency_symbol),
I18n.t(:report_header_total_price, currency: currency_symbol)
].join(" "))
select I18n.t(:report_itemised_payment), from: "report_type"
find("[type='submit']").click
expect(page.find("#listing_orders tbody tr").text).to eq([
order.payment_state,
order.distributor.name,
order.item_total.to_f,
order.ship_total.to_f,
order.outstanding_balance.to_f,
order.total.to_f
].join(" "))
expect(page.find("#listing_orders thead tr").text).to eq([
I18n.t(:report_header_payment_state),
I18n.t(:report_header_distributor),
I18n.t(:report_header_product_total_price, currency: currency_symbol),
I18n.t(:report_header_shipping_total_price, currency: currency_symbol),
I18n.t(:report_header_outstanding_balance_price, currency: currency_symbol),
I18n.t(:report_header_total_price, currency: currency_symbol)
].join(" "))
expect(page.find("#listing_orders tbody tr").text).to eq([
order.payment_state,
order.distributor.name,
order.item_total.to_f + other_order.item_total.to_f,
order.ship_total.to_f + other_order.ship_total.to_f,
order.outstanding_balance.to_f + other_order.outstanding_balance.to_f,
order.total.to_f + other_order.total.to_f
].join(" "))
end
end
context 'when choosing payment totals report type' do
let(:paypal) { create(:payment_method, name: "PayPal") }
let!(:paypal_payment) { create(:payment, order: order, payment_method: paypal, state: "completed", amount: 5) }
let(:eft) { create(:payment_method, name: "EFT") }
let!(:eft_payment) { create(:payment, order: other_order, payment_method: eft, state: "completed", amount: 6) }
it 'shows orders with payment state, their balance and and payment totals' do
visit spree.payments_admin_reports_path
select I18n.t(:report_payment_totals), from: "report_type"
find("[type='submit']").click
expect(page.find("#listing_orders thead tr").text).to eq([
I18n.t(:report_header_payment_state),
I18n.t(:report_header_distributor),
I18n.t(:report_header_product_total_price, currency: currency_symbol),
I18n.t(:report_header_shipping_total_price, currency: currency_symbol),
I18n.t(:report_header_total_price, currency: currency_symbol),
I18n.t(:report_header_eft_price, currency: currency_symbol),
I18n.t(:report_header_paypal_price, currency: currency_symbol),
I18n.t(:report_header_outstanding_balance_price, currency: currency_symbol),
].join(" "))
expect(page.find("#listing_orders tbody tr").text).to eq([
order.payment_state,
order.distributor.name,
order.item_total.to_f + other_order.item_total.to_f,
order.ship_total.to_f + other_order.ship_total.to_f,
order.total.to_f + other_order.total.to_f,
eft_payment.amount.to_f,
paypal_payment.amount.to_f,
order.outstanding_balance.to_f + other_order.outstanding_balance.to_f,
].join(" "))
end
end
end

View File

@@ -324,4 +324,38 @@ describe OrderBalance do
end
end
end
describe '#+' do
let(:other_order_balance) { described_class.new(order) }
context 'when the customer_balance feature is disabled' do
before do
allow(OpenFoodNetwork::FeatureToggle)
.to receive(:enabled?).with(:customer_balance, user) { false }
end
before do
allow(order).to receive(:old_outstanding_balance) { 10 }
end
it 'returns the sum of balances' do
expect(order_balance + other_order_balance).to eq(20.0)
end
end
context 'when the customer_balance feature is enabled' do
before do
allow(OpenFoodNetwork::FeatureToggle)
.to receive(:enabled?).with(:customer_balance, user) { true }
end
before do
allow(order).to receive(:new_outstanding_balance) { 10 }
end
it 'returns the balance as a string' do
expect(order_balance + other_order_balance).to eq(20.0)
end
end
end
end