X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Fquery_commod;h=4869e5c50fad00d6222fa0a1ce8e2df110f488a9;hb=890faab73220a817300d53a362464d6a7fc06788;hp=2358b4167cc373874dd8bf959f3099a610612eeb;hpb=9d01242d0991d15f7ea84454264c868e1c4ed8ad;p=ypp-sc-tools.db-test.git diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod index 2358b41..4869e5c 100644 --- a/yarrg/web/query_commod +++ b/yarrg/web/query_commod @@ -35,30 +35,145 @@ <%args> $quri +$dbh $commodstring => ''; +$prselector +$someresults +$emsgokorprint -% my $qa= \%ARGS; +<%perl> +my $emsg; +my ($commodname,$commodid); + +my $qa= \%ARGS; + + +

Commodity enquiry

+ +% $prselector->('BuySell'); %#---------- textbox, user enters route as string ---------- % if (!$qa->{Dropdowns}) { -

Select commodity

- Enter commodity (abbreviations are OK):
-<&| 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 ---------- + -% my $ours= sub { $_[0] =~ m/^commodstring/; }; +% my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; }; <& "lookup:formhidden", ours => $ours &>
-% } 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'); +#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 + + + +

Offers to <% uc $bs |h %> <% $commodname |h %>

+ + + + + +% $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 "; + +% } +
+Prices +Quantities available +
Archipelago +Island +Unique best stall +Best +Median +At best +Within 10% +Total +
<% $island->{'archipelago'} |h %> + <% $island->{'islandname'} |h %> + <% $nstalls==0 ? '-' : + $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %> + <% length $bestqty ? $bestprice : '-' %> + <% $median %> + <% $bestqty %> + <% $approxqty %> + <% $cqty %> +
+ +<%perl> +} +