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
|
#!/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
}
|