X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Fquery_commod;h=0bd36cbffa4db9e958ce30cbc84521a181e70862;hb=5c74b3871323fe2d06d5fcbdcd6c248a88a9ca00;hp=9cb14ca6870e70abf887b518f2e9698313d67158;hpb=98610392fde2add293bee6199f2de1d6f88559d8;p=ypp-sc-tools.db-live.git
diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod
index 9cb14ca..0bd36cb 100644
--- a/yarrg/web/query_commod
+++ b/yarrg/web/query_commod
@@ -37,6 +37,7 @@
$quri
$dbh
$commodstring => '';
+$prselector
$someresults
$emsgokorprint
%args>
@@ -48,7 +49,10 @@ my ($commodname,$commodid);
my $qa= \%ARGS;
%perl>
-
Select commodity
+Commodity enquiry
+
+% $prselector->('BuySell');
+% $prselector->('ShowBlank');
%#---------- textbox, user enters route as string ----------
% if (!$qa->{Dropdowns}) {
@@ -77,10 +81,146 @@ Not yet implemented.
%#========== results ==========
-% $emsgokorprint->($emsg) or $commodid=undef;
+<%perl>
+
+$emsgokorprint->($emsg) or $commodid=undef;
+return unless defined $commodid;
+$someresults->();
+
+#---------- actually compute the results and print them ----------
+
+my $onloads= "";
+
+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"
+ );
+ # fixme this query is utterly wrong
+
+%perl>
-% if (defined $commodid) {
-% $someresults->();
-COMMODITY <% $commodid %> named <% $commodname |h %>
+Offers to <% uc $bs |h %> <% $commodname |h %>
+
+
+
+
+ | Prices
+ | Quantity at price
+ |
+Archipelago
+ | Island
+ | Stall(s)
+ | Best
+ | Median
+ | Best
+ | +/-10%
+ | Any
+ |
+% $islands->execute($commodid);
+% my $island;
+% my %ts_sortkeys;
+% 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 $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 $commodid $islandid ";
+ >
+ <% $s->[0]= $island->{'archipelago'} |h %>
+ | <% $s->[1]= $island->{'islandname'} |h %>
+ | <% $stallname |h %>
+ | <% $s->[3]= (length $bestqty ? $bestprice : '-') %>
+ | <% $s->[4]= $median %>
+ | <% $s->[5]= $bestqty %>
+ | <% $s->[6]= $approxqty %>
+ | <% $s->[7]= $cqty %>
+ |
+% for my $cix (0..$#$s) {
+% $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
+% }
+% }
+
+
+<& tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys",
+ throw => "${bs}_table_thr", 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 },
+ ] &>
+<&| script &>
+ <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
+ function ts_Pricemap_<% $bs %>(price) {
+ if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; }
+ return price;
+ }
+&>
+% $onloads .= " ts_onload__${bs}_table();\n";
-% }
+<%perl>
+}
+%perl>
+
+<&| script &>
+ function all_onload() {
+<% $onloads %>
+ }
+ window.onload= all_onload;
+&>