Module: Models::Payable

Extended by:
ActiveSupport::Concern
Included in:
Delivery
Defined in:
app/concerns/models/payable.rb

Overview

ActiveSupport::Concern mixin: payable.

Has many collapse

Instance Method Summary collapse

Instance Method Details

#balance(ignore_cod = false, excluding_payment: nil) ⇒ BigDecimal

Outstanding amount the customer still owes on this payable —
total − total_payments_authorized, clamped at zero.

Parameters:

  • ignore_cod (Boolean) (defaults to: false)

    count COD deliveries (otherwise they
    contribute zero since payment is collected on delivery)

  • excluding_payment (Payment, nil) (defaults to: nil)

    forwarded to
    #total_payments_authorized; see that method for semantics

Returns:

  • (BigDecimal)


49
50
51
52
53
54
# File 'app/concerns/models/payable.rb', line 49

def balance(ignore_cod = false, excluding_payment: nil)
  return BigDecimal('0.0') if order&.is_store_transfer? || (!ignore_cod && funded_by_cod?)

  balance = (total - total_payments_authorized(nil, excluding_payment: excluding_payment))
  balance < 0 ? BigDecimal('0.0') : balance
end

#funded_by_cod?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/concerns/models/payable.rb', line 56

def funded_by_cod?
  !cod_collection_type.nil? and chosen_shipping_method&.cod?
end

#paymentsActiveRecord::Relation<Payment>

Returns:

  • (ActiveRecord::Relation<Payment>)

See Also:



10
# File 'app/concerns/models/payable.rb', line 10

has_many :payments, dependent: :nullify

#total_payments_authorized(in_currency = nil, excluding_payment: nil) ⇒ BigDecimal

Sum of payment obligations on this payable — authorized + captured
amounts on active payments, plus capture amounts on voided payments,
plus any PayPal over-capture headroom.

Parameters:

  • in_currency (String, nil) (defaults to: nil)

    restrict to payments in this currency

  • excluding_payment (Payment, nil) (defaults to: nil)

    drop this payment's authorized
    amount from the active sum but keep its captured portion in the total.
    Used by the reauth flow so an existing over-sized auth doesn't make
    a payable look already-paid when we're trying to replace it.

Returns:

  • (BigDecimal)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/concerns/models/payable.rb', line 23

def total_payments_authorized(in_currency = nil, excluding_payment: nil)
  active = payments.where(state: %w[authorized captured])
  active = active.where(currency: in_currency) if in_currency
  active = active.where.not(id: excluding_payment.id) if excluding_payment
  active_total = active.sum(:amount) || BigDecimal('0.0')

  # When replacing a payment, its prior captures still represent money the
  # customer paid against this payable — keep them in the obligation total
  # so we don't try to re-authorize what's already been captured.
  active_total += excluding_payment.total_captured if excluding_payment&.persisted? && excluding_payment.state.in?(%w[authorized captured])

  voided_scope = payments.where(state: 'voided').includes(:transactions)
  voided_scope = voided_scope.where(currency: in_currency) if in_currency
  voided_captured_total = voided_scope.sum(&:total_captured)

  active_total + voided_captured_total + paypal_over_capture_headroom_for_balance(in_currency)
end