chiark / gitweb /
Sortable commodity price table
[ypp-sc-tools.web-live.git] / yarrg / web / query_commod
index 9a7d2bc36dc7bad67e67bfbc6c92688e6902862a..052d019f998b137cd103f806c46bbbb0f3e0fd4a 100644 (file)
@@ -52,6 +52,7 @@ my $qa= \%ARGS;
 <h1>Commodity enquiry</h1>
 
 % $prselector->('BuySell');
+% $prselector->('ShowBlank');
 
 %#---------- textbox, user enters route as string ----------
 % if (!$qa->{Dropdowns}) {
@@ -88,18 +89,22 @@ $someresults->();
 
 #---------- actually compute the results and print them ----------
 
+my $onloads= "";
+
 foreach my $bs (split /_/, $ARGS{BuySell}) {
        $bs =~ m/^(buy|sell)$/ or die;
        $bs= $1;
        my ($ascdesc) = ($bs eq 'buy')
                ? ('DESC')
                : ('ASC');
-
+       my $joinkind= $ARGS{ShowBlank} eq 'show'
+               ? 'LEFT OUTER JOIN' : 'INNER JOIN';
        my $islands= $dbh->prepare(
-       "SELECT islandid, archipelago, islandname, sum(qty) as tqty
-               FROM $bs NATURAL JOIN islands
-               WHERE commodid = ?
-               GROUP BY islandid,
+       "SELECT islands.islandid AS islandid, archipelago, islandname,
+                       sum(qty) as tqty
+               FROM islands $joinkind $bs offers
+               ON islands.islandid == offers.islandid AND commodid == ?
+               GROUP BY islands.islandid,
                ORDER BY archipelago, islandname"
                );
 
@@ -115,51 +120,108 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
 
 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
 
-<table>
+<table id="<% $bs %>_table">
 <tr>
+<th colspan=3>
+<th colspan=2>Prices
+<th colspan=3>Quantity at price
+<tr id="<% $bs %>_table_thr">
 <th>Archipelago
 <th>Island
-<th>Unique best stall
-<th>Best price
-<th>Qty at best
-<th>Median price
-<th>Total qty
+<th>Stall(s)
+<th>Best
+<th>Median
+<th>Best
+<th>+/-10%
+<th>Any
 </tr>
 %      $islands->execute($commodid);
 %      my $island;
+%      my %ts_sortkeys;
 %      while ($island= $islands->fetchrow_hashref) {
 %              my $islandid= $island->{'islandid'};
 %              $offers->execute($commodid, $islandid);
-%              my ($offer, $best, $median);
+%              my ($offer, $bestprice, $marginal, @beststalls);
 %              my $tqty= $island->{'tqty'};
-%              my $cqty= 0;
+%              my $cqty= '';
+%              my $bestqty= '';
+%              my $approxqty= '';
+%              my $median= '-';
 %              while ($offer= $offers->fetchrow_hashref) {
-%                      if (!$best) {
-%                              $best= { 'price' => $offer->{'price'} };
+%                      my $price= $offer->{'price'};
+%                      my $qty= $offer->{'qty'};
+%                      length $bestqty or $bestprice= $price;
+%                      if ($price == $bestprice) {
+%                              $bestqty += $qty;
+%                              push @beststalls, $offer->{'stallname'};
 %                      }
-%                      if ($offer->{'price'} == $best->{'price'}) {
-%                              $best->{'qty'} += $offer->{'qty'};
-%                              push @{ $best->{'stalls'} },
-%                                      $offer->{'stallname'};
+%                      $cqty += $qty;
+%                      if ($cqty*2 >= $tqty && $median eq '-') {
+%                              $median= $price;
 %                      }
-%                      $cqty += $offer->{'qty'};
-%                      if ($cqty*2 >= $tqty && !defined $median) {
-%                              $median= $offer->{'price'};
+%                      if ($bestprice*9 <= $price*10 and
+%                          $price*10 <= $bestprice*11) {
+%                              $approxqty += $qty;
 %                      }
 %              }
+%              my $stallname;
+%
+%              my $rowid= "id_${bs}_$islandid";
+%              my $s= [ ];
+%
+%              $s->[2]= sprintf "%06d", scalar @beststalls;
+%              if (!@beststalls) {
+%                      $stallname= '-';
+%              } elsif (@beststalls==1) {
+%                      $stallname= $beststalls[0];
+%                      $s->[2] .= " $stallname";
+%              } else {
+%                      $stallname= sprintf "%d offers", scalar @beststalls;
+%              }
+%
 %              $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
-%              my $nstalls= @{ $best->{'stalls'} };
-<tr> <td><% $island->{'archipelago'} |h %>
-     <td><% $island->{'islandname'} |h %>
-     <td><% $nstalls==1 ? $best->{'stalls'}[0] : "$nstalls offers" |h %>
-     <td><% $best->{'price'} %>
-     <td><% $best->{'qty'} %>
-     <td><% $median %>
-     <td><% $tqty %>
+<tr id=<% $rowid %> >
+     <td><% $s->[0]= $island->{'archipelago'} |h %>
+     <td><% $s->[1]= $island->{'islandname'} |h %>
+     <td><%          $stallname |h %>
+     <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
+     <td><% $s->[4]= $median %>
+     <td><% $s->[5]= $bestqty %>
+     <td><% $s->[6]= $approxqty %>
+     <td><% $s->[7]= $cqty %>
 </tr>
+%              for my $cix (0..$#$s) {
+%                      $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
+%              }
 %      }
 </table>
 
+<& tabsort,    table => "${bs}_table", sortkeys => "${bs}_sortkeys",
+               throw => "${bs}_table_thr", cols => [
+       {}, {},
+       { DoReverse => 1 },
+       { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
+       { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" },
+       { DoReverse => 1, Numeric => 1 },
+       { DoReverse => 1, Numeric => 1 },
+       { DoReverse => 1, Numeric => 1 },
+       ] &>
+<&| script &>
+  <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
+  function ts_Pricemap_<% $bs %>(price) {
+    if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
+    return price;
+  }
+</&>
+%      $onloads .= "    ts_onload__${bs}_table();\n";
+
 <%perl>
 }
 </%perl>
+
+<&| script &>
+  function all_onload() {
+<% $onloads %>
+  }
+  window.onload= all_onload;
+</&>