Class: Vfwcash::Gcash

Inherits:
Object
  • Object
show all
Defined in:
lib/models/gcash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Gcash

Returns a new instance of Gcash.



15
16
17
18
19
20
21
22
23
# File 'lib/models/gcash.rb', line 15

def initialize(config)
  @config = config
  @checking_acct = @config[:checking_acct]
  @savings_acct = @config[:savings_acct]
  @checking_funds = CashAccount.find_by(name:@checking_acct).children.pluck(:name)
  @savings_funds = CashAccount.find_by(name:@savings_acct).children.pluck(:name)
  @dates = Vfwcash.transaction_range
  set_tmonths 
end

Instance Attribute Details

#balancesObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def balances
  @balances
end

#bmonthObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def bmonth
  @bmonth
end

#checkingObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def checking
  @checking
end

#checking_acctObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def checking_acct
  @checking_acct
end

#checking_fundsObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def checking_funds
  @checking_funds
end

#configObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def config
  @config
end

#datesObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def dates
  @dates
end

#savingsObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def savings
  @savings
end

#savings_acctObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def savings_acct
  @savings_acct
end

#savings_fundsObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def savings_funds
  @savings_funds
end

#tmonthsObject

API calls provide informtion needed for specific reports.



12
13
14
# File 'lib/models/gcash.rb', line 12

def tmonths
  @tmonths
end

Instance Method Details

#audit_api(report_date) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/models/gcash.rb', line 95

def audit_api(report_date)
  date = Vfwcash.set_date(report_date)
  boq = date.beginning_of_quarter
  eoq = boq.end_of_quarter
  if eoq.month >= date.month
    boq = (boq - 1.month).beginning_of_quarter
    eoq = boq.end_of_quarter
  end
  get_fund_balances(boq,eoq)
  rbalances = @balances
  rbalances["Cash"] = {bbalance:100000,ebalance:100000,debits:0,credits:0}
  rbalances[:dates] = {boq:boq,eoq:eoq, report_date:date}
  rbalances
end

#between_balance(from, to) ⇒ Object

REPORT Specific Methods



85
86
87
88
89
90
91
92
93
# File 'lib/models/gcash.rb', line 85

def between_balance(from,to)
  between = {}
  accts = @checking_funds + @savings_funds
  accts.each do |f|
    acct = CashAccount.find_by(name:f)
    between[f] = acct.balances_between(from,to)
  end
  between
end

#get_all_balancesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/models/gcash.rb', line 60

def get_all_balances
  balances = {checking:{},savings:{}}
  accts = @checking_funds + @savings_funds
  mbalance = {}
  tmonths.each do |m|
    bom = Date.parse(m+"01")
    eom = bom.end_of_month
    get_fund_balances(bom,eom)
    mbalance[m] = @balances
  end
  accts.each do |f|
    acct = CashAccount.find_by(name:f)
    balances[f] = {}
    tmonths.each do |m|
      balances[f][m] = mbalance[m][f]
      balances[:checking][m] = mbalance[m][:checking]
      balances[:savings][m] = mbalance[m][:savings]
    end
  end
  @balances = balances
  @checking = @balances[:checking]
  @savings = @balances[:savings]
end

#get_fund_balances(bdate, edate) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/models/gcash.rb', line 36

def get_fund_balances(bdate,edate)
  @balances = {}
  @balances[:checking] =  {bbalance:0,diff:0,debits:0,credits:0,ebalance:0}
  @balances[:savings] =  {bbalance:0,diff:0,debits:0,credits:0,ebalance:0}
  accts = @checking_funds + @savings_funds
  accts.each do |f|
    acct = CashAccount.find_by(name:f)
    @balances[f] = acct.balances_between(bdate,edate)
    if @checking_funds.include?(f)
      sum_from_to(@balances[f],@balances[:checking])
    else
      sum_from_to(@balances[f],@balances[:savings])
    end
  end
  @checking = @balances[:checking]
  @savings = @balances[:savings]
end

#month_ledger(date) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/models/gcash.rb', line 161

