Class: LucaBook::ListByHeader

Inherits:
Journal
  • Object
show all
Defined in:
lib/luca_book/list_by_header.rb

Overview

Journal List on specified term

Constant Summary

Constants inherited from Journal

Journal::ACCEPTED_HEADERS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Journal

add_header, create, filter_by_code, journal2csv, load_data, save, serialize_on_key, update_codes, validate

Constructor Details

#initialize(data, start_date, code = nil, header_name = nil) ⇒ ListByHeader

Returns a new instance of ListByHeader.



16
17
18
19
20
21
22
# File 'lib/luca_book/list_by_header.rb', line 16

def initialize(data, start_date, code = nil, header_name = nil)
  @data = data
  @code = code
  @header = header_name
  @start = start_date
  @dict = LucaRecord::Dict.load('base.tsv')
end

Class Method Details

.term(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, header: nil, basedir: @dirname) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/luca_book/list_by_header.rb', line 24

def self.term(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, header: nil, basedir: @dirname)
  data = Journal.term(from_year, from_month, to_year, to_month, code).select do |dat|
    if code.nil?
      true
    else
      [:debit, :credit].map { |key| serialize_on_key(dat[key], :code) }.flatten.include?(code)
    end
  end
  new data, Date.new(from_year.to_i, from_month.to_i, 1), code, header
end

Instance Method Details

#accumulate_codeObject



57
58
59
60
61
62
63
# File 'lib/luca_book/list_by_header.rb', line 57

def accumulate_code
  @data.each_with_object({}) do |dat, sum|
    idx = dat.dig(:headers, @header) || 'others'
    sum[idx] ||= BigDecimal('0')
    sum[idx] += Util.diff_by_code(dat[:debit], @code) - Util.diff_by_code(dat[:credit], @code)
  end
end

#list_by_codeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/luca_book/list_by_header.rb', line 35

def list_by_code
  calc_code
  convert_label
  @data = @data.each_with_object([]) do |(k, v), a|
    journals = v.map do |dat|
      date, txid = decode_id(dat[:id])
      {}.tap do |res|
        res['header'] = k
        res['date'] = date
        res['no'] = txid
        res['id'] = dat[:id]
        res['diff'] = dat[:diff]
        res['balance'] = dat[:balance]
        res['counter_code'] = dat[:counter_code].length == 1 ? dat[:counter_code].first : dat[:counter_code]
        res['note'] = dat[:note]
      end
    end
    a << { 'code' => v.last[:code], 'header' => k, 'balance' => v.last[:balance], 'count' => v.count, 'journals' => journals }
  end
  readable(@data)
end