#!/usr/bin/perl
# $Id: showtxn,v 1.2 1994/11/14 07:06:59 cbbrowne Exp cbbrowne $
#  Written by Christopher B. Browne
#
#  Copyright (C) 1994  Christopher B. Browne cbrowne@io.org
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Parse arguments...
$name = shift(@ARGV); $name =~ tr/A-Z/a-z/;
if ($name eq "") {
    print "Aborting: no account specified!\n";
    die -1;
}
# This list is used to construct the key
%KEYLIST = ("date", "\$date",
	    "cat", "\$cat",
	    "payee", "\$desc",
	    "ref", "\$check",
	    "cleared", "\$cleared");

$sortkey = "default";
print "Listing transactions for $name\n";
@ORIGARGS = @ARGV;
$all = "FALSE";
while (@ARGV) {
    $option = shift(@ARGV);
    $value = shift(@ARGV);

    if (($option eq "-from") || ($option eq "-f")) {
	$FROM = $value;
    } elsif (($option eq "-to") || ($option eq "-t")) {
  	$TO = $value;	    
    } elsif (($option eq "-category") || ($option eq "-c")) {
  	$CATEGORY = $value;
    } elsif (($option eq "-payee") || ($option eq "-p")) {
	$PAYEE = $value;
    } elsif (($option eq "-ref") || ($option eq "-r")) {
  	$REF = $value;	       
    } elsif (($option eq "-any") || ($option eq "-a")) {
  	$ANY = $value;
    } elsif (($option eq "-all") || ($option eq "-a")) {
        if ($#ORIGARGS != 0) {
	  &bad_args();
        } else {
          $all = "TRUE";
        }
  } else {
      &bad_args();
  }	
}
require "cbbshutils.pl";
&need_txn($name);

$count = 0;
while (($key,$value) = each (TXNS)) {
    $skip = 0;
    $skip |= (($FROM ne "") && ($FROM ge $key));
    $skip |= (($TO ne "") && ($TO le $key));
    if ($all eq "TRUE") {$skip = 0;}
    if (!$skip) {
	($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	    split(/:/, $value);
	if (length($date) == 6) {
	    $date = "19$date";
	    &put_TXNS($key, "$date:$check:$desc:$debit:$credit:$cat:$com:$cleared:$total", "Fix Date", $cleared, $cleared, 0);
	}

	if ((&match($ref, $REF) &&
	    &match($cat, $CATEGORY) &&
	    &match($value, $ANY) &&
	    &match($desc, $PAYEE)) ||
	    ($all eq "TRUE"))
	{
	    $count ++;
	    $TEMP{$count} = $value;
	}
    }
}

foreach $i (keys %TEMP) {
    &showtxn($TEMP{$i});
}

sub match {
    local ($field, $query) = ($_[0], $_[1]);
    return ($query eq "") || (index ($field, $query, 0) != -1);
}

sub bad_args {
  print 
"Incorrect options to showtxn: @ORIGARGS

showtxn - `show transactions'
options:	-from, -f	Chooses date ranges
		-to, -t        	Chooses date ranges
		-category, -c	Pick category
		-payee, -p	Pick payee
		-ref, -r	Reference number (e.g. cheque number)
		-any, -g	Find a match in ANY field
		-all, -a	Pick ALL records

Examples:
% showtxn chequing -ref '125' # finds cheque number 125
% showtxn cash -from '940101' # finds all cash transactions since 1 Jan 1994\n";

die -1;
}

sub showtxn {
	local ($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	        split(/:/, $_[0]);
	printf "txn %s %6s '%s' '%s' '%s' %.2f '%s'\n",
		$name, $date, $check, $desc, $cat, $debit-$credit, $com;
}
