Files
openfoodnetwork/app/models/concerns/balance.rb
Pau Perez d26c52c2fa Memoize OrderBalance instance
There's no need to create an instance of such class for every call to
one of its methods. The balance is computed each time anyway, so it'll
always be up-to-date.
2021-04-19 11:52:41 +02:00

38 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require 'active_support/concern'
# Contains the methods to compute an order balance form the point of view of the enterprise and not
# the individual shopper.
module Balance
FINALIZED_NON_SUCCESSFUL_STATES = %w(canceled returned).freeze
# Branches by the OrderBalance abstraction
def outstanding_balance
@order_balance ||= OrderBalance.new(self)
end
# Returns the order balance by considering the total as money owed to the order distributor aka.
# the shop, and as a positive balance of said enterprise. If the customer pays it all, the
# distributor and customer are even.
#
# Note however, this is meant to be used only in the context of a single order object. When
# working with a collection of orders, such as an index controller action, please consider using
# `app/queries/outstanding_balance.rb` instead so we avoid potential N+1s.
def new_outstanding_balance
if state.in?(FINALIZED_NON_SUCCESSFUL_STATES)
-payment_total
else
total - payment_total
end
end
def outstanding_balance?
!outstanding_balance.zero?
end
def display_outstanding_balance
outstanding_balance.display_amount
end
end