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
This commit is contained in:
Mathew Button
2020-05-01 21:15:57 +10:00
parent 407d890d23
commit 8087e6b31b
4 changed files with 49 additions and 5 deletions

View File

@@ -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

View File

@@ -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}}

View File

@@ -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

View File

@@ -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