Class: Ekylibre::Record::Bookkeep::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ekylibre/record/bookkeep.rb

Constant Summary collapse

@@id =
'0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, action, draft) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)

19
20
21
22
23
24
# File 'lib/ekylibre/record/bookkeep.rb', line 19

def initialize(resource, action, draft)
  raise ArgumentError, "Unvalid action #{action.inspect} (#{Ekylibre::Record::Bookkeep.actions.to_sentence} are accepted)" unless Ekylibre::Record::Bookkeep.actions.include? action
  @resource = resource
  @action = action
  @draft = draft
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action


9
10
11
# File 'lib/ekylibre/record/bookkeep.rb', line 9

def action
  @action
end

#draftObject (readonly)

Returns the value of attribute draft


9
10
11
# File 'lib/ekylibre/record/bookkeep.rb', line 9

def draft
  @draft
end

#resourceObject (readonly)

Returns the value of attribute resource


9
10
11
# File 'lib/ekylibre/record/bookkeep.rb', line 9

def resource
  @resource
end

Class Method Details

.next_idObject


14
15
16
17
# File 'lib/ekylibre/record/bookkeep.rb', line 14

def self.next_id
  @@id.succ!
  @@id
end

Instance Method Details

#journal_entry(journal, options = {}, &_block) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ekylibre/record/bookkeep.rb', line 26

def journal_entry(journal, options = {}, &_block)
  column = options.delete(:column) || :journal_entry_id
  condition = (options.key?(:if) ? options.delete(:if) : !options.delete(:unless))

  attributes = options
  attributes[:resource] ||= @resource
  # attributes[:state]      ||= @state
  attributes[:printed_on] ||= @resource.created_at.to_date if @resource.respond_to? :created_at
  unless attributes[:printed_on].is_a?(Date)
    raise ArgumentError, "Date of journal_entry (printed_on) must be given. Date expected, got #{attributes[:printed_on].class.name} (#{attributes[:printed_on].inspect})"
  end
  if condition
    unless journal.is_a? Journal
      raise ArgumentError, "Unknown journal: (#{journal.inspect})"
    end
    attributes[:journal_id] = journal.id
  end

  Ekylibre::Record::Base.transaction do
    if journal_entry = JournalEntry.find_by(id: @resource.send(column))
      # Cancel the existing journal_entry
      if journal_entry.draft? && condition && (attributes[:journal_id] == journal_entry.journal_id)
        journal_entry.items.destroy_all
        journal_entry.reload
        journal_entry.update_attributes!(attributes)
      else
        journal_entry.cancel
        journal_entry = nil
      end
    end

    # Add journal items
    if block_given? && condition && @action != :destroy
      journal_entry ||= JournalEntry.create!(attributes)
      yield(journal_entry)
      journal_entry.reload.confirm unless @draft
    end

    # Set accounted columns
    @resource.class.where(id: @resource.id).update_all(:accounted_at => Time.zone.now, column => (journal_entry ? journal_entry.id : nil))
  end
end