+% 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 class="data" 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=5>
+% $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=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->{'commodname'}= $f->{'commodname'};
+ $$todo->{'posinclass'}= '';
+ if ($f->{'posinclass'}) {
+ my $findclass= $dbh->prepare(<<END);
+SELECT commodclass, maxposinclass FROM commodclasses WHERE commodclassid = ?
+END
+ $findclass->execute($f->{'commodclassid'});
+ my $classinfo= $findclass->fetchrow_hashref();
+ $$todo->{'posinclass'}=
+ sprintf "(%s %d/%d)",
+ $classinfo->{'commodclass'},
+ $f->{'posinclass'},
+ $classinfo->{'maxposinclass'}
+ if $classinfo->{'maxposinclass'} > 9;
+ }
+ $$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=3><% $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 %>
+<<% $td %>><% $t->{'posinclass'} |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>