chiark / gitweb /
Make including islands with no offers optional
[ypp-sc-tools.web-live.git] / yarrg / web / query_commod
index 9a7d2bc36dc7bad67e67bfbc6c92688e6902862a..c9b17ae6700b72a0e5ba135a14d95ff381ae3a98 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}) {
@@ -94,12 +95,14 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
        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"
                );
 
@@ -117,45 +120,58 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
 
 <table>
 <tr>
+<th colspan=3>
+<th colspan=2>Prices
+<th colspan=3>Quantities available
+<tr>
 <th>Archipelago
 <th>Island
 <th>Unique best stall
-<th>Best price
-<th>Qty at best
-<th>Median price
-<th>Total qty
+<th>Best
+<th>Median
+<th>At best
+<th>Within 10%
+<th>Total
 </tr>
 %      $islands->execute($commodid);
 %      my $island;
 %      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 $nstalls= @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><% $nstalls==0 ? '-' :
+            $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
+     <td><% length $bestqty ? $bestprice : '-' %>
      <td><% $median %>
-     <td><% $tqty %>
+     <td><% $bestqty %>
+     <td><% $approxqty %>
+     <td><% $cqty %>
 </tr>
 %      }
 </table>