chiark / gitweb /
Include islands with no offers at all
[ypp-sc-tools.web-live.git] / yarrg / web / query_commod
index 5a7497bac9d3d8d042bf40c031d565bf952607d2..4869e5c50fad00d6222fa0a1ce8e2df110f488a9 100644 (file)
 </%doc>
 <%args>
 $quri
+$dbh
 $commodstring => '';
+$prselector
+$someresults
+$emsgokorprint
 </%args>
 
-% my $qa= \%ARGS;
+<%perl>
+my $emsg;
+my ($commodname,$commodid);
+
+my $qa= \%ARGS;
+</%perl>
+
+<h1>Commodity enquiry</h1>
+
+% $prselector->('BuySell');
 
 %#---------- textbox, user enters route as string ----------
 % if (!$qa->{Dropdowns}) {
 
-<h1>Select commodity</h1>
+Enter commodity (abbreviations are OK):<br>
 
 <form action="<% $quri->() |h %>" method="get">
 
-<&| qtextstring, qa => $qa, thingstring => 'commodstring' &>
+<&| qtextstring, qa => $qa, dbh => $dbh,
+    thingstring => 'commodstring', emsgstore => \$emsg,
+    perresult => sub { ($commodname,$commodid)= @_; }
+ &>
  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/; };
+<& "lookup:formhidden", ours => $ours &>
+
+</form>
+
+%#========== results ==========
+<%perl>
+
+$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');
+#INNER JOIN 
+       my $islands= $dbh->prepare(
+       "SELECT islands.islandid AS islandid, archipelago, islandname,
+                       sum(qty) as tqty
+               FROM islands LEFT OUTER JOIN $bs offers
+               ON islands.islandid == offers.islandid AND commodid == ?
+               GROUP BY islands.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, $marginal, @beststalls);
+%              my $tqty= $island->{'tqty'};
+%              my $cqty= '';
+%              my $bestqty= '';
+%              my $approxqty= '';
+%              my $median= '-';
+%              while ($offer= $offers->fetchrow_hashref) {
+%                      my $price= $offer->{'price'};
+%                      my $qty= $offer->{'qty'};
+%                      length $bestqty or $bestprice= $price;
+%                      if ($price == $bestprice) {
+%                              $bestqty += $qty;
+%                              push @beststalls, $offer->{'stallname'};
+%                      }
+%                      $cqty += $qty;
+%                      if ($cqty*2 >= $tqty && $median eq '-') {
+%                              $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==0 ? '-' :
+            $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
+     <td><% length $bestqty ? $bestprice : '-' %>
+     <td><% $median %>
+     <td><% $bestqty %>
+     <td><% $approxqty %>
+     <td><% $cqty %>
+</tr>
+%      }
+</table>
+
+<%perl>
+}
+</%perl>