class Gnucash::Account
Represent a GnuCash account object.
Attributes
@return [String] The account description.
@return [String] The GUID of the account.
@return [String] The name of the account (unqualified).
@since 1.4.0
@return [String, nil] The GUID of the parent account, if any.
@return [Boolean] Whether the account is a placeholder or not.
@return [Array<AccountTransaction>]
List of transactions associated with this account.
@return [String] The account type (such as “EXPENSE”).
Public Class Methods
Create an Account
object.
@param book [Book] The {Gnucash::Book} containing the account. @param node [Nokogiri::XML::Node] Nokogiri XML node.
# 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
Public Instance Methods
Internal method used to associate a transaction with the account.
@return [void]
# File lib/gnucash/account.rb, line 70 def add_transaction(act_txn) @transactions << act_txn end
Attributes available for inspection
@return [Array<Symbol>] Attributes used to build the inspection string @see Gnucash::Support::LightInspect
# File lib/gnucash/account.rb, line 143 def attributes %i[id name description type placeholder parent_id] end
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.
@param date [String, Date]
Date on which to query the balance.
@param options [Hash]
Optional parameters.
@option options [Boolean] :recursive
Whether to include children account balances.
@return [Value]
Balance of the account as of the date given.
# 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 { |account| account.parent != self }.each do |child_account| return_value += child_account.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
Return the final balance of the account as a {Value}.
@return [Value] The final balance of the account.
# File lib/gnucash/account.rb, line 93 def final_balance return Value.new(0) unless @balances.size > 0 @balances.last[:value] end
Internal method used to complete initialization of the Account
after all transactions have been associated with it.
@return [void]
# 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
Return the fully qualified account name.
@return [String] Fully qualified account name.
# File lib/gnucash/account.rb, line 54 def full_name @full_name ||= calculate_full_name end
Lookup for the parent account in the book.
@since 1.4.0
@return [Account, nil] Account
object, or nil if not found.
# File lib/gnucash/account.rb, line 63 def parent @parent ||= @parent_id ? @book.find_account_by_id(@parent_id) : nil end
Private Instance Methods
@return [String] Fully qualified account name.
# File lib/gnucash/account.rb, line 150 def calculate_full_name prefix = "" if @parent_id parent = @book.find_account_by_id(@parent_id) if parent and parent.type != 'ROOT' prefix = parent.full_name + ":" end end prefix + name end