-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV2Ledger.pl
executable file
·672 lines (512 loc) · 19.3 KB
/
CSV2Ledger.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/usr/bin/perl
######################################################################
# CSV2Ledger.pl
#
# This script reads a CSV file and outputs it in Ledger format. It is
# useful for converting data from financial institutions into your
# Ledger.
#
# Optional processing matches records by regexp and can perform
# substitutions, as well as choose a source and destination account
# for that record. This can automatically rename vendors, and assign
# records to specific accounts.
#
# Written by: Russell Adams <[email protected]>
# License: GPLv2
#
#
# Ledger entry model
#
# CSV from bank:
#
# 2008/08/08,2134,Exxon,20
#
# %Transaction = (
# 'Date' = '2008/08/08' # CSV
# , 'Cleared' = '' # Default
# , 'CheckNum' = '2134' # CSV
# , 'Desc' = 'Exxon' # CSV
# , 'Source' = 'Liabilities:VISA' # Via Lookup
# , 'Dest' = 'Expenses:Auto:Gas' # Via Lookup
# , 'Amount' = 20 # CSV
# , 'MD5Sum' = '.......' # Generated
# , 'CSV' = 'csv data' # Generated
# );
#
# becomes
#
# 2008/08/08 (2134) Exxon
# Liabilities:VISA $20.00
# Expenses:Auto:Gas
# (CSV2Ledger:MD5Sum) ; MD5Sum: .......
# (CSV2Ledger:CSV) ; CSV: csv data
#
#
#
#
#######################################################################
# New TT support on a per-file basis, now the FileMatches.yaml file
# can contain a TT template to override the default.
#
# The variables available are from TempTrans, including:
# $TempTrans: {
# Amount => -90.00,
# CSV => '"12/21/2009","12/19/2009","Debit","CAB SERVIC HOUSTON, TX","Travel",$90.00',
# CheckNum => '(12/21/2009) ',
# Cleared => '',
# Date => '2009/12/19',
# Desc => 'CAB SERVIC HOUSTON, TX',
# Dest => 'UnknownImported',
# FilesList => '/home/rladams/ScannedDocuments/Receipts/20091218_Taxi_90_00.jpg',
# MD5Sum => '8cba5105c58c415bd4b1d6acb15b20bf',
# Source => 'Source:AISVisa'
# }
#
##################################################
# Always
use strict;
use warnings;
# Modules
use YAML; # Dispatch Table Loading
use Getopt::Long; # Options processing
use File::Find;
use Date::Format; # Date conversion
use Date::Parse;
use Date::Calc qw/Localtime Add_Delta_Days/;
use Smart::Comments -ENV, "###"; # Ignore my headers
use Digest::MD5 qw/md5_hex/; # Help detect duplicates later
# Globals
use Template;
my $TT = Template->new();
use Text::CSV; # Required for import
my $CSV = Text::CSV->new();
my @Data;
my %Options;
my $Total = 0;
my $Skipped = 0;
my $MatchedFiles = 0;
##################################################
# Defaults
#
# Different CSV files use different fields.
# These are the defaults from my financial
# institution, and can be specified via arguments.
%Options = (
# RecordRE is used to match lines to import, and exclude any comments or junk, Mine all start with the date
'RecordRE' => "^[0-9][0-9]\/[0-9][0-9]\/",
# CSV field layout, a comma delimited list of fields being read from each line, This is used to match position to label
'CSVFields' =>
"Posted Date,Check Number,Description,Transaction Amount,Principal Amount,Interest Amount,Balance,Fee Amount",
'DateField' => "Posted Date", # Which field name is our date?
'CheckField' => "Check Number", # Which field is a check number?
'DescField' => "Description", # Which field is the description?
'AmountField' => "Transaction Amount", # Which field is the amount?
'DefaultSource' => "Assets:Unknown", # What default source account?
# Include default files now if they exist in the current dir
'FileMatchFile' => -e "./.FileMatches.yaml"
? "./.FileMatches.yaml"
: undef,
'AccountMatchFile' => -e "./.AccountMatches.yaml"
? "./.AccountMatches.yaml"
: undef,
'PreProcessFile' => -e "./.Preprocess.yaml"
? "./.Preprocess.yaml"
: undef,
# Configurable in FileMatches.yaml, like this example with metadata!
# TxnOutputTemplate: "[% USE format %][% ToDollars = format('%30.2f') %][% Date %] [% Cleared %][% CheckNum %][% Desc %]\n ; RECEIPT: [% FilesList %]\n ; ER: Unfiled\n ; PROJECT: Unfiled\n ; OWNER: Adams, Russell\n [% Source %] [% ToDollars(Amount) %]\n [% Dest %]\n (CSV2Ledger:MD5Sum) ; MD5Sum: [% MD5Sum %]\n (CSV2Ledger:CSV) ; CSV: [% CSV %]\n\n\n"
'TxnOutputTemplate' => <<EOF
[% USE format %][% ToDollars = format('\$%.2f') %][% IndentDollars = format('%40s') %][% Date %] [% Cleared %][% CheckNum %][% Desc %]
; RECEIPT: [% FilesList %]
; CSV: [% CSV %]
; MD5Sum: [% MD5Sum %]
[% Source %] [% IndentDollars(FormattedAmount) %]
[% Dest %]
; ER: [% ER %]
; PROJECT: [% PROJECT %]
; CATEGORY: [% Category %]
EOF
,
# 2.6 style
# 'TxnOutputTemplate' => " [% USE format %][% ToDollars = format('%30.2f') %]
# [% Date %] [% Cleared %][% CheckNum %][% Desc %] [% FilesList %]
# [% Source %] [% ToDollars(Amount) %]
# [% Dest %]
# (CSV2Ledger:MD5Sum) ; MD5Sum: [% MD5Sum %]
# (CSV2Ledger:CSV) ; CSV: [% CSV %]
# ",
# Simple options
'ShowHelp' => undef,
'Cleared' => undef,
'Negate' => undef,
'DetectDups' => undef,
'FindFiles' => undef,
'FindDir' => ".",
'InputFile' => undef,
'OutputDir' => ".",
'OutputFile' => "./CSV2Ledger.dat",
'MD5Cache' => "./.MD5Sum.cache",
'FuzzyDates' => 3,
'MetaData' => { "ER" => "Unassigned",
"PROJECT" => "Unassigned" },
);
# End defaults
##################################################
##################################################
# Options processing
GetOptions(
'r=s' => \$Options{'RecordRE'},
'c=s' => \$Options{'CSVFields'},
'd=s' => \$Options{'DateField'},
'n=s' => \$Options{'CheckField'},
't=s' => \$Options{'DescField'},
'a=s' => \$Options{'AmountField'},
's=s' => \$Options{'DefaultSource'},
'x' => \$Options{'Cleared'},
'g' => \$Options{'Negate'},
'z' => \$Options{'DetectDups'},
'X' => \$Options{'FindFiles'},
'G=s' => \$Options{'FindDir'},
'f=s' => \$Options{'FileMatchFile'},
'F=i' => \$Options{'FuzzyDates'},
'p=s' => \$Options{'PreProcessFile'},
'm=s' => \$Options{'AccountMatchFile'},
'M=s' => \$Options{'MD5Cache'},
'i=s' => \$Options{'InputFile'},
'o=s' => \$Options{'OutputFile'},
'D=s' => \$Options{'OutputDir'},
'E=s%' => \$Options{'MetaData'},
'h|help' => \$Options{'ShowHelp'}
) || die 'Failed to get options';
defined $Options{'ShowHelp'} && do {
print <<EOF;
Usage: CSV2Ledger [OPTION] -i FILE -o FILE
Converts CSV entries to Ledger format.
Options:
-i <file> Input filename
-o <file> Output filename
-D <directory> Base directory for output files
-r "regexp" Record matching RE
-c "label,label,<...>" Field label list
-d "label" Date field label
-n "label" Check Number field label
-t "label" Description field label
-a "label" Amount field label
-x Mark transactions cleared
-g Negate the transaction amount
-z Turn on duplicate detection (SLOW)
-X Turn on file location
-G <directory> Source directory for files search
-s "Account" Default Source Account
-F # Fuzzy match file dates by X days (3 default)
-f <file> File matching table YAML file
-p <file> Preprocess table YAML file
-m <file> Account matching table YAML file
-M <file> MD5 sum cache
-E KEY=VALUE Add metadata
EOF
exit;
};
$Options{'Cleared'} = defined $Options{'Cleared'} ? "* " : "";
$Options{'Negate'} = defined $Options{'Negate'} ? -1 : 1;
defined $Options{'InputFile'} || die "Specify an input file";
defined $Options{'OutputFile'} || die "Specify an output file";
( -f $Options{'InputFile'} ) || die "Specify an existing file";
( defined $Options{'FindFiles'} ) && do {
( -d $Options{'FindDir'} ) || die "Specify an existing directory for files";
};
### Default + CLI Options: %Options
######################################################################
# Table of dynamic preprocessing
#
# Each match will be compared against the transaction prior to being
# split. The the match works, then the substitution will execute.
#
my @PreProcessReTable = (); # Now imported via YAML
sub PreProcess {
my ($orig) = @_;
for my $CurRe (@PreProcessReTable) {
# If we match
eval "\$orig =~ $CurRe->[0]" &&
# Apply the substition
eval "\$orig =~ $CurRe->[1]";
}
return $orig;
}
#
##################################################
######################################################################
# Table of dynamic preprocessing
#
# Each match will be compared against the transaction prior to being
# split. The the match works, then the accounts are returned.
#
my @AccountMatchTable = (); # Now imported via YAML
sub AccountMatch {
my ($orig) = @_;
for my $CurRe (@AccountMatchTable) {
# If we match
eval "\$orig =~ $CurRe->[0]" &&
# Return first match
return {
'Source' => $CurRe->[1],
'Dest' => $CurRe->[2],
'Category' => $CurRe->[3]
};
}
return {
'Source' => $Options{'DefaultSource'},
'Dest' => 'Expense:Unknown',
'Category' => 'Unknown'
};
}
#
##################################################
######################################################################
# Table of file specific options
#
# Options to load based on the input filename
my @FileMatchTable = (); # Now imported via YAML
sub FileMatch {
for my $CurHash (@FileMatchTable) {
# If we match the input filename
if ( defined $CurHash->{'FileRe'} ) {
if ( eval "\$Options{'InputFile'} =~ $CurHash->{'FileRe'}" ) {
# Override the options hash by loading from the file
map( $Options{$_} = $CurHash->{$_}, keys %{$CurHash} );
return;
}
}
# If we match a line in the file, like a header with account #
if ( defined $CurHash->{'HeaderRe'} ) {
# Open input file
open( TEST, "<" . "$Options{'InputFile'}" )
|| die "Failed to open $Options{'InputFile'} for reading";
# Grep the input file for the header
if ( grep( /$CurHash->{'HeaderRe'}/, <TEST> ) ) {
# Override the options hash by loading from the file
map( $Options{$_} = $CurHash->{$_}, keys %{$CurHash} );
return;
}
close TEST;
}
}
}
#
##################################################
##############################
# MAIN
# Load FileMatch dispatch table
defined $Options{'FileMatchFile'} && do {
@FileMatchTable = YAML::LoadFile( $Options{'FileMatchFile'} );
# Load our options.
&FileMatch();
### FileMatch Updated Options: %Options
};
# Parse the field names into the field array
my @Fields = split( /,/, $Options{'CSVFields'} );
### @Fields
# Load PreProcess dispatch table
defined $Options{'PreProcessFile'} && do {
@PreProcessReTable = YAML::LoadFile( $Options{'PreProcessFile'} );
#### @PreProcessReTable
};
# Load AccountMatch dispatch table
defined $Options{'AccountMatchFile'} && do {
@AccountMatchTable = YAML::LoadFile( $Options{'AccountMatchFile'} );
#### @AccountMatchTable
};
# Open input/output
open( FH, $Options{'InputFile'} ) || die "Failed to open $Options{'InputFile'}";
# Main loop, one line at a time from our input CSV
while (<FH>) {
my %Transaction;
chomp;
s/\r//g;
#### Original CSV: $_
# Only process records matching the RecordRE
# This skips comments and cruft
/$Options{'RecordRE'}/ || next;
# Up transaction count
$Total++;
#### $Total
# Keep the original for ledger comments
my $OrigCSV = $_;
# Run the preprocessor, don't assume $_
$_ = &PreProcess($_);
#### CSV After PreProcess: $_
# Use the CSV module to pull in the input line
my $Status = $CSV->parse($_);
my @Columns = $CSV->fields();
#### @Columns
# Check column count
die "Field count did not match in $Options{'InputFile'}"
if ( $#Columns ne $#Fields );
# Fast way to put the columns into the hash by field
# both arrays must match by position
@Transaction{@Fields} = @Columns;
#### %Transaction
# Create transaction
# Includes date processing, Date::Parse is pretty intelligent
# so I don't have to give it an input format string
my $TempTrans = {
'Date' => time2str(
"%Y/%m/%d", str2time( $Transaction{ $Options{'DateField'} } )
),
'Cleared' => $Options{'Cleared'},
'CheckNum' => $Transaction{ $Options{'CheckField'} },
'Desc' => $Transaction{ $Options{'DescField'} },
'Amount' => $Transaction{ $Options{'AmountField'} } *
$Options{'Negate'},
'MD5Sum' => md5_hex($OrigCSV), # later dup detect
'MD5SumCR' => md5_hex($OrigCSV . "\r") # urg, i left cr on old records, match either
, 'CSV' => $OrigCSV # reference
};
map { $TempTrans->{$_} = $Options{'MetaData'}{$_} } keys %{$Options{'MetaData'}};
# Prettify the amount
$TempTrans->{'FormattedAmount'} = sprintf("\$%0.2f",$TempTrans->{'Amount'});
# Match our source / dest accounts
$TempTrans = { %{$TempTrans}, %{ &AccountMatch($_) } };
# Set to default if undefined
if ( !defined $TempTrans->{'Source'} ) {
$TempTrans->{'Source'} = $Options{'DefaultSource'};
}
# Set to default if undefined
if ( !defined $TempTrans->{'Category'} ) {
$TempTrans->{'Category'} = 'Unknown';
}
# Set to default if empty
if ( $TempTrans->{'Source'} eq '' ) {
$TempTrans->{'Source'} = $Options{'DefaultSource'};
}
# Only include check num ()'s if present
if ( $TempTrans->{'CheckNum'} ne '' ) {
$TempTrans->{'CheckNum'} = "(" . $TempTrans->{'CheckNum'} . ") ";
}
#### $TempTrans
#########################################################
# Duplicate Detection
#
# Yes, I'm reopening and scanning the file each time.
# Its terribly inefficient, and a real hack.
# If your ledger file is long enough this is causing a
# problem, consider archiving. ;]
defined $Options{'DetectDups'} && do {
# If the output file doesn't exist, don't sweat it
( -f $Options{'OutputFile'} ) && do {
# Open output file
open( DUP, "<" . "$Options{'OutputFile'}" )
|| die "Failed to open $Options{'OutputFile'} for reading";
# Grep the output file for the MD5 sum, skip if present.
if ( grep( /$TempTrans->{'MD5Sum'}/, <DUP> ) ) {
close DUP;
print STDERR "Skipping $TempTrans->{'MD5Sum'}\n";
$Skipped++;
next;
}
close DUP;
# Open output file
open( DUP, "<" . "$Options{'OutputFile'}" )
|| die "Failed to open $Options{'OutputFile'} for reading";
# Grep the output file for the MD5 sum, skip if present.
if ( grep( /$TempTrans->{'MD5SumCR'}/, <DUP> ) ) {
close DUP;
print STDERR "Skipping $TempTrans->{'MD5Sum'}\n";
$Skipped++;
next;
}
close DUP;
};
# If the cache file doesn't exist, don't sweat it
( -f $Options{'MD5Cache'} ) && do {
# Open output file
open( DUP, "<" . "$Options{'MD5Cache'}" )
|| die "Failed to open $Options{'MD5Cache'} for reading";
# Grep the output file for the MD5 sum, skip if present.
if ( grep( /$TempTrans->{'MD5Sum'}/, <DUP> ) ) {
close DUP;
print STDERR "Skipping $TempTrans->{'MD5Sum'}\n";
$Skipped++;
next;
}
close DUP;
# Open output file
open( DUP, "<" . "$Options{'MD5Cache'}" )
|| die "Failed to open $Options{'MD5Cache'} for reading";
# Grep the output file for the MD5 sum, skip if present.
if ( grep( /$TempTrans->{'MD5SumCR'}/, <DUP> ) ) {
close DUP;
print STDERR "Skipping $TempTrans->{'MD5Sum'}\n";
$Skipped++;
next;
}
close DUP;
};
};
##################################################
# Find Files
#
# Attach files that match the txn to the payee.
# Filenames should be in the form: YYYYMMDD_Vendor_Dollars_Cents.*$
# Vendor name isn't likely to match, so lets focus on the date and dollars
# Then we use the /dir/filename [,file:...] attachment syntax
my $TempAmount = sprintf( "%.2f", $Transaction{ $Options{'AmountField'} } );
$TempAmount =~ tr/./_/;
# Date::Calc::Localtime returns ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst)
my @DateFieldCalc =
( Localtime( str2time( $Transaction{ $Options{'DateField'} } ) ) )
[ 0, 1, 2 ];
# Seed with proper date
my @SearchRegexp = ( sprintf( "%04d%02d%02d", @DateFieldCalc)
. '_' . '.*?' . '_' . $TempAmount . '[_.]' );
# Fuzz days before or after...
foreach
my $FuzzFactor ( $Options{'FuzzyDates'} * -1 .. $Options{'FuzzyDates'} )
{
push(
@SearchRegexp, sprintf( "%04d%02d%02d",
Add_Delta_Days( @DateFieldCalc, $FuzzFactor ) )
. '_' . '.*?' . '_' . $TempAmount . '[_.]'
);
}
#### @SearchRegexp
my $FilesList = "";
my $Found = 0;
find(
sub {
foreach my $RE (@SearchRegexp) {
unless ($Found) {
if (/$RE/) {
if ($FilesList) { $FilesList .= ','; }
my $TempName = $File::Find::name;
$TempName =~ s/$Options{'FindDir'}//g;
$FilesList .= $TempName;
print STDERR "Matched $_ to "
. $Transaction{ $Options{'DateField'} } . " "
. $Transaction{ $Options{'DescField'} } . " "
. $Transaction{ $Options{'AmountField'} } . "\n";
$MatchedFiles++;
$Found++;
}
}
}
},
$Options{'FindDir'}
);
########################################
# Output
# Add fileslist for tempate
$TempTrans->{'FilesList'} = $FilesList;
$TT->process( \$Options{'TxnOutputTemplate'}, $TempTrans,
">" . "$Options{'OutputFile'}" );
}
close FH;
# Output totals
printf(
"*** Imported %d / Skipped %d / Total %d\n",
$Total - $Skipped,
$Skipped, $Total
);
printf( "*** Files Matched %d of Imported %d\n",
$MatchedFiles, $Total - $Skipped );
# END
##################################################