module LucaBook::Util

Public Instance Methods

amount_by_code(items, code) click to toggle source
# File lib/luca_book/util.rb, line 25
def amount_by_code(items, code)
  items
    .select { |item| /^#{code}/.match(item[:code]) }
    .inject(BigDecimal('0')) { |sum, item| sum + item[:amount] }
end
calc_diff(num, code) click to toggle source
# File lib/luca_book/util.rb, line 31
def calc_diff(num, code)
  num * pn_debit(code.to_s)
end
current_fy(date = nil, to: nil) click to toggle source
# File lib/luca_book/util.rb, line 46
def current_fy(date = nil, to: nil)
  date ||= Date.today
  start_month = LucaSupport::CONFIG['fy_start']
  start_year = date.month >= start_month ? date.year : date.year - 1
  @start_date = Date.new(start_year, start_month, 1)
  @end_date = Date.new(start_year + 1, start_month - 1, -1)
  @end_date = [to, @end_date].min if to
  [@start_date, @end_date]
end
diff_by_code(items, code) click to toggle source

items assumed as bellows:

[{ code: '113', amount: 1000 }, ... ]
# File lib/luca_book/util.rb, line 21
def diff_by_code(items, code)
  calc_diff(amount_by_code(items, code), code)
end
pn_debit(code) click to toggle source
# File lib/luca_book/util.rb, line 35
def pn_debit(code)
  case code
  when /^[0-4BCEGH]/
    1
  when /^[5-9ADF]/
    -1
  else
    nil
  end
end
validate_balance(items) click to toggle source
# File lib/luca_book/util.rb, line 10
def validate_balance(items)
  d = items.select { |k, _v| /^[1-4]/.match(k) }
        .inject(BigDecimal('0')) { |sum, (_k, v)| sum + BigDecimal(v[:balance] || 0) }
  c = items.select { |k, _v| /^[5-9]/.match(k) }
        .inject(BigDecimal('0')) { |sum, (_k, v)| sum + BigDecimal(v[:balance] || 0) }
  [d - c, { debit: d, credit: c }]
end