blib/lib/Finance/Tiller2QIF.pm

filter: Statementstmt 100% 178 / 178 Branchbran 93.1% 87 / 88 (7 uncoverable) Conditioncond 83.7% 36 / 43 MC/DCmcdc 72.0% 18 / 25 Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 1 totaltotal 94.2% 346 / 362 (7 uncoverable) CC 63 SCAR 35.2
CC
63
Cov
96%
run_cli
54.2
_confirm
6.1
_emit
2.1
CRAP 33.7
↑ Summary blib/lib/Finance/Tiller2QIF/Map.pm »
1 package Finance::Tiller2QIF;
2 # ABSTRACT: Convert Tiller CSV exports to QIF format
3 2 $Finance::Tiller2QIF::VERSION = '1.21';
4 2 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use feature qw/signatures postderef try/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Getopt::Long::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Finance::Tiller2QIF::ReadCSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Finance::Tiller2QIF::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Finance::Tiller2QIF::Util qw/vPrint/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Finance::Tiller2QIF::WriteQIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # use Data::Printer;
22  
23  
24 =pod
25  
26 =encoding utf8
27  
28 =head1 NAME
29  
30 tiller2qif
31  
32 Finance::Tiller2QIF
33  
34 =head1 DESCRIPTION
35  
36 Convert Tiller CSV exports to QIF for import into Financial software like GnuCash, KMyMoney, Quicken, HomeBank, Money Manager Ex and many others.
37  
38 =head1 SYNOPSIS
39  
40 # Command-line
41 tiller2qif run --input export.csv --db tiller.sqlite3 \
42 --output import.qif [--mapfile mapping.txt]
43  
44 # Programmatic — see PROGRAMMATIC USE below
45 use Finance::Tiller2QIF::ReadCSV;
46 use Finance::Tiller2QIF::Map;
47 use Finance::Tiller2QIF::WriteQIF;
48  
49 Finance::Tiller2QIF::ReadCSV::Ingest( 'export.csv', 'tiller.sqlite3' );
50 Finance::Tiller2QIF::Map::Map({ db_path => 'tiller.sqlite3', mapfile => 'mapping.txt' });
51 Finance::Tiller2QIF::WriteQIF::Emit( 'tiller.sqlite3', 'import.qif' );
52  
53 =head1 OVERVIEW
54  
55 Tiller Money (tillerapp.com) aggregates bank and credit-card transactions
56 into a Google Sheet and lets you export a CSV. This module ingests that CSV
57 into a SQLite database, optionally applies a category-mapping file to
58 translate Tiller's auto-assigned categories to match your accounts/categories, then emits a QIF file ready for import.
59  
60 The three phases can be run individually or together:
61  
62 =over 4
63  
64 =item B<ingest> — parse the CSV and load rows into the SQLite database.
65  
66 =item B<map> — apply a user-supplied mapping file that rewrites categories,
67 suppresses duplicates (card-payment credits), and assigns destination accounts.
68  
69 =item B<emit> — read unexported rows from the database and write a QIF file.
70  
71 =back
72  
73 =head1 INSTALLATION
74  
75 =head2 From CPAN
76  
77 cpan Finance::Tiller2QIF
78 # or
79 cpanm Finance::Tiller2QIF
80  
81 =head2 Perl Dependencies
82  
83 Runtime: Cpanel::JSON::XS, DateTime::Format::Flexible, DBD::SQLite, DBI,
84 Getopt::Long::Descriptive, Path::Tiny, Text::CSV
85  
86 Testing: Capture::Tiny, Test2::V0, Test2::Bundle::More, Test2::Tools::Exception
87  
88 =head3 On Debian/Ubuntu:
89  
90 All of Tiller2QIF’s dependencies are available through package management if you need to install to system Perl on Debian 13 or Ubuntu 26.04 or later.
91  
92 sudo apt install libpath-tiny-perl libtext-csv-perl libtest2-suite-perl libcapture-tiny-perl \
93 libdbi-perl libdbd-sqlite3-perl libgetopt-long-descriptive-perl \
94 libcpanel-json-xs-perl libdatetime-format-flexible-perl
95 sudo cpan install Finance::Tiller2QIF
96  
97 =head3 On Windows
98  
99 tiller2qif works with Strawberry Perl, after installing Strawberry Perl, install from CPAN.
100  
101 =head1 CLI COMMANDS
102  
103 The command word may be given either before or after the options; any other stray argument on the command line is an error.
104  
105 =over 4
106  
107 =item B<run> -- ingest, map, and emit in one step
108  
109 tiller2qif run --input export.csv --db tiller.sqlite3 \
110 --output import.qif [--mapfile mapping.txt]
111  
112 run will always create a checkpoint even when the flag is not set.
113  
114 =item B<interactive> -- edit and review a complete import before committing it
115  
116 tiller2qif interactive --input export.csv --db tiller.sqlite3 \
117 --output import.qif --mapfile mapping.txt --viewer 'code --wait'
118  
119 Creates an isolated session with a checkpoint of the original database, a fixed
120 copy of the input CSV, and a writable C<corrections.t2qsql> SQL file. The viewer opens
121 the read-only preview, mapping file (when supplied), and corrections together;
122 C<--multipreview> and C<--confirm> are not needed. With C<--viewer console>, the
123 preview is printed and the SQL path is reported for editing separately.
124  
125 Choose C<A> to apply saved edits and refresh, C<V> to reopen the files, C<E> to
126 export, or C<C> to cancel. End of input also cancels. Each refresh rebuilds from
127 the initial checkpoint and captured CSV, then runs C<beforemap>, the current
128 mapping rules, C<aftermap>, and session corrections, in that order. Hooks also
129 run when no mapping file is supplied. Changes do not accumulate across refreshes;
130 removing a correction removes its effect. Mapping edits can restore transactions
131 skipped earlier in this session, but not historical skips in the checkpoint.
132  
133 The preview includes full transaction IDs for targeted SQL, for example:
134  
135 UPDATE transactions SET mapped_category = 'Expenses:Food'
136 WHERE id = 'transaction-id-from-preview';
137  
138 Session SQL runs last, so it can override mapped categories, payees, memos,
139 amounts, accounts, and skip flags. Use C<mapped_category> to override the exported
140 category. Restoring a skipped transaction via SQL requires setting both
141 C<skipped = 0> and C<exported = 0>. SQL scripts may contain multiple statements;
142 C<ATTACH>, C<DETACH>, C<PRAGMA>, and explicit transaction statements are not
143 supported in interactive hooks or corrections. The session controls isolation.
144  
145 Save editor changes before choosing Apply. An export is refused after a failed
146 refresh or when saved mapping, hook, or correction files differ from the last
147 successful review. Invalid CSV rows, mapping errors, and SQL failures cannot
148 silently produce a partial import. Fix the files and refresh, or cancel; changes
149 to the captured CSV require starting a new session.
150  
151 The original database is locked against competing access for the session.
152 Only Export publishes the reviewed database (including export flags) and QIF.
153 Cancel or an error before publication leaves the original database and any
154 existing output QIF unchanged. Publication failures roll back both files.
155 Because two filesystem renames cannot be atomic together, a process crash during
156 publication can temporarily leave mixed outputs: a journal beside the database
157 restores the originals on the next C<interactive> invocation, or preserves both
158 outputs if the commit marker was already written. Restart C<interactive> before
159 using either output after an interrupted publication. Recovery refuses to
160 overwrite files modified externally since the interruption.
161  
162 Session artifacts are retained in the printed C<.tiller2qif-session-*> directory
163 beside the database, including checkpoint, CSV, SQL, previews, and working
164 databases. Mapping-file edits survive cancellation. Corrections are specific to
165 this session and are never automatically reused in a future run. These artifacts
166 contain financial data; remove them manually when no longer needed, and never
167 remove artifacts referenced by a pending C<.interactive.json> journal.
168  
169 Requires an existing database (create it with C<newdb>), writable database and
170 output directories, and a rollback-journal SQLite database; WAL mode is not
171 supported. Do not modify the original database or output from another process
172 during the session. Existing C<run>, C<emit --confirm>, and standalone C<preview>
173 behavior is unchanged.
174  
175 =item B<ingest> -- load CSV into the database
176  
177 tiller2qif ingest --input export.csv --db tiller.sqlite3
178  
179 =item B<map> -- apply category mapping rules
180  
181 tiller2qif map --db tiller.sqlite3 [--mapfile mapping.txt] \
182 [--beforemap before.sql] [--aftermap after.sql]
183  
184 =item B<preview> -- preview the records that would be emitted
185  
186 tiller2qif preview --db tiller.sqlite3
187  
188 Each transaction is shown as an untruncated block, with its ID, date, and account
189 on the first line and indented, labeled detail lines:
190  
191 ID: transaction-id | Date: 2026-09-27 | Account: Assets:Checking
192 Amount: -42.00
193 Payee: Corner Market
194 Memo: Full transaction memo
195 Category: [Groceries] Expenses:Groceries
196  
197 Long values are retained in full. Embedded newlines in detail fields use indented
198 continuation lines; newlines and tabs in the first-line fields are displayed as
199 escaped text so the transaction header stays on one line. Original categories
200 appear in brackets when mapping changes them.
201  
202 # read the preview in an editor or pager instead of the terminal
203 tiller2qif preview --db tiller.sqlite3 --viewer less
204  
205 # and open the mapping file beside it
206 tiller2qif preview --db tiller.sqlite3 --mapfile tiller.mapping \
207 --viewer 'code --wait' --multipreview
208  
209 =item B<emit> -- write QIF from the database
210  
211 tiller2qif emit --db tiller.sqlite3 --output import.qif [--qifdate mdy]
212  
213 =item B<newdb> -- initialise a new SQLite database
214  
215 tiller2qif newdb --db tiller.sqlite3
216  
217 =item B<newconfig> -- create a starter config file
218  
219 tiller2qif newconfig [--config ~/.config/tiller2qif.conf]
220  
221 =item B<checkconfig> -- check the merged values of cli arguments and config file
222  
223 # The verbose flag will run checkconfig before beginning any operations.
224 tiller2qif checkconfig [--config ~/.config/tiller2qif.conf]
225  
226 =item B<clean> -- remove checkpoint copies of the database
227  
228 Deletes all timestamped checkpoint files created by C<--checkpoint> or C<run>,
229 leaving the live database intact.
230  
231 tiller2qif clean --db tiller.sqlite3
232  
233 =item B<version> -- print the installed version number
234  
235 tiller2qif version
236  
237 =back
238  
239 =head1 OPTIONS
240  
241 All options can be supplied on the command line or in a JSON config file.
242 Use C<tiller2qif newconfig --config path/to/file.conf> to generate a starter
243 config file. A typical config file looks like:
244  
245 {
246 "input": "~/Downloads/mytillerdump.csv",
247 "output": "/tmp/tillerout.qif",
248 "db": "~/.data/tiller2qif.sqlite3",
249 "mapfile": "~/.config/tiller.mapping",
250 "viewer": "console",
251 "verbose": false,
252 "checkpoint": false,
253 "confirm": false,
254 "multipreview": false
255 }
256  
257 Pass the config file with C<--config>. Command-line options override config
258 file values.
259  
260 =over 4
261  
262 =item B<--config> Path to a JSON config file.
263  
264 =item B<--input> CSV export from Tiller.
265  
266 =item B<--output> QIF file to create.
267  
268 =item B<--db> SQLite database file used to store and transform transactions between phases.
269  
270 =item B<--mapfile> File containing category mapping rules. Optional; omitting it passes
271 transactions through with their original Tiller categories.
272  
273 =item B<--beforemap> Path to a SQL script to execute against the database immediately
274 before the mapping rules are applied. Useful for preprocessing transactions — for
275 example, renaming accounts or correcting data — in ways that affect which map rules
276 fire.
277  
278 =item B<--aftermap> Path to a SQL script to execute against the database immediately
279 after the mapping rules are applied. Useful for post-processing the mapped results —
280 for example, marking or transforming rows based on what the map phase produced.
281  
282 =item B<--qifdate> QIF output date format. Accepts C<ymd> (default, ISO 8601:
283 C<2026-04-24>), C<mdy> (US: C<04/24/2026>), or C<dmy> (European: C<24/04/2026>).
284 Use C<mdy> or C<dmy> when your financial software does not recognise ISO dates
285 during import.
286  
287 =item B<--checkpoint> Copy the database with a timestamp suffix before any operations.
288 The C<run> command always checkpoints, even without this flag.
289  
290 =item B<--confirm> Run preview before emit (including on C<run>) and prompt for
291 confirmation before writing the QIF file. When used with C<--checkpoint>, adds
292 a revert option (press C<r>) to restore the database to its checkpoint state,
293 useful if you want to undo changes made during ingest or mapping.
294  
295 =item B<--viewer> A program to display preview output instead of the default, C<console>. The command may include arguments if given as a quoted string, for example C<less>, C<gedit>, or C<"code --wait">. The preview is written to a read-only temporary file named C<tiller2qif-preview-*.t2qpv>, the command is run with that file's path appended.
296  
297 On Windows, the command runs through C<cmd.exe>. Use double quotes around program paths containing spaces. Preview, mapping and SQL file arguments are quoted for Windows automatically.
298  
299 =item B<--multipreview> Opens the mapping file in the preview along with the preview , so you can read the pending transactions and edit the rules that produced them side by side. Both files are passed to a single invocation of the viewer. This requires C<--viewer> and C<--mapfile>.
300  
301 =item B<--verbose> Print detailed progress information during each phase. Also
302 runs C<checkconfig> automatically before any operations begin.
303  
304 =item B<--version> Print the installed version number and exit.
305  
306 =back
307  
308 =head1 MAPPING FILE
309  
310 The mapping file controls how Tiller categories are translated into destination
311 account or category names and which transactions to suppress. Each non-comment
312 line has the form:
313  
314 [AccountFilter] field | pattern | destination
315  
316 Lines beginning with C<#> and blank lines are ignored. Rules are evaluated in
317 order; the first matching rule wins and no further rules are checked for that
318 transaction.
319  
320 =over 4
321  
322 =item B<AccountFilter> (optional) — a Perl regex in square brackets that
323 restricts the rule to transactions on matching accounts. Alternation works
324 naturally: C<[Checking|Savings]>. Omit to match all accounts.
325  
326 =item B<field> — the transaction field to test: C<category>, C<payee>,
327 C<memo>, C<date>, or C<amount>.
328  
329 =item B<pattern> — a Perl regex applied case-insensitively to the field value.
330  
331 For a simple pattern containing no C<|>, write it as-is:
332  
333 payee | Starbucks | Expenses:Coffee
334  
335 To allow setting a category with only an AccountFilter, use C<*> as the entire pattern to match against any field value:
336  
337 # Without an account filter all transactions will match!
338 [AccountFilter] any_matchable_field | * | new_category
339  
340 To use regex alternation (matching either of several values), enclose the
341 pattern in forward slashes:
342  
343 payee | /Starbucks|Dunkin/ | Expenses:Coffee
344  
345 To match a literal pipe character in the data, escape it with a backslash:
346  
347 payee | Cash\|App Payment | Expenses:Transfers
348  
349 Patterns are Perl regular expressions, so escape other regex metacharacters
350 when they should be literal (C<.>, C<*>, C<+>, the question mark, C<(>, C<)>,
351 C<[>, C<]>, C<$>, C<^>, C<\>, or C</> in a slash-delimited pattern). Apostrophes
352 have no special meaning and do not need escaping:
353  
354 payee | /^kaplan's new model$/ | Expenses:Food
355  
356 More complex regular expressions are supported when a simple pattern is not
357 enough. For example, this matches Kaplan, Kaplan's, or Kaplans followed by
358 “New” and an optional “Model”:
359  
360 payee | /kaplan(?:'s|s)? new(?: model)?/ | Expenses:Bakeries
361  
362 Test complex patterns carefully, to make sure they are interpreted as expected.
363  
364 =item C<source> — keep the original Tiller category unchanged.
365  
366 =item C<blank> — emit no category field in the QIF output.
367  
368 =item C<skip> — exclude the transaction from QIF output entirely (useful for
369 suppressing the credit-side of card payments that appear in both accounts).
370  
371 =back
372  
373 For double-entry programs such as GnuCash, destination is a full account name
374 (e.g. C<Expenses:Groceries>). For single-entry programs such as Quicken it is
375 a category name.
376  
377  
378 The optional C<default> line sets the fallback for transactions that match no
379 rule. It must appear as the last non-comment line:
380  
381 default | source
382  
383 If the C<default> line is omitted, unmatched transactions behave as
384 C<default | source>.
385  
386 =head2 EXAMPLES
387  
388 =over 4
389  
390 =item * Map by category
391  
392 category | Groceries | Expenses:Groceries
393  
394 =item * Map by payee with alternation (slash-delimited pattern)
395  
396 payee | /Starbucks|Dunkin/ | Expenses:Coffee
397  
398 =item * Map by payee, scoped to one account
399  
400 [Checking] payee | Payroll | Income:Salary
401  
402 =item * Scope to multiple accounts using alternation in the account filter
403  
404 [Checking|Savings] category | Transfer | skip
405  
406 =item * Skip card-payment credits on the card account
407  
408 [CapitalOne] category | Transfer | skip
409  
410 =item * Match a literal pipe character in a payee name
411  
412 payee | Cash\|App | Expenses:Transfers
413  
414 =item * Suppress category in QIF (no L field)
415  
416 category | Miscellaneous | blank
417  
418 =item * Default: leave unmatched transactions with their Tiller category
419  
420 default | source
421  
422 =back
423  
424 =head1 VS CODE EXTENSION
425  
426 The repository includes a Visual Studio Code extension, C<vscode-tiller-map/>, which highlights mapping files and preview files and completes destination account names from your chart of accounts. It is not published to the Marketplace, it can only be installed from a checkout of the repository.
427  
428 With B<--viewer code> and B<--multipreview> the actions B<preview> and B<run> will open the preview and map file in vscode
429  
430 =head2 Installation
431  
432 git clone https://github.com/brainbuz/tiller2qif
433 cp -r tiller2qif/vscode-tiller-map ~/.vscode/extensions/
434 # or, to track the checkout:
435 ln -s "$PWD/tiller2qif/vscode-tiller-map" ~/.vscode/extensions/
436  
437 Reload VS Code afterwards.
438  
439 =head2 Syntax Highlighting
440  
441 Files with C<.map> and C<.mapping> extensions are recognized as mappings, preview
442 files have the C<.t2qpv> extension. Session correction files use C<.t2qsql> and the
443 dedicated B<Tiller2QIF SQL> language mode, with SQL highlighting. Ordinary C<.sql>
444 file associations are unchanged. Preview files enable word wrapping by default.
445  
446 =head2 Completion Hinting
447  
448 In addition to completion of Tiller2QIF keywords you can also configure your Chart of Accounts for Completion! The extension's settings C<tiller2qifMap.coaPath> and C<tiller2qifMap.coaFormat> control this. Currently the only available coaFormat is C<gnucash-csv>.
449  
450 In B<Tiller2QIF SQL> files, the same chart of accounts supplies completions inside
451 single-quoted values assigned to or compared with C<account>, C<category>, and
452 C<mapped_category>. For example, type inside the quotes in
453 C<UPDATE transactions SET mapped_category = '' WHERE id = '...';>.
454 Suggestions appear while typing, after an opening quote or colon, or with
455 C<Ctrl+Space>. Account names containing apostrophes are escaped for SQL. Map-only
456 keywords such as C<skip> and C<source> are not offered as SQL values, and hints do
457 not appear in comments, payees, memos, or transaction IDs.
458  
459 For an older session's C<corrections.sql>, manually select B<Tiller2QIF SQL> using
460 VS Code's language-mode selector; no rename is necessary. Other Tiller SQL files
461 can opt in using the same language mode or the C<.t2qsql> extension. Refresh the
462 installed extension copy (if not symlinked to this checkout) and reload VS Code
463 after updating it.
464  
465 =head2 Row Colors
466  
467 C<tiller2qifMap.rowColors> controls row backgrounds in mappings and previews.
468 C<rainbow>, the default, gives transactions a repeating series of subtle background
469 colors; C<none> turns backgrounds off. Every line in a preview transaction block,
470 including multiline field continuations, receives the same color.
471  
472 =head1 Advanced Use
473  
474 You can write SQL scripts or use an interactive sqlite3 client to make changes between steps. For example your Tiller sheet might have an account "Checking", while your table of accounts has "Assets::Current Assets::Bank::Checking". With custom SQL you can keep the short name in Tiller even though mapping rules can't rename accounts.
475  
476 The C<--beforemap> and C<--aftermap> options allow SQL scripts to run immediately before
477 and after the map phase without having to break the workflow into separate commands.
478 This is the preferred way to preprocess or post-process transactions when using C<run>,
479 or C<map> as a single step.
480  
481 The C<preview> command is meant to be run between map and emit. You may run the steps individually (ingest, map, preview, emit), or use the C<--confirm> option to run preview before emit (including run). C<--viewer> lets you read the full transaction blocks in an editor or pager; combined with C<--confirm> it lets you review the pending transactions and then answer the export prompt.
482  
483 When using C<--confirm> with C<--checkpoint>, three choices Y=Yes N=No R=Revert are offered. No keeps the database state while not completing the export, Revert restores the database to the checkpoint in addition to aborting.
484  
485 While other CSV export sources are not directly supported, you can write a script to remap the fields for ingestion or just import into the table, and then use the map and emit stages to complete your export. If translating other CSV sources be aware that Tiller currently only provides it's data in the US 'MM/DD/YYYY' format, this program can also accept dates in ISO 8601 'YYYY-MM-DD'. Data is written into the SQLite database using the ISO 8601 format.
486  
487 =head1 PROGRAMMATIC USE
488  
489 C<Finance::Tiller2QIF> is primarily a CLI tool; the public functions exist to
490 support the command dispatcher. Programmatic users will likely use this module
491 as example code and call the sub-modules directly (C<Finance::Tiller2QIF::ReadCSV>,
492 C<Finance::Tiller2QIF::Map>, C<Finance::Tiller2QIF::WriteQIF>).
493  
494 Note that all functions expect C<db_path> as the database parameter. The CLI
495 normalises the C<--db> option to C<db_path> internally; programmatic callers
496 should use C<db_path> directly.
497  
498 =for Pod::Coverage run_cli
499  
500 =head1 AUTHOR
501  
502 John Karr E<lt>cpan@brainbuz.orgE<gt>
503  
504 =head1 LICENSE
505  
506 GPL version 3 or later.
507  
508 =cut
509  
510 # ---------------------------------------------------------------------------
511 # Public API
512 # ---------------------------------------------------------------------------
513  
514 22 ▶sub _ingest (%options) {
Subroutine _ingest: called CC=1 SCAR=0.0
515 Finance::Tiller2QIF::ReadCSV::Ingest( $options{input}, $options{db_path},
516 22 $options{verbose} );
517 }
518  
519 4 ▶sub _apply_map (%options) {
Subroutine _apply_map: called CC=1 SCAR=0.0
520 4 Finance::Tiller2QIF::Map::Map( \%options );
521 }
522  
523 10 ▶sub _emit (%options) {
Subroutine _emit: called CC=2 SCAR=7.6
524 Finance::Tiller2QIF::WriteQIF::Emit( $options{db_path}, $options{output},
525 10 ▶ $options{verbose}, $options{qifdate} // 'ymd' );
Condition50%1/2
$options{"qifdate"} // "ymd"
l!l
100
MC/DC0%0/1
$options{"qifdate"} // "ymd"
$options{"qifdate"}
Truth table50%1/2
A
A= $options{"qifdate"}
Aresult
11
00
526 }
527  
528 10 ▶sub _preview (%options) {
Subroutine _preview: called CC=2 SCAR=6.9
529 10 ▶ my @also = $options{multipreview} ? ( $options{mapfile} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$options{'multipreview'} ? : 2 8 10
530 Finance::Tiller2QIF::WriteQIF::Preview( $options{db_path}, $options{verbose},
531 10 $options{viewer}, @also );
532 }
533  
534 1 ▶sub _run (%options) {
Subroutine _run: called CC=1 SCAR=0.0
535 1 _ingest(%options);
536 1 _apply_map(%options);
537 1 _emit(%options);
538 }
539  
540 10 ▶sub _checkpoint($file) {
Subroutine _checkpoint: called CC=1 SCAR=0.0
541 10 my $new = "$file." . localtime()->datetime();
542 10 $new =~ s/[T:]/_/g;
543 10 path($file)->copy($new);
544 10 return $new;
545 }
546  
547 1 ▶sub _clean_checkpoints($file) {
Subroutine _clean_checkpoints: called CC=2 SCAR=6.9
548 2 ▶ my @checkpoints = grep { /\.\d{4}-\d{2}-\d{2}_\d{2}_\d{2}_\d{2}$/ } glob( $file . '.*' );
Subroutine BEGIN: called CC=1 SCAR=0.0
549 1 for my $cp (@checkpoints) {
550 2 path($cp)->remove;
551 2 say "Removed checkpoint: $cp";
552 }
553 1 return scalar @checkpoints;
554 }
555  
556 55 ▶sub _version_text {
Subroutine _version_text: called CC=2 SCAR=7.0
557 # dzil is in charge of the $VERSION variable which means it is undefined during development
558 # uncoverable branch true
559 # uncoverable branch false
560 2 ▶ my $v = do { no strict 'vars'; $VERSION // 'unversioned' };
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition50%1/2
$VERSION // "unversioned"
l!l
550
MC/DC0%0/1
$VERSION // "unversioned"
$VERSION
Truth table50%1/2
A
A= $VERSION
Aresult
11
00
561 55 return "Tiller2QIF VERSION: ${v}";
562 }
563  
564 10 ▶sub _confirm ( %options ) {
Subroutine _confirm: called CC=6 SCAR=18.0
565 # uncoverable branch true
566 # uncoverable branch false
567 10 ▶ if ( $options{confirm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"confirm"}) 3 7 10
568 3 my $count = _preview(%options);
569 3 say '-'x60;
570 3 say "${count} transaction(s) pending export.";
571 3 say '-'x60;
572 3 my $msg = "Complete export? Y/n: ";
573 3 ▶ if( $options{confirm} && $options{checkpoint}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"confirm"} and $options{"checkpoint"}) 1 2 3
Condition66%2/3
$options{"confirm"} and $options{"checkpoint"}
!ll&&!rl&&r
021
MC/DC50%1/2
$options{"confirm"} and $options{"checkpoint"}
$options{"confirm"} $options{"checkpoint"}
Truth table66%2/3
A and B
A= $options{"confirm"}B= $options{"checkpoint"}
ABresult
0X0
100
111
574 1 $msg = "Y=Yes N=No R=No and Revert to last Checkpoint\nComplete export? Y/n/r: ";
575 }
576 3 say $msg;
577 3 my $response = <STDIN>;
578 3 ▶ return 1 if $response =~ /^y/i; # user confirmed
Branch: 1 branch
BranchTrueFalseTotal
if $response =~ /^y/iu 1 2 3
579 2 ▶ if ( $response =~ /^r/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /^r/iu) 1 1 2
580 # copy checkpoint over db
581 1 path($options{checkpointfile})->copy($options{db_path});
582 }
583 2 return 0; # didn't confirm
584 }
585 7 return 1; # confirmation not requested return true as if confirmed.
586 }
587  
588 # ---------------------------------------------------------------------------
589 # CLI entry point — called by bin/tiller2qif
590 # ---------------------------------------------------------------------------
591  
592 2 my $VALID_COMMANDS = join '|',
593 qw(run interactive ingest map emit preview newdb newconfig checkconfig clean version);
594  
595 2 my $hlpmsg = <<'END_USAGE';
596 tiller2qif — Convert Tiller Money CSV exports to QIF format.
597  
598 Ingests a Tiller CSV into a SQLite database, optionally applies a
599 category-mapping file, then writes a QIF file ready for import into
600 GnuCash, KMyMoney, Quicken, HomeBank, and similar programs.
601  
602 Usage: tiller2qif <command> %o
603  
604 Commands: run | interactive | ingest | map | emit | preview | newdb | newconfig | checkconfig | version
605  
606 For the full manual: perldoc Finance::Tiller2QIF
607 END_USAGE
608  
609 2 my $badcmdhelp = <<'BADCMD';
610  
611 There was an error in your command line.
612 Common causes are:
613 * mistyping an option
614 * accidental text in the line
615  
616 BADCMD
617  
618 67 ▶sub run_cli {
Subroutine run_cli: called CC=54 SCAR=39.9
Pod undocumented
619 67 my $cmd = '';
620 # uncoverable branch true
621 # uncoverable branch false
622 67 ▶ if ( @ARGV && $ARGV[0] !~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and not $ARGV[0] =~ /^-/u) 59 8 67
Condition100%3/3
@ARGV and not $ARGV[0] =~ /^-/u
!ll&&!rl&&r
1759
MC/DC100%2/2
@ARGV and not $ARGV[0] =~ /^-/u
@ARGV not $ARGV[0] =~ /^-/u
Truth table100%3/3
A and not(B)
A= @ARGVB= not $ARGV[0] =~ /^-/u
ABresult
0X0
100
111
623 59 $cmd = lc shift @ARGV;
624 }
625  
626 67 my ( $opt, $usage ) = eval {
627 67 describe_options(
628 $hlpmsg,
629 [ 'config|c=s', "JSON config file" ],
630 [ 'checkpoint|C', "copy database with timestamp before run." ],
631 [ 'input|i=s', "CSV file to read (ingest, run, interactive)" ],
632 [ 'output|o=s', "QIF file to write (emit, run, interactive)" ],
633 [ 'db|d=s', "SQLite database file (all commands except newconfig)" ],
634 [ 'mapfile|f=s', "Category mapping file (map, run, interactive — optional)" ],
635 [ 'beforemap=s', "sql script to run prior to map" ],
636 [ 'aftermap=s', "sql script to run after map" ],
637 [ 'qifdate=s', "QIF date format: ymd (default), mdy, or dmy" ],
638 [ 'confirm', "run preview before emit and confirm export"],
639 [ 'viewer=s',
640 "external program to view preview output (default: console)" ],
641 [ 'multipreview',
642 "also open the mapping file in the preview viewer" ],
643 [ 'verbose|v', "Print detailed progress information" ],
644 [ 'version', "Print the installed version and exit", { shortcircuit => 1 } ],
645 [],
646 [ 'help|h', "Print usage and exit", { shortcircuit => 1 } ],
647 );
648 };
649 # uncoverable branch true
650 67 ▶ die $@, $badcmdhelp if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 67 67
651  
652 67 ▶ if ( $opt->help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->help) 2 65 67
653 2 say $usage;
654 2 return;
655 }
656  
657 65 ▶ if ( $opt->version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->version) 1 64 65
658 1 say _version_text();
659 1 return;
660 }
661  
662 # Getopt::Long permutes @ARGV, so a command word given after the options
663 # is left behind here rather than eaten as an option value; pick it up
664 # if the pre-parse grab above didn't already find one.
665 64 ▶ if ( !$cmd && @ARGV && $ARGV[0] !~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cmd and @ARGV and not $ARGV[0] =~ /^-/u) 3 61 64
Condition83%5/6
not $cmd and @ARGV and not $ARGV[0] =~ /^-/u
!ll&&!rl&&r
6103
not $cmd and @ARGV
!ll&&!rl&&r
5923
MC/DC66%2/3
not $cmd and @ARGV and not $ARGV[0] =~ /^-/u
not $cmd @ARGV not $ARGV[0] =~ /^-/u
Truth table75%3/4
not(A) and B and not(C)
A= not $cmdB= @ARGVC= not $ARGV[0] =~ /^-/u
ABCresult
0XX0
10X0
1100
1111
666 3 $cmd = lc shift @ARGV;
667 }
668  
669 64 ▶ die "tiller2qif: unexpected argument(s): @ARGV\n$badcmdhelp" if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 1 63 64
670  
671 63 ▶ if ( !$cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 2 61 63
672 2 die
673 "Command Missing! Valid commands: $VALID_COMMANDS\nFor help: tiller2qif --help\n";
674 }
675  
676 61 ▶ die qq{Unknown command '$cmd'.
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ /^(?:$VALID_COMMANDS)$/u 2 59 61
677 Valid commands: $VALID_COMMANDS
678 for help tiller2qif --help or perldoc tiller2qif
679 \n}
680 unless $cmd =~ /^(?:$VALID_COMMANDS)$/;
681  
682 59 ▶ if ( $cmd eq 'version' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "version") 1 58 59
683 1 say _version_text();
684 1 return;
685 }
686  
687 58 ▶ if ( $cmd eq 'newconfig' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "newconfig") 2 56 58
688 2 ▶ die "newconfig requires --config\n" unless $opt->config;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->config 1 1 2
689 1 vPrint( $opt->verbose, _version_text() );
690 # say "Creating config file: " . $opt->config if $opt->verbose;
691 1 vPrint( $opt->verbose, "Creating config file: " . $opt->config );
692 1 Finance::Tiller2QIF::Util::InitConfig( $opt->config );
693 1 say "Config file created: " . $opt->config;
694 1 return;
695 }
696  
697 # Precedence: defaults < config file < CLI args
698 56 my %options = (
699 input => undef,
700 output => undef,
701 db => undef,
702 mapfile => undef,
703 beforemap => undef,
704 aftermap => undef,
705 qifdate => 'ymd',
706 verbose => 0,
707 checkpoint => 0,
708 confirm => 0,
709 viewer => 'console',
710 multipreview => 0,
711 );
712  
713 56 ▶ if ( $opt->config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->config) 4 52 56
714 4 my $config = Cpanel::JSON::XS->new->utf8->relaxed->decode(
715 path( $opt->config )->slurp_utf8 );
716 4 for my $key ( keys %options ) {
717 48 ▶ $options{$key} = $config->{$key} if defined $config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$key} 9 39 48
718 }
719 }
720  
721 56 for my $key ( keys %options ) {
722 672 my $val = $opt->$key();
723 672 ▶ $options{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 165 507 672
724 }
725 56 ▶ $options{checkpoint} = 1 if $cmd eq 'run';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "run" 5 51 56
726  
727 # Only validate the viewer when this command will actually use it —
728 # otherwise a blank/odd viewer in a shared config file would block
729 # unrelated commands like newdb or ingest that never touch it.
730 56 ▶ if ( $cmd eq 'preview' || $cmd eq 'interactive' || ( $cmd =~ /^(?:emit|run)$/ && $options{confirm} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "preview" or $cmd eq "interactive" or $cmd =~ /^(?:emit|run)$/u and $options{"confirm"}) 36 20 56
Condition100%9/9
$cmd eq "preview" or $cmd eq "interactive" or $cmd =~ /^(?:emit|run)$/u and $options{"confirm"}
l!l&&r!l&&!r
33320
$cmd eq "preview" or $cmd eq "interactive"
l!l&&r!l&&!r
112223
$cmd =~ /^(?:emit|run)$/u and $options{"confirm"}
!ll&&!rl&&r
1283
MC/DC100%4/4
$cmd eq "preview" or $cmd eq "interactive" or $cmd =~ /^(?:emit|run)$/u and $options{"confirm"}
$cmd eq "preview" $cmd eq "interactive" $cmd =~ /^(?:emit|run)$/u $options{"confirm"}
Truth table100%5/5
A or B or C and D
A= $cmd eq "preview"B= $cmd eq "interactive"C= $cmd =~ /^(?:emit|run)$/uD= $options{"confirm"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
731 die "--viewer must name a program or 'console'\n"
732 36 ▶ unless defined $options{viewer} && $options{viewer} =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"viewer"} and $options{"viewer"} =~ /\S/u 2 34 36
Condition66%2/3
defined $options{"viewer"} and $options{"viewer"} =~ /\S/u
!ll&&!rl&&r
0234
MC/DC50%1/2
defined $options{"viewer"} and $options{"viewer"} =~ /\S/u
defined $options{"viewer"} $options{"viewer"} =~ /\S/u
Truth table66%2/3
A and B
A= defined $options{"viewer"}B= $options{"viewer"} =~ /\S/u
ABresult
0X0
100
111
733  
734 34 ▶ if ( $options{multipreview} && $cmd ne 'interactive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"multipreview"} and $cmd ne "interactive") 4 30 34
Condition66%2/3
$options{"multipreview"} and $cmd ne "interactive"
!ll&&!rl&&r
3004
MC/DC50%1/2
$options{"multipreview"} and $cmd ne "interactive"
$options{"multipreview"} $cmd ne "interactive"
Truth table66%2/3
A and B
A= $options{"multipreview"}B= $cmd ne "interactive"
ABresult
0X0
100
111
735 die "--multipreview requires --viewer, the console can't open files\n"
736 4 ▶ if lc $options{viewer} eq 'console';
Branch: 1 branch
BranchTrueFalseTotal
if lc $options{"viewer"} eq "console" 1 3 4
737 3 ▶ die "--multipreview requires --mapfile\n" unless $options{mapfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"mapfile"} 1 2 3
738 }
739 }
740  
741 52 vPrint( $options{verbose}, _version_text() );
742  
743 52 ▶ $options{db_path} = delete $options{db} if defined $options{db};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"db"} 49 3 52
744  
745 52 ▶ if ( $cmd eq 'newdb' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "newdb") 4 48 52
746 4 ▶ die "newdb requires --db\n" unless $options{db_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"db_path"} 1 3 4
747 3 vPrint( $opt->verbose, "Creating database: " . $options{db_path} );
748 # say "Creating database: " . $options{db_path} if $opt->verbose;
749 3 Finance::Tiller2QIF::Util::InitDB( $options{db_path} );
750 3 say "Database initialized: " . $options{db_path};
751 3 return;
752 }
753  
754 48 ▶ if ( $cmd eq 'clean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "clean") 2 46 48
755 2 ▶ die "clean requires --db\n" unless $options{db_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"db_path"} 1 1 2
756 1 my $removed = _clean_checkpoints( $options{db_path} );
757 1 say "Removed $removed checkpoint(s)";
758 1 return;
759 }
760  
761 46 my @missing;
762 46 ▶ push @missing, '--input' if $cmd =~ /^(?:ingest|run|interactive)$/ && !$options{input};
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^(?:ingest|run|interactive)$/u and not $options{"input"} 1 45 46
Condition100%3/3
$cmd =~ /^(?:ingest|run|interactive)$/u and not $options{"input"}
!ll&&!rl&&r
14311
MC/DC100%2/2
$cmd =~ /^(?:ingest|run|interactive)$/u and not $options{"input"}
$cmd =~ /^(?:ingest|run|interactive)$/u not $options{"input"}
Truth table100%3/3
A and not(B)
A= $cmd =~ /^(?:ingest|run|interactive)$/uB= not $options{"input"}
ABresult
0X0
100
111
763 46 ▶ push @missing, '--db' if !$options{db_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"db_path"} 1 45 46
764 46 ▶ push @missing, '--output' if $cmd =~ /^(?:emit|run|interactive)$/ && !$options{output};
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^(?:emit|run|interactive)$/u and not $options{"output"} 1 45 46
Condition100%3/3
$cmd =~ /^(?:emit|run|interactive)$/u and not $options{"output"}
!ll&&!rl&&r
13321
MC/DC100%2/2
$cmd =~ /^(?:emit|run|interactive)$/u and not $options{"output"}
$cmd =~ /^(?:emit|run|interactive)$/u not $options{"output"}
Truth table100%3/3
A and not(B)
A= $cmd =~ /^(?:emit|run|interactive)$/uB= not $options{"output"}
ABresult
0X0
100
111
765  
766 46 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 3 43 46
767 3 die "tiller2qif $cmd: missing required option(s): "
768 . join( ', ', @missing ) . "\n";
769 }
770  
771 43 ▶ if ( $cmd eq 'interactive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "interactive") 22 21 43
772 22 require Finance::Tiller2QIF::Interactive;
773 22 return Finance::Tiller2QIF::Interactive::Run(\%options);
774 }
775  
776 21 ▶ if ( $options{verbose} || $cmd eq 'checkconfig' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"verbose"} or $cmd eq "checkconfig") 3 18 21
Condition100%3/3
$options{"verbose"} or $cmd eq "checkconfig"
l!l&&r!l&&!r
2118
MC/DC100%2/2
$options{"verbose"} or $cmd eq "checkconfig"
$options{"verbose"} $cmd eq "checkconfig"
Truth table100%3/3
A or B
A= $options{"verbose"}B= $cmd eq "checkconfig"
ABresult
1X1
011
000
777 3 Finance::Tiller2QIF::Util::CheckConfig(%options);
778 }
779  
780 21 ▶ if ( $options{checkpoint} || $cmd eq 'run' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"checkpoint"} or $cmd eq "run") 7 14 21
Condition66%2/3
$options{"checkpoint"} or $cmd eq "run"
l!l&&r!l&&!r
7014
MC/DC50%1/2
$options{"checkpoint"} or $cmd eq "run"
$options{"checkpoint"} $cmd eq "run"
Truth table66%2/3
A or B
A= $options{"checkpoint"}B= $cmd eq "run"
ABresult
1X1
011
000
781 7 $options{checkpointfile} = _checkpoint( $options{db_path} );
782 }
783  
784 21 ▶ if ( $cmd =~ /^(?:ingest|run)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^(?:ingest|run)$/u) 8 13 21
785 8 vPrint( $options{verbose}, "Ingesting CSV: " . $options{input} );
786  
787 8 my $newitems = _ingest(%options);
788 8 say "Ingested: ${newitems} transactions from: " . $options{input};
789 }
790  
791 21 ▶ if ( $cmd =~ /^(?:map|run)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^(?:map|run)$/u) 5 16 21
792 5 ▶ if ( $options{mapfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'mapfile'}) { } 2 3 5
793 2 ▶ say "Applying mapping: " . $options{mapfile} if $options{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"verbose"} 1 1 2
794 2 _apply_map(%options);
795 2 say "Mapping applied: " . $options{mapfile};
796 }
797 else {
798 3 say "No mapfile provided, skipping mapping phase";
799 }
800 }
801  
802 21 ▶ if ( $cmd =~ /^(?:emit|run)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^(?:emit|run)$/u) 10 11 21
803 # uncoverable branch true
804 # uncoverable branch false
805 10 ▶ return unless _confirm ( %options );
Branch: 1 branch
BranchTrueFalseTotal
unless _confirm(%options) 2 8 10
806 8 vPrint( $options{verbose}, "Writing QIF: " . $options{output} );
807 8 my $changed = _emit(%options);
808 8 say "QIF written: ${\ $options{output}}, ${changed} records emitted!";
809 }
810  
811 19 ▶ if ( $cmd eq 'preview' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "preview") 7 12 19
812 7 my $count = _preview(%options);
813 5 say "${count} transaction(s) pending export.";
814 }
815 }
816  
817 2 1;
818  
819 =for Pod::Coverage run_cli
↑ Summary blib/lib/Finance/Tiller2QIF/Map.pm »