lucabook 0.2.19 → 0.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28e5bc747a4ba316a5badc9f7845637f4b27350cb199911c1cafc8771dc0925b
4
- data.tar.gz: db3d419a9a20643d2235305a6c7d0ab6f776826cd062751878b3710772b03428
3
+ metadata.gz: 82bb098ac3acfbb37c600908b75174c34a665f4d7c9003d9956738783bb9a617
4
+ data.tar.gz: b963a24873e226c898c7b14dba4cf21edaacf4cfdc9f7e6eeab0d04959fa788b
5
5
  SHA512:
6
- metadata.gz: b0a7ecb865541848cd4a0bcbeb7cb6e590bdba4b084a62ae656a2df7303580b7294bcc728991e41c7d2d6ef00cc068c0a6c140858126c9b2ba5d0afef4d5d68b
7
- data.tar.gz: 1fe4d3b9ce540f6424792771ed1ed9b7e40f991a281258e1b4ba415ffbab328fa148c0db917047ca5d06437c9a3126b61db2a7099bdf99b45050adda7cfa0deb
6
+ metadata.gz: b56ef4dd42ee782bf488cd50e6992415d878f388663ed0fb4f983f7353310ea53a2a0a4e1d94be5f06b51102eb73f7e9d0a7404a05cdccf6487248a1041196d3
7
+ data.tar.gz: 12cfe5f1194d34236ebe69541a9d4b0adf3333a39c1e73024ce0d3ad43639e4147ab48c19c5fc52e08bcbe6473821c850189c0b81e252101456ce70eea80a3c4
@@ -40,8 +40,9 @@ module LucaCmd
40
40
  end
41
41
 
42
42
  def self.profitloss(args, params)
43
+ level = params[:level] || 2
43
44
  args = LucaCmd.gen_range(params[:n]) if args.empty?
44
- LucaBook::State.term(*args).pl.to_yaml
45
+ LucaBook::State.term(*args).pl(level).to_yaml
45
46
  end
46
47
  end
47
48
 
@@ -121,6 +122,7 @@ when /reports?/, 'r'
121
122
  when 'pl'
122
123
  OptionParser.new do |opt|
123
124
  opt.banner = 'Usage: luca-book reports pl [options] [YYYY M YYYY M]'
125
+ opt.on('-l', '--level VAL', 'account level') { |v| params[:level] = v.to_i }
124
126
  opt.on('-n VAL', 'report count') { |v| params[:n] = v.to_i }
125
127
  args = opt.parse!(ARGV)
126
128
  LucaCmd::Report.profitloss(args, params)
@@ -81,7 +81,7 @@ module LucaBook
81
81
  end
82
82
 
83
83
  def to_yaml
84
- YAML.dump(code2label).tap { |data| puts data }
84
+ YAML.dump(readable(code2label)).tap { |data| puts data }
85
85
  end
86
86
 
87
87
  def code2label
@@ -137,7 +137,7 @@ module LucaBook
137
137
  end
138
138
  end
139
139
  end
140
- puts YAML.dump(@statement)
140
+ puts YAML.dump(readable(@statement))
141
141
  self
142
142
  end
143
143
 
@@ -159,13 +159,25 @@ module LucaBook
159
159
  end
160
160
  end
161
161
 
162
- def pl
163
- @statement = @data.map { |data| data.select { |k, _v| /^[A-H_].+/.match(k) } }
164
- term = @statement.each_with_object({}) { |item, h| item.each { |k, v| h[k].nil? ? h[k] = v : h[k] += v } }
162
+ def pl(level = 2)
163
+ term_keys = @data.inject([]) { |a, data| a + data.keys }
164
+ .compact.select { |k| /^[A-H_].+/.match(k) }
165
165
  fy = @start_balance.select { |k, _v| /^[A-H].+/.match(k) }
166
+ keys = (term_keys + fy.keys).uniq.sort
167
+ keys.select! { |k| k.length <= level }
168
+ @statement = @data.map do |data|
169
+ {}.tap do |h|
170
+ keys.each { |k| h[k] = data[k] || BigDecimal('0') }
171
+ end
172
+ end
173
+ term = @statement.each_with_object({}) do |item, h|
174
+ item.each do |k, v|
175
+ h[k] = h[k].nil? ? v : h[k] + v if /^[^_]/.match(k)
176
+ end
177
+ end
166
178
  fy = {}.tap do |h|
167
- (term.keys + fy.keys).uniq.each do |k|
168
- h[k] = (fy[k] || 0).to_i + (term[k] || 0).to_i
179
+ keys.each do |k|
180
+ h[k] = BigDecimal(fy[k] || '0') + BigDecimal(term[k] || '0')
169
181
  end
170
182
  end
171
183
  @statement << term.tap { |h| h['_d'] = 'Period Total' }
@@ -214,7 +226,7 @@ module LucaBook
214
226
  res['H0'] = sum_matched(report, /^[H][0-9][0-9A-Z]{1,}/)
215
227
  res['HA'] = res['GA'] - res['H0']
216
228
 
217
- report['9142'] = (report['9142'] || 0) + res['HA']
229
+ report['9142'] = (report['9142'] || BigDecimal('0')) + res['HA']
218
230
  res['9142'] = report['9142']
219
231
  res['10'] = sum_matched(report, /^[123][0-9A-Z]{2,}/)
