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
-
#balance(ignore_cod = false, excluding_payment: nil) ⇒ BigDecimal
Outstanding amount the customer still owes on this payable —
total − total_payments_authorized, clamped at zero. - #funded_by_cod? ⇒ Boolean
-
#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.
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.
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 - (nil, excluding_payment: excluding_payment)) balance < 0 ? BigDecimal('0.0') : balance end |
#funded_by_cod? ⇒ 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 |
#payments ⇒ ActiveRecord::Relation<Payment>
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.
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 (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 |