diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2021-06-08 20:07:21 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2021-06-08 20:07:21 +0100 |
commit | 054cc04c9db6aa14a737ccd1fbee23be22a11853 (patch) | |
tree | ade24a2f3a0385a81c9ae7c894ed477c79c62430 | |
parent | 10f5ba796d54410c72787b6b341e3307455c1520 (diff) |
perl tidy and removed .pl extension
Diffstat (limited to '')
-rw-r--r-- | ledgerscripts/csv_processor | 110 | ||||
-rw-r--r-- | ledgerscripts/csv_processor.pl | 111 |
2 files changed, 110 insertions, 111 deletions
diff --git a/ledgerscripts/csv_processor b/ledgerscripts/csv_processor new file mode 100644 index 0000000..bfd7fc9 --- /dev/null +++ b/ledgerscripts/csv_processor @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.010; + +use Text::CSV; +use JSON; + +my $csv = Text::CSV->new( + { sep_char => ',', + binary => 1, + quote => "\N{FULLWIDTH QUOTATION MARK}" + } +); + +my %transaction; +my @jlist; # used to create the categories json fil0 +my $cat_json; + +my $file = $ARGV[0] or die "Need to get CSV file on the command line\n"; +open( my $csvdata, '<:encoding(UTF-8)', $file ) + or die "Could not open '$file' $!\n"; + +{ + open( my $category_file, '<', "categories.json" ) + or die "Could not open category file $!\n"; + local $/ = undef; # slurp mode! + $cat_json = <$category_file>; +} + +my $catref = decode_json $cat_json; +my $cats = $catref->{"data"}; + +# say qq($catref_d->[0]->{"desc"}); + +my @descs = map $_->{"desc"}, @{$cats}; + +sub get_category_from_desc { + my $desc = shift; + for my $hsh ( @{$cats} ) { + if ( $desc eq $hsh->{"desc"} ) { + return $hsh->{"category"}; + } + } +} + +while ( my $line = <$csvdata> ) { + $line =~ s/^\N{BOM}//; + chomp $line; + if ( $csv->parse($line) ) { + my @fields = $csv->fields(); + $transaction{day} = substr $fields[0], 0, 2; + $transaction{month} = substr $fields[0], 3, 2; + $transaction{year} = substr $fields[0], 6, 4; + $transaction{date} = $fields[0]; + $fields[1] =~ s/\s+/ /g; + + # used to create the categories json file - see below + push @jlist, { "desc" => $fields[1], "category" => "NONE" }; + + $transaction{desc} = $fields[1]; + $transaction{cost} = $fields[2]; + + for my $d (@descs) { + if ( $transaction{desc} eq $d ) { + $transaction{exp_type} = get_category_from_desc $d; + } + } + + if ( $fields[1] =~ /^.+(VIS|DR|DD|TFR|CR|SO|ATM|\)\)\))$/ ) { + $transaction{type} = $1; + } + else { die("CANNOT DETERMINE TYPE!\n") } + + if ( $fields[2] =~ /^\-/ ) { + $transaction{expense} = 1; + } + else { $transaction{expense} = 0; } + + print join "", + ( + $transaction{year}, "/", $transaction{month}, "/", + $transaction{day}, " ", "*", " ", + $transaction{desc} + ), + "\n"; + if ( $transaction{expense} == 1 ) { + ( my $cost = $transaction{cost} ) =~ s/^\-//; + print qq(\t$transaction{exp_type}\t$cost\n); + print "\tassets:hsbc current\t$transaction{cost}\n"; + print "\n"; + } + else { + print "\tincome:baws\t-$transaction{cost}\n"; + print "\tassets:hsbc current\t$transaction{cost}\n"; + print "\n"; + } + } + else { warn "Line could not be parsed: $line\n"; } +} + +# The following code is used to output a JSON file +# to be used for categories. Uncomment for use. +# my $data = encode_json {data => \@jlist}; + +# open(my $fh, '>', "/tmp/categories.json") or die "Could not open file '/tmp/toss.json' $!"; +# print $fh $data; +# close $fh; +# print "done\n"; diff --git a/ledgerscripts/csv_processor.pl b/ledgerscripts/csv_processor.pl deleted file mode 100644 index dafbe95..0000000 --- a/ledgerscripts/csv_processor.pl +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.010; - -use Text::CSV; -use JSON; - -my $csv = Text::CSV->new({ - sep_char => ',', - binary => 1, - quote => "\N{FULLWIDTH QUOTATION MARK}"} -); - -my %transaction; -my @jlist; # used to create the categories json fil0 -my $cat_json; - -my $file = $ARGV[0] or die "Need to get CSV file on the command line\n"; -open(my $csvdata, '<:encoding(UTF-8)', $file) or die "Could not open '$file' $!\n"; - -{ - open(my $category_file, '<', "categories.json") or die "Could not open category file $!\n"; - local $/ = undef; # slurp mode! - $cat_json = <$category_file>; -} - -my $catref = decode_json $cat_json; -my $cats = $catref->{"data"}; -# say qq($catref_d->[0]->{"desc"}); - -my @descs = map $_->{"desc"}, @{$cats}; - -sub get_category_from_desc { - my $desc = shift; - for my $hsh (@{$cats}) { - if ($desc eq $hsh->{"desc"}) { - return $hsh->{"category"}; - } - } -} - -while (my $line = <$csvdata>) { - $line =~ s/^\N{BOM}//; - chomp $line; - if ($csv->parse($line)) { - my @fields = $csv->fields(); - $transaction{day} = substr $fields[0], 0, 2; - $transaction{month} = substr $fields[0], 3, 2; - $transaction{year} = substr $fields[0], 6, 4; - $transaction{date} = $fields[0]; - $fields[1] =~ s/\s+/ /g; - - # used to create the categories json file - see below - push @jlist, {"desc" => $fields[1], "category" => "NONE"}; - - $transaction{desc} = $fields[1]; - $transaction{cost} = $fields[2]; - - for my $d (@descs) { - if ($transaction{desc} eq $d) { - $transaction{exp_type} = get_category_from_desc $d; - } - } - - if ($fields[1] =~ /^.+(VIS|DR|DD|TFR|CR|SO|ATM|\)\)\))$/) { - $transaction{type} = $1; - } else - { die("CANNOT DETERMINE TYPE!\n")} - - if ($fields[2] =~ /^\-/) { - $transaction{expense} = 1; - } else - { $transaction{expense} = 0; } - - print join "", ( - $transaction{year}, - "/", - $transaction{month}, - "/", - $transaction{day}, - " ", - "*", - " ", - $transaction{desc} - ), "\n"; - if ($transaction{expense} == 1) { - (my $cost = $transaction{cost}) =~ s/^\-//; - print qq(\t$transaction{exp_type}\t$cost\n); - print "\tassets:hsbc current\t$transaction{cost}\n"; - print "\n"; - } else { - print "\tincome:baws\t-$transaction{cost}\n"; - print "\tassets:hsbc current\t$transaction{cost}\n"; - print "\n"; - } - } else - { warn "Line could not be parsed: $line\n";} -} - - - -# The following code is used to output a JSON file -# to be used for categories. Uncomment for use. -# my $data = encode_json {data => \@jlist}; - -# open(my $fh, '>', "/tmp/categories.json") or die "Could not open file '/tmp/toss.json' $!"; -# print $fh $data; -# close $fh; -# print "done\n"; |