hledger-forecast 0.1.5 → 0.1.6
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 +4 -4
- data/Gemfile +1 -0
- data/README.md +9 -0
- data/bin/hledger-forecast +1 -1
- data/example.yml +2 -2
- data/hledger-forecast.gemspec +1 -0
- data/lib/hledger_forecast/{command.rb → cli.rb} +3 -1
- data/lib/hledger_forecast/generator.rb +1 -1
- data/lib/hledger_forecast/options.rb +5 -0
- data/lib/hledger_forecast/summarize.rb +68 -0
- data/lib/hledger_forecast/version.rb +1 -1
- data/lib/hledger_forecast.rb +3 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60e8bd939c2bb57d5d1fdf01d39d4fd7bc1cd59886a189b8b0d8a8fe7a7b6640
|
4
|
+
data.tar.gz: e752946a04d49b531ca303a8832d11a686d610392ce29d5a7d96c5aae2658dee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7b44e5ceebc727be1a578a1210741fc5bbb9ec5e6d5a13feb36012bd35fb253173594ef0a35fb8448e1da697575ecdc97797473f7d5751db032a5065cce2bf8
|
7
|
+
data.tar.gz: c341817f928c9120662319a6ef9c1434d22b8872e685283b162ad4043efd9a0d1366eeeed4a58196e54708689833fa24af1b6b9e92dc59b8a75a98ad77810390
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -37,6 +37,7 @@ Running `hledger-forecast -h` shows the available options:
|
|
37
37
|
-s, --start-date DATE The date to start generating from (yyyy-mm-dd)
|
38
38
|
-e, --end-date DATE The date to start generating to (yyyy-mm-dd)
|
39
39
|
--force Force an overwrite of the output file
|
40
|
+
--summarize Summarize the forecast file and output to the terminal
|
40
41
|
-h, --help Show this message
|
41
42
|
--version Show version
|
42
43
|
|
@@ -113,6 +114,14 @@ settings:
|
|
113
114
|
thousands_separator: true # Separate thousands with a comma?
|
114
115
|
```
|
115
116
|
|
117
|
+
### Summarizing the config file
|
118
|
+
|
119
|
+
As your config file grows, it can be helpful to sum up the total amounts and output them in the CLI. This can be achieved by:
|
120
|
+
|
121
|
+
hledger-forecast -f forecast.yml --summarize
|
122
|
+
|
123
|
+
where `forecast.yml` is the config file to sum up.
|
124
|
+
|
116
125
|
## :brain: Rationale
|
117
126
|
|
118
127
|
Firstly, I've come to realise from reading countless blog and Reddit posts on [plain text accounting](https://plaintextaccounting.org), that everyone does it __completely__ differently!
|
data/bin/hledger-forecast
CHANGED
data/example.yml
CHANGED
data/hledger-forecast.gemspec
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module HledgerForecast
|
2
|
-
class
|
2
|
+
class CLI
|
3
3
|
def self.run(args)
|
4
4
|
end_date = args[:end_date]
|
5
5
|
start_date = args[:start_date]
|
6
6
|
forecast = File.read(args[:forecast_file])
|
7
7
|
transactions = args[:transactions_file] ? File.read(args[:transactions_file]) : nil
|
8
8
|
|
9
|
+
return HledgerForecast::Summarize.generate(forecast) if args[:summarize]
|
10
|
+
|
9
11
|
puts "[Using default dates: #{start_date} to #{end_date}]" if args[:default_dates]
|
10
12
|
|
11
13
|
transactions = Generator.create_journal_entries(transactions, forecast, start_date, end_date)
|
@@ -29,7 +29,7 @@ module HledgerForecast
|
|
29
29
|
Money.from_cents(formatted_transaction['amount'].to_i * 100, @settings[:currency]).format(
|
30
30
|
symbol: @settings[:show_symbol],
|
31
31
|
sign_before_symbol: @settings[:sign_before_symbol],
|
32
|
-
thousands_separator: @settings[:thousands_separator] ? ',' : nil
|
32
|
+
thousands_separator: @settings[:thousands_separator] ? ',' : nil,
|
33
33
|
)
|
34
34
|
|
35
35
|
formatted_transaction
|
@@ -32,6 +32,11 @@ module HledgerForecast
|
|
32
32
|
options[:end_date] = a
|
33
33
|
end
|
34
34
|
|
35
|
+
opts.on("--summarize",
|
36
|
+
"Summarize the forecast file and output to the terminal") do |a|
|
37
|
+
options[:summarize] = a
|
38
|
+
end
|
39
|
+
|
35
40
|
opts.on("--force",
|
36
41
|
"Force an overwrite of the output file") do |a|
|
37
42
|
options[:force] = a
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module HledgerForecast
|
2
|
+
# Summarise a forecast YAML file and output it to the CLI
|
3
|
+
class Summarize
|
4
|
+
def self.sum_transactions(forecast_data, period)
|
5
|
+
category_totals = Hash.new(0)
|
6
|
+
forecast_data[period]&.each do |entry|
|
7
|
+
entry['transactions'].each do |transaction|
|
8
|
+
category_totals[transaction['category']] += transaction['amount']
|
9
|
+
end
|
10
|
+
end
|
11
|
+
category_totals
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.print_category_totals(period, category_totals, generator)
|
15
|
+
puts "#{period.capitalize}:"
|
16
|
+
period_total = 0
|
17
|
+
|
18
|
+
category_totals.each do |category, amount|
|
19
|
+
formatted_amount = generator.format_transaction({ 'amount' => amount })['amount']
|
20
|
+
formatted_amount = amount.to_i < 0 ? formatted_amount.red : formatted_amount.green
|
21
|
+
puts " #{category.ljust(40)}#{formatted_amount}"
|
22
|
+
period_total += amount
|
23
|
+
end
|
24
|
+
|
25
|
+
formatted_period_total = generator.format_transaction({ 'amount' => period_total })['amount']
|
26
|
+
formatted_period_total = period_total.to_i < 0 ? formatted_period_total.red : formatted_period_total.green
|
27
|
+
puts " TOTAL".ljust(42) + formatted_period_total
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.sum_all_periods(forecast_data)
|
31
|
+
periods = %w[monthly quarterly half-yearly yearly once]
|
32
|
+
total = {}
|
33
|
+
grand_total = 0
|
34
|
+
|
35
|
+
periods.each do |period|
|
36
|
+
total[period] = sum_transactions(forecast_data, period)
|
37
|
+
grand_total += total[period]
|
38
|
+
end
|
39
|
+
|
40
|
+
total['total'] = grand_total
|
41
|
+
total
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.generate(forecast)
|
45
|
+
forecast_data = YAML.safe_load(forecast)
|
46
|
+
|
47
|
+
category_totals_by_period = {}
|
48
|
+
%w[monthly quarterly half-yearly yearly once].each do |period|
|
49
|
+
category_totals_by_period[period] = sum_transactions(forecast_data, period)
|
50
|
+
end
|
51
|
+
|
52
|
+
grand_total = category_totals_by_period.values.map(&:values).flatten.sum
|
53
|
+
|
54
|
+
generator = HledgerForecast::Generator
|
55
|
+
generator.configure_settings(forecast_data)
|
56
|
+
|
57
|
+
puts
|
58
|
+
category_totals_by_period.each do |period, category_totals|
|
59
|
+
print_category_totals(period, category_totals, generator)
|
60
|
+
puts
|
61
|
+
end
|
62
|
+
|
63
|
+
formatted_grand_total = generator.format_transaction({ 'amount' => grand_total })['amount']
|
64
|
+
formatted_grand_total = grand_total.to_i < 0 ? formatted_grand_total.red : formatted_grand_total.green
|
65
|
+
puts "TOTAL:".ljust(42) + formatted_grand_total
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/hledger_forecast.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
require 'colorize'
|
3
4
|
require 'date'
|
4
5
|
require 'highline'
|
5
6
|
require 'money'
|
@@ -12,4 +13,5 @@ Money.rounding_mode = BigDecimal::ROUND_HALF_UP
|
|
12
13
|
require_relative 'hledger_forecast/version'
|
13
14
|
require_relative 'hledger_forecast/options'
|
14
15
|
require_relative 'hledger_forecast/generator'
|
15
|
-
require_relative 'hledger_forecast/
|
16
|
+
require_relative 'hledger_forecast/summarize'
|
17
|
+
require_relative 'hledger_forecast/CLI'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hledger-forecast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oli Morris
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 6.16.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: colorize
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.1
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,9 +84,10 @@ files:
|
|
70
84
|
- example.yml
|
71
85
|
- hledger-forecast.gemspec
|
72
86
|
- lib/hledger_forecast.rb
|
73
|
-
- lib/hledger_forecast/
|
87
|
+
- lib/hledger_forecast/cli.rb
|
74
88
|
- lib/hledger_forecast/generator.rb
|
75
89
|
- lib/hledger_forecast/options.rb
|
90
|
+
- lib/hledger_forecast/summarize.rb
|
76
91
|
- lib/hledger_forecast/version.rb
|
77
92
|
- spec/command_spec.rb
|
78
93
|
- spec/half-yearly_spec.rb
|