% my %da_ages;
%
<h1>Voyage trading plan</h1>
-<table>
+<table rules=groups>
% foreach my $i (0..$#islandids) {
+<tbody>
<tr><td colspan=3><strong>
% $iquery->execute($islandids[$i]);
% my ($islandname) = $iquery->fetchrow_array();
Sail to <% $islandname |h %>
% }
</strong>
-% my $age_reported= 0;
-% foreach my $od (qw(dst org)) {
-% my $sign= $od eq 'dst' ? -1 : +1;
-% my %todo;
-% foreach my $f (@flows) {
-% next if $f->{Suppress};
-% next unless $f->{"${od}_id"} == $islandids[$i];
-% next unless $f->{OptQty};
-% my $price= $f->{"${od}_price"};
-% my $stallname= $f->{"${od}_stallname"};
-% my $todo= \$todo{ $f->{'commodname'},
-% (sprintf "%07d", $price),
-% $stallname };
-% $$todo= { Qty => 0 } unless $$todo;
-% $$todo->{'commodname'}= $f->{'commodname'};
-% $$todo->{'stallname'}= $stallname;
-% $$todo->{Price}= $price;
-% $$todo->{Timestamp}= $f->{"${od}_timestamp"};
-% $$todo->{Qty} += $f->{OptQty};
-% $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
-% $$todo->{Stalls}= $f->{"${od}Stalls"};
-% }
-% if (%todo && !$age_reported++) {
-% my $age= $now - (values %todo)[0]->{Timestamp};
-% my $cellid= "da_${i}";
-% $da_ages{$cellid}= $age;
-<td colspan=2>\
+<%perl>
+ my $age_reported= 0;
+ my %flowlists;
+ foreach my $od (qw(org dst)) {
+ foreach my $f (@flows) {
+ next if $f->{Suppress};
+ next unless $f->{"${od}_id"} == $islandids[$i];
+ next unless $f->{OptQty};
+ 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} += $f->{OptQty};
+ $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
+ $$todo->{Stalls}= $f->{"${od}Stalls"};
+ if ($f->{'org_id'} == $f->{'dst_id'}) {
+ $$todo->{"${od}Arbitrage"}= 1;
+ }
+ }
+ }
+
+ my $total;
+ my $dline= 0;
+ my $show_flows= sub {
+ my ($od,$arbitrage,$collectdeliver) = @_;
+</%perl>
+%
+% my $todo= $flowlists{$od};
+% return unless $todo;
+% foreach my $tkey (sort keys %$todo) {
+% my $t= $todo->{$tkey};
+% next if $t->{"${od}Arbitrage"} != $arbitrage;
+% if (!$age_reported++) {
+% my $age= $now - $t->{Timestamp};
+% my $cellid= "da_${i}";
+% $da_ages{$cellid}= $age;
+<td colspan=3>\
(Data age: <span id="<% $cellid %>"><% prettyprint_age($age) %></span>)
-% }
-% my $total= 0;
-% my $dline= 0;
-% foreach my $tkey (sort keys %todo) {
-% my $t= $todo{$tkey};
+% } elsif (!defined $total) {
+% $total= 0;
+<tbody>
+% }
% $total += $t->{Total};
% my $span= 0 + keys %{ $t->{Stalls} };
% my $td= "td rowspan=$span";
<tr class="datarow<% $dline %>">
-<<% $td %>><% $od eq 'org' ? 'Collect' : 'Deliver' %>
+<<% $td %>><% $collectdeliver %>
<<% $td %>><% $t->{'commodname'} |h %>
%
% my @stalls= sort keys %{ $t->{Stalls} };
% my $pstall= sub {
% my $name= $stalls[$_[0]];
-% my $avail= $t->{Stalls}{$name};
-<td align=right><% $avail |h %> <% $od eq 'org' ? 'avail.' : 'wanted' %>
<td><% $name |h %>
% };
%
% $pstall->(0);
-<<% $td %> align=right><% $t->{Price} |h %> each
+<<% $td %> align=right><% $t->{Price} |h %> poe ea.
<<% $td %> align=right><% $t->{Qty} |h %> unit(s)
<<% $td %> align=right><% $t->{Total} |h %> total
%
%
% $dline ^= 1;
% }
-% if (%todo) {
+% };
+% my $show_total= sub {
+% my ($totaldesc)= @_;
+% if (defined $total) {
<tr>
-<td colspan=5><td align=right><% $od eq 'org' ? 'Outlay' : 'Proceeds' %>
+<td colspan=3>
+<td colspan=2 align=right><% $totaldesc %>
<td align=right><% $total |h %> total
% }
-% }
-% }
+% $total= undef;
+% $dline= 0;
+<%perl>
+ };
+
+ $show_flows->('dst',0,'Deliver'); $show_total->('Proceeds');
+ $show_flows->('org',1,'Collect'); $show_total->('(Arbitrage) outlay');
+ $show_flows->('dst',1,'Deliver'); $show_total->('(Arbitrage) proceeds');
+ $show_flows->('org',0,'Collect'); $show_total->('Outlay');
+
+}
+</%perl>
</table>
<& query_age:dataages, id2age => \%da_ages &>
%