220
232
  res['40'] = sum_matched(report, /^[4][0-9A-Z]{2,}/)
@@ -260,13 +272,14 @@ module LucaBook
260
272
  end
261
273
 
262
274
  base = Dict.latest_balance.each_with_object({}) do |(k, v), h|
263
- h[k] = v[:balance].to_i if v[:balance]
275
+ h[k] = BigDecimal(v[:balance].to_s) if v[:balance]
264
276
  end
265
277
  if pre
266
278
  idx = (pre.keys + base.keys).uniq
267
- base = {}.tap { |h| idx.each { |k| h[k] = (base[k] || 0) + (pre[k] || 0) } }
279
+ base = {}.tap do |h|
280
+ idx.each { |k| h[k] = (base[k] || BigDecimal('0')) + (pre[k] || BigDecimal('0')) }
281
+ end
268
282
  end
269
- #code_sum(base).merge(self.class.total_subaccount(base))
270
283
  self.class.total_subaccount(base)
271
284
  end
272
285
 
@@ -275,6 +288,7 @@ module LucaBook
275
288
  end
276
289
 
277
290
  # for assert purpose
291
+ #
278
292
  def self.gross(year, month = nil, code = nil, date_range = nil, rows = 4)
279
293
  if ! date_range.nil?
280
294
  raise if date_range.class != Range
@@ -287,27 +301,28 @@ module LucaBook
287
301
  CSV.new(f, headers: false, col_sep: "\t", encoding: 'UTF-8')
288
302
  .each_with_index do |row, i|
289
303
  break if i >= rows
304
+
290
305
  case i
291
306
  when 0
292
307
  idx_memo = row.map(&:to_s)
293
308
  idx_memo.each do |r|
294
- sum[:debit][r] ||= 0
309
+ sum[:debit][r] ||= BigDecimal('0')
295
310
  sum[:debit_count][r] ||= 0
296
311
  end
297
312
  when 1
298
313
  row.each_with_index do |r, j|
299
- sum[:debit][idx_memo[j]] += r.to_i # TODO: bigdecimal support
314
+ sum[:debit][idx_memo[j]] += BigDecimal(r.to_s)
300
315
  sum[:debit_count][idx_memo[j]] += 1
301
316
  end
302
317
  when 2
303
318
  idx_memo = row.map(&:to_s)
304
319
  idx_memo.each do |r|
305
- sum[:credit][r] ||= 0
320
+ sum[:credit][r] ||= BigDecimal('0')
306
321
  sum[:credit_count][r] ||= 0
307
322
  end
308
323
  when 3
309
324
  row.each_with_index do |r, j|
310
- sum[:credit][idx_memo[j]] += r.to_i # TODO: bigdecimal support
325
+ sum[:credit][idx_memo[j]] += BigDecimal(r.to_s)
311
326
  sum[:credit_count][idx_memo[j]] += 1
312
327
  end
313
328
  else
@@ -319,37 +334,29 @@ module LucaBook
319
334
  end
320
335
 
321
336
  # netting vouchers in specified term
337
+ #
322
338
  def self.net(year, month = nil, code = nil, date_range = nil)
323
339
  g = gross(year, month, code, date_range)
324
340
  idx = (g[:debit].keys + g[:credit].keys).uniq.sort
325
341
  count = {}
326
342
  diff = {}.tap do |sum|
327
343
  idx.each do |code|
328
- sum[code] = g.dig(:debit, code).nil? ? 0 : Util.calc_diff(g[:debit][code], code)
329
- sum[code] -= g.dig(:credit, code).nil? ? 0 : Util.calc_diff(g[:credit][code], code)
344
+ sum[code] = g.dig(:debit, code).nil? ? BigDecimal('0') : Util.calc_diff(g[:debit][code], code)
345
+ sum[code] -= g.dig(:credit, code).nil? ? BigDecimal('0') : Util.calc_diff(g[:credit][code], code)
330
346
  count[code] = (g.dig(:debit_count, code) || 0) + (g.dig(:credit_count, code) || 0)
331
347
  end
332
348
  end
333
349
  [diff, count]
334
350
  end
335
351
 
336
- # TODO: replace load_tsv -> generic load_tsv_dict
352
+ # TODO: obsolete in favor of Dict.latest_balance()
337
353
  def load_start
338
354
  file = Pathname(LucaSupport::Config::Pjdir) / 'data' / 'balance' / 'start.tsv'
339
355
  {}.tap do |dict|
340
- load_tsv(file) do |row|
341
- dict[row[0]] = row[2].to_i if ! row[2].nil?
342
- end
356
+ LucaRecord::Dict.load_tsv_dict(file).each { |k, v| h[k] = v[:balance] if !v[:balance].nil? }
343
357
  end
344
358
  end
345
359
 
346
- def load_tsv(path)
347
- return enum_for(:load_tsv, path) unless block_given?
348
-
349
- data = CSV.read(path, headers: true, col_sep: "\t", encoding: 'UTF-8')
350
- data.each { |row| yield row }
351
- end
352
-
353
360
  private
354
361
 
355
362
  def legal_items
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaBook
4
- VERSION = '0.2.19'
4
+ VERSION = '0.2.20'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucabook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.19
4
+ version: 0.2.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-20 00:00:00.000000000 Z
11
+ date: 2020-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lucarecord