</%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/; };
+% my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
<& "lookup:formhidden", ours => $ours &>
</form>
-% } else { #---------- dropdowns, user selects from menus ----------
-
-% } #---------- end of dropdowns, now common middle of page code ----------
+%#========== 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');
+
+ 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>Archipelago
+<th>Island
+<th>Unique best stall
+<th>Best price
+<th>Qty at best
+<th>Median price
+<th>Total qty
+</tr>
+% $islands->execute($commodid);
+% my $island;
+% while ($island= $islands->fetchrow_hashref) {
+% my $islandid= $island->{'islandid'};
+% $offers->execute($commodid, $islandid);
+% my ($offer, $best, $median);
+% my $tqty= $island->{'tqty'};
+% my $cqty= 0;
+% while ($offer= $offers->fetchrow_hashref) {
+% if (!$best) {
+% $best= { 'price' => $offer->{'price'} };
+% }
+% if ($offer->{'price'} == $best->{'price'}) {
+% $best->{'qty'} += $offer->{'qty'};
+% push @{ $best->{'stalls'} },
+% $offer->{'stallname'};
+% }
+% $cqty += $offer->{'qty'};
+% if ($cqty*2 >= $tqty && !defined $median) {
+% $median= $offer->{'price'};
+% }
+% }
+% $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>
+% }
+</table>
+
+<%perl>
+}
+</%perl>