class Skr::Payment

A payment can be for either incoming or outgoing, if incoming must be associated with an Invoice, otherwise a Vendor or PaymentCategory

Constants

SEQUENTIAL_ID_PREFIX

Attributes

credit_card[RW]

Public Instance Methods

incoming?() click to toggle source
# File lib/skr/models/payment.rb, line 36
def incoming?
    vendor.present?
end
latex_template_variables() click to toggle source
# File lib/skr/models/payment.rb, line 32
def latex_template_variables
    { 'position' => :absolute }
end
outgoing?() click to toggle source
# File lib/skr/models/payment.rb, line 40
def outgoing?
    vendor.present? || category.present?
end

Private Instance Methods

apply_transaction() click to toggle source
# File lib/skr/models/payment.rb, line 70
def apply_transaction
    (credit, debit) = gl_accounts
    GlTransaction.push_or_save(
        owner:  self,  amount: amount,
        debit:  debit, credit: credit
    )
end
attempt_charging_provided_card() click to toggle source
# File lib/skr/models/payment.rb, line 46
def attempt_charging_provided_card
    return unless credit_card.present?

    card = ActiveMerchant::Billing::CreditCard.new(credit_card)
    gw = Skr::MerchantGateway.get
    resp = gw.purchase(amount, card)
    if resp.success?
        metadata['authorization'] = resp.authorization
    else
        errors.add(:credit_card, "purchase failed: #{resp.message}")
    end
end
gl_accounts() click to toggle source
# File lib/skr/models/payment.rb, line 59
def gl_accounts
    if vendor
        [vendor.gl_payables_account, bank_account.gl_account]
    elsif invoice
        [bank_account.gl_account,
         invoice.customer.gl_receivables_account]
    else
        [category.gl_account, bank_account.gl_account]
    end
end
set_defaults() click to toggle source
# File lib/skr/models/payment.rb, line 78
def set_defaults
    self.location     ||= Location.default
    self.date         ||= Date.today
    if name.blank?
        if self.vendor
            self.name = vendor.name
        elsif self.invoice
            self.name = invoice.billing_address.name
        end
    end
    if bank_account.present? && outgoing?
        self.check_number ||= SequentialId.next_for(SEQUENTIAL_ID_PREFIX + bank_account.id.to_s)
    end
    self.bank_account ||= BankAccount.default
end