Class: Pacioli::JournalEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/pacioli/journal_entry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(source) ⇒ Object



93
94
95
# File 'lib/pacioli/journal_entry.rb', line 93

def self.for(source)
  where(source_documentable_type: source.class.name, source_documentable_id: source.id).includes(transactions: [:account])
end

Instance Method Details

#balanced?Boolean

Returns:

  • (Boolean)


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

def balanced?
  debits.sum(&:amount) == credits.sum(&:amount)
end

#calculate_amountObject



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

def calculate_amount
  credits.sum(&:amount)
end

#credit(options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/pacioli/journal_entry.rb', line 34

def credit(options={})
   = self.company.accounts.where(name: options[:account]).first
  credit = Credit.new(journal_entry: self, account: , amount: options[:amount], dated: self.dated)
  credit.party = options[:against_party] if options.has_key? :against_party
  self.transactions << credit
end

#creditsObject



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

def credits
  transactions.where(type: 'Pacioli::Credit')
end

#debit(options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/pacioli/journal_entry.rb', line 27

def debit(options={})
   = self.company.accounts.where(name: options[:account]).first
  debit = Debit.new(journal_entry: self, account: , amount: options[:amount], dated: self.dated)
  debit.party = options[:against_party] if options.has_key? :against_party
  self.transactions << debit
end

#debitsObject



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

def debits
  transactions.where(type: 'Pacioli::Debit')
end

#execute_posting_rulesObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pacioli/journal_entry.rb', line 57

def execute_posting_rules
  return if journal_type.blank?
  posting_rule = self.company.posting_rules.where(name: self.journal_type).first
  
  posting_rule.rules[:debits].each do |debit|
    self.debit(account: debit[:account], amount: (self.amount / 100) * debit[:percentage])
  end

  posting_rule.rules[:credits].each do |credit|
    self.credit(account: credit[:account], amount: (self.amount / 100) * credit[:percentage])
  end
end

#prettyObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pacioli/journal_entry.rb', line 97

def pretty
  strings = ["".center(101, "-")]
  strings << "| Description".ljust(30, " ") + "| #{description}".ljust(70, " ") + "|"
  strings << "| Amount".ljust(30, " ") + "| #{amount}".ljust(70, " ") + "|"
  strings << "| Dated".ljust(30, " ") + "| #{dated}".ljust(70, " ") + "|"
  strings << "| Source Document".ljust(30, " ") + "| #{source_documentable_type} with id: #{source_documentable_id}".ljust(70, " ") + "|"
  strings << "".center(101, "-")
  strings << ""

  transactions.map do |transaction|
    strings << "(#{transaction..code}) #{transaction..name}".center(101, "-")
    if transaction.party.nil?
      party_name = ""
    else
      party_name = "(#{transaction.party.partyable_type}: #{transaction.party.name})"
    end

    if transaction.debit?
      item = ( "#{party_name} " + transaction.amount.to_s + " |").rjust(51, " ")
    else
      item = ("| ".rjust(52) + transaction.amount.to_s + " #{party_name}")
    end

    strings << item
    strings << ""
  end

  strings.join("\n")
end

#record(&block) ⇒ Object

record Could be used to update a journal entry and affect other accounts eg. a payment into our bank would result in a journal entry but we may not know where it came from. At a later stage we could allocate the payment to a debtor/invoice etc.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pacioli/journal_entry.rb', line 80

def record(&block)
  self.transaction do
    self.instance_eval(&block)

    self.execute_posting_rules

    JournalEntryValidator.for(self).execute

    self.save!
    self
  end
end

#sourceObject



70
71
72
# File 'lib/pacioli/journal_entry.rb', line 70

def source
  source_documentable
end

#type(type = nil) ⇒ Object



23
24
25
# File 'lib/pacioli/journal_entry.rb', line 23

def type(type=nil)
  self.journal_type = type
end

#with_amount(amt) ⇒ Object



11
12
13
# File 'lib/pacioli/journal_entry.rb', line 11

def with_amount(amt)
  self.amount = amt
end

#with_date(time = Time.now) ⇒ Object



19
20
21
# File 'lib/pacioli/journal_entry.rb', line 19

def with_date(time=Time.now)
  self.dated = time
end

#with_description(desc) ⇒ Object



7
8
9
# File 'lib/pacioli/journal_entry.rb', line 7

def with_description(desc)
  self.description = desc
end

#with_source_document(source_document) ⇒ Object



15
16
17
# File 'lib/pacioli/journal_entry.rb', line 15

def with_source_document(source_document)
  self.source_documentable = source_document
end