From 8087e6b31b1e3a0a881cbaa0c2f67d3db45b8370 Mon Sep 17 00:00:00 2001 From: Mathew Button Date: Fri, 1 May 2020 21:15:57 +1000 Subject: [PATCH] Show outstanding balance on orders page Prior to this change, there was no way of knowing how much was owed (or in need of refunding) at a glance for each order on the orders index page, this information was only available by clicking through to an order payment page This change adds the outstanding balance for each order --- app/serializers/api/admin/order_serializer.rb | 8 ++++- app/views/spree/admin/orders/index.html.haml | 1 + spec/factories/order_factory.rb | 16 +++++++--- .../api/admin/order_serializer_spec.rb | 29 +++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 spec/serializers/api/admin/order_serializer_spec.rb diff --git a/app/serializers/api/admin/order_serializer.rb b/app/serializers/api/admin/order_serializer.rb index d6888994d3..a7b6eccd80 100644 --- a/app/serializers/api/admin/order_serializer.rb +++ b/app/serializers/api/admin/order_serializer.rb @@ -3,7 +3,7 @@ class Api::Admin::OrderSerializer < ActiveModel::Serializer :edit_path, :state, :payment_state, :shipment_state, :payments_path, :ready_to_ship, :ready_to_capture, :created_at, :distributor_name, :special_instructions, - :item_total, :adjustment_total, :payment_total, :total + :item_total, :adjustment_total, :payment_total, :total, :display_outstanding_balance has_one :distributor, serializer: Api::Admin::IdSerializer has_one :order_cycle, serializer: Api::Admin::IdSerializer @@ -16,6 +16,12 @@ class Api::Admin::OrderSerializer < ActiveModel::Serializer object.distributor.andand.name end + def display_outstanding_balance + return "" unless ["balance_due", "credit_owed"].include?(object.payment_state) + + "(#{object.display_outstanding_balance.to_html})" + end + def edit_path return '' unless object.id diff --git a/app/views/spree/admin/orders/index.html.haml b/app/views/spree/admin/orders/index.html.haml index 5359422dd0..ea9a0de153 100644 --- a/app/views/spree/admin/orders/index.html.haml +++ b/app/views/spree/admin/orders/index.html.haml @@ -68,6 +68,7 @@ %span.state{'ng-class' => 'order.payment_state', 'ng-if' => 'order.payment_state'} %a{'ng-href' => '{{order.payments_path}}' } {{'js.admin.orders.payment_states.' + order.payment_state | t}} + {{order.display_outstanding_balance}} %td.align-center %span.state{'ng-class' => 'order.shipment_state', 'ng-if' => 'order.shipment_state'} {{'js.admin.orders.shipment_states.' + order.shipment_state | t}} diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 9b1e60c0ff..38f2bc456e 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -46,8 +46,12 @@ FactoryBot.define do distributor { create(:distributor_enterprise) } order_cycle { create(:simple_order_cycle) } - after(:create) do |order| - create(:payment, amount: order.total + 10_000, order: order, state: "completed") + transient do + credit_amount { 10_000 } + end + + after(:create) do |order, evaluator| + create(:payment, amount: order.total + evaluator.credit_amount, order: order, state: "completed") order.reload end end @@ -56,8 +60,12 @@ FactoryBot.define do distributor { create(:distributor_enterprise) } order_cycle { create(:simple_order_cycle) } - after(:create) do |order| - create(:payment, amount: order.total - 1, order: order, state: "completed") + transient do + unpaid_amount { 1 } + end + + after(:create) do |order, evaluator| + create(:payment, amount: order.total - evaluator.unpaid_amount, order: order, state: "completed") order.reload end end diff --git a/spec/serializers/api/admin/order_serializer_spec.rb b/spec/serializers/api/admin/order_serializer_spec.rb new file mode 100644 index 0000000000..fd533f52ac --- /dev/null +++ b/spec/serializers/api/admin/order_serializer_spec.rb @@ -0,0 +1,29 @@ +require "spec_helper" + +describe Api::Admin::OrderSerializer do + let(:serializer) { described_class.new order } + + describe "#display_outstanding_balance" do + let(:order) { create(:order) } + + it "returns empty string" do + expect(serializer.display_outstanding_balance).to eql("") + end + + context "with outstanding payments" do + let(:order) { create(:order_without_full_payment, unpaid_amount: 10) } + + it "generates the outstanding balance" do + expect(serializer.display_outstanding_balance).to eql("($10.00)") + end + end + + context "with credit owed" do + let(:order) { create(:order_with_credit_payment, credit_amount: 20) } + + it "generates the outstanding balance" do + expect(serializer.display_outstanding_balance).to eql("($-20.00)") + end + end + end +end