class TaskJuggler::AccountScenario

This class handles the scenario specific features of a Account object.

Public Class Methods

new(account, scenarioIdx, attributes) click to toggle source
Calls superclass method
# File lib/taskjuggler/AccountScenario.rb, line 21
def initialize(account, scenarioIdx, attributes)
  super
  %w( credits ).each do |attr|
    @property[attr, @scenarioIdx]
  end
end

Public Instance Methods

query_balance(query) click to toggle source
# File lib/taskjuggler/AccountScenario.rb, line 28
def query_balance(query)
  # The account balance is the turnover from project start (index 0) to
  # the start of the query period. It's the start because that's what the
  # label in the column header says.
  startIdx = 0
  endIdx = @project.dateToIdx(query.start)

  query.sortable = query.numerical = amount = turnover(startIdx, endIdx)
  query.string = query.currencyFormat.format(amount)
end
query_turnover(query) click to toggle source
# File lib/taskjuggler/AccountScenario.rb, line 39
def query_turnover(query)
  startIdx = @project.dateToIdx(query.start)
  endIdx = @project.dateToIdx(query.end)

  query.sortable = query.numerical = amount = turnover(startIdx, endIdx)
  query.string = query.currencyFormat.format(amount)
end

Private Instance Methods

turnover(startIdx, endIdx) click to toggle source

Compute the turnover for the period between startIdx end endIdx. TODO: This method is horribly inefficient!

# File lib/taskjuggler/AccountScenario.rb, line 51
def turnover(startIdx, endIdx)
  amount = 0.0

  # Accumulate the amounts that were directly credited to the account
  # during the given interval.
  unless @credits.empty?
    # For this, we need the real dates again. Conver the indices back to
    # dates.
    startDate = @project.idxToDate(startIdx)
    endDate = @project.idxToDate(endIdx)

    @credits.each do |credit|
      if startDate <= credit.date && credit.date < endDate
        amount += credit.amount
      end
    end
  end

  if @property.container?
    if @property.adoptees.empty?
      # Normal case. Accumulate turnover of child accounts.
      @property.children.each do |child|
        amount += child.turnover(@scenarioIdx, startIdx, endIdx)
      end
    else
      # Special case for meta account that is used to calculate a balance.
      # The first adoptee is the top-level cost account, the second the
      # top-level revenue account.
      amount +=
        -@property.adoptees[0].turnover(@scenarioIdx, startIdx, endIdx) +
        @property.adoptees[1].turnover(@scenarioIdx, startIdx, endIdx)
    end
  else
    case @property.get('aggregate')
    when :tasks
      @project.tasks.each do |task|
        amount += task.turnover(@scenarioIdx, startIdx, endIdx, @property,
                                nil, false)
      end
    when :resources
      @project.resources.each do |resource|
        next unless resource.leaf?

        amount += resource.turnover(@scenarioIdx, startIdx, endIdx,
                                    @property, nil, false)
      end
    else
      raise "Unknown aggregation type #{@property.get('aggregate')}"
    end
  end
  amount
end