</%doc>
<%args>
$quri
+$dbh
+$commodid => undef;
$commodstring => '';
+$prselector
+$someresults
+$emsgokorprint
</%args>
-% my $qa= \%ARGS;
+<%perl>
+my $emsg;
+my ($commodname,$cmid);
-%#---------- textbox, user enters route as string ----------
-% if (!$qa->{Dropdowns}) {
+my $qa= \%ARGS;
+</%perl>
+
+<h1>Commodity enquiry</h1>
-<h1>Select commodity</h1>
+% $prselector->('BuySell');
+% $prselector->('ShowBlank');
<form action="<% $quri->() |h %>" method="get">
-<&| qtextstring, qa => $qa, thingstring => 'commodstring' &>
+%#---------- textbox, user enters route as string ----------
+% if (!$qa->{Dropdowns}) {
+
+Enter commodity (abbreviations are OK):<br>
+
+<&| qtextstring, qa => $qa, dbh => $dbh,
+ thingstring => 'commodstring', emsgstore => \$emsg,
+ perresult => sub { ($commodname,$cmid)= @_; }
+ &>
size=80
</&>
% } else { #---------- dropdowns, user selects from menus ----------
+% my $sth= $dbh->prepare("SELECT commodname,commodid FROM commods
+% ORDER BY commodname");
+% $sth->execute();
+% my $row;
+<select name="commodid">
+<option value="">Select commodity...</option>
+% while ($row= $sth->fetchrow_arrayref) {
+% my $selected= $commodid eq $row->[1] ? 'selected' : '';
+<option value="<% $row->[1] %>" <% $selected %>><% $row->[0] |h %></option>
+% ($commodname,$cmid) = @$row if $selected;
+% }
+</select>
+
% } #---------- 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 $cmid=undef;
+return unless defined $cmid;
+$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 $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 $joinkind $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"
+ );
+
+</%perl>
+
+<h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
+% $islands->execute($cmid);
+% my $island;
+% my %ts_sortkeys;
+% my $rowix= 0;
+% while ($island= $islands->fetchrow_hashref) {
+% if (!$rowix) {
+<table id="<% $bs %>_table">
+<tr>
+<th colspan=3>
+<th colspan=2>Prices
+<th colspan=3>Quantity at price
+<tr id="<% $bs %>_table_thr">
+<th>Archipelago
+<th>Island
+<th>Stall(s)
+<th>Best
+<th>Median
+<th>Best
+<th>+/-10%
+<th>Any
+</tr>
+% }
+% my $islandid= $island->{'islandid'};
+% $offers->execute($cmid, $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 $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><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
+ <td><% $s->[4]= $median %>
+ <td><% $s->[5]= $bestqty %>
+ <td><% $s->[6]= $approxqty %>
+ <td><% $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>