Class: LucaBook::List
- Defined in:
- lib/luca_book/list.rb
Overview
Journal List on specified term
Constant Summary collapse
- @@dict =
LucaRecord::Dict.new('base.tsv')
Constants inherited from Journal
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .add_header(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, header_key: nil, header_val: nil) ⇒ Object
- .search_code(code) ⇒ Object
- .term(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, basedir: @dirname, recursive: false) ⇒ Object
Instance Method Summary collapse
- #accumulate_code ⇒ Object
- #filter_by_code(voucher, code) ⇒ Object
- #group_by_code(level = 3) ⇒ Object
-
#initialize(data, start_date, code = nil) ⇒ List
constructor
A new instance of List.
- #list_accounts(level = 3) ⇒ Object
- #list_by_code(recursive = false) ⇒ Object
- #list_journals ⇒ Object
- #render_html(file_type = :html) ⇒ Object
- #render_line(view) ⇒ Object
- #table_footer ⇒ Object
- #table_header ⇒ Object
Methods inherited from Journal
create, filter_by_code, journal2csv, load_data, save, serialize_on_key, update_codes, validate
Constructor Details
#initialize(data, start_date, code = nil) ⇒ List
Returns a new instance of List.
18 19 20 21 22 |
# File 'lib/luca_book/list.rb', line 18 def initialize(data, start_date, code = nil) @data = data @code = code @start = start_date end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/luca_book/list.rb', line 16 def data @data end |
Class Method Details
.add_header(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, header_key: nil, header_val: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/luca_book/list.rb', line 40 def self.add_header(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, header_key: nil, header_val: nil) return nil if code.nil? return nil unless Journal::ACCEPTED_HEADERS.include?(header_key) term(from_year, from_month, to_year, to_month, code: code) .data.each do |journal| Journal.add_header(journal, header_key, header_val) end end |
.search_code(code) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/luca_book/list.rb', line 93 def self.search_code(code) return code if @@dict.dig(code) @@dict.search(code).tap do |new_code| if new_code.nil? puts "Search word is not matched with labels" exit 1 end end end |
.term(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, basedir: @dirname, recursive: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/luca_book/list.rb', line 24 def self.term(from_year, from_month, to_year = from_year, to_month = from_month, code: nil, basedir: @dirname, recursive: false) code = search_code(code) if code data = LucaBook::Journal.term(from_year, from_month, to_year, to_month, code).select do |dat| if code.nil? true else if recursive ! [:debit, :credit].map { |key| serialize_on_key(dat[key], :code) }.flatten.select { |idx| /^#{code}/.match(idx) }.empty? else [:debit, :credit].map { |key| serialize_on_key(dat[key], :code) }.flatten.include?(code) end end end new data, Date.new(from_year.to_i, from_month.to_i, 1), code end |
Instance Method Details
#accumulate_code ⇒ Object
87 88 89 90 91 |
# File 'lib/luca_book/list.rb', line 87 def accumulate_code @data.inject(BigDecimal('0')) do |sum, dat| sum + Util.diff_by_code(dat[:debit], @code) - Util.diff_by_code(dat[:credit], @code) end end |
#filter_by_code(voucher, code) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/luca_book/list.rb', line 156 def filter_by_code(voucher, code) [:debit, :credit].each_with_object([]) do |balance, lines| voucher[balance].each do |record| next unless /^#{code}/.match(record[:code]) counter_balance = (balance == :debit) ? :credit : :debit view = { code: record[:code], amount: {} } view[:date], view[:txid] = decode_id(voucher[:id]) view[:label] = @@dict.dig(record[:code], :label) if record[:code].length >= 4 view[:label] = voucher.dig(:headers, 'x-customer') if view[:label].nil? view[:amount][balance] = readable(record[:amount]) view[:counter_code] = voucher.dig(counter_balance, 0, :code) view[:counter_label] = @@dict.dig(view[:counter_code], :label) || '' view[:counter_label] += ' sundry a/c' if voucher[counter_balance].length > 1 view[:note] = voucher[:note] lines << view end end end |
#group_by_code(level = 3) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/luca_book/list.rb', line 187 def group_by_code(level = 3) list_accounts.map do |code| vouchers = @data.filter do |voucher| codes = [:debit, :credit].map do |balance| voucher[balance].map { |record| record[:code][0, level] } end codes.flatten.include?(code) end { code: code, vouchers: vouchers } end end |
#list_accounts(level = 3) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/luca_book/list.rb', line 199 def list_accounts(level = 3) return nil if level < 3 list = @data.each_with_object([]) do |voucher, codes| [:debit, :credit].each do |balance| voucher[balance].each do |record| next if record[:code].length < level codes << record[:code][0, level] end end end list.uniq.sort end |
#list_by_code(recursive = false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/luca_book/list.rb', line 50 def list_by_code(recursive = false) calc_code(recursive: recursive) convert_label @data = [code_header] + @data.map do |dat| date, txid = LucaSupport::Code.decode_id(dat[:id]) {}.tap do |res| res['code'] = dat[:code].length == 1 ? dat[:code].first : dat[:code] 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 readable(@data) end |
#list_journals ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/luca_book/list.rb', line 69 def list_journals convert_label @data = @data.map do |dat| date, txid = LucaSupport::Code.decode_id(dat[:id]) {}.tap do |res| res['date'] = date res['no'] = txid res['id'] = dat[:id] res['debit_code'] = dat[:debit].length == 1 ? dat[:debit][0][:code] : dat[:debit].map { |d| d[:code] } res['debit_amount'] = dat[:debit].inject(0) { |sum, d| sum + d[:amount] } res['credit_code'] = dat[:credit].length == 1 ? dat[:credit][0][:code] : dat[:credit].map { |d| d[:code] } res['credit_amount'] = dat[:credit].inject(0) { |sum, d| sum + d[:amount] } res['note'] = dat[:note] end end readable(@data) end |
#render_html(file_type = :html) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/luca_book/list.rb', line 104 def render_html(file_type = :html) start_balance = set_balance(true) @journals = group_by_code.map do |account| balance = start_balance[account[:code]] || BigDecimal('0') table = [] table << %Q(<h2 class="title">#{@@dict.dig(account[:code], :label)}</h2>) account[:vouchers].map { |voucher| filter_by_code(voucher, account[:code]) }.flatten .unshift({ balance: readable(balance) }) .map { |row| balance += Util.pn_debit(account[:code]) * ((row.dig(:amount, :debit) || 0) - (row.dig(:amount, :credit) || 0)) row[:balance] = readable(balance) row } .map { |row| render_line(row) } .each_slice(28) do |rows| table << table_header table << rows.join("\n") table << table << %Q(<hr class='pgbr' />) end table[0..-2].join("\n") end case file_type when :html render_erb(search_template('journals.html.erb')) when :pdf erb2pdf(search_template('journals.html.erb')) else raise 'This filetype is not supported.' end end |
#render_line(view) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/luca_book/list.rb', line 176 def render_line(view) %Q(<tr> <td class="date">#{view[:date]}<br /><div>#{view[:txid]}</div></td> <td class="counter">#{view[:counter_label]}</td> <td class="note">#{view[:label]}<br /><div class="note">#{view[:note]}</div></td> <td class="debit amount">#{view.dig(:amount, :debit)}</td> <td class="credit amount">#{view.dig(:amount, :credit)}</td> <td class="balance">#{view[:balance]}</td> </tr>) end |
#table_footer ⇒ Object
151 152 153 154 |
# File 'lib/luca_book/list.rb', line 151 def %Q(</tbody> </table>) end |
#table_header ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/luca_book/list.rb', line 138 def table_header %Q(<table> <thead> <th>Date<br />No</th> <th>Counter account</th> <th>Sub account<br />Note</th> <th>Debit</th> <th>Credit</th> <th>Balance</th> </thead> <tbody>) end |