bdgtr-cli 0.2 → 0.3
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/README.md +1 -1
- data/bin/bdgtr +26 -5
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/help.rb +168 -0
- data/lib/bdgtr/cli/ledger_sync.rb +102 -0
- data/lib/bdgtr/cli/prompt.rb +47 -0
- data/lib/bdgtr/cli/psd2.rb +530 -0
- data/lib/bdgtr/cli/rows.rb +38 -0
- data/lib/bdgtr/cli/runtime.rb +88 -0
- data/lib/bdgtr/cli/table.rb +458 -0
- data/lib/bdgtr/cli.rb +126 -0
- data/lib/bdgtr/core/binder.rb +79 -0
- data/lib/bdgtr/core/exchange.rb +66 -0
- data/lib/bdgtr/core/models/account.rb +177 -0
- data/lib/bdgtr/core/models/account_requisition.rb +12 -0
- data/lib/bdgtr/core/models/account_sync.rb +12 -0
- data/lib/bdgtr/core/models/import.rb +12 -0
- data/lib/bdgtr/core/models/ledger.rb +10 -0
- data/lib/bdgtr/core/models/requisition.rb +27 -0
- data/lib/bdgtr/core/models/transaction.rb +98 -0
- data/lib/bdgtr/core/models/transaction_category.rb +12 -0
- data/lib/bdgtr/core/record.rb +11 -0
- data/lib/bdgtr/core/schema.rb +290 -0
- data/lib/bdgtr/core/session.rb +76 -0
- data/lib/bdgtr/core/syncable.rb +272 -0
- data/lib/bdgtr/core/transfer.rb +90 -0
- data/lib/bdgtr/core.rb +18 -0
- data/lib/bdgtr/portfolio/Rakefile +20 -0
- data/lib/bdgtr/portfolio/cli/LICENSE +21 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/app.rb +77 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/import.rb +30 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/spreadsheet.rb +25 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli.rb +52 -0
- data/lib/bdgtr/portfolio/exe/bdgtr +7 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/accounts_csv.rb +67 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/app.rb +104 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/auth.rb +52 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/balances.rb +157 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/cache.rb +46 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_menu.rb +90 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_selection.rb +68 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/config.rb +138 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/decryptor.rb +70 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/drive_client.rb +182 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/errors.rb +10 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/file_reader.rb +80 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_import.rb +609 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_xls.rb +235 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import/fineco.rb +243 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import.rb +85 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/parser.rb +254 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_source.rb +30 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_store.rb +124 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/preview_window.rb +98 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/proto/client_pb.rb +29 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/row_preview.rb +174 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sheets_client.rb +272 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/snapshot.rb +27 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync.rb +247 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync_preview.rb +63 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_identity.rb +49 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_sync.rb +948 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_types.rb +142 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/version.rb +5 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api.rb +42 -0
- data/lib/bdgtr/portfolio/proto/client.proto +114 -0
- data/lib/bdgtr/psd2/account.rb +24 -0
- data/lib/bdgtr/psd2/account_snapshot.rb +96 -0
- data/lib/bdgtr/psd2/callback.rb +49 -0
- data/lib/bdgtr/psd2/connect.rb +75 -0
- data/lib/bdgtr/psd2/enable_banking.rb +250 -0
- data/lib/bdgtr/psd2/go_cardless.rb +133 -0
- data/lib/bdgtr/psd2/institution.rb +17 -0
- data/lib/bdgtr/psd2/platform.rb +37 -0
- data/lib/bdgtr/psd2/requisition.rb +21 -0
- data/lib/bdgtr/psd2/transaction.rb +29 -0
- data/lib/bdgtr/psd2.rb +25 -0
- data/lib/bdgtr/sync/importer.rb +125 -0
- data/lib/bdgtr/sync/library.rb +18 -0
- data/lib/bdgtr/sync/portfolio.rb +14 -0
- data/lib/bdgtr/sync/projection.rb +38 -0
- data/lib/bdgtr/sync/spreadsheet.rb +23 -0
- data/lib/bdgtr/sync.rb +25 -0
- data/lib/bdgtr/version.rb +1 -1
- metadata +353 -30
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
5
|
+
module PortfolioPerformanceApi
|
|
6
|
+
class FinecoXls
|
|
7
|
+
Row = Struct.new(:date, :description, :amount_cents, :type, :raw, :security, :offset_account, :proto_type, keyword_init: true)
|
|
8
|
+
|
|
9
|
+
DEFAULT_SKIP_LINES = 13
|
|
10
|
+
DATE_VALUTA_HEADERS = /data[_\s-]*valuta/i
|
|
11
|
+
DATE_OPERAZIONE_HEADERS = /data[_\s-]*operazione/i
|
|
12
|
+
DATE_GENERIC_HEADERS = /\Adata\z/i
|
|
13
|
+
DESC_HEADERS = /\Adescrizione\z/i
|
|
14
|
+
DESC_COMPLETA_HEADERS = /descrizione[_\s-]*completa/i
|
|
15
|
+
MONEYMAP_HEADERS = /moneymap/i
|
|
16
|
+
CREDIT_HEADERS = /\Aentrate?\z/i
|
|
17
|
+
DEBIT_HEADERS = /\Auscite\z/i
|
|
18
|
+
AMOUNT_HEADERS = /\Aimporto\z|\Aamount\z/i
|
|
19
|
+
|
|
20
|
+
# Fineco export columns after the preamble + header row.
|
|
21
|
+
DEFAULT_COLUMNS = {
|
|
22
|
+
date: 1,
|
|
23
|
+
credit: 2,
|
|
24
|
+
debit: 3,
|
|
25
|
+
description: 4,
|
|
26
|
+
description_completa: 5,
|
|
27
|
+
moneymap: 7
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
def self.default_skip_lines
|
|
31
|
+
raw = ENV["IMPORT_FINECO_XLS_SKIP_LINES"]
|
|
32
|
+
raw.nil? || raw.empty? ? DEFAULT_SKIP_LINES : Integer(raw)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.parse(path, skip_lines: default_skip_lines)
|
|
36
|
+
parse_rows(read_sheet(path), skip_lines: skip_lines)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.partition_excluded(rows, pattern)
|
|
40
|
+
rows = Array(rows)
|
|
41
|
+
patterns = Array(pattern).map(&:to_s).reject(&:empty?)
|
|
42
|
+
return [[], rows] if patterns.empty?
|
|
43
|
+
|
|
44
|
+
regexp = Regexp.union(patterns.map { |item| Regexp.new(item, Regexp::IGNORECASE) })
|
|
45
|
+
rows.partition { |row| row_cells(row).any? { |cell| cell.match?(regexp) } }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.exclude_matching(rows, pattern)
|
|
49
|
+
partition_excluded(rows, pattern)[1]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.parse_rows(rows, skip_lines: 0)
|
|
53
|
+
rows = Array(rows).drop(skip_lines)
|
|
54
|
+
header_index, mapping = find_header(rows)
|
|
55
|
+
body =
|
|
56
|
+
if mapping
|
|
57
|
+
rows[(header_index + 1)..]
|
|
58
|
+
else
|
|
59
|
+
mapping = DEFAULT_COLUMNS
|
|
60
|
+
rows
|
|
61
|
+
end
|
|
62
|
+
raise ArgumentError, "Fineco XLS: header row not found (need Data_Valuta, Entrate/Uscite, Descrizione)" if mapping.nil?
|
|
63
|
+
|
|
64
|
+
body.filter_map { |row| build_row(row, mapping) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.read_sheet(path)
|
|
68
|
+
bytes = File.binread(path)
|
|
69
|
+
return read_html(path) if html_spreadsheet?(bytes)
|
|
70
|
+
|
|
71
|
+
require_spreadsheet_gems
|
|
72
|
+
|
|
73
|
+
without_zip_date_warnings do
|
|
74
|
+
book = Roo::Spreadsheet.open(path.to_s)
|
|
75
|
+
sheet = book.sheet(0)
|
|
76
|
+
(sheet.first_row..sheet.last_row).map { |index| sheet.row(index) }
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def self.require_spreadsheet_gems
|
|
81
|
+
require "roo"
|
|
82
|
+
require "roo-xls"
|
|
83
|
+
require "zip"
|
|
84
|
+
rescue Gem::ConflictError => error
|
|
85
|
+
raise LoadError,
|
|
86
|
+
"#{error.message}. Use the Gemfile gems: bundle exec rake test"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def self.without_zip_date_warnings
|
|
90
|
+
previous = Zip.warn_invalid_date
|
|
91
|
+
Zip.warn_invalid_date = false
|
|
92
|
+
yield
|
|
93
|
+
ensure
|
|
94
|
+
Zip.warn_invalid_date = previous
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def self.read_html(path)
|
|
98
|
+
require "nokogiri"
|
|
99
|
+
|
|
100
|
+
doc = Nokogiri::HTML(File.read(path))
|
|
101
|
+
tables = doc.css("table").map do |table|
|
|
102
|
+
table.css("tr").map { |tr| tr.css("th, td").map { |cell| cell.text.strip } }
|
|
103
|
+
end
|
|
104
|
+
tables.max_by(&:size) || []
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def self.html_spreadsheet?(bytes)
|
|
108
|
+
sample = bytes.byteslice(0, 512).to_s.b
|
|
109
|
+
sample = sample.byteslice(3..) if sample.start_with?("\xEF\xBB\xBF".b)
|
|
110
|
+
stripped = sample.lstrip
|
|
111
|
+
stripped.start_with?("<".b) || stripped.downcase.include?("<html".b) || stripped.downcase.include?("<table".b)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def self.find_header(rows)
|
|
115
|
+
rows.each_with_index do |row, index|
|
|
116
|
+
labels = Array(row).map { |cell| cell.to_s.strip }
|
|
117
|
+
next if labels.all?(&:empty?)
|
|
118
|
+
|
|
119
|
+
mapping = {
|
|
120
|
+
date: find_column(labels, DATE_VALUTA_HEADERS) || find_column(labels, DATE_GENERIC_HEADERS),
|
|
121
|
+
description: find_column(labels, DESC_HEADERS) || find_column(labels, /\Adescriz/i),
|
|
122
|
+
description_completa: find_column(labels, DESC_COMPLETA_HEADERS),
|
|
123
|
+
moneymap: find_column(labels, MONEYMAP_HEADERS),
|
|
124
|
+
credit: find_column(labels, CREDIT_HEADERS),
|
|
125
|
+
debit: find_column(labels, DEBIT_HEADERS),
|
|
126
|
+
amount: find_column(labels, AMOUNT_HEADERS)
|
|
127
|
+
}
|
|
128
|
+
next if mapping[:date] && labels[mapping[:date]].to_s.match?(DATE_OPERAZIONE_HEADERS)
|
|
129
|
+
next unless mapping[:date] && mapping[:description]
|
|
130
|
+
next unless mapping[:credit] || mapping[:debit] || mapping[:amount]
|
|
131
|
+
|
|
132
|
+
return [index, mapping]
|
|
133
|
+
end
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def self.find_column(labels, pattern)
|
|
138
|
+
labels.index { |label| label.match?(pattern) }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def self.build_row(row, mapping)
|
|
142
|
+
cells = Array(row)
|
|
143
|
+
date = parse_date(cells[mapping[:date]])
|
|
144
|
+
return if date.nil?
|
|
145
|
+
|
|
146
|
+
credit = parse_amount(cells[mapping[:credit]]) if mapping[:credit]
|
|
147
|
+
debit = parse_amount(cells[mapping[:debit]]) if mapping[:debit]
|
|
148
|
+
signed = parse_amount(cells[mapping[:amount]]) if mapping[:amount]
|
|
149
|
+
|
|
150
|
+
amount_cents, type =
|
|
151
|
+
if mapping[:credit] || mapping[:debit]
|
|
152
|
+
if credit.to_i != 0
|
|
153
|
+
[credit.abs, :DEPOSIT]
|
|
154
|
+
elsif debit.to_i != 0
|
|
155
|
+
[debit.abs, :REMOVAL]
|
|
156
|
+
end
|
|
157
|
+
elsif signed.to_i != 0
|
|
158
|
+
signed.positive? ? [signed, :DEPOSIT] : [signed.abs, :REMOVAL]
|
|
159
|
+
end
|
|
160
|
+
return if amount_cents.nil? || amount_cents.zero?
|
|
161
|
+
|
|
162
|
+
note = build_note(cells, mapping)
|
|
163
|
+
return if note.empty?
|
|
164
|
+
|
|
165
|
+
Row.new(date: date, description: note, amount_cents: amount_cents, type: type, raw: cells)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def self.build_note(cells, mapping)
|
|
169
|
+
parts = [
|
|
170
|
+
cell_text(cells, mapping[:description]),
|
|
171
|
+
cell_text(cells, mapping[:description_completa])
|
|
172
|
+
]
|
|
173
|
+
moneymap = cell_text(cells, mapping[:moneymap])
|
|
174
|
+
parts << "Categoria: #{moneymap}" unless moneymap.empty?
|
|
175
|
+
parts.reject(&:empty?).join(" ")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def self.cell_text(cells, index)
|
|
179
|
+
return "" if index.nil?
|
|
180
|
+
|
|
181
|
+
cells[index].to_s.strip
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def self.parse_date(value)
|
|
185
|
+
return value.to_date if date_like?(value)
|
|
186
|
+
return Date.new(1899, 12, 30) + value.to_i if value.is_a?(Numeric)
|
|
187
|
+
|
|
188
|
+
text = value.to_s.strip
|
|
189
|
+
return if text.empty? || text == "-"
|
|
190
|
+
|
|
191
|
+
begin
|
|
192
|
+
Date.strptime(text, "%d/%m/%Y")
|
|
193
|
+
rescue ArgumentError
|
|
194
|
+
Date.parse(text)
|
|
195
|
+
end
|
|
196
|
+
rescue ArgumentError, TypeError
|
|
197
|
+
nil
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def self.date_like?(value)
|
|
201
|
+
!value.is_a?(String) && value.respond_to?(:to_date) && value.respond_to?(:year)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def self.parse_amount(value)
|
|
205
|
+
return 0 if value.nil? || value.to_s.strip.empty?
|
|
206
|
+
return (value.to_f * 100).round if value.is_a?(Numeric)
|
|
207
|
+
|
|
208
|
+
text = value.to_s.strip.gsub(/[€$£\s]/, "")
|
|
209
|
+
text = if text.include?(",") && text.include?(".")
|
|
210
|
+
text.delete(".").tr(",", ".")
|
|
211
|
+
elsif text.include?(",")
|
|
212
|
+
text.tr(",", ".")
|
|
213
|
+
else
|
|
214
|
+
text
|
|
215
|
+
end
|
|
216
|
+
(Float(text) * 100).round
|
|
217
|
+
rescue ArgumentError
|
|
218
|
+
0
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def self.row_cells(row)
|
|
222
|
+
cells = Array(row.raw).map { |cell| cell.to_s }
|
|
223
|
+
return cells unless cells.empty?
|
|
224
|
+
|
|
225
|
+
[row.description.to_s]
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def self.matching_cells(row)
|
|
229
|
+
row_cells(row)
|
|
230
|
+
end
|
|
231
|
+
private_class_method :read_sheet, :read_html, :html_spreadsheet?, :find_header, :find_column,
|
|
232
|
+
:build_row, :build_note, :cell_text, :parse_date, :parse_amount, :date_like?,
|
|
233
|
+
:require_spreadsheet_gems, :without_zip_date_warnings
|
|
234
|
+
end
|
|
235
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require "tty-screen"
|
|
5
|
+
|
|
6
|
+
require_relative "../config"
|
|
7
|
+
require_relative "../errors"
|
|
8
|
+
require_relative "../fineco_import"
|
|
9
|
+
require_relative "../fineco_xls"
|
|
10
|
+
require_relative "../row_preview"
|
|
11
|
+
|
|
12
|
+
module PortfolioPerformanceApi
|
|
13
|
+
class Import
|
|
14
|
+
class Fineco
|
|
15
|
+
PREVIEW_ROWS = 10
|
|
16
|
+
|
|
17
|
+
def initialize(session: Session.new)
|
|
18
|
+
@session = session
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def run(argv)
|
|
22
|
+
options = self.class.parse_argv(argv)
|
|
23
|
+
abort "Fineco XLS not found: #{options[:xls]}" unless File.file?(options[:xls])
|
|
24
|
+
|
|
25
|
+
backup = nil
|
|
26
|
+
uploaded = false
|
|
27
|
+
drive, loaded, backup = @session.download_and_backup
|
|
28
|
+
account = FinecoImport.find_account(loaded.client, options[:account])
|
|
29
|
+
abort "deposit account not found: #{options[:account]}" unless account
|
|
30
|
+
|
|
31
|
+
rows = FinecoXls.parse(options[:xls], skip_lines: options[:skip_lines])
|
|
32
|
+
result = FinecoImport.prepare(
|
|
33
|
+
loaded.client,
|
|
34
|
+
account,
|
|
35
|
+
rows,
|
|
36
|
+
security_specs: options[:match_security],
|
|
37
|
+
offset_specs: options[:match_offset_account],
|
|
38
|
+
exclude: options[:exclude]
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
skipped = Array(result.skipped)
|
|
42
|
+
if result.candidates.empty? && result.excluded.empty? && result.existing.empty? && skipped.empty?
|
|
43
|
+
warn "no Fineco transactions found in #{options[:xls]}"
|
|
44
|
+
return
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
print_summary(account, loaded.client, options, result, backup)
|
|
48
|
+
confirmed = result.candidates.any? || result.excluded.any? || result.existing.any?
|
|
49
|
+
if confirmed && preview(account.name, result, exclude: options[:exclude]) != "Y"
|
|
50
|
+
puts "#{account.name}: skipped"
|
|
51
|
+
return
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
imported = 0
|
|
55
|
+
repaired = 0
|
|
56
|
+
if confirmed
|
|
57
|
+
repaired = FinecoImport.repair_cross_entries!(loaded.client)
|
|
58
|
+
imported = if result.candidates.empty?
|
|
59
|
+
0
|
|
60
|
+
else
|
|
61
|
+
FinecoImport.append!(loaded.client, account, result.candidates, skipped: skipped)
|
|
62
|
+
end
|
|
63
|
+
if imported.positive? || repaired.positive?
|
|
64
|
+
@session.persist(loaded, drive)
|
|
65
|
+
uploaded = true
|
|
66
|
+
puts "Imported #{imported} transactions into #{loaded.path}" if imported.positive?
|
|
67
|
+
puts "Repaired #{repaired} cross-entry UUIDs" if repaired.positive?
|
|
68
|
+
puts "Uploaded portfolio #{File.basename(loaded.path)}"
|
|
69
|
+
elsif skipped.empty?
|
|
70
|
+
warn "nothing to import"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
review_discarded(account.name, skipped)
|
|
74
|
+
rescue Error, ArgumentError, RegexpError => error
|
|
75
|
+
abort error.message
|
|
76
|
+
ensure
|
|
77
|
+
Session.discard_backup(backup) unless uploaded
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def self.parse_argv(argv)
|
|
81
|
+
options = {
|
|
82
|
+
exclude: [],
|
|
83
|
+
skip_lines: FinecoXls.default_skip_lines,
|
|
84
|
+
match_security: [],
|
|
85
|
+
match_offset_account: []
|
|
86
|
+
}
|
|
87
|
+
parser = OptionParser.new do |opts|
|
|
88
|
+
opts.banner = "Usage: #{Import::COMMAND} fineco ACCOUNT [--file XLS] [XLS] [--exclude REGEXP]... [--skip-lines N] " \
|
|
89
|
+
"[--match-security [/REGEXP/VALUE]] [--match-offset-account /REGEXP/VALUE]"
|
|
90
|
+
opts.on("--file PATH", "--xls PATH", "Fineco .xls / .xlsx export (tab-completes as a file)") do |value|
|
|
91
|
+
options[:xls] = File.expand_path(unquote(value))
|
|
92
|
+
end
|
|
93
|
+
opts.on("--exclude REGEXP", "Drop Excel rows matching REGEXP in any column (repeatable)") do |value|
|
|
94
|
+
pattern = unquote(value)
|
|
95
|
+
options[:exclude] << pattern unless pattern.empty?
|
|
96
|
+
end
|
|
97
|
+
opts.on("--skip-lines N", Integer, "Preamble rows to skip (default IMPORT_FINECO_XLS_SKIP_LINES)") do |value|
|
|
98
|
+
options[:skip_lines] = value
|
|
99
|
+
end
|
|
100
|
+
opts.on("--match-security [SPEC]",
|
|
101
|
+
"Set security from capture or /REGEXP/VALUE (default: #{FinecoImport::DEFAULT_SECURITY_SPEC})") do |value|
|
|
102
|
+
spec = value.nil? ? FinecoImport::DEFAULT_SECURITY_SPEC : unquote(value)
|
|
103
|
+
FinecoImport.parse_match_spec(spec, flag: "--match-security") unless spec.empty?
|
|
104
|
+
options[:match_security] << spec unless spec.empty?
|
|
105
|
+
end
|
|
106
|
+
opts.on("--match-offset-account SPEC", "Set offset account from capture or /REGEXP/VALUE") do |value|
|
|
107
|
+
spec = unquote(value)
|
|
108
|
+
FinecoImport.parse_match_spec(spec, flag: "--match-offset-account")
|
|
109
|
+
options[:match_offset_account] << spec
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
rest = parser.parse(Array(argv).map(&:to_s)).map { |value| unquote(value) }
|
|
113
|
+
raise ArgumentError, "--skip-lines must be >= 0" if options[:skip_lines].negative?
|
|
114
|
+
|
|
115
|
+
xls = options[:xls]
|
|
116
|
+
if xls.to_s.empty?
|
|
117
|
+
raise ArgumentError, parser.banner if rest.size < 2
|
|
118
|
+
|
|
119
|
+
xls = File.expand_path(rest.pop)
|
|
120
|
+
end
|
|
121
|
+
account = rest.join(" ")
|
|
122
|
+
raise ArgumentError, parser.banner if account.empty? || xls.to_s.empty?
|
|
123
|
+
|
|
124
|
+
options.merge(account: account, xls: xls, exclude: options[:exclude].empty? ? nil : options[:exclude])
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def self.discard_backup(path)
|
|
128
|
+
Session.discard_backup(path)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def self.format_row(row, width: TTY::Screen.width, reason: nil)
|
|
132
|
+
amount = format("%.2f", (row.amount_cents / 100.0).round(2))
|
|
133
|
+
sign = row.type.to_s == "REMOVAL" ? "-" : "+"
|
|
134
|
+
prefix = "#{sign} #{row.date} #{FinecoImport.preview_type(row).ljust(16)} #{amount.rjust(8)} "
|
|
135
|
+
reason_part = reason.to_s.empty? ? "" : " #{reason}"
|
|
136
|
+
available = [Integer(width) - 2 - prefix.size - reason_part.size, 0].max
|
|
137
|
+
security = row.security.to_s.empty? ? "" : "S #{row.security} "
|
|
138
|
+
dest = row.offset_account.to_s.empty? ? "" : " → #{row.offset_account}"
|
|
139
|
+
middle = if dest.size >= available
|
|
140
|
+
truncate_note(dest, available)
|
|
141
|
+
elsif security.size + dest.size >= available
|
|
142
|
+
"#{truncate_note(security, available - dest.size)}#{dest}"
|
|
143
|
+
else
|
|
144
|
+
note = truncate_note(row.description.to_s, available - security.size - dest.size)
|
|
145
|
+
"#{security}#{note}#{dest}"
|
|
146
|
+
end
|
|
147
|
+
"#{prefix}#{middle}#{reason_part}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def self.preview_sections(import_rows, excluded_rows, existing_rows = [], exclude: nil, width: TTY::Screen.width)
|
|
151
|
+
sections = []
|
|
152
|
+
excluded_lines = Array(excluded_rows).map { |row| format_row(row, width: width) }
|
|
153
|
+
if exclude || excluded_lines.any?
|
|
154
|
+
sections << RowPreview::Section.new(
|
|
155
|
+
title: "EXCLUDED",
|
|
156
|
+
lines: excluded_lines,
|
|
157
|
+
page_size: PREVIEW_ROWS
|
|
158
|
+
)
|
|
159
|
+
end
|
|
160
|
+
existing_lines = Array(existing_rows).map { |row| format_row(row, width: width) }
|
|
161
|
+
if existing_lines.any?
|
|
162
|
+
sections << RowPreview::Section.new(
|
|
163
|
+
title: "EXISTING",
|
|
164
|
+
lines: existing_lines,
|
|
165
|
+
page_size: PREVIEW_ROWS
|
|
166
|
+
)
|
|
167
|
+
end
|
|
168
|
+
sections << RowPreview::Section.new(
|
|
169
|
+
title: "IMPORT",
|
|
170
|
+
lines: Array(import_rows).map { |row| format_row(row, width: width) },
|
|
171
|
+
counts: { create: Array(import_rows).size, update: 0 },
|
|
172
|
+
page_size: PREVIEW_ROWS
|
|
173
|
+
)
|
|
174
|
+
sections
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def self.discarded_sections(skipped, width: TTY::Screen.width)
|
|
178
|
+
lines = Array(skipped).map { |item| format_row(item.row, width: width, reason: item.reason) }
|
|
179
|
+
[
|
|
180
|
+
RowPreview::Section.new(
|
|
181
|
+
title: "DISCARDED",
|
|
182
|
+
lines: lines,
|
|
183
|
+
page_size: PREVIEW_ROWS
|
|
184
|
+
)
|
|
185
|
+
]
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def self.unquote(value)
|
|
189
|
+
text = value.to_s.strip
|
|
190
|
+
return text[1..-2] if text.match?(/\A(["']).*\1\z/)
|
|
191
|
+
|
|
192
|
+
text
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def self.truncate_note(text, max)
|
|
196
|
+
return "" if max <= 0
|
|
197
|
+
return text if text.length <= max
|
|
198
|
+
return "..."[0, max] if max <= 3
|
|
199
|
+
|
|
200
|
+
"#{text[0, max - 3]}..."
|
|
201
|
+
end
|
|
202
|
+
private_class_method :unquote, :truncate_note
|
|
203
|
+
|
|
204
|
+
private
|
|
205
|
+
|
|
206
|
+
def print_summary(account, client, options, result, backup)
|
|
207
|
+
last_date = FinecoImport.last_transaction_date(client, account.uuid)
|
|
208
|
+
puts "Account #{account.name} (#{account.currencyCode})"
|
|
209
|
+
puts last_date ? "Last portfolio transaction: #{last_date}" : "No dated transactions on this account yet"
|
|
210
|
+
Array(options[:exclude]).each { |pattern| puts "Exclude: #{pattern}" }
|
|
211
|
+
options[:match_security].each { |pattern| puts "Match security: #{pattern}" }
|
|
212
|
+
options[:match_offset_account].each { |spec| puts "Match offset account: #{spec}" }
|
|
213
|
+
puts "Already in portfolio: #{result.existing.size}" if result.existing.any?
|
|
214
|
+
puts "Discarded: #{result.skipped.size}" if Array(result.skipped).any?
|
|
215
|
+
puts "Backup: #{backup}"
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def preview(account_name, result, exclude: nil)
|
|
219
|
+
RowPreview.new(
|
|
220
|
+
account_name,
|
|
221
|
+
self.class.preview_sections(result.candidates, result.excluded, result.existing, exclude: exclude),
|
|
222
|
+
page_size: PREVIEW_ROWS,
|
|
223
|
+
prompt: "[#{account_name}] import (Y)es or Esc/Q to skip?",
|
|
224
|
+
choices: %w[Y]
|
|
225
|
+
).run
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def review_discarded(account_name, skipped)
|
|
229
|
+
return if Array(skipped).empty?
|
|
230
|
+
|
|
231
|
+
RowPreview.new(
|
|
232
|
+
account_name,
|
|
233
|
+
self.class.discarded_sections(skipped),
|
|
234
|
+
page_size: PREVIEW_ROWS,
|
|
235
|
+
prompt: "[#{account_name}] discarded #{skipped.size} — Enter/Q to continue",
|
|
236
|
+
choices: [],
|
|
237
|
+
acknowledge: true,
|
|
238
|
+
help: "↑/↓ row u/v page ±#{PREVIEW_ROWS} enter/q continue"
|
|
239
|
+
).run
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
require_relative "errors"
|
|
7
|
+
require_relative "config"
|
|
8
|
+
require_relative "drive_client"
|
|
9
|
+
require_relative "portfolio_store"
|
|
10
|
+
|
|
11
|
+
module PortfolioPerformanceApi
|
|
12
|
+
class Import
|
|
13
|
+
PROVIDERS = { "fineco" => "Fineco" }.freeze
|
|
14
|
+
|
|
15
|
+
def self.run(argv = [])
|
|
16
|
+
provider, *rest = Array(argv)
|
|
17
|
+
klass = provider_class(provider)
|
|
18
|
+
abort usage if klass.nil?
|
|
19
|
+
|
|
20
|
+
klass.new.run(rest)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
COMMAND = "bdgtr import:xls"
|
|
24
|
+
|
|
25
|
+
def self.usage
|
|
26
|
+
"Usage: #{COMMAND} PROVIDER ACCOUNT [--file XLS] [XLS] [--exclude REGEXP]... [--skip-lines N] " \
|
|
27
|
+
"[--match-security [/REGEXP/VALUE]] [--match-offset-account /REGEXP/VALUE]\n" \
|
|
28
|
+
"Providers: #{PROVIDERS.keys.join(", ")}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.provider_class(name)
|
|
32
|
+
const_name = PROVIDERS[name.to_s.downcase]
|
|
33
|
+
return if const_name.nil?
|
|
34
|
+
|
|
35
|
+
const_get(const_name)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.import_dir
|
|
39
|
+
File.expand_path("import", Dir.pwd)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.backup_path(path, at: Time.now)
|
|
43
|
+
directory = File.dirname(path)
|
|
44
|
+
basename = File.basename(path, ".portfolio")
|
|
45
|
+
File.join(directory, "#{basename}-import-backup-#{at.strftime("%Y%m%dT%H%M%S")}.portfolio")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class Session
|
|
49
|
+
def initialize(dir: Import.import_dir)
|
|
50
|
+
@dir = dir
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def download_and_backup
|
|
54
|
+
FileUtils.mkdir_p(@dir)
|
|
55
|
+
portfolio_id = Config.drive_file_id
|
|
56
|
+
abort "missing PORTFOLIO_GOOGLE_DRIVE_FILE_ID" if portfolio_id.to_s.empty?
|
|
57
|
+
abort "missing GOOGLE_SERVICE_ACCOUNT_JSON" if Config.service_account_json.to_s.empty?
|
|
58
|
+
|
|
59
|
+
drive = DriveClient.new(file_id: portfolio_id, scope: DriveClient::SYNC_SCOPE)
|
|
60
|
+
downloaded = drive.download
|
|
61
|
+
name = downloaded.dig(:meta, :name).to_s
|
|
62
|
+
name = "portfolio.portfolio" if name.empty?
|
|
63
|
+
path = File.join(@dir, File.basename(name))
|
|
64
|
+
File.binwrite(path, downloaded.fetch(:bytes))
|
|
65
|
+
backup = Import.backup_path(path)
|
|
66
|
+
FileUtils.cp(path, backup)
|
|
67
|
+
loaded = PortfolioStore.load(path, password: Config.portfolio_password)
|
|
68
|
+
[drive, loaded, backup]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def persist(loaded, drive)
|
|
72
|
+
PortfolioStore.save(loaded)
|
|
73
|
+
drive.upload(PortfolioStore.dump(loaded))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def self.discard_backup(path)
|
|
77
|
+
return if path.to_s.empty? || !File.file?(path)
|
|
78
|
+
|
|
79
|
+
File.delete(path)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
require_relative "import/fineco"
|