class Vfwcash::Gcash

Attributes

balances[RW]

API calls provide informtion needed for specific reports.

bmonth[RW]

API calls provide informtion needed for specific reports.

checking[RW]

API calls provide informtion needed for specific reports.

checking_acct[RW]

API calls provide informtion needed for specific reports.

checking_funds[RW]

API calls provide informtion needed for specific reports.

config[RW]

API calls provide informtion needed for specific reports.

dates[RW]

API calls provide informtion needed for specific reports.

savings[RW]

API calls provide informtion needed for specific reports.

savings_acct[RW]

API calls provide informtion needed for specific reports.

savings_funds[RW]

API calls provide informtion needed for specific reports.

tmonths[RW]

API calls provide informtion needed for specific reports.

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

audit_api(report_date) click to toggle source
# 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) click to toggle source

REPORT Specific Methods

# 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_balances() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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.post_date)
    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) click to toggle source
# 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) click to toggle source
# 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_tmonths() click to toggle source

COMMON Methods

# 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) click to toggle source
# 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.post_date)
    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) click to toggle source
# 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) click to toggle source
# File lib/models/gcash.rb, line 54
def sum_from_to(ffund,tfund)
  ffund.each do |k,v|
    tfund[k] += v
  end
end

Private Instance Methods

period_splits(acct) click to toggle source
# File lib/models/gcash.rb, line 218
def period_splits(acct)
  flipper = acct.account_type == 'INCOME' ? -1 : 1
  # sp = acct.splits.joins(:tran).where('transactions.post_date between ? and ?',@from.strftime('%Y%m%d')+'00',@to.strftime('%Y%m%d')+'24')
  sp = acct.splits.joins(:tran).where(transactions:{post_date: Vfwcash.str_date_range(@from,@to)})


  sp.sum(:value_num) * flipper
end
tree(branch,hash) click to toggle source
# File lib/models/gcash.rb, line 210
def tree(branch,hash)
  branch.children.each do |c|
    hash[:children][c.name] = {amount:period_splits(c),total:0,children: {}}
    tree(c,hash[:children][c.name])
    hash[:total] += (hash[:children][c.name][:amount] + hash[:children][c.name][:total])
  end
end