chiark / gitweb /
Reorganise columns
[ypp-sc-tools.db-test.git] / yarrg / web / query_commod
index 9cb14ca6870e70abf887b518f2e9698313d67158..a4063c12204e703bbb748e07e3d444c3c0f89bc3 100644 (file)
@@ -37,6 +37,7 @@
 $quri
 $dbh
 $commodstring => '';
+$prselector
 $someresults
 $emsgokorprint
 </%args>
@@ -48,7 +49,9 @@ my ($commodname,$commodid);
 my $qa= \%ARGS;
 </%perl>
 
-<h1>Select commodity</h1>
+<h1>Commodity enquiry</h1>
+
+% $prselector->('BuySell');
 
 %#---------- textbox, user enters route as string ----------
 % if (!$qa->{Dropdowns}) {
@@ -77,10 +80,97 @@ Not yet implemented.
 </form>
 
 %#========== results ==========
-% $emsgokorprint->($emsg) or $commodid=undef;
+<%perl>
 
-% if (defined $commodid) {
-%      $someresults->();
-COMMODITY <% $commodid %> named <% $commodname |h %>
+$emsgokorprint->($emsg) or $commodid=undef;
+return unless defined $commodid;
+$someresults->();
+
+#---------- actually compute the results and print them ----------
+
+foreach my $bs (split /_/, $ARGS{BuySell}) {
+       $bs =~ m/^(buy|sell)$/ or die;
+       $bs= $1;
+       my ($ascdesc) = ($bs eq 'buy')
+               ? ('DESC')
+               : ('ASC');
+
+       my $islands= $dbh->prepare(
+       "SELECT islandid, archipelago, islandname, sum(qty) as tqty
+               FROM $bs NATURAL JOIN islands
+               WHERE commodid = ?
+               GROUP BY islandid,
+               ORDER BY archipelago, islandname"
+               );
+
+       my $offers= $dbh->prepare(
+       "SELECT stallname, price, qty
+               FROM $bs NATURAL JOIN stalls
+               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>
+<tr>
+<th colspan=3>
+<th colspan=2>Prices
+<th colspan=3>Quantities available
+<tr>
+<th>Archipelago
+<th>Island
+<th>Unique best stall
+<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, $bestprice, $median, $marginal, @beststalls);
+%              my $tqty= $island->{'tqty'};
+%              my $cqty= 0;
+%              my $bestqty= 0;
+%              my $approxqty= 0;
+%              while ($offer= $offers->fetchrow_hashref) {
+%                      my $price= $offer->{'price'};
+%                      my $qty= $offer->{'qty'};
+%                      defined $bestprice or $bestprice= $price;
+%                      if ($price == $bestprice) {
+%                              $bestqty += $qty;
+%                              push @beststalls, $offer->{'stallname'};
+%                      }
+%                      $cqty += $qty;
+%                      if ($cqty*2 >= $tqty && !defined $median) {
+%                              $median= $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 ";
+<tr> <td><% $island->{'archipelago'} |h %>
+     <td><% $island->{'islandname'} |h %>
+     <td><% $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
+     <td><% $bestprice %>
+     <td><% $median %>
+     <td><% $bestqty %>
+     <td><% $approxqty %>
+     <td><% $tqty %>
+</tr>
+%      }
+</table>
+
+<%perl>
+}
+</%perl>