Class: SplttyCLI::Commands::Totals

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/spltty_cli/commands/totals.rb

Overview

spltty totals [LEDGER...] — per-person totals + settlement per ledger. Splits are resolved against split groups (ledger header groups, then global config groups); an unmatched Responsible value is a person (100%). --combined merges the non-reference ledgers into one settlement.

Instance Method Summary collapse

Instance Method Details

#call(ledgers: [], **opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spltty_cli/commands/totals.rb', line 18

def call(ledgers: [], **opts)
  config = SplttyCLI.load_config(opts)
  Discovery.sync(config)
  NotesSync.run(config)
  config.save

  names = resolve_names(config, ledgers)
  return if names.empty?

  if opts[:combined]
    combined(config, names)
  else
    names.each do |name|
      rows = read_ledger(config, name)
      reference = SplttyCLI::Totals.reference?(config, name)
      print SplttyCLI::Totals.report(name, SplttyCLI::Totals.tally(rows, SplttyCLI::Totals.resolver(config, name)), reference: reference)
    end
  end
rescue ArgumentError => e
  warn "spltty totals: #{e.message}"
  exit 1
rescue Config::Error => e
  warn "spltty totals: #{e.message}"
  exit 1
end