Class: BankTransaction

Inherits:
ApplicationRecord show all
Includes:
LocalizeInput
Defined in:
app/models/bank_transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LocalizeInput

parse

Instance Attribute Details

#amountNumber

Returns Amount credited.

Returns:

  • (Number)

    Amount credited.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#dateDate

Returns Date of the transaction.

Returns:

  • (Date)

    Date of the transaction.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#external_idString

Returns Unique Identifier of the transaction within the bank account.

Returns:

  • (String)

    Unique Identifier of the transaction within the bank account.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#ibanString

Returns Internation Bank Account Number of the sending/receiving account.

Returns:

  • (String)

    Internation Bank Account Number of the sending/receiving account.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#imageBinary

Returns Optional PNG image for e.g. scan of paper receipt.

Returns:

  • (Binary)

    Optional PNG image for e.g. scan of paper receipt.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#receiptString

Returns Optional additional more detailed description of the transaction.

Returns:

  • (String)

    Optional additional more detailed description of the transaction.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#referenceString

Returns 140 character long reference field as defined by SEPA.

Returns:

  • (String)

    140 character long reference field as defined by SEPA.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

#textString

Returns Short description of the transaction.

Returns:

  • (String)

    Short description of the transaction.



20
# File 'app/models/bank_transaction.rb', line 20

belongs_to :bank_account

Instance Method Details

#assign_to_invoiceObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/bank_transaction.rb', line 37

def assign_to_invoice
  return false unless supplier

  content = text || ''
  content += "\n" + reference if reference.present?
  invoices = supplier.invoices.unpaid.select { |i| content.include? i.number }
  invoices_sum = invoices.map(&:amount).sum
  return false if amount != -invoices_sum

  transaction do
    link = FinancialLink.new
    invoices.each { |i| i.update!(financial_link: link, paid_on: date) }
    update_attribute :financial_link, link
  end

  true
end

#assign_to_ordergroupObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/bank_transaction.rb', line 55

def assign_to_ordergroup
  m = BankTransactionReference.parse(reference)
  return unless m

  return false if m[:parts].values.sum != amount

  group = Ordergroup.find_by_id(m[:group])
  return false unless group

  usr = m[:user] ? User.find_by_id(m[:user]) : group.users.first
  return false unless usr

  transaction do
    note = "ID=#{id} (#{amount})"
    link = FinancialLink.new

    m[:parts].each do |short, value|
      ftt = FinancialTransactionType.find_by_name_short(short)
      return false unless ftt

      group.add_financial_transaction! value, note, usr, ftt, link if value > 0
    end

    update_attribute :financial_link, link
  end

  true
end

#image_urlObject



33
34
35
# File 'app/models/bank_transaction.rb', line 33

def image_url
  'data:image/png;base64,' + Base64.encode64(image)
end