| 1 | package Finance::Tiller2QIF::Util; | ||||||||||||
| 2 | # ABSTRACT: Utility functions for Tiller2QIF processing | ||||||||||||
| 3 | 16 | $Finance::Tiller2QIF::Util::VERSION = '1.23'; | |||||||||||
| 4 | =head1 DESCRIPTION | ||||||||||||
| 5 | |||||||||||||
| 6 | Provides utility functions for initializing the SQLite database and configuration files. | ||||||||||||
| 7 | |||||||||||||
| 8 | =head1 FUNCTIONS | ||||||||||||
| 9 | |||||||||||||
| 10 | =head2 InitDB | ||||||||||||
| 11 | |||||||||||||
| 12 | Finance::Tiller2QIF::Util::InitDB( $db_path ); | ||||||||||||
| 13 | |||||||||||||
| 14 | Create a new SQLite database at C<$db_path> with the transactions table schema. Dies if unable to create the database. | ||||||||||||
| 15 | |||||||||||||
| 16 | =head2 InitConfig | ||||||||||||
| 17 | |||||||||||||
| 18 | Finance::Tiller2QIF::Util::InitConfig( $config_path ); | ||||||||||||
| 19 | |||||||||||||
| 20 | Create a starter JSON configuration file at C<$config_path> with commented examples of all available options. Dies if unable to write the file. | ||||||||||||
| 21 | |||||||||||||
| 22 | =head2 CheckConfig | ||||||||||||
| 23 | |||||||||||||
| 24 | Finance::Tiller2QIF::Util::CheckConfig( %options ); | ||||||||||||
| 25 | |||||||||||||
| 26 | Prints the current merged option values to STDOUT and checks that referenced | ||||||||||||
| 27 | files exist and are readable/writable. Called automatically when C<--verbose> | ||||||||||||
| 28 | is set or when the C<checkconfig> command is used. | ||||||||||||
| 29 | |||||||||||||
| 30 | =head2 vPrint | ||||||||||||
| 31 | |||||||||||||
| 32 | vPrint( $verbose, @messages ); | ||||||||||||
| 33 | |||||||||||||
| 34 | Prints each message in C<@messages> to STDOUT only when C<$verbose> is true. | ||||||||||||
| 35 | Exported on request; used internally by other Finance::Tiller2QIF modules. | ||||||||||||
| 36 | |||||||||||||
| 37 | =head1 AUTHOR | ||||||||||||
| 38 | |||||||||||||
| 39 | John Karr E<lt>cpan@brainbuz.orgE<gt> | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 LICENSE | ||||||||||||
| 42 | |||||||||||||
| 43 | GPL version 3 or later. | ||||||||||||
| 44 | |||||||||||||
| 45 | =cut | ||||||||||||
| 46 | |||||||||||||
| 47 | 16 | ▶ | use 5.036; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | |||||||||||||
| 49 | 16 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 16 | our @EXPORT_OK = qw( vPrint ); | |||||||||||
| 51 | |||||||||||||
| 52 | 16 | ▶ | use Path::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 16 | ▶ | use Text::CSV; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 16 | ▶ | use Finance::Tiller2QIF::DB qw( connect_db ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 16 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 16 | ▶ | use warnings FATAL => 'utf8'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 16 | ▶ | use open ':std', ':encoding(UTF-8)'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | 16 | ▶ | use feature qw/signatures postderef/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | 16 | my $newDB = q/ | |||||||||||
| 61 | CREATE TABLE IF NOT EXISTS transactions ( | ||||||||||||
| 62 | id TEXT PRIMARY KEY, -- Transaction ID from CSV | ||||||||||||
| 63 | account TEXT NOT NULL, | ||||||||||||
| 64 | date TEXT NOT NULL, -- YYYY-MM-DD | ||||||||||||
| 65 | amount REAL NOT NULL, | ||||||||||||
| 66 | payee TEXT, | ||||||||||||
| 67 | memo TEXT, | ||||||||||||
| 68 | category TEXT, -- source category from Tiller (immutable after ingest) | ||||||||||||
| 69 | mapped_category TEXT, -- destination category set by Map phase; NULL = not yet mapped | ||||||||||||
| 70 | check_number TEXT, -- NULL when absent or zero-filled by Tiller | ||||||||||||
| 71 | skipped INTEGER NOT NULL DEFAULT 0, -- 1 = suppressed by map rule; excluded from QIF | ||||||||||||
| 72 | exported INTEGER NOT NULL DEFAULT 0 | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | /; | ||||||||||||
| 75 | |||||||||||||
| 76 | 126 | ▶ | sub InitDB ($db_path) { | ||||||||||
|
Subroutine
InitDB: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||
| 77 | 126 | my $dbh = connect_db($db_path); | |||||||||||
| 78 | # uncoverable branch true | ||||||||||||
| 79 | 125 | ▶ | $dbh->do($newDB) || die "unable to initialize database $!\n"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 125 | $dbh->disconnect; | |||||||||||
| 81 | 125 | say "Database $db_path created"; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 16 | my $example = q| | |||||||||||
| 85 | { | ||||||||||||
| 86 | # Uncomment and customize values for your needs | ||||||||||||
| 87 | # file format is json with comments | ||||||||||||
| 88 | # tiller2qif ignores everything after '#' on a line | ||||||||||||
| 89 | |||||||||||||
| 90 | # "input": "~/Downloads/mytillerdump.csv", | ||||||||||||
| 91 | # "output": "/tmp/tillerout.qif", | ||||||||||||
| 92 | # "db": "~/.data/tiller2qif.sqlite3", | ||||||||||||
| 93 | # "mapfile": "~/data/tiller2qif.mapfile", | ||||||||||||
| 94 | # "beforemap" : "~/some.sql", | ||||||||||||
| 95 | # "aftermap" : "~/other.sql", | ||||||||||||
| 96 | |||||||||||||
| 97 | # QIF date format: ymd (ISO, default), mdy (US), dmy (European) | ||||||||||||
| 98 | # "qifdate": "ymd", | ||||||||||||
| 99 | |||||||||||||
| 100 | # Where preview output goes: console (default) prints to the terminal, | ||||||||||||
| 101 | # anything else names an external program, eg "less" or "code --wait" | ||||||||||||
| 102 | # "viewer": "console", | ||||||||||||
| 103 | |||||||||||||
| 104 | # Open the mapping file in the viewer alongside the preview. | ||||||||||||
| 105 | # Requires viewer and mapfile. | ||||||||||||
| 106 | # "multipreview": true, | ||||||||||||
| 107 | |||||||||||||
| 108 | "verbose": false, | ||||||||||||
| 109 | "checkpoint": false, | ||||||||||||
| 110 | "confirm": false | ||||||||||||
| 111 | } | ||||||||||||
| 112 | |; | ||||||||||||
| 113 | |||||||||||||
| 114 | 2 | ▶ | sub InitConfig ($config) { | ||||||||||
|
Subroutine
InitConfig: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||
| 115 | # devel coverage errors | ||||||||||||
| 116 | # uncoverable branch true | ||||||||||||
| 117 | # uncoverable branch false | ||||||||||||
| 118 | 2 | ▶ | path($config)->spew_utf8($example) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | || die "unable to create $config : $! \n"; | ||||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 293 | ▶ | sub vPrint ( $verbose, @messages ) { | ||||||||||
|
Subroutine
vPrint: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 123 | 293 | ▶ | if ($verbose) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | 14 | for (@messages) { say $_ } | |||||||||||
| 125 | } | ||||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | 11 | ▶ | sub CheckConfig (%options) { | ||||||||||
|
Subroutine
CheckConfig: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||
| 129 | |||||||||||||
| 130 | 11 | say "Options Provided:"; | |||||||||||
| 131 | 11 | for ( sort keys %options ) { | |||||||||||
| 132 | 44 | ▶ | say sprintf " %-14s : %s", $_, $options{$_} // '(unset)'; | ||||||||||
|
Condition100%2/2
$options{$_} // "(unset)"
MC/DC100%1/1
$options{$_} // "(unset)"
$options{$_}
Truth table100%2/2
A
A= $options{$_}
| |||||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | 11 | say ''; | |||||||||||
| 136 | 11 | ▶ | if ( defined $options{db_path} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 4 | ▶ | unless ( -r $options{db_path} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | 1 | say "Problem: db '${options{db_path}}' does not exist or can't be read."; | |||||||||||
| 139 | } | ||||||||||||
| 140 | } | ||||||||||||
| 141 | 11 | ▶ | if ( defined $options{input} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 3 | ▶ | unless ( -r $options{input} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 1 | say | |||||||||||
| 144 | "Problem: input '${options{input}}' does not exist or can't be read."; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | } | ||||||||||||
| 147 | 11 | ▶ | if ( defined $options{mapfile} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 2 | ▶ | unless ( -r $options{mapfile} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 1 | say | |||||||||||
| 150 | "Problem: mapfile '${options{mapfile}}' does not exist or can't be read."; | ||||||||||||
| 151 | } | ||||||||||||
| 152 | } | ||||||||||||
| 153 | 11 | ▶ | if ( defined $options{output} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | 7 | ▶ | if ( -r $options{output} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 2 | say "Alert ${options{output}} already exists and would be overwritten"; | |||||||||||
| 156 | say "Problem ${options{output}} is not writable" | ||||||||||||
| 157 | 2 | ▶ | unless ( -w $options{output} ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | } | ||||||||||||
| 159 | 7 | ▶ | unless ( -w path( $options{output} )->parent ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | 1 | say "Problem ${options{output}} parent directory is not writable"; | |||||||||||
| 161 | } | ||||||||||||
| 162 | } | ||||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | 16 | 1; | |||||||||||