Add label to customer balance so it's clear to the manager what's the balance status: credit owed or balance due

This commit is contained in:
Luis Ramos
2020-10-14 12:03:14 +01:00
parent da69dca471
commit e860e2ca57
5 changed files with 41 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
class Api::Admin::CustomerSerializer < ActiveModel::Serializer
attributes :id, :email, :enterprise_id, :user_id, :code, :tags, :tag_list, :name,
:allow_charges, :default_card_present?, :balance
:allow_charges, :default_card_present?, :balance, :balance_status
has_one :ship_address, serializer: Api::AddressSerializer
has_one :bill_address, serializer: Api::AddressSerializer
@@ -14,8 +14,17 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer
end
def balance
balance = OpenFoodNetwork::UserBalanceCalculator.new(object.email, object.enterprise).balance
Spree::Money.new(balance, { currency: Spree::Config[:currency] }).to_s
Spree::Money.new(balance_value, { currency: Spree::Config[:currency] }).to_s
end
def balance_status
if balance_value > 0
"credit_owed"
elsif balance_value < 0
"balance_due"
else
""
end
end
def tags
@@ -38,4 +47,8 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer
options[:customer_tags].andand[object.id] || []
end
def balance_value
@balance_value ||= OpenFoodNetwork::UserBalanceCalculator.new(object.email, object.enterprise).balance
end
end