X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=blobdiff_plain;f=yarrg%2Fweb%2Fquery_commod;h=c9b17ae6700b72a0e5ba135a14d95ff381ae3a98;hp=eddc0975d05f067358c6835a81710d97f8039b8d;hb=00ba9271fd8409f29fcae6ee2af15824810bc939;hpb=72587d0d7358ca77ecd0ede859abba7f89b1d9d2 diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod index eddc097..c9b17ae 100644 --- a/yarrg/web/query_commod +++ b/yarrg/web/query_commod @@ -35,7 +35,9 @@ <%args> $quri +$dbh $commodstring => ''; +$prselector $someresults $emsgokorprint @@ -47,7 +49,10 @@ my ($commodname,$commodid); my $qa= \%ARGS; -

Select commodity

+

Commodity enquiry

+ +% $prselector->('BuySell'); +% $prselector->('ShowBlank'); %#---------- textbox, user enters route as string ---------- % if (!$qa->{Dropdowns}) { @@ -56,7 +61,8 @@ Enter commodity (abbreviations are OK):
-<&| qtextstring, qa => $qa, thingstring => 'commodstring', emsgstore => \$emsg, +<&| qtextstring, qa => $qa, dbh => $dbh, + thingstring => 'commodstring', emsgstore => \$emsg, perresult => sub { ($commodname,$commodid)= @_; } &> size=80 @@ -75,10 +81,101 @@ Not yet implemented.
%#========== results ========== -% $emsgokorprint->($emsg) or $commodid=undef; +<%perl> -% if (defined $commodid) { -% $someresults->(); -COMMODITY <% $commodid %> named <% $commodname |h %> +$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 $joinkind= $ARGS{ShowBlank} eq 'show' + ? '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 + + -% } +

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> +} +