chiark / gitweb /
routesearch: support circular routes
[ypp-sc-tools.db-live.git] / yarrg / web / query_commod
index 4869e5c50fad00d6222fa0a1ce8e2df110f488a9..3b2bf3554533f40a334d63690dac86f023b16764 100644 (file)
@@ -36,6 +36,7 @@
 <%args>
 $quri
 $dbh
+$commodid => undef;
 $commodstring => '';
 $prselector
 $someresults
@@ -44,7 +45,7 @@ $emsgokorprint
 
 <%perl>
 my $emsg;
-my ($commodname,$commodid);
+my ($commodname,$cmid);
 
 my $qa= \%ARGS;
 </%perl>
@@ -52,26 +53,14 @@ my $qa= \%ARGS;
 <h1>Commodity enquiry</h1>
 
 % $prselector->('BuySell');
-
-%#---------- textbox, user enters route as string ----------
-% if (!$qa->{Dropdowns}) {
-
-Enter commodity (abbreviations are OK):<br>
+% $prselector->('ShowBlank');
 
 <form action="<% $quri->() |h %>" method="get">
 
-<&| qtextstring, qa => $qa, dbh => $dbh,
-    thingstring => 'commodstring', emsgstore => \$emsg,
-    perresult => sub { ($commodname,$commodid)= @_; }
+<& enter_commod, qa => $qa, dbh => $dbh, emsg_r => \$emsg,
+       commodname_r => \$commodname,
+       cmid_r => \$cmid
  &>
- size=80
-</&>
-
-% } else { #---------- dropdowns, user selects from menus ----------
-
-Not yet implemented.
-
-% } #---------- end of dropdowns, now common middle of page code ----------
 
 <input type=submit name=submit value="Go">
 % my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
@@ -82,8 +71,8 @@ Not yet implemented.
 %#========== results ==========
 <%perl>
 
-$emsgokorprint->($emsg) or $commodid=undef;
-return unless defined $commodid;
+$emsgokorprint->($emsg) or $cmid=undef;
+return unless defined $cmid;
 $someresults->();
 
 #---------- actually compute the results and print them ----------
@@ -94,13 +83,13 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
        my ($ascdesc) = ($bs eq 'buy')
                ? ('DESC')
                : ('ASC');
-#INNER JOIN 
+       my $joinkind= $ARGS{ShowBlank} ? 'LEFT OUTER JOIN' : 'INNER JOIN';
        my $islands= $dbh->prepare(
        "SELECT islands.islandid AS islandid, archipelago, islandname,
                        sum(qty) as tqty
-               FROM islands LEFT OUTER JOIN $bs offers
+               FROM islands $joinkind $bs offers
                ON islands.islandid == offers.islandid AND commodid == ?
-               GROUP BY islands.islandid,
+               GROUP BY islands.islandid
                ORDER BY archipelago, islandname"
                );
 
@@ -110,32 +99,39 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
                WHERE commodid = ? AND islandid = ?
                ORDER BY price $ascdesc"
                );
-       # fixme this query is utterly wrong
 
 </%perl>
 
 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
-
-<table>
+%      $islands->execute($cmid);
+%      my $island;
+%      my %ts_sortkeys;
+%      my $rowix= 0;
+%      while ($island= $islands->fetchrow_hashref) {
+%              if (!$rowix) {
+<table id="<% $bs %>_table" rules=groups>
+<colgroup span=2>
+<colgroup span=1>
+<colgroup span=2>
+<colgroup span=3>
 <tr>
-<th colspan=3>
+<th colspan=2>
+<th colspan=1>
 <th colspan=2>Prices
-<th colspan=3>Quantities available
-<tr>
+<th colspan=3>Quantity at price
+<tr id="<% $bs %>_table_thr">
 <th>Archipelago
 <th>Island
-<th>Unique best stall
+<th>Stall(s)
 <th>Best
 <th>Median
-<th>At best
-<th>Within 10%
-<th>Total
+<th>Best
+<th>+/-10%
+<th>Any
 </tr>
-%      $islands->execute($commodid);
-%      my $island;
-%      while ($island= $islands->fetchrow_hashref) {
+%              }
 %              my $islandid= $island->{'islandid'};
-%              $offers->execute($commodid, $islandid);
+%              $offers->execute($cmid, $islandid);
 %              my ($offer, $bestprice, $marginal, @beststalls);
 %              my $tqty= $island->{'tqty'};
 %              my $cqty= '';
@@ -159,21 +155,60 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
 %                              $approxqty += $qty;
 %                      }
 %              }
-%              my $nstalls= @beststalls;
-%              $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
-<tr> <td><% $island->{'archipelago'} |h %>
-     <td><% $island->{'islandname'} |h %>
-     <td><% $nstalls==0 ? '-' :
-            $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
-     <td><% length $bestqty ? $bestprice : '-' %>
-     <td><% $median %>
-     <td><% $bestqty %>
-     <td><% $approxqty %>
-     <td><% $cqty %>
+%              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 $cmid $islandid ";
+<tr id=<% $rowid %> class="<% 'datarow'.($rowix & 1) %>">
+     <td><% $s->[0]= $island->{'archipelago'} |h %>
+     <td><% $s->[1]= $island->{'islandname'} |h %>
+     <td><%          $stallname |h %>
+     <td align=right><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
+     <td align=right><% $s->[4]= $median %>
+     <td align=right><% $s->[5]= $bestqty %>
+     <td align=right><% $s->[6]= $approxqty %>
+     <td align=right><% $s->[7]= $cqty %>
 </tr>
+%              for my $cix (0..$#$s) {
+%                      $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
+%              }
+%              $rowix++;
 %      }
+%      if ($rowix) {
 </table>
 
+<&| tabsort,   table => "${bs}_table", sortkeys => "${bs}_sortkeys",
+               throw => "${bs}_table_thr", rowclass => 'datarow', 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 },
+       ] &>
+  <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
+  function ts_Pricemap_<% $bs %>(price) {
+    if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
+    return price;
+  }
+</&tabsort>
+%      } else {
+No offers.
+%      }
+
 <%perl>
 }
 </%perl>