Module: LucaBook::Util

Included in:
Dict, Test
Defined in:
lib/luca_book/util.rb

Class Method Summary collapse

Class Method Details

.amount_by_code(items, code) ⇒ Object



25
26
27
28
29
# 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) ⇒ Object



31
32
33
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/luca_book/util.rb', line 46

def current_fy(date = nil, to: nil)
  date ||= Date.today
  start_month = LucaSupport::CONST.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 = @start_date.next_year.prev_day
  @end_date = [to, @end_date].min if to
  [@start_date, @end_date]
end

.diff_by_code(items, code) ⇒ Object

items assumed:

[{ code: '113', amount: 1000 }, ... ]


21
22
23
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



10
11
12
13
14
15
16
# 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