Class: Subledger::Domain::JournalEntry

Defined Under Namespace

Classes: Entity

Instance Attribute Summary collapse

Attributes included from Roles::Versionable

#version

Attributes included from Roles::Storable

#client, #store

Attributes included from Roles::Identifiable

#id

Attributes included from Roles::Describable

#description, #reference

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Roles::Restable

#patch_hash, #post_hash, #serializable_hash, #to_json

Methods included from Roles::Collectable

included

Methods included from Roles::Progressable

#progress

Methods included from Roles::Updatable

included, #update

Methods included from Roles::Readable

included, #read

Methods included from Roles::Creatable

#create, included

Methods included from Roles::Versionable

included

Methods included from Roles::Attributable

#attributes

Methods included from Subledger::Domain

#==, #collection_name, #entity_name, included, #to_s

Constructor Details

#initialize(args) ⇒ JournalEntry

Returns a new instance of JournalEntry.



114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



25
26
27
# File 'lib/subledger/domain/journal_entry.rb', line 25

def book
  @book
end

#effective_atObject

Returns the value of attribute effective_at.



27
28
29
# File 'lib/subledger/domain/journal_entry.rb', line 27

def effective_at
  @effective_at
end

#orgObject (readonly)

Returns the value of attribute org.



25
26
27
# File 'lib/subledger/domain/journal_entry.rb', line 25

def org
  @org
end

#post_delayObject (readonly)

Returns the value of attribute post_delay.



25
26
27
# File 'lib/subledger/domain/journal_entry.rb', line 25

def post_delay
  @post_delay
end

Class Method Details

.active_klassObject



45
46
47
# File 'lib/subledger/domain/journal_entry.rb', line 45

def self.active_klass
  ActiveJournalEntry
end

.archived_klassObject



49
50
51
# File 'lib/subledger/domain/journal_entry.rb', line 49

def self.archived_klass
  ArchivedJournalEntry
end

.create_and_post(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

.patch_keysObject



33
34
35
# File 'lib/subledger/domain/journal_entry.rb', line 33

def self.patch_keys
  [ :id, :effective_at, :description, :reference, :version ]
end

.post_keysObject



29
30
31
# File 'lib/subledger/domain/journal_entry.rb', line 29

def self.post_keys
  [ :effective_at, :description, :reference ]
end

.posted_klassObject



57
58
59
# File 'lib/subledger/domain/journal_entry.rb', line 57

def self.posted_klass
  PostedJournalEntry
end

.posting_klassObject



53
54
55
# File 'lib/subledger/domain/journal_entry.rb', line 53

def self.posting_klass
  PostingJournalEntry
end

.root_klassObject



37
38
39
# File 'lib/subledger/domain/journal_entry.rb', line 37

def self.root_klass
  JournalEntry
end

.sub_klassesObject



41
42
43
# File 'lib/subledger/domain/journal_entry.rb', line 41

def self.sub_klasses
  [ active_klass, archived_klass, posting_klass, posted_klass ]
end

Instance Method Details

#balanceObject



143
144
145
146
147
148
149
150
151
152
153
154
# 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?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/subledger/domain/journal_entry.rb', line 156

def balanced?
  balance.balanced?
end

#line(args) ⇒ Object



128
129
130
# File 'lib/subledger/domain/journal_entry.rb', line 128

def line args
  client.lines args.merge( :journal_entry => self )
end

#lines(args = { }, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# 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