diff options
author | Bradley M. Kuhn <bkuhn@ebb.org> | 2011-12-04 20:06:32 -0500 |
---|---|---|
committer | Bradley M. Kuhn <bkuhn@ebb.org> | 2012-11-26 13:54:46 -0500 |
commit | d817000b828c8ce1808d95cb7fae259fe05630ec (patch) | |
tree | 3ab35ad6609f4d6a8edbda7df3d97df4ed4b9a8c /contrib | |
parent | ca359f0606d80160e075100a290e8d05312ac7c9 (diff) | |
download | fork-ledger-d817000b828c8ce1808d95cb7fae259fe05630ec.tar.gz fork-ledger-d817000b828c8ce1808d95cb7fae259fe05630ec.tar.bz2 fork-ledger-d817000b828c8ce1808d95cb7fae259fe05630ec.zip |
Various changes to get the script working.
Diffstat (limited to 'contrib')
-rwxr-xr-x[-rw-r--r--] | contrib/non-profit-audit-reports/cash-receipts-and-disbursments-journals.plx | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/contrib/non-profit-audit-reports/cash-receipts-and-disbursments-journals.plx b/contrib/non-profit-audit-reports/cash-receipts-and-disbursments-journals.plx index 6b704c08..5dbe7033 100644..100755 --- a/contrib/non-profit-audit-reports/cash-receipts-and-disbursments-journals.plx +++ b/contrib/non-profit-audit-reports/cash-receipts-and-disbursments-journals.plx @@ -26,6 +26,7 @@ use warnings; use Math::BigFloat; use Date::Manip; +use File::Temp qw/tempfile/; my $LEDGER_CMD = "/usr/bin/ledger"; @@ -37,9 +38,10 @@ sub ParseNumber($) { } sub LedgerAcctToFilename($) { - $_[0] =~ s/ /-/g; - $_[0] =~ s/:/-/g; - returen $_[0]; + my $x = $_[0]; + $x =~ s/ /-/g; + $x =~ s/:/-/g; + return $x; } Math::BigFloat->precision(-2); @@ -83,36 +85,40 @@ foreach my $acct (@accounts) { { name => 'receipts', query => 'a>0' }) { my $fileNameBase = $acctFilename . '-' . $typeData->{name}; + open(TEXT_OUT, ">", "$fileNameBase.txt") or die "unable to open $fileNameBase.txt: $!"; + open(CSV_OUT, ">", "$fileNameBase.csv") or die "unable to open $fileNameBase.csv: $!"; + print TEXT_OUT "\n\nACCOUNT: $acct\nFROM: $beginDate TO $formattedEndDate\n\n"; print CSV_OUT "\n\"ACCOUNT:\",\"$acct\"\n\"PERIOD START:\",\"$beginDate\"\n\"PERIOD END:\",\"$formattedEndDate\"\n"; print CSV_OUT '"DATE","CHECK NUM","NAME","ACCOUNT","AMOUNT"', "\n"; + my @entryLedgerOpts = ('-l', $typeData->{query}, '-b', $beginDate, '-e', $endDate, @otherLedgerOpts, 'print', $acct); open(ENTRY_DATA, "-|", $LEDGER_CMD, @entryLedgerOpts) or die "Unable to run $LEDGER_CMD @entryLedgerOpts: $!"; - my $tempFile = "tmp$$"; + my($tempFH, $tempFile) = tempfile("cashreportsXXXXXXXX", TMPDIR => 1); - open(ENTRY_OUT, ">", $tempFile) or die "Unable to write $tempFile: $!"; - - while (my $line = <ENTRY_DATA>) { print ENTRY_OUT $line; } + while (my $line = <ENTRY_DATA>) { print $tempFH $line; } close(ENTRY_DATA); die "Error reading ledger output for entries: $!" unless $? == 0; - close(ENTRY_OUT); die "Error writing ledger output for entries to temp file, $tempFile: $!" unless $? == 0; + $tempFH->close() or die "Error writing ledger output for entries to temp file, $tempFile: $!"; + + goto SKIP_REGISTER_COMMANDS if (-z $tempFile); my @txtRegLedgerOpts = ('-f', $tempFile, '--wide-register-format', "%D %-.70P %-.10C %-.80A %18t\n%/%68|%15|%-.80A %18t\n", '-w', '--sort', 'd', '-b', $beginDate, '-e', $endDate, 'reg'); my @csvRegLedgerOpts = ('-f', $tempFile, '--wide-register-format', - '"%D","%C","%P",%A","%t"\n%/|"","","","%A","%t"\n"', '-w', '--sort', 'd', + '"%D","%C","%P",%A","%t"\n%/"","","","%A","%t"\n', '-w', '--sort', 'd', '-b', $beginDate, '-e', $endDate, 'reg'); + open(TXT_DATA, "-|", $LEDGER_CMD, @txtRegLedgerOpts) or die "unable to run ledger command for $fileNameBase.txt: $!"; while (my $line = <TXT_DATA>) { print TEXT_OUT $line; } - close(TXT_DATA); die "Error read from txt ledger command $!" unless $? == 0; close(TEXT_OUT); die "Error read write text out to $fileNameBase.txt: $!" unless $? == 0; open(CSV_DATA, "-|", $LEDGER_CMD, @csvRegLedgerOpts) @@ -120,7 +126,11 @@ foreach my $acct (@accounts) { while (my $line = <CSV_DATA>) { print CSV_OUT $line; } close(CSV_DATA); die "Error read from csv ledger command $!" unless $? == 0; + + SKIP_REGISTER_COMMANDS: + close(TXT_DATA); die "Error read from txt ledger command $!" unless $? == 0; close(CSV_OUT); die "Error read write csv out to $fileNameBase.csv: $!" unless $? == 0; + unlink($tempFile); } } ############################################################################### |