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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gnucash/account.rb', line 49

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
  code_raw = node.at_xpath('act:code')&.text
  @code = (code_raw.nil? || code_raw.empty?) ? nil : code_raw
  @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

  cmd = node.at_xpath("act:commodity")
  if cmd
    @commodity_space = cmd.at_xpath("cmdty:space")&.text&.strip
    @commodity_id = cmd.at_xpath("cmdty:id")&.text&.strip
    @commodity_space = nil if @commodity_space.nil? || @commodity_space.empty?
    @commodity_id = nil if @commodity_id.nil? || @commodity_id.empty?
  else
    @commodity_space = nil
    @commodity_id = nil
  end
end

Instance Attribute Details

#codeString? (readonly)

Returns Account code (act:code), if set in GnuCash.

Returns:

  • (String, nil)

    Account code (act:code), if set in GnuCash.

Since:

  • 1.6.0



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

def code
  @code
end

#commodity_idString? (readonly)

Returns Commodity id (cmdty:id) from act:commodity, if present.

Returns:

  • (String, nil)

    Commodity id (cmdty:id) from act:commodity, if present.

Since:

  • 1.6.0



38
39
40
# File 'lib/gnucash/account.rb', line 38

def commodity_id
  @commodity_id
end

#commodity_spaceString? (readonly)

Returns Commodity namespace (cmdty:space) from act:commodity, if present.

Returns:

  • (String, nil)

    Commodity namespace (cmdty:space) from act:commodity, if present.

Since:

  • 1.6.0



33
34
35
# File 'lib/gnucash/account.rb', line 33

def commodity_space
  @commodity_space
end

#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



43
44
45
# File 'lib/gnucash/account.rb', line 43

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.



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

def placeholder
  @placeholder
end

#transactionsArray<AccountTransaction> (readonly)

Returns List of transactions associated with this account.

Returns:



25
26
27
# File 'lib/gnucash/account.rb', line 25

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.



110
111
112
# File 'lib/gnucash/account.rb', line 110

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:



183
184
185
# File 'lib/gnucash/account.rb', line 183

def attributes
  %i[id name description type code placeholder parent_id commodity_space commodity_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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gnucash/account.rb', line 151

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.



133
134
135
136
# File 'lib/gnucash/account.rb', line 133

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.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gnucash/account.rb', line 118

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.



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

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



103
104
105
# File 'lib/gnucash/account.rb', line 103

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

#securitySecurity?

Priced Security for this account’s commodity, if the commodity appears in the book’s price database (gnc:pricedb). Otherwise nil.

Returns:

Since:

  • 1.6.0



85
86
87
88
89
# File 'lib/gnucash/account.rb', line 85

def security
  return nil unless @commodity_space && @commodity_id

  @book.find_security(@commodity_space, @commodity_id)
end