mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
This avoids consumers of `OrderBalance` having to couple with the inner details of this abstraction, which makes the code more changeable.
38 lines
634 B
Ruby
38 lines
634 B
Ruby
# frozen_string_literal: true
|
|
|
|
class OrderBalance
|
|
delegate :zero?, :abs, :to_s, to: :to_f
|
|
|
|
def initialize(order)
|
|
@order = order
|
|
end
|
|
|
|
def label
|
|
to_f.negative? ? I18n.t(:credit_owed) : I18n.t(:balance_due)
|
|
end
|
|
|
|
def amount
|
|
Spree::Money.new(to_f, currency: order.currency)
|
|
end
|
|
|
|
def to_f
|
|
if customer_balance_enabled?
|
|
order.new_outstanding_balance
|
|
else
|
|
order.old_outstanding_balance
|
|
end
|
|
end
|
|
|
|
def +(other)
|
|
to_f + other.to_f
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :order
|
|
|
|
def customer_balance_enabled?
|
|
OpenFoodNetwork::FeatureToggle.enabled?(:customer_balance, order.user)
|
|
end
|
|
end
|