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)

35
36
37
38
39
40
# File 'lib/ekylibre/record/bookkeep.rb', line 35

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


25
26
27
# File 'lib/ekylibre/record/bookkeep.rb', line 25

def action
  @action
end

#draftObject (readonly)

Returns the value of attribute draft


25
26
27
# File 'lib/ekylibre/record/bookkeep.rb', line 25

def draft
  @draft
end

#resourceObject (readonly)

Returns the value of attribute resource


25
26
27
# File 'lib/ekylibre/record/bookkeep.rb', line 25

def resource
  @resource
end

Class Method Details

.next_idObject


30
31
32
33
# File 'lib/ekylibre/record/bookkeep.rb', line 30

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

Instance Method Details

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

Raises:

  • (ArgumentError)

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
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
113
114
115
116
117
# File 'lib/ekylibre/record/bookkeep.rb', line 42

def journal_entry(journal, options = {}, &block)
  if (options.keys & %i[if unless]).size > 1
    raise ArgumentError, 'Options :if and :unless are incompatible.'
  end
  if options.key? :list
    raise ArgumentError, 'Option :list is not supported anymore.'
  end
  raise ArgumentError, 'Block is missing' unless block_given?
  condition = (options.key?(:if) ? options.delete(:if) : !options.delete(:unless))
  prism = options.delete(:as)
  column = options.delete(:column)
  if prism.blank?
    prism ||= resource.class.name.underscore
    column ||= :journal_entry_id
  else
    column ||= "#{prism}_journal_entry_id".to_sym
  end

  attributes = options
  attributes[:resource] ||= @resource
  attributes[:resource_prism] ||= prism
  # 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
    journal_entry = JournalEntry.find_by(id: @resource.send(column))
    list = record(&block)

    if journal_entry && (!journal_entry.draft? || list.empty? ||
                         attributes[:journal_id] != journal_entry.journal_id ||
                         @action == :destroy)
      journal_entry.cancel
      journal_entry = nil
    end

    # Add journal items
    if condition && list.any? && @action != :destroy
      attributes[:items] = []

      for cmd in list
        direction = cmd.shift
        unless %i[add_debit add_credit].include?(direction)
          raise 'Can accept only add_debit and add_credit commands'
        end
        cmd[3] ||= {}
        cmd[3][:credit] = true if direction == :add_credit
        attributes[:items] << JournalEntryItem.new_for(*cmd)
      end

      attributes[:financial_year] = FinancialYear.at(attributes[:printed_on])
      attributes[:currency] = attributes[:financial_year].currency if attributes[:financial_year]
      attributes[:real_currency] = Journal.find(attributes[:journal_id]).currency
      journal_entry ||= JournalEntry.new
      journal_entry.attributes = attributes
      journal_entry.save!
      journal_entry.confirm unless @draft
    end

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

#record {|recorder| ... } ⇒ Object

Yields:

  • (recorder)

119
120
121
122
123
# File 'lib/ekylibre/record/bookkeep.rb', line 119

def record
  recorder = EntryRecorder.new
  yield(recorder)
  recorder.list
end