#!/usr/bin/perl -s

################################################################
# Fazedor dos recibos pra etaps
#
# Se nao funcionar, nao fui eu que fiz.
# Se funcionar, fui O:-) <rubenfonseca@di.uminho.pt>
################################################################

use strict;
use warnings;
use Text::CSV_XS;
use utf8;

# Ligar a opcao duplex se quisermos imprimir uma pagina
# em branco por cada pagina impessa... Util se a impressora
# insistir em imprimir dum lado e do outro...

our ($duplex);

my $csv = Text::CSV_XS->new({binary => 1});
my $attendants = shift or die "Need a attendants CSV file";
my $payments   = shift or die "Need a payments CSV file";

# Carregar dados para a DB
my $db = {};
open F, "<$payments";
while(<F>) {
	if($csv->parse($_)) {
		my @fields = $csv->fields;
		# Protect some symbols LaTex doesn't like
		map { s!\\!\$\\backslash\$!g } @fields;
		map { s!([\{\}\_\^\$\#\%\&])!\\$1!g } @fields;

		$db->{$fields[0]} = \@fields;
	} else {
		die "parse error\n";
	}
}
close F;

# Preambulo
open my $g, ">out.tex";
print $g <<EOF;
\\documentclass{article}
\\usepackage{a4wide}

\\begin{document}
\\newlength{\\headwidth}
EOF

# Processar o novo ficheiro
$csv = Text::CSV_XS->new({binary => 1});
open F, "<$attendants";
<F>;
while(<F>) {
	if($csv->parse($_)) {
		my @fields = $csv->fields;

		# Protect some symbols LaTex doesn't like
		map { s!\\!\$\\backslash\$!g } @fields;
		map { s!([\{\}\_\^\$\#\%\&])!\\$1!g } @fields;

		my $cromo = $db->{$fields[0]};
		
		unless($cromo) {
			warn ">>> $fields[0] not found\n";
			next;
		}

		if($db->{$fields[0]}[1] eq 'conference') {
			generate1($g, $fields[1], $fields[3], $fields[5], $db->{$fields[0]}[4]);
			print $g "\\mbox{}\\newpage\n" if($duplex);
		}

		if($db->{$fields[0]}[1] eq 'hotel') {
			generate2($g, $fields[1], $fields[3], $fields[5], $db->{$fields[0]}[4]);
			print $g "\\mbox{}\\newpage\n" if($duplex);
			print $g "\\mbox{}\\newpage\n" if($duplex);
		}
	} else {
		die "parse error\n";
	}
}
close F;

print $g <<EOF;
\\end{document}
EOF

sub generate1 {
	my($g, $a, $b, $c, $v) = @_;

	print $g <<EOF;
\\pagestyle{empty}
\\headheight 0pt
\\headsep 0pt
\\def\\\@texttop{\\ifnum\\c\@page=1\\vskip 0pt\\relax\\fi}



 \\setlength{\\headwidth}{\\textwidth}
 \\addtolength{\\headwidth}{2cm}

 \\mbox{}
 \\vskip 3cm

 \\hfill\\makebox[0pt][l]{\\hspace{1.3cm}\\makebox[0pt][r]{
     \\parbox{\\headwidth}{
       \\begin{tabular}[b]{l}
         \\Huge\\bf ETAPS 2007\\\\[0.2cm]
         \\huge\\bf March 24 -- April 1, 2007, Braga, Portugal \\\\[0.2cm]
         \\huge\\bf Registration Receipt\\\\[0.2cm]
         \\\\[0.5cm]
         We have received from
       \\end{tabular}\\hfill
       \\rule{\\headwidth}{1.5pt}
       \\\\[0.5cm]
       \\begin{tabular*}{\\headwidth}[b]{l}
         $a\\\\[0.1cm]
         $b\\\\[0.1cm]
         $c\\\\[0.1cm]
       \\end{tabular*}\\hfill
       \\\\[0.5cm]
       \\begin{tabular*}{\\headwidth}{l\@{\\extracolsep{\\fill}}r}
         \\bf Description & \\hfill \\bf  Total Fee\\\\
       \\end{tabular*}
       \\rule{\\headwidth}{1.5pt}
       \\\\[0.2cm]
       \\begin{tabular*}{\\headwidth}{l\@{\\extracolsep{\\fill}}r}
         Conferences and Satelite Events & $v\\ EUR
         \\\\[0.5cm]
         {\\bf Paid in Full}
         \\\\[0.5cm]
         The ETAPS 2007 Organising Committee
         \\end{tabular*}
       }
     }
   }

   \\newpage
EOF
}

sub generate2 {
	my($g, $a, $b, $c, $v) = @_;

	print $g <<EOF;
\\pagestyle{empty}
\\headheight 0pt
\\headsep 0pt
\\def\\\@texttop{\\ifnum\\c\@page=1\\vskip 0pt\\relax\\fi}
\\setlength{\\headwidth}{\\textwidth}
\\addtolength{\\headwidth}{2cm}

 \\mbox{}
 \\vskip 3cm

 \\hfill\\makebox[0pt][l]{\\hspace{1.3cm}\\makebox[0pt][r]{
     \\parbox{\\headwidth}{
       \\begin{tabular}[b]{l}
         \\Huge\\bf ETAPS 2007\\\\[0.2cm]
         \\huge\\bf March 24 -- April 1, 2007, Braga, Portugal \\\\[0.2cm]
         \\huge\\bf Accommodation Receipt\\\\[0.2cm]
         \\\\[0.5cm]
         We have received from
       \\end{tabular}\\hfill
       \\rule{\\headwidth}{1.5pt}
       \\\\[0.5cm]
       \\begin{tabular*}{\\headwidth}[b]{l}
         $a\\\\[0.1cm]
         $b\\\\[0.1cm]
         $c\\\\[0.1cm]
       \\end{tabular*}\\hfill
       \\\\[0.5cm]
       \\begin{tabular*}{\\headwidth}{l\@{\\extracolsep{\\fill}}r}
         \\bf Description & \\hfill \\bf  Total Fee\\\\
       \\end{tabular*}
       \\rule{\\headwidth}{1.5pt}
       \\\\[0.2cm]
       \\begin{tabular*}{\\headwidth}{l\@{\\extracolsep{\\fill}}r}
         ETAPS 2007 Accomodation & $v EUR
         \\\\[0.5cm]
         {\\bf Paid in Full}
         \\\\[0.5cm]
         The ETAPS 2007 Organising Committee
         \\end{tabular*}
       }
     }
   }

   \\newpage
EOF
}
