-% 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= { } 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};
-% }
-% if (%todo && !$age_reported++) {
-<td colspan=2>
-% my $age= $now - (values %todo)[0]->{Timestamp};
-% my $cellid= "da_${i}";
-% $da_ages{$cellid}= $age;
+<%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", $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>\