#!/usr/bin/perl
# $Id: gt,v 1.4 1994/10/27 12:16:01 cbbrowne Exp $
#  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 "-by") {
	$newkey = $value;
	if ($KEYLIST{$value}) {
	    if ($sortkey eq "default") {
		$sortkey = $KEYLIST{$value};
	    } else {
		$sortkey = $sortkey.":".$KEYLIST{$value};
	    }	
	} else {		
	    &bad_args();
	}
  } elsif (($option eq "-all") || ($option eq "-a")) {
        if ($#ORIGARGS != 0) {
	  &bad_args();
        } else {
          $all = "TRUE";
        }
  } else {
      &bad_args();
  }	
}
if ($sortkey eq "default") {
    $sortkey = "\$date";
}
print "key on :$sortkey\n";
require "cbbshutils.pl";
&need_txn($name);

$count = 0.0;   # This is used just to make sure that the key is unique
while (($key,$value) = each (TXNS)) {
    $skip = 0;
    $skip |= (($FROM ne "") && ($FROM gt $key));
    $skip |= (($TO ne "") && ($TO lt $key));
    if ($all eq "TRUE") {$skip = 0;}
    if (!$skip) {
	($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	    split(/:/, $value);
	if ((&match($ref, $REF) &&
	    &match($cat, $CATEGORY) &&
	    &match($value, $ANY) &&
	    &match($desc, $PAYEE)) ||
	    ($all eq "TRUE"))
	{
	    # Need to determine $tkey, basis for sort
	    $count ++;
	    $TEMP{$count} = $value;
	}
    }
}

@KEYS = (keys %TEMP);
@KEYS = sort byspecified (@KEYS);

foreach $i (0..$#KEYS) {
    $value = $TEMP{$KEYS[$i]};
    $ky = $KEYS[$i];
    &listtxn($ky, $value);
}

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

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

gt - `grep 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  (must be the only arg.)
		-by [date,cat,payee,ref,cleared]
				Chooses the sort key.

Examples:
% gt chequing -ref '125' # finds cheque number 125
% gt cash -from '940101' # finds all cash transactions since 1 Jan 1994
% gt chequing -by ref -by payee
 # lists transactions with the ref. # as the primary sort key, 
 # payee as the secondary key.";

die -1;
}

sub byspecified {
  local ($value1) = $TEMP{$a};
  local ($value2) = $TEMP{$b};
  local ($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	split(/:/, $value1);
  local($k1) = eval $sortkey;
  ($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	split(/:/, $value2);
  local ($k2) = eval $sortkey;
#  print "compare: $k1 to $k2\n";
  return $k1 <=> $k2; 
}
