+#!/usr/bin/perl -w
+
+sub gettok {
+ if(/\G\"([^\"]*)\"\s+/gc) {
+ return $1;
+ } elsif(/\G(\S+)\s+/gc) {
+ return $1;
+ } else {
+ return undef;
+ }
+}
+
+while(@ARGV) {
+ if(!open IF, $ARGV[0]) {
+ last;
+ }
+ shift @ARGV;
+ while(<IF>) {
+ if(/^prod\s+/g) {
+ $cp = {};
+ $id = gettok;
+ $cp->{"s"} = gettok;
+ $cp->{"p"} = gettok;
+ $cp->{"w"} = gettok;
+ $prod{$id} = $cp;
+ }
+ if(/^prt\s+/g) {
+ $id = gettok;
+ $prts{$id} = {"name" => gettok, "items" => {}};
+ }
+ if(/^item\s+/g) {
+ $prt = gettok;
+ if(!exists $prts{$prt}) {
+ die "invalid participant $prt in item line";
+ }
+ $a = 1;
+ while($t = gettok) {
+ if($t =~ /^\d+(\.\d+)?$/) {
+ $a = $t;
+ next;
+ }
+ $i = $prts{$prt}->{"items"};
+ if(exists $i->{$t}) {
+ $i->{$t} += $a;
+ } else {
+ $i->{$t} = $a;
+ }
+ $a = 1;
+ }
+ }
+ if(/^freight\s+(\d+)/) {
+ $fc = $1;
+ }
+ if(/^realprice\s+(\d+)/) {
+ $rp = $1;
+ }
+ }
+ close IF;
+}
+
+$yt = $wt = 0;
+for $p (sort keys %prts) {
+ $I = $prts{$p}->{"items"};
+ $sp = $sw = 0;
+ for $i (keys %$I) {
+ $yt += $I->{$i} * $prod{$i}->{"p"};
+ $wt += $I->{$i} * $prod{$i}->{"w"};
+ $sp += $I->{$i} * $prod{$i}->{"p"};
+ $sw += $I->{$i} * $prod{$i}->{"w"};
+ }
+ $prts{$p}->{"sp"} = $sp;
+ $prts{$p}->{"sw"} = $sw;
+}
+$er = (($yt + $fc) / $rp);
+
+while($cmd = shift @ARGV) {
+ if($cmd eq "stat") {
+ print "Total: $yt ($rp SEK), $wt g\n";
+ print "Freight: $fc\n";
+ print ("Ratio: " . $er . "\n");
+ } elsif($cmd eq "judgement") {
+ for $prt (keys %prts) {
+ print "$prts{$prt}->{name}:\n";
+ $sp = $prts{$prt}->{"sp"};
+ $sw = $prts{$prt}->{"sw"};
+ printf "\tProducts ¥%i (%i SEK), %i g\n", $sp, ($sp / $er), $sw;
+ $ff = $sw / $wt;
+ $sfc = $fc * $ff;
+ printf "\tFreight ¥%i (%.1f%%, %i SEK)\n", $sfc, (100 * $ff), ($sfc / $er);
+ $st = $sfc + $sp;
+ printf "\tTotal: ¥%i (%i SEK)\n", $st, ($st / $er);
+ }
+ } elsif($cmd eq "rcpt-txt") {
+ $prt = shift @ARGV;
+ $I = $prts{$prt}->{"items"};
+ for $i (keys %$I) {
+ printf "%.1f %s\t\t%i (%i SEK)\t%i g\n", $I->{$i}, $prod{$i}->{"s"}, $I->{$i} * $prod{$i}->{"p"}, $I->{$i} * $prod{$i}->{"p"} / $er, $I->{$i} * $prod{$i}->{"w"};
+ }
+ } elsif($cmd eq "rcpt-tex") {
+ print <<EOF
+\\documentclass[a4paper,11pt]{article}
+\\usepackage[swedish]{babel}
+\\usepackage[T1]{fontenc}
+\\usepackage[utf8x]{inputenc}
+\\usepackage{longtable}
+\\title{Kvitto}
+\\begin{document}
+\\begin{longtable}{lllllll}
+Artikel & Antal & \\\'{A}-pris & Pris & Pris (SEK) & Vikt \\\\
+\\hline
+EOF
+;
+ $prt = shift @ARGV;
+ $I = $prts{$prt}->{"items"};
+ $ni = 0;
+ for $i (keys %$I) {
+ printf "%s & ", $prod{$i}->{"s"};
+ printf "%.1f & ", $I->{$i};
+ printf "%i & ", $prod{$i}->{"p"};
+ printf "%i & ", $prod{$i}->{"p"} * $I->{$i};
+ printf "%.2f & ", $prod{$i}->{"p"} * $I->{$i} / $er;
+ printf "%i & ", $prod{$i}->{"w"} * $I->{$i};
+ #printf "%.1f %s\t\t%i (%i SEK)\t%i g", $I->{$i}, $prod{$i}->{"s"}, $I->{$i} * $prod{$i}->{"p"}, $I->{$i} * $prod{$i}->{"p"} / $er, $I->{$i} * $prod{$i}->{"w"};
+ print "\\\\\n";
+ $ni += $I->{$i};
+ }
+ print "\\hline\nDelsumma & ";
+ printf "%i & & ", $ni;
+ printf "%i & ", $prts{$prt}->{"sp"};
+ printf "%.2f & ", $prts{$prt}->{"sp"} / $er;
+ printf "%i & ", $prts{$prt}->{"sw"};
+ print "\\\\\n";
+ $ff = $prts{$prt}->{"sw"} / $wt;
+ printf "Frakt & %.1f\\%% & ", 100 * $ff;
+ printf "%i & ", $fc;
+ printf "%i & ", $fc * $ff;
+ printf "%.2f", $fc * $ff / $er;
+ print "\\\\\n";
+ print "\\hline\nTotalt & & & ";
+ printf "%i & ", $fc * $ff + $prts{$prt}->{"sp"};
+ printf "\\textbf{%.2f} & ", ($fc * $ff + $prts{$prt}->{"sp"}) / $er;
+ print "\n";
+ print "\\end{longtable}\n";
+ print "\n";
+ print "\\end{document}\n";
+ } else {
+ print STDERR "usage: ordershare DESCFILE... COMMAND...\n";
+ print STDERR "\tcommands:\n";
+ print STDERR "\tstat General statistics\n";
+ print STDERR "\tjudgement Overview for each participant\n";
+ print STDERR "\trcpt-txt PARTICIPANT Receipt in text format\n";
+ print STDERR "\trcpt-tex PARTICIPANT Receipt in LaTeX format\n";
+ }
+}