X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Fquery_commod;h=8d55f2686130568ba92d2c578dd787d2362e368e;hb=3a24aecf857292701d755173ffdb91d10a3426c8;hp=aeee3cee83d0bf8288ee79903ec005e20b36f4fc;hpb=dbd70ca20132a55c55b5d876b59a85d10f9cdd25;p=ypp-sc-tools.db-live.git
diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod
index aeee3ce..8d55f26 100644
--- a/yarrg/web/query_commod
+++ b/yarrg/web/query_commod
@@ -36,6 +36,7 @@
<%args>
$quri
$dbh
+$commodid => undef;
$commodstring => '';
$prselector
$someresults
@@ -44,34 +45,23 @@ $emsgokorprint
<%perl>
my $emsg;
-my ($commodname,$commodid);
+my ($commodname,$cmid);
my $qa= \%ARGS;
%perl>
+
Commodity enquiry
% $prselector->('BuySell');
-
-%#---------- textbox, user enters route as string ----------
-% if (!$qa->{Dropdowns}) {
-
-Enter commodity (abbreviations are OK):
+% $prselector->('ShowBlank');
%#========== results ==========
+
+
<%perl>
-$emsgokorprint->($emsg) or $commodid=undef;
-return unless defined $commodid;
+$emsgokorprint->($emsg) or $cmid=undef;
+print("
"), return
+ unless defined $cmid;
$someresults->();
#---------- actually compute the results and print them ----------
@@ -91,39 +84,136 @@ $someresults->();
foreach my $bs (split /_/, $ARGS{BuySell}) {
$bs =~ m/^(buy|sell)$/ or die;
$bs= $1;
-
- my $sth= $dbh->prepare(
- "SELECT archipelago, islandname, stallname, price, qty
- FROM $bs NATURAL JOIN stalls NATURAL JOIN islands
- WHERE commodid = ?"
+ 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"
);
- # fixme this query is utterly wrong
- $sth->execute($commodid);
+ my $offers= $dbh->prepare(
+ "SELECT stallname, price, qty
+ FROM $bs NATURAL JOIN stalls
+ WHERE commodid = ? AND islandid = ?
+ ORDER BY price $ascdesc"
+ );
%perl>
Offers to <% uc $bs |h %> <% $commodname |h %>
-
-
+% $islands->execute($cmid);
+% my $island;
+% my %ts_sortkeys;
+% my $rowix= 0;
+% while ($island= $islands->fetchrow_hashref) {
+% if (!$rowix) {
+
+
+
+
+
+
+ |
+ | Prices
+ | Quantity at price
+ |
Archipelago
| Island
- | Best stall
- | Best price
- | Qty at best
+ | Stall(s)
+ | Best
+ | Median
+ | Best
+ | +/-10%
+ | Any
|
-% my $row;
-% while ($row=$sth->fetchrow_hashref) {
- <% $row->{'archipelago'} %>
- | <% $row->{'islandname'} %>
- | <% $row->{'stallname'} %>
- | <% $row->{'price'} %>
- | <% $row->{'qty'} %>
+% }
+% 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 ";
+ |
class="<% 'datarow'.($rowix & 1) %>">
+ <% $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];
+% }
+% $rowix++;
% }
+% if ($rowix) {
+<&| 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>
+