class Rledger::Statement

Public Class Methods

new(transactions) click to toggle source
# File lib/rledger/report/statement.rb, line 3
def initialize(transactions)
  @transactions = transactions
end

Public Instance Methods

compute(voice) click to toggle source
# File lib/rledger/report/statement.rb, line 7
def compute voice
  @statement = []
  cumulative = Amount.new

  @transactions.each do |transaction|
    if transaction.contains? voice

      # the cumulative is computed by adding all posts with voice
      #
      # this is to ensure cumulative is in the same currency of
      # voice (if we used the currency in the posts not containing
      # voice to compute the cumulative, we would risk using
      # different currencies, if the voice appears in multi
      # currency transactions
      transaction.posts_with(voice).each do |post|
        cumulative.add!(post.amount)
      end
      
      # This is to generate a new amount per line
      # (if we used cumulative instead, the array will
      # insert a reference to the object and since add! has a side effect,
      # we would build N-references to the same object, which has the last
      # value assigned
      line_total = Amount.new
      line_total.add!(cumulative)

      complement = transaction.posts_without(voice)
      statement_line = { :date => transaction.date, 
        :id => transaction.id, 
        :payee => transaction.payee, 
        :voice => complement.size > 1 ? "-- split --" : complement[0].voice,
        :amount => complement[0].amount,
        :cumulative => line_total }
      @statement << statement_line

      # transaction.posts_without(voice).each do |post|
      #   # This is to generate a new amount per line
      #   # (if we used cumulative instead, the array will
      #   # insert a reference to the object and since add! has a side effect,
      #   # we would build N-references to the same object, which has the last
      #   # value assigned
      #   line_total = Amount.new
      #   line_total.add!(cumulative)
      #
      #   statement_line = { :date => transaction.date,
      #     :id => transaction.id,
      #     :payee => transaction.payee,
      #     :voice => post.voice,
      #     :amount => post.amount,
      #     :cumulative => line_total }
      #   @statement << statement_line
      # end

    end
  end
end
to_s() click to toggle source
# File lib/rledger/report/statement.rb, line 65
def to_s
  @statement.each do |line|
    printf "%10s %5.5s %-30.30s %-30.30s %10s %10s\n", 
    line[:date], line[:id], line[:payee], 
    line[:voice], line[:amount], line[:cumulative]
  end
  ""
end