+% if (!printable($m)) {
+<h2>Contents</h2>
+<ul>
+% if ($optimise) {
+ <li><a href="#plan">Voyage trading plan</a>
+ <ul>
+ <li><a href="#summary">Summary statistics</a>
+ <li>Printable:
+ <input type=submit name=printable_pdf value="PDF">
+ <input type=submit name=printable_html value="HTML">
+ <input type=submit name=printable_ps value="PostScript">
+ <input type=submit name=printable_pdf2 value="PDF 2-up">
+ <input type=submit name=printable_ps2 value="PostScript 2-up">
+ </ul>
+% }
+ <li><a href="#dataage">Data age summary</a>
+ <li><a href="#trades">Relevant trades</a>
+</ul>
+% } else {
+% my @tl= gmtime $now or die $!;
+<p>
+Generated by YARRG at <strong><%
+ sprintf "%04d-%02d-%02d %02d:%02d:%02d UTC",
+ $tl[5]+1900, @tl[4,3,2,1,0]
+ |h %></strong>.
+% }
+
+% if ($optimise) { # ========== TRADING PLAN ==========
+%
+% my $iquery= $dbh->prepare('SELECT islandname FROM islands
+% WHERE islandid = ?');
+% my %da_ages;
+% my $total_total= 0;
+% my $total_dist= 0;
+%
+<h2><a name="plan">Voyage trading plan</a></h2>
+
+<table rules=groups <% printable($m) ? 'width=100%' : '' %> >
+% my $tbody= sub {
+% if (!printable($m)) { return '<tbody>'; }
+%# return "<tr><td colspan=7><hr>";
+% my ($c)= qw(40 00)[$_[0]];
+% return "<tr><td bgcolor=\"#${c}${c}${c}\" height=1 colspan=7>";
+% };
+%
+% foreach my $i (0..$#islandids) {
+<% $tbody->(1) %>
+<tr><td colspan=4>
+% $iquery->execute($islandids[$i]);
+% my ($islandname) = $iquery->fetchrow_array();
+% if (!$i) {
+<strong>Start at <% $islandname |h %></strong>
+% } else {
+% my $this_dist= $distance->($islandids[$i-1],$islandids[$i]);
+% $total_dist += $this_dist;
+<%perl>
+ my $total_value= 0;
+ foreach my $sf (@subflows) {
+ next unless $sf->{Org} < $i && $sf->{Dst} >= $i;
+ $total_value +=
+ $sf->{OptQty} * $sf->{Flow}{'dst_price'};
+ }
+</%perl>
+<strong>Sail to <% $islandname |h %></strong>
+- <% $this_dist |h %> leagues,
+ <% $total_value %>poe at risk
+ </td>
+% }
+<%perl>
+ my $age_reported= 0;
+ my %flowlists;
+ #print "<tr><td colspan=6>" if $qa->{'debug'};
+ foreach my $od (qw(org dst)) {
+ #print " [[ i $i od $od " if $qa->{'debug'};
+ foreach my $sf (@subflows) {
+ my $f= $sf->{Flow};
+ next unless $sf->{ucfirst $od} == $i;
+ #print " FLOW $f->{Ix} SUB #$sf->{Org}..$sf->{Dst} "
+ # if $qa->{'debug'};
+ next unless $sf->{OptQty};
+ my $arbitrage= $f->{'org_id'} == $f->{'dst_id'};
+ die if $arbitrage and $sf->{Org} != $sf->{Dst};
+ my $price= $f->{"${od}_price"};
+ my $stallname= $f->{"${od}_stallname"};
+ my $todo= \$flowlists{$od}{
+ $f->{'commodname'},
+ (sprintf "%07d", ($od eq 'dst' ?
+ 9999999-$price : $price)),
+ $stallname
+ };
+ $$todo= {
+ Qty => 0,
+ orgArbitrage => 0,
+ dstArbitrage => 0,
+ } unless $$todo;
+ $$todo->{'commodname'}= $f->{'commodname'};
+ $$todo->{'stallname'}= $stallname;
+ $$todo->{Price}= $price;
+ $$todo->{Timestamp}= $f->{"${od}_timestamp"};
+ $$todo->{Qty} += $sf->{OptQty};
+ $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
+ $$todo->{Stalls}= $f->{"${od}Stalls"};
+ $$todo->{"${od}Arbitrage"}= 1 if $arbitrage;
+ }
+ #print "]] " if $qa->{'debug'};
+ }
+ #print "</tr>" if $qa->{'debug'};
+
+ my ($total, $total_to_show);
+ my $dline= 0;
+ my $show_total= sub {
+ my ($totaldesc, $sign) = @_;
+ if (defined $total) {
+ die if defined $total_to_show;
+ $total_total += $sign * $total;
+ $total_to_show= [ $totaldesc, $total ];
+ $total= undef;
+ }
+ $dline= 0;
+ };
+ my $show_total_now= sub {
+ my ($xinfo) = @_;
+ return unless defined $total_to_show;
+ my ($totaldesc,$totalwas) = @$total_to_show;
+</%perl>
+<tr>
+<td colspan=1>
+<td colspan=2><% $xinfo %>
+<td colspan=2 align=right><% $totaldesc %>
+<td align=right><% $totalwas |h %> total
+<%perl>
+ $total_to_show= undef;
+ };
+</%perl>
+% my $show_flows= sub {
+% my ($od,$arbitrage,$collectdeliver) = @_;
+% my $todo= $flowlists{$od};
+% return unless $todo;
+% foreach my $tkey (sort keys %$todo) {
+% my $t= $todo->{$tkey};
+% next if $t->{"${od}Arbitrage"} != $arbitrage;
+% $show_total_now->('');
+% if (!$age_reported++) {
+% my $age= $now - $t->{Timestamp};
+% my $cellid= "da_${i}";
+% $da_ages{$cellid}= $age;
+<td colspan=2>\
+(Data age: <span id="<% $cellid %>"><% prettyprint_age($age) %></span>)
+% } elsif (!defined $total) {
+% $total= 0;
+<% $tbody->(0) %>
+% }
+% $total += $t->{Total};
+% my $span= 0 + keys %{ $t->{Stalls} };
+% my $td= "td rowspan=$span";
+% tr_datarow($m,$dline);
+<<% $td %>><% $collectdeliver %>
+<<% $td %>><% $t->{'commodname'} |h %>
+%
+% my @stalls= sort keys %{ $t->{Stalls} };
+% my $pstall= sub {
+% my $name= $stalls[$_[0]];
+<td><% $name |h %>
+% };
+%
+% $pstall->(0);
+<<% $td %> align=right><% $t->{Price} |h %> poe ea.
+<<% $td %> align=right><% $t->{Qty} |h %> unit(s)
+<<% $td %> align=right><% $t->{Total} |h %> total
+%
+% foreach my $stallix (1..$#stalls) {
+% tr_datarow($m,$dline);
+% $pstall->($stallix);
+% }
+%
+% $dline ^= 1;
+% }
+% };
+<%perl>
+
+ $show_flows->('dst',0,'Deliver'); $show_total->('Proceeds',1);
+ $show_flows->('org',1,'Collect'); $show_total->('(Arbitrage) outlay',-1);
+ $show_flows->('dst',1,'Deliver'); $show_total->('(Arbitrage) proceeds',1);
+ $show_flows->('org',0,'Collect'); $show_total->('Outlay',-1);
+ my $totals= '';
+ if ($i < $#islandids) {
+ $totals .= "In hold $sail_total[$i]{mass}kg,".
+ " $sail_total[$i]{volume} l";
+ my $delim= '; spare ';
+ my $domv= sub {
+ my ($max, $got, $units) = @_;
+ return unless defined $max;
+ $totals .= $delim;
+ $totals .= sprintf "%g %s", ($max-$got), $units;
+ $delim= ', ';
+ };
+ $domv->($routeparams->{MaxMass}, $sail_total[$i]{mass}, 'kg');
+ $domv->($routeparams->{MaxVolume}, $sail_total[$i]{volume}, 'l');
+ $totals .= ".\n";
+ }
+ $show_total_now->($totals);
+}
+</%perl><a name="summary"></a>
+<% $tbody->(1) %><tr>
+<td colspan=2>Total distance: <% $total_dist %> leagues.
+<td colspan=3 align=right>Overall net cash flow
+<td align=right><strong><%
+ $total_total < 0 ? -$total_total." loss" : $total_total." gain"
+ %></strong>
+</table>
+<& query_age:dataages, id2age => \%da_ages &>
+Expected average profit:
+ approx. <strong><% sprintf "%d", $expected_total_profit %></strong> poe
+ (considering expected losses, but ignoring rum consumed)
+%
+% } # ========== TRADING PLAN ==========
+
+% if (!printable($m)) {
+<h2><a name="dataage">Data age summary</a></h2>
+<%perl>
+ my $sth_i= $dbh->prepare(<<END);
+ SELECT archipelago, islandid, islandname, timestamp
+ FROM uploads NATURAL JOIN islands
+ WHERE islandid = ?
+END
+ my $sth_a= $dbh->prepare(<<END);
+ SELECT archipelago, islandid, islandname, timestamp
+ FROM uploads NATURAL JOIN islands
+ WHERE archipelago = ?
+ ORDER BY islandname
+END
+ my $ix=$#islandids;
+ my $sth_current;
+ my %idone;
+ my $fetchrow= sub {
+ for (;;) {
+ if ($sth_current) {
+ my $row= $sth_current->fetchrow_hashref();
+ if ($row) {
+ next if $idone{$row->{'islandid'}}++;
+ return $row;
+ }
+ }
+ return undef if $ix < 0;
+ my $iid= $islandids[$ix];
+ if (defined $iid) {
+ $sth_i->execute($iid);
+ $sth_current= $sth_i;
+ } else {
+ my $arch= $archipelagoes[$ix];
+ die unless defined $arch && length $arch;
+ $sth_a->execute($arch);
+ $sth_current= $sth_a;
+ }
+ $ix--;
+ }
+ };
+</%perl>
+<&| query_age:agestable, now => $now, fetchrow => $fetchrow &>
+Islands shown in reverse order of visits.<br>
+</&>
+% }
+
+% if (!printable($m)) {
+% my %ts_sortkeys;
+% {