#!/usr/bin/perl

# gc2latex by Stefan Tomanek <stefan@pico.ruhr.de>

use Gnucash::Business::Invoice;
use Gnucash::Business::InvoiceEntry;
use Gnucash::Business::Customer;
use IO::File;
use Date::Format;

use strict;

sub main($$$) {
    my ($template, $gnucash, $id) = @_;

    unless (defined $template && defined $gnucash && defined $id) {
	print "Usage: gc2latex <template file> <account file> <invoice id>\n";
	return;
    }

    my $parser = new XML::Parser (ErrorContext => 2, Style => "Tree");
    my $xmlobj = new XML::SimpleObject( $parser->parsefile($gnucash) ) || die "Unable to open Gnucash file";
    
    my $invoice = new Invoice( $xmlobj, $id);

    replace($template, $invoice);
}

# LaTeX encode: use LaTeX::Encode if available
sub l_enc {
    my ($data) = @_;
    my $result = $data;
    eval {
        # try to load LaTeX::Encode and encode the supplied data
        # if the module cannot be found, continue silently and
        # just leave the data untouched
        require LaTeX::Encode;
        $result = LaTeX::Encode::latex_encode( $data, {iquotes=>1} );
    };
    return $result;
}

sub createLaTeXTable($) {
    my ($invoice) = @_;

    my $cur = $invoice->getCurrency();

    my $text = '\begin{longtable}{l|p{150pt}|l|l|r|r}'."\n";
    $text .= 'Datum&Beschreibung&Einheit&Anzahl&Stckpreis&Betrag\\\\'."\n";
    $text .= '\hline\hline'."\n";
    foreach my $e (@{ $invoice->getEntries() }) {
        $text .= time2str("%e.%L.%Y", $e->getDate())."&";
        $text .= l_enc( $e->getDescription() )."&";
        $text .= l_enc( $e->getAction() )."&";
        $text .= $e->getQuantity()."&";
        $text .= sprintf("%0.2f", $e->getNetPrice())." $cur&";
        $text .= sprintf("%0.2f", $e->getNetSum())." ".$cur.'\\\\'."\n";
        $text .= '\hline'."\n";
    }
    $text .= '\hline'."\n";
    $text .= '\multicolumn{5}{r|}{Summe} &'.sprintf("%0.2f", $invoice->getNetSum()).' '.$cur.'\\\\'."\n";
    ## FIXME
    ##	* Retrieve the right percentage from gnucash file
    ##	* Support multiple taxes
    $text .= '\multicolumn{5}{r|}{zuzglich 19\% Umsatzsteuer} &'.sprintf("%0.2f", $invoice->getTaxes()).' '.$cur.'\\\\'."\n";
    $text .= '\hline\hline'."\n";
    $text .= '\multicolumn{5}{r|}{Gesamtsumme} &'.sprintf("%0.2f", $invoice->getNetSum()+$invoice->getTaxes()).' '.$cur.'\\\\'."\n";
    $text .= '\end{longtable}'."\n";
    return $text;
}

sub replace($$) {
    my ($template, $invoice) = @_;
    
    my $file = new IO::File "< $template" || die "Unable to open template";

    while ( my $l = $file->getline() ) {
        my $id = $invoice->getID();
        my $date = l_enc( time2str("%e.%L.%Y", $invoice->getPostingDate()) );
        my $address = l_enc( $invoice->getCustomer()->getAddress() );
        $address =~ s/\n/\\\\/g;
        my $table = createLaTeXTable($invoice);

        $l =~ s/__GNUCASH-INVOICE-ID__/$id/;
        $l =~ s/__GNUCASH-POSTING-DATE__/$date/;
        $l =~ s/__GNUCASH-CUSTOMER__/$address/;
        $l =~ s/__GNUCASH-TABLE__/$table/;

        print $l;
    }
    $file->close();
}

main($ARGV[0], $ARGV[1], $ARGV[2]);
