Sha256: fb104e5faeb45c3e024de7ddbedfd8bea1a01fe1fd9e4ac4a25dac2089a0d3ab

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

require "date"

module Gnucash
  # Represent a GnuCash transaction.
  #
  # Transactions have multiple splits with individual values.
  # Splits are created as AccountTransaction objects which are associated
  # with an individual account.
  class Transaction
    # @return [Date] The date of the transaction.
    attr_reader :date

    # @return [String] The GUID of the transaction.
    attr_reader :id

    # @return [String] The description of the transaction.
    attr_reader :description

    # @return [Array<Hash>] Hashes with keys +:account+ and +:value+.
    attr_reader :splits

    # Create a new Transaction object.
    #
    # @param book [Book] The {Gnucash::Book} containing the transaction.
    # @param node [Nokogiri::XML::Node] Nokogiri XML node.
    def initialize(book, node)
      @book = book
      @node = node
      @id = node.xpath('trn:id').text
      @date = Date.parse(node.xpath('trn:date-posted/ts:date').text.split(' ').first)
      @description = node.xpath('trn:description').text
      @splits = node.xpath('trn:splits/trn:split').map do |split_node|
        # Note: split:value represents the split value in the transaction's
        # currency while split:quantity represents it in the currency
        # associated with the account associated with this split.
        value = Value.new(split_node.xpath('split:quantity').text)
        account_id = split_node.xpath('split:account').text
        account = @book.find_account_by_id(account_id)
        unless account
          raise "Could not find account with ID #{account_id} for transaction #{@id}"
        end
        account.add_transaction(AccountTransaction.new(self, value))
        {
          account: account,
          value: value,
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gnucash-1.3.1 lib/gnucash/transaction.rb
gnucash-1.3.0 lib/gnucash/transaction.rb
gnucash-1.2.2 lib/gnucash/transaction.rb
gnucash-1.2.1 lib/gnucash/transaction.rb