def month_ledger(date)
  @bmonth = Vfwcash.yyyymm(date)
  trans = Tran.month_transactions(date)
  lines = []
  trans.each do |t|
    date = Date.parse(t.)
    line = {date: date.strftime("%m/%d/%Y"),num:t.num,r:nil,desc:t.description,checking:{db:0,cr:0}}
    @checking_funds.each do |f|
      line[f] = {db:0,cr:0}
    end
    line[:savings] = {db:0,cr:0}
    t.splits.each do |s|
      details = s.details
      if details[:name].include?("#{@checking_acct}:")
        line[details[:fund]][:db] += details[:db]
        line[details[:fund]][:cr] += details[:cr]
        line[:checking][:db] += details[:db]
        line[:checking][:cr] += details[:cr]
        line[:r] = details[:r]
      elsif details[:name].include?("#{@savings_acct}:")
        line[:savings][:db] += details[:db]
        line[:savings][:cr] += details[:cr]
        line[:r] = details[:r]
      end
    end

    lines << line
  end
  lines
end

#month_ledger_api(date) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/models/gcash.rb', line 149

def month_ledger_api(date)
  response = {rows:month_ledger(date)}
  fund_bal = {}
  @checking_funds.each do |f|
    fund_bal[f] = @balances[f]
  end
  response[:balances] = {savings:@savings,checking:@checking,funds:fund_bal}
  response[:funds] = {savings:@savings_funds,checking:@checking_funds}
  response[:month] = @bmonth
  return response
end

#profit_loss(options) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/models/gcash.rb', line 192

def profit_loss(options)
  today = Date.today
  @from = options[:from].nil? ? today.beginning_of_month  : Vfwcash.set_date(options[:from])
  @to = options[:to].nil? ? today.end_of_month  : Vfwcash.set_date(options[:to])
  level = options[:level] ||= options[:lev]
  i = CashAccount.find_by(name:'Income')
  e = CashAccount.find_by(name:'Expenses')
  report = {"Income" => {amount:period_splits(i),total:0,children:{}}, 
  "Expense" =>  {amount:period_splits(e),total:0,children:{}},
  "options" => {level:level,from:@from,to:@to}}
  tree(i,report['Income'])
  tree(e,report['Expense'])
  return report
end

#set_tmonthsObject

COMMON Methods



25
26
27
28
29
30
31
32
33
34
# File 'lib/models/gcash.rb', line 25

def set_tmonths
  @tmonths = []
  first = @dates.first
  last = @dates.last
  curr = first
  while curr < last do
    @tmonths << "#{curr.year}#{curr.month.to_s.rjust(2,'0')}"
    curr += 1.month
  end
end

#split_ledger(date) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/models/gcash.rb', line 117

def split_ledger(date)
  @bmonth = Vfwcash.yyyymm(date)
  trans = Tran.month_transactions(date)
  lines = []
  b = @checking[:bbalance]
  trans.each do |t|
    date = Date.parse(t.)
    line = {date: date.strftime("%m/%d/%Y"),num:t.num,desc:t.description,
      checking:{db:0,cr:0},details:[],balance:0, memo:nil,r:nil}
    t.splits.each do |s|
      details = s.details
      if details[:name].include?("#{@checking_acct}:")
        line[:checking][:db] += details[:db]
        line[:checking][:cr] += details[:cr]
        b += (details[:db] - details[:cr])
        line[:balance] = b
        line[:r] = details[:r]
      else
        line[:balance] = b
        if line[:memo].nil?
          line[:memo] = details[:name]
        else
          line[:memo] = "- Split Transaction -"
        end
      end
      line[:details] << details
    end
    lines << line
  end
  lines
end

#split_ledger_api(date) ⇒ Object



110
111
112
113
114
115
# File 'lib/models/gcash.rb', line 110

def split_ledger_api(date)
  response = {rows:split_ledger(date)}
  response[:balances] = {checking:@checking}
  response[:month] = @bmonth
  return response
end

#sum_from_to(ffund, tfund) ⇒ Object



54
55
56
57
58
# File 'lib/models/gcash.rb', line 54

def sum_from_to(ffund,tfund)
  ffund.each do |k,v|
    tfund[k] += v
  end
end