Class: Gnucash::Account

Inherits:
Object
  • Object
show all
Includes:
Support::LightInspect
Defined in:
lib/gnucash/account.rb

Overview

Represent a GnuCash account object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::LightInspect

#inspect

Constructor Details

#initialize(book, node) ⇒ Account

Create an Account object.

Parameters:

  • book (Book)

    The Book containing the account.

  • node (Nokogiri::XML::Node)

    Nokogiri XML node.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gnucash/account.rb', line 34

def initialize(book, node)
  @book = book
  @node = node
  @name = node.xpath('act:name').text
  @type = node.xpath('act:type').text
  @description = node.xpath('act:description').text
  @id = node.xpath('act:id').text
  @parent_id = node.xpath('act:parent').text
  @parent_id = nil if @parent_id == ""
  @transactions = []
  @balances = []
  @placeholder = node.xpath("act:slots/slot").find do |slot|
    (slot.xpath("slot:key").first.text == "placeholder" and
     slot.xpath("slot:value").first.text == "true")
  end ? true : false
end

Instance Attribute Details

#descriptionString (readonly)

Returns The account description.

Returns:

  • (String)

    The account description.



10
11
12
# File 'lib/gnucash/account.rb', line 10

def description
  @description
end

#idString (readonly)

Returns The GUID of the account.

Returns:

  • (String)

    The GUID of the account.



16
17
18
# File 'lib/gnucash/account.rb', line 16

def id
  @id
end

#nameString (readonly)

Returns The name of the account (unqualified).

Returns:

  • (String)

    The name of the account (unqualified).



7
8
9
# File 'lib/gnucash/account.rb', line 7

def name
  @name
end

#parent_idString? (readonly)

Returns The GUID of the parent account, if any.

Returns:

  • (String, nil)

    The GUID of the parent account, if any.

Since:

  • 1.4.0



28
29
30
# File 'lib/gnucash/account.rb', line 28

def parent_id
  @parent_id
end

#placeholderBoolean (readonly)

Returns Whether the account is a placeholder or not.

Returns:

  • (Boolean)

    Whether the account is a placeholder or not.



23
24
25
# File 'lib/gnucash/account.rb', line 23

def placeholder
  @placeholder
end

#transactionsArray<AccountTransaction> (readonly)

Returns List of transactions associated with this account.

Returns:



20
21
22
# File 'lib/gnucash/account.rb', line 20

def transactions
  @transactions
end

#typeString (readonly)

Returns The account type (such as “EXPENSE”).

Returns:

  • (String)

    The account type (such as “EXPENSE”).



13
14
15
# File 'lib/gnucash/account.rb', line 13

def type
  @type
end

Instance Method Details

#add_transaction(act_txn) ⇒ void

This method returns an undefined value.

Internal method used to associate a transaction with the account.



70
71
72
# File 'lib/gnucash/account.rb', line 70

def add_transaction(act_txn)
  @transactions << act_txn
end

#attributesArray<Symbol>

Attributes available for inspection

Returns:

  • (Array<Symbol>)

    Attributes used to build the inspection string

See Also:



143
144
145
# File 'lib/gnucash/account.rb', line 143

def attributes
  %i[id name description type placeholder parent_id]
end

#balance_on(date, options = {}) ⇒ Value

Return the balance of the account as of the date given as a Value. Transactions that occur on the given date are included in the returned balance.

Parameters:

  • date (String, Date)

    Date on which to query the balance.

  • options (Hash) (defaults to: {})

    Optional parameters.

Options Hash (options):

  • :recursive (Boolean)

    Whether to include children account balances.

Returns:

  • (Value)

    Balance of the account as of the date given.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gnucash/account.rb', line 111

def balance_on(date, options = {})
  date = Date.parse(date) if date.is_a?(String)
  return_value = Value.new(0)

  if options[:recursive]
    # Get all child accounts from this account and accumulate the balances of them.
    @book.accounts.reject { || .parent != self }.each do ||
      return_value += .balance_on(date, recursive: true)
    end
  end

  return return_value unless @balances.size > 0
  return return_value if @balances.first[:date] > date
  return @balances.last[:value] if date >= @balances.last[:date]
  imin = 0
  imax = @balances.size - 2
  idx = imax / 2
  until @balances[idx][:date] <= date and @balances[idx + 1][:date] > date
    if @balances[idx][:date] <= date
      imin = idx + 1
    else
      imax = idx
    end
    idx = (imin + imax) / 2
  end
  @balances[idx][:value]
end

#final_balanceValue

Return the final balance of the account as a Value.

Returns:

  • (Value)

    The final balance of the account.



93
94
95
96
# File 'lib/gnucash/account.rb', line 93

def final_balance
  return Value.new(0) unless @balances.size > 0
  @balances.last[:value]
end

#finalizevoid

This method returns an undefined value.

Internal method used to complete initialization of the Account after all transactions have been associated with it.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gnucash/account.rb', line 78

def finalize
  @transactions.sort! { |a, b| a.date <=> b.date }
  balance = Value.new(0)
  @balances = @transactions.map do |act_txn|
    balance += act_txn.value
    {
      date: act_txn.date,
      value: balance,
    }
  end
end

#full_nameString

Return the fully qualified account name.

Returns:

  • (String)

    Fully qualified account name.



54
55
56
# File 'lib/gnucash/account.rb', line 54

def full_name
  @full_name ||= calculate_full_name
end

#parentAccount?

Lookup for the parent account in the book.

Returns:

  • (Account, nil)

    Account object, or nil if not found.

Since:

  • 1.4.0



63
64
65
# File 'lib/gnucash/account.rb', line 63

def parent
  @parent ||= @parent_id ? @book.(@parent_id) : nil
end