Module: Ekylibre::Record::Acts::Reconcilable::ClassMethods

Defined in:
lib/ekylibre/record/acts/reconcilable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_reconcilable(expense_owner, payment_owner, options = {}) ⇒ Object

Add methods for reconciliating


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ekylibre/record/acts/reconcilable.rb', line 11

def acts_as_reconcilable(expense_owner, payment_owner, options = {})
  use = options[:use] || 'use'
  uses = use.pluralize
  expense = options[:expense] || 'expense'
  payment = options[:payment] || 'payment'
  attorney = options[:attorney] || 'attorney'
  expenses = expense.pluralize
  payments = payment.pluralize
  expense_owners = expense_owner.to_s.pluralize
  payment_owners = payment_owner.to_s.pluralize
  neighbours = options[:neighbours] || 'neighbours'

  code  = ''
  code += "def #{neighbours}(#{uses}=[])\n"
  code += "  for #{use} in self.#{expense}.#{uses}+self.#{payment}.#{uses}\n"
  code += "    unless #{uses}.include? #{use}\n"
  code += "      #{uses} << #{use}\n"
  code += "      #{use}.#{neighbours}(#{uses})\n"
  code += "    end\n"
  code += "  end\n"
  code += "  return #{uses}\n"
  code += "end\n"
  code += "def reconciliate\n"
  code += "  #{expense_owners}, #{payment_owners}, amount = {}, {}, 0.0\n"
  code += "  for #{use} in self.#{neighbours}\n"
  code += "    unless #{expense_owners}.values.flatten.include? #{use}.#{expense}\n"
  code += "      #{expense_owners}[#{use}.#{expense}.#{expense_owner}.id.to_s] ||= []\n"
  code += "      #{expense_owners}[#{use}.#{expense}.#{expense_owner}.id.to_s] << #{use}.#{expense}\n"
  code += "      amount += #{use}.#{expense}.amount\n"
  code += "    end\n"
  code += "    unless #{payment_owners}.values.flatten.include? #{use}.#{payment}\n"
  code += "      #{payment_owners}[#{use}.#{payment}.#{payment_owner}.id.to_s] ||= []\n"
  code += "      #{payment_owners}[#{use}.#{payment}.#{payment_owner}.id.to_s] << #{use}.#{payment}\n"
  code += "      amount -= #{use}.#{payment}.amount\n"
  code += "    end\n"
  code += "  end\n"
  code += "  return nil unless amount.zero?\n"
  code += "  for #{expense_owner}, #{expenses} in #{expense_owners}\n"
  code += "    account = self.company.entities.find(#{expense_owner}.to_i).account(:#{expense_owner})\n"
  code += "    next unless account.reconcilable?\n"
  code += "    journal_entries = []\n"
  code += "    for #{expense} in #{expenses}\n"
  code += "      journal_entries << #{expense}.journal_entry\n"
  code += "      for #{use} in #{expense}.#{uses}\n"
  code += "        if #{use}.#{payment}.#{payment_owner}_id == #{expense}.#{expense_owner}_id \n"
  code += "          journal_entries << #{use}.#{payment}.journal_entry\n"
  code += "        else #{use}.journal_entry\n"
  code += "          journal_entries << #{use}.journal_entry\n"
  code += "        end\n"
  code += "      end\n"
  code += "    end\n"
  code += "    account.mark_entries(journal_entries)\n"
  code += "  end\n"
  code += "  for #{payment_owner}, #{payments} in #{payment_owners}\n"
  code += "    account = self.company.entities.find(#{payment_owner}.to_i).account(:#{attorney})\n"
  code += "    next unless account.reconcilable?\n"
  code += "    journal_entries = []\n"
  code += "    for #{payment} in #{payments}\n"
  code += "      e = [#{payment}.journal_entry]\n"
  code += "      for #{use} in #{payment}.#{use}s\n"
  code += "        e << #{use}.journal_entry if #{use}.#{payment}.#{payment_owner}.id != #{use}.#{expense}.#{expense_owner}.id\n"
  code += "      end\n"
  code += "      journal_entries += e if e.size > 1\n"
  code += "    end\n"
  code += "    account.mark_entries(journal_entries) if journal_entries.size > 0\n"
  code += "  end\n"
  code += "  return true\n"
  code += "end\n"
  # puts code

  # list = code.split("\n"); list.each_index{|x| puts((x+1).to_s.rjust(4)+": "+list[x])}

  class_eval code
end