class Subledger::Domain::JournalEntry

Attributes

book[R]
effective_at[RW]
org[R]
post_delay[R]

Public Class Methods

active_klass() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 45
def self.active_klass
  ActiveJournalEntry
end
archived_klass() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 49
def self.archived_klass
  ArchivedJournalEntry
end
create_and_post(args) click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 61
def self.create_and_post args
  begin
    client = args[:client]

    active_journal_entry = client.active_journal_entry args

    JournalEntry.send :validate_creatability, active_journal_entry.attributes

    order = 1

    arg_lines = args[:lines]

    arg_lines.each do |line_args|
      if line_args[:description].nil?
        line_args.merge! :description => active_journal_entry.description
      end

      if line_args[:reference].nil?
        line_args.merge! :reference => active_journal_entry.reference
      end

      line_args.merge! :order => '%07.2f' % order

      order += 1
    end

    store = args[:store]

    active_lines = []

    arg_lines.each do |line_args|
      line_args.merge! :journal_entry => active_journal_entry

      line = client.active_line line_args.merge( :id => UUID.as_string )

      Line.send :raise_unless_create_and_postable, line.attributes
      Line.send :validate_creatability_modules, line.attributes

      active_lines << line
    end

    active_journal_entry.send :set_active_lines, active_lines

    store.create_and_post(
            :active_journal_entry  => active_journal_entry,
            :active_lines          => active_lines,
            :posting_journal_entry => client.posting_journal_entry( { } ) )

  rescue Exception => e
    raise JournalEntryError, "Cannot create and post: #{e}"
  end
end
new(args) click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 114
def initialize args
  describable args
  identifiable args
  storable args
  versionable args

  @org          = args[:org]
  @book         = args[:book]
  @effective_at = utc_or_nil args[:effective_at]
  @post_delay   = args[:post_delay] || 0

  @reason = args[:reason] if args.has_key? :reason
end
patch_keys() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 33
def self.patch_keys
  [ :id, :effective_at, :description, :reference, :version ]
end
post_keys() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 29
def self.post_keys
  [ :effective_at, :description, :reference ]
end
posted_klass() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 57
def self.posted_klass
  PostedJournalEntry
end
posting_klass() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 53
def self.posting_klass
  PostingJournalEntry
end
root_klass() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 37
def self.root_klass
  JournalEntry
end
sub_klasses() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 41
def self.sub_klasses
  [ active_klass, archived_klass, posting_klass, posted_klass ]
end

Private Class Methods

raise_unless_creatable(args) click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 180
def self.raise_unless_creatable args
  book = args[:book]

  if book.nil? or not book.kind_of? Book
    raise JournalEntryError, ':book is required and must be a Book'
  elsif UUID.invalid? book.id
    raise JournalEntryError, ':book must have a valid :id'
  end

  effective_at = args[:effective_at]

  if effective_at.nil? or not effective_at.kind_of? Time
    raise JournalEntryError, ':effective_at is required and must be a Time'
  end
end

Public Instance Methods

balance() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 143
def balance
  if @active_lines
    active_lines_balance = client.balance
    @active_lines.each { |line| active_lines_balance += line }
    active_lines_balance
  else
    store.journal_entry_balance :store         => store,
                                :client        => client,
                                :journal_entry => self,
                                :state         => line_state
  end
end
balanced?() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 156
def balanced?
  balance.balanced?
end
line(args) click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 128
def line args
  client.lines args.merge( :journal_entry => self )
end
lines(args={ }) click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 132
def lines args={ }, &block
  return @active_lines if @active_lines

  args.merge! :action        => args[:action] || :starting,
              :state         => args[:state]  || line_state,
              :order         => args[:order]  || '0',
              :journal_entry => self

  client.lines.collect args, &block
end

Private Instance Methods

line_state() click to toggle source
# File lib/subledger/domain/journal_entry.rb, line 196
def line_state
  :active
end