diff options
| author | St33v <github@f3rr3t.com> | 2021-07-24 08:36:48 +1000 |
|---|---|---|
| committer | St33v <github@f3rr3t.com> | 2021-07-24 08:36:48 +1000 |
| commit | 0e94cc4d06bb47f2ea1830a11700cd52d17ac81c (patch) | |
| tree | 19139c402eb0546b9a3cc049750e546963839107 | |
| parent | ed28435f8005a68cb2b7f74d5ba872fd8f1d0fc0 (diff) | |
| -rw-r--r-- | README.md | 9 | ||||
| -rwxr-xr-x | broker.pl | 135 | ||||
| -rwxr-xr-x | gst.pl | 117 |
3 files changed, 261 insertions, 0 deletions
@@ -1,2 +1,11 @@ # hledgerTools Scripts for processing raw (csv) or journal files + +## Symlink the executables +Make it easy to run the scripts by symlinking them to +`/usr/local/bin/` +Start at /usr/local/bin: + +/usr/local/bin$ sudo ln -s /home/user/path-to/hledgerTools/xyz.pl . + + diff --git a/broker.pl b/broker.pl new file mode 100755 index 0000000..264b222 --- /dev/null +++ b/broker.pl @@ -0,0 +1,135 @@ +#!/usr/bin/perl +# BROKER.pl +# Add Brokerage to JOURNAL files that have been generated from csv +# +use strict; +use warnings; +use Cwd; # Current working directory + +my $thisDir = cwd; +my $rawFile; # the name of the file to process +my $outputFile; +#die "hello "; + +if (@ARGV > 0) { + $rawFile = $ARGV[0]; + if ($rawFile =~ /^raw-/ ) { + print "Processing raw journal file $rawFile for Brokerage\n"; + $outputFile = $'; # the rest of the matched string + } + else {die " The input filename must begin with 'raw-'.";} +} else { # no commmand line argument + # if ("No COMMAND line, arguments" =~ /\s(\w+),/) { print "That was ($`)($&)($').\n"; } + opendir(DIR, $thisDir); + my @rawFiles = grep(/^raw-.+\.journal$/,readdir(DIR)); + closedir(DIR); + my $numFiles = @rawFiles; + #print "Number of files: ".$numFiles . "\n"; + if ($numFiles > 1) { + die "Usage: you can only have one RAW .journal file in the directory."; + } elsif ($numFiles = 1) { + $rawFile = $rawFiles[0]; + $rawFile =~ /^raw-/; + $outputFile = $'; # strip off the raw- prefix + # print $outputFile . "\n"; + } else { die " Usage: name the .journal file to process as a command line argument.";} +} + +if ( ! open IN, "< ".$thisDir."/".$rawFile) { + die " Cannot open journal file in ". $thisDir. ": $!"; +} + +if (! open OUT, "> ${outputFile}") { + die " Could not open $outputFile for output in ".$thisDir.": $!"; +} + +my $i=0; +my $brokerageACC = "expenses:misc:shares:brokerage"; +my $brokerageTransaction = 0; # default is FALSE +my $brokerageTotal =0; +my $buysellFlag; +my ($numShares, $coCode, $sharePrice); + +while (<IN>) { + chomp; + &writeOUT and next if (/^;/); # write out comments + if (/^\s*$/) { # Blank line means end of (previous) transaction + &writeBrokerage if ($brokerageTransaction); # Write summed Brokerage amount from prev. + &writeOUT; # Write the blank line + next; + } + + # check for transaction top lines + if (/^\d{4}\/\d{2}\/\d{2}/) { # we found a date at the start of the line + $i++; # increment transaction counter + &buySell; + &getShareCode; # scrape code and num shares + $brokerageTransaction = (/Fees:[0-9.]+/) ? 1 : 0; + my ($fee, $gst) = /Fees:(\d+.\d\d)\+([0-9.]+)/; + $brokerageTotal = $fee + $gst; + &writeOUT and next; + } + if ($brokerageTransaction) {; # top line was tagged 'Fees:' + applyBrokerage(); + } + else { + writeOUT(); + } +} + +# special case if last transaction in file was GST +&writeBrokerage if ($brokerageTransaction); # Write summed GST amount from prev. +print "Processed $i transactions, written to $outputFile.\n"; + +################################################################ + +sub applyBrokerage { + my($account, $amt, $desc) = (split /\s{2,}/)[1, 2, 3]; + my $posting; + # print "account:$account\n"; + # print " dollars $amt\n"; + #if (($buysellFlag eq "BUY") && ($account =~ /assets:shares/)) { + if ($account =~ /assets:shares/) { + print "this is a share\n$_\n"; + # Unprocessed share amounts are always NEGATIVE + # Therefore ADD the brokerage to reduce ABS() + $amt = sprintf("%.2f", $amt + $brokerageTotal); + } + # Correct sign of transaction if BUY + $amt = ($buysellFlag eq "BUY") ? -1 * $amt : $amt; + $amt = sprintf("%.2f", $amt); + $desc = (length($desc)) ? $desc : ""; + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: + print OUT " $account $numShares $coCode @ $sharePrice $desc\n"; +} + +sub writeBrokerage { + $brokerageTotal = sprintf("%.2f", $brokerageTotal); # format to 2 decimals for cents + print OUT " $brokerageACC $brokerageTotal\n"; + $brokerageTransaction = 0; +} + +sub buySell { + s/B Contract/BUY Contract/; + s/S Contract/SELL Contract/; + # set flag for adjustSign subroutine + $buysellFlag = (/BUY Contract/) ? "BUY" : "SELL"; +} + +sub getShareCode{ + print "getShareCode\n$_\n"; + s/(Price:)\s+/$1/; + s/Num Shares:(\d+)\.\d\d/$1/; + print $_."\n"; + $numShares = $1; + ($coCode) = /Company:(\w+)/; + if ($coCode eq "CL8") { $coCode = "CL"}; + ($sharePrice) = /Price:(\d+.\d+)/; + print "numShares: $numShares\t $coCode\n"; +} + +sub writeOUT { + if (/^\s*$/) {$_ = ""}; # replace whitespace-only line with clean blank line + print OUT $_ . "\n"; + #next; <-- don't use next in a subroutine bc bad ok +} @@ -0,0 +1,117 @@ +#!/usr/bin/perl +# Can I read a file and mark the items for GST processing? +# +use strict; +use warnings; +# use diagnostics; + +my $rawFile; # the name of the file to process +my $outputFile; +# my $numArgs = scalar(@ARGV); +# my $blah = "I saw ${numArgs} Arguments: " . $ARGV[0] . "\n"; +# print $blah; + + +if (@ARGV > 0) { + $rawFile = $ARGV[0]; + if ($rawFile =~ /^raw-/ ) { + print "Processing raw journal file $rawFile for GST\n"; + $outputFile = $'; # the rest of the matched string + } + else {die " The input filename must begin with 'raw-'.";} +} else { # no commmand line argument + # if ("No COMMAND line, arguments" =~ /\s(\w+),/) { print "That was ($`)($&)($').\n"; } + opendir(DIR, "."); + my @rawFiles = grep(/^raw-.+\.journal$/,readdir(DIR)); + closedir(DIR); + my $numFiles = @rawFiles; + #print $numFiles . "\n"; + if ($numFiles > 1) { + die "Usage: you can only have one RAW .journal file in the directory."; + } elsif ($numFiles = 1) { + $rawFile = $rawFiles[0]; + $rawFile =~ /^raw-/; + $outputFile = $'; # strip off the raw- prefix + # print $outputFile . "\n"; + } else { die " Usage: name the .journal file to process as a command line argument.";} +} + +if ( ! open IN, "< $rawFile") { + die " Cannot open journal file: $!"; +} + +if (! open OUT, "> ${outputFile}") { + die " Could not open $outputFile for output: $!"; +} + +my $i=0; +my $gstCount=0; +my $gstRoot = "liabilities:current:gst:"; +my $gstACC; +my $isGST = 0; # default is FALSE +my $gstTotal =0; + +while (<IN>) { + chomp; + + &writeOUT and next if (/^;/); # write out comments + + if (/^\s*$/) { # Blank line means end of (previous) transaction + &writeGST if ($isGST); # Write summed GST amount from prev. + &writeOUT; # Write the blank line + next; + } + + # check for transaction top lines + if (/^\d{4}\-\d{2}\-\d{2}/) { # we found a date at the start of the line + $i++; # increment transaction counter + $isGST = (/tax:GST/) ? 1 : 0; + $gstCount = $gstCount + $isGST; + &writeOUT and next; + } + + # this is a ledger line (i.e. either a credit or a debit) + if ($isGST) {; # top line was tagged 'tax:GST' + writeOUT() and next if (/assets:/ || /liabilities:/); + if (/^\s+expenses:/) { + $gstACC = $gstRoot . "paid"; + } + if (/^\s+income:/) { + $gstACC = $gstRoot . "collected"; + } + applyGST(); + } + else { + writeOUT(); + } +} + +# special case if last transaction in file was GST +&writeGST if ($isGST); # Write summed GST amount from prev. +print "Processed $gstCount GST items from $i transactions, written to $outputFile.\n"; + +################################################################ + +sub applyGST { + my($account, $amt, $desc) = (split /\s{2,}/)[1, 2, 3]; + # print "account:$account\n"; + # print " dollars $amt\n"; + my $examt = sprintf("%.2f", $amt * 10 / 11); + $gstTotal = $gstTotal + sprintf("%.2f", $amt - $examt); + $desc = (length($desc)) ? $desc : ""; + print OUT " $account\t\t\t$examt $desc\n"; + #print OUT " $gstACC\t\t\t$gst\n"; +} + +sub writeGST { + $gstTotal = sprintf("%.2f", $gstTotal); # format to 2 decimals for cents + print OUT " $gstACC\t\t\t$gstTotal\n"; + $isGST = 0; + $gstTotal = 0; +} + +sub writeOUT { + if (/^\s*$/) {$_ = ""}; # replace whitespace-only line with clean blank line + print OUT $_ . "\n"; + #next; <-- don't use next in a subroutine bc bad ok +} |
