#!/usr/bin/perl -w

=head1 NAME

dfxml-mileage - parse a QSF XML file and summarise mileage expenses

=head1 DATAFREEDOM

These scripts developed from the 'pilot-qof' package but now include
support for other packages and formats and will continue to be extended
along the lines of http://www.data-freedom.org/ - liberating user data 
from the application. Therefore, the datafreedom scripts use a 'df' prefix.

The scripts continue to be developed within the pilot-qof CVS until such
time as the scripts are sufficiently cohesive to form a new source package.

Please feel free to contribute any of your own scripts, under the provisions
of the GNU General Public Licence v3 or later, via the QOF-devel mailing list.
http://lists.sourceforge.net/lists/listinfo/qof-devel

=head1 VERSION

Version 0.0.1

=head1 SYNOPSIS

 dfxml-mileage FILENAME
 dfxml-mileage -h|--help|--version

=head1 DESCRIPTION

dfxml-mileage parses a QSF XML file output by pilot-qof
and prepares a simple summary of the mileage claims
within the file.

Specify '-' as the filename to parse STDIN.

e.g.
pilot-qof -x data.xml -d pilot_expenses -t 2006-11 | dfxml-mileage -

=head1 OBJECTS

pilot_expenses is part of pilot-qof.
Can also be used with gpe-expenses - compatibility with the
default SQLite gpe-expenses backend is pending.

L<http://qof.sourceforge.net/>

L<http://pilot-qof.sourceforge.net/>

L<http://gpe-expenses.sourceforge.net/>

=head1 AUTHOR

Neil Williams, C<< <codehelp at debian.org> >>

=head1 BUGS

Please report bugs via the pilot-qof package, either
in the Debian BTS or via SourceForge trackers.

=head1 COPYRIGHT & LICENSE

  Copyright 2007 Neil Williams.

  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 3 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, see <http://www.gnu.org/licenses/>.

=cut

# currently converts all distances to miles. Needs configuration.

use strict;
use Date::Format;
use Number::Format qw(:subs);
use XML::QOFQSF qw(QSFParse);

my $our_version = "0.0.1";
sub usageversion {
    print(STDERR <<END)
dfxml-mileage version $our_version

Usage:
 dfxml-mileage FILENAME
 dfxml-mileage -h|--help|--version

Options:
 -h|--help:           print this usage message and exit
 --version:           print this usage message and exit

dfxml-mileage parses a QSF XML file output by pilot-qof
and prepares a simple summary of the mileage claims
within the file.

Specify '-' as the filename to parse STDIN.

e.g.
pilot-qof -x data.xml -d pilot_expenses -t 2006-11 | dfxml-mileage -

END
        || die "$0: failed to write usage: $!\n";
exit 0;
}

my $stdin = "false";
while( @ARGV ) {
    $_= shift( @ARGV );
    if (/^-$/) {
 		$stdin = "true";
    	next;
    }
    last if m/^--$/;
    if (!/^-/) {
        unshift(@ARGV,$_);
        last;
    }
    if (/^(-h|--help|--version)$/) {
        &usageversion();
        exit( 0 );
    }
}

my $file = "";
$file = "-" if ($stdin eq "true");
$file = $ARGV[0] if ($stdin eq "false");
&usageversion if (!$file);
die "Cannot find $file. Please specify a mileage XML file.\n" 
	if ((! -f $file) && ($file ne '-'));

my %obj = QSFParse("$file");
my $expenses = $obj{'pilot_expenses'};
my $exp_count = @$expenses;
$expenses = $obj{'gpe_expenses'} if ($exp_count == 0);
$exp_count = @$expenses;
print "$exp_count expenses\n";
my $template = "%A, %o %B %Y";
my $total_miles = 0;
my $first = time();
my $last = 0;
foreach my $a (@$expenses)
{
	if ($a->type_of_expense eq "Mileage")
	{
		my $miles = 0;
		$miles = $a->expense_amount if ($a->distance_unit eq "Miles");
		$miles = ($a->expense_amount * 1.60934) if ($a->distance_unit ne "Miles");
		$total_miles += $miles;
		$first = $a->expense_date if ($a->expense_date < $first);
		$last = $a->expense_date if ($a->expense_date > $last);
		print format_number($a->expense_amount, 1) . " " . $a->distance_unit . " : ";
		print $a->expense_vendor . " " . $a->expense_city;
		print " on " . time2str($template, $a->expense_date) . "\n";
	}
}

print "Total: " . format_number($total_miles, 1) . " miles\n";
print "Covering the period from ". time2str($template, $first);
print " to " . time2str($template, $last) . "\n";
