+% if (!printable($m)) {
+<h2>Contents</h2>
+<ul>
+% if ($optimise) {
+ <li><a href="#summary">Summary</a>
+ <li><a href="#plan">Voyage trading plan</a>
+ <ul>
+ <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">Relevant data ages</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 ==========
+<%perl>
+my $iquery= $dbh->prepare('SELECT islandname FROM islands
+ WHERE islandid = ?');
+my %da_ages;
+my $total_total= 0;
+my $total_dist= 0;
+my @oldest= (-1, 'nowhere');
+my $plan_html= '';
+
+my $plan_table_info= printable($m) ? 'width=100%' : '';
+$plan_html .= <<END;
+<table class="data" rules=groups $plan_table_info >
+END
+
+my $tbody= sub {
+ if (!printable($m)) { return '<tbody>'; }
+ my ($c)= qw(40 00)[$_[0]];
+ return "<tr><td bgcolor=\"#${c}${c}${c}\" height=1 colspan=7>";
+};
+
+foreach my $i (0..$#islandids) {
+ $plan_html .= $tbody->(1);
+ $plan_html .= "<tr>\n";
+ $iquery->execute($islandids[$i]);
+ my ($islandnamepr)= encode_entities( $iquery->fetchrow_array() );
+
+ if (!$i) {
+ $plan_html .= <<END;
+<td colspan=2>
+<strong>Start at $islandnamepr</strong>
+<td colspan=2><a href="docs#posinclass">[what are these codes?]</a>
+<td>
+END
+ } else {
+ my $this_dist= $distance->($islandids[$i-1],$islandids[$i]);
+ $total_dist += $this_dist;
+ $plan_html .= <<END;
+<td colspan=5>
+END
+ my $total_value= 0;
+ foreach my $sf (@subflows) {
+ next unless $sf->{Org} < $i && $sf->{Dst} >= $i;
+ $total_value +=
+ $sf->{OptQty} * $sf->{Flow}{'dst_price'};
+ }
+ $plan_html .= <<END;
+<strong>Sail to $islandnamepr</strong>
+- $this_dist leagues, $total_value poe at risk
+ </td>
+END
+ }
+ my $age_reported= 0;
+ my %flowlists;
+ #print "<tr><td colspan=7>" 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}{
+ (sprintf "%010d", $f->{'ordval'}),
+ $f->{'commodname'},
+ (sprintf "%07d", ($od eq 'dst' ?
+ 9999999-$price : $price)),
+ $stallname
+ };
+ $$todo= {
+ Qty => 0,
+ orgArbitrage => 0,
+ dstArbitrage => 0,
+ } unless $$todo;
+ $$todo->{'commodid'}= $f->{'commodid'};
+ $$todo->{'commodname'}= $f->{'commodname'};
+ $$todo->{'posinclass'}= '';
+ my $incl= $f->{'posinclass'};
+
+ my $findclass= $dbh->prepare(<<END);
+SELECT commodclass, maxposinclass FROM commodclasses WHERE commodclassid = ?
+END
+ $findclass->execute($f->{'commodclassid'});
+ my $classinfo= $findclass->fetchrow_hashref();
+ if ($classinfo) {
+ my $clname= $classinfo->{'commodclass'};
+ my $desc= encode_entities(sprintf "%s is under %s",
+ $f->{'commodname'}, $clname);
+ my $abbrev= substr($clname,0,1);
+ if ($incl) {
+ my $maxpic= $classinfo->{'maxposinclass'};
+ $desc.= (sprintf ", commodity %d of %d",
+ $incl, $maxpic);
+ if ($classinfo->{'maxposinclass'} >= 8) {
+ my @tmbs= qw(0 1 2 3 4 5 6 7 8 9);
+ my $tmbi= ($incl+0.5)*$#tmbs/$maxpic;
+ $abbrev.= " ".$tmbs[$tmbi]." ";
+ }
+ }
+ $$todo->{'posinclass'}=
+ "<div class=mouseover title=\"$desc\">"
+ .$abbrev."</div>";
+ }
+ $$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;
+ $plan_html .= <<END;
+<tr>
+<td colspan=1>
+<td colspan=3>$xinfo
+<td colspan=2 align=right>$totaldesc
+<td align=right>$totalwas total
+END
+ $total_to_show= undef;
+ };
+ 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};
+ @oldest= ($age,$islandnamepr) if $oldest[0] < $age;
+ my $cellid= "da_${i}";
+ my $agepr= prettyprint_age($age);
+ $da_ages{$cellid}= $age;
+ $plan_html .= <<END
+<td colspan=2>(Data age: <span id="$cellid">$agepr</span>)
+END
+ } elsif (!defined $total) {
+ $total= 0;
+ $plan_html .= $tbody->(0);
+ }
+ $total += $t->{Total};
+ my $span= 0 + keys %{ $t->{Stalls} };
+ my $td= "td rowspan=$span";
+ my %linkqf= (%{ $qa->{'baseqf'} }, %{ $qa->{'queryqf'} });
+ $linkqf{'query'}= 'commod';
+ $linkqf{'commodstring'}= $t->{'commodname'};
+ $linkqf{'commodid'}= $t->{'commodid'};
+ my $linkqfpr= $quri->(%linkqf);
+ my $commodnamepr= encode_entities($t->{'commodname'});
+ $plan_html .= tr_datarow_s($m,$dline) . <<END;
+<$td>$collectdeliver
+<$td><a href="$linkqfpr">$commodnamepr</a>
+<$td>$t->{'posinclass'}
+END
+ my @stalls= sort keys %{ $t->{Stalls} };
+ my $pstall= sub {
+ my $namepr= encode_entities( $stalls[$_[0]] );
+ $plan_html .= <<END;
+<td>$namepr
+END
+ };
+
+ $pstall->(0);
+ $plan_html .= <<END;
+<$td align=right>$t->{Price} poe ea.
+<$td align=right>$t->{Qty} unit(s)
+<$td align=right>$t->{Total} total
+END
+ foreach my $stallix (1..$#stalls) {
+ tr_datarow_s($m,$dline);
+ $pstall->($stallix);
+ }
+
+ $dline ^= 1;
+ }
+ };
+
+ $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);
+}
+
+my $cashflowpr= $total_total < 0
+ ? -$total_total." loss"
+ : $total_total." gain";
+
+my $max_capital= 0;
+foreach my $cap (map { $_->{capital} } @sail_total) {
+ $max_capital= $cap if $cap > $max_capital;
+}
+
+$da_ages{'oldest'}= $oldest[0];
+
+$plan_html .= $tbody->(1) . <<END;
+<tr>
+<td colspan=3>Total distance: $total_dist leagues.
+<td colspan=3 align=right>Overall net cash flow
+<td align=right><strong>$cashflowpr</strong>
+</table>
+END
+
+</%perl>
+% if (!printable($m)) {
+<h2><a name="summary">Summary</a></h2>
+% }
+
+<table>
+<tr>
+ <td>Distance:
+ <td><strong><% $total_dist %></strong> leagues,
+ <strong><% scalar(@islandids) %></strong> island(s)
+<tr>
+ <td>Planned net cash flow:
+ <td><strong><% $cashflowpr %></strong>
+<tr>
+ <td>Expected profit on average: approx.
+ <td>
+ <strong><% sprintf "%d", $expected_total_profit %></strong> poe
+ (considering expected losses, but ignoring rum consumed)
+<tr>
+ <td>Capital required:
+ <td>
+ <strong><% $max_capital %></strong> poe or less
+<tr>
+ <td>Oldest market data used:
+ <td><strong id="oldest"><% prettyprint_age($oldest[0]) %></strong>
+ (<% $oldest[1] %>)
+<tr>
+ <td colspan=2><% $opt_how %>
+</table>
+<p>
+
+<h2><a name="plan">Voyage trading plan</a></h2>
+<% $plan_html %>
+<& query_age:dataages, id2age => \%da_ages &>
+%
+% } # ========== TRADING PLAN ==========
+
+% if (!printable($m)) {
+<h2><a name="dataage">Relevant data ages</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;
+% {