X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Froutetrade;h=dcf3671620f32b8680c7375b6d714e911442ed0d;hb=4311ae355f0a94637c94d1d02a6edcbe65bebaa7;hp=f8e0f89145b11888878067f383a74f5e9c1872fd;hpb=641e98def7ea904b48b0f7dc22d868b40f3cff1b;p=ypp-sc-tools.web-live.git diff --git a/yarrg/web/routetrade b/yarrg/web/routetrade index f8e0f89..dcf3671 100644 --- a/yarrg/web/routetrade +++ b/yarrg/web/routetrade @@ -98,30 +98,43 @@ my $stmt= " SELECT sell_islands.islandname org_name, sell_islands.islandid org_id, sell.price org_price, - sum(sell.qty) org_qty, buy_islands.islandname dst_name, buy_islands.islandid dst_id, buy.price dst_price, +".($qa->{ShowStalls} ? " + sell.stallid org_stallid, + sell_stalls.stallname org_stallname, + sell.qty org_qty, + buy.stallid dst_stallid, + buy_stalls.stallname dst_stallname, + buy.qty dst_qty, +" : " + sum(sell.qty) org_qty, sum(buy.qty) dst_qty, +")." commods.commodname commodname, commods.commodid commodid, commods.unitmass unitmass, commods.unitvolume unitvolume, - buy.price - sell.price unitprofit, - min(sell.qty,buy.qty) max_qty, - min(sell.qty,buy.qty) * (buy.price-sell.price) max_profit + buy.price - sell.price unitprofit FROM commods JOIN buy on commods.commodid = buy.commodid JOIN sell on commods.commodid = sell.commodid JOIN islands as sell_islands on sell.islandid = sell_islands.islandid JOIN islands as buy_islands on buy.islandid = buy_islands.islandid +".($qa->{ShowStalls} ? " + JOIN stalls as sell_stalls on sell.stallid = sell_stalls.stallid + JOIN stalls as buy_stalls on buy.stallid = buy_stalls.stallid +" : "")." WHERE ( ".join(" OR ", @flow_conds)." ) AND buy.price > sell.price +".($qa->{ShowStalls} ? "" : " GROUP BY commods.commodid, org_id, org_price, dst_id, dst_price - ORDER BY org_name, dst_name, max_profit DESC, commodname, +")." + ORDER BY org_name, dst_name, commodname, unitprofit DESC, org_price, dst_price DESC "; @@ -129,9 +142,16 @@ my $sth= $dbh->prepare($stmt); $sth->execute(@query_params); my @flows; -my @columns= qw(org_name dst_name commodname - org_price org_qty dst_price dst_qty - max_qty max_profit); +my @columns; +if ($qa->{ShowStalls}) { + push @columns, qw(org_name org_stallname dst_name dst_stallname); +} else { + push @columns, qw(org_name dst_name); +} +push @columns, qw(commodname + org_price org_qty dst_price dst_qty + unitprofit PctProfit + MaxQty MaxCapital MaxProfit); @@ -142,19 +162,50 @@ my @columns= qw(org_name dst_name commodname % } +<& dumptable:start, qa => $qa, sth => $sth &> % { -<& dumpqueryresults:start, sth => $sth &> -% my $flow; -% while ($flow= $sth->fetchrow_hashref()) { -% $flow->{Ix}= @flows; -% $flow->{Var}= "f$flow->{Ix}"; -% push @flows, $flow; -<& dumpqueryresults:row, sth => $sth, row => $flow &> +% my $f; +% while ($f= $sth->fetchrow_hashref()) { +<%perl> + + $f->{Ix}= @flows; + $f->{Var}= "f$f->{Ix}"; + + $f->{MaxQty}= $f->{'org_qty'} < $f->{'dst_qty'} + ? $f->{'org_qty'} : $f->{'dst_qty'}; + $f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'}; + $f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'}; + + $f->{PctProfit}= sprintf "%3.1f%%", + $f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0; + + $f->{"org_stallid"}= $f->{"dst_stallid"}= 'all' + if !$qa->{ShowStalls}; + + my @uid= $f->{commodid}; + foreach my $od (qw(org dst)) { + push @uid, + $f->{"${od}_id"}, + $f->{"${od}_price"}, + $f->{"${od}_stallid"}; + } + $f->{Uid}= join '_', @uid; + + if ($qa->{"rtsela_$f->{Uid}"} && + !$qa->{"rtselg_$f->{Uid}"}) { + $f->{Suppress}= 1; + } + + push @flows, $f; + + +<& dumptable:row, qa => $qa, sth => $sth, row => $f &> % } -<& dumpqueryresults:end &> +<& dumptable:end, qa => $qa &> % } -% if (!$specific || $confusing || @islandids<=1) { +% my $optimise= $specific && !$confusing && @islandids>1; +% if (!$optimise) {

% if (@islandids<=1) { @@ -184,13 +235,22 @@ Subject To my %avail_csts; foreach my $flow (@flows) { + if ($flow->{Suppress}) { + $cplex .= " + $flow->{Var} = 0 +"; + next; + } foreach my $od (qw(org dst)) { - my $cstname= join '_', + my $cstname= join '_', ( 'avail', $flow->{'commodid'}, $od, $flow->{"${od}_id"}, - $flow->{"${od}_price"}; + $flow->{"${od}_price"}, + $flow->{"${od}_stallid"}, + ); + push @{ $avail_csts{$cstname}{Flows} }, $flow->{Var}; $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty"}; } @@ -242,8 +302,9 @@ if ($qa->{'debug'}) { my ($ix, $qty) = m/^\s*\d+\s+f(\d+)\s+\S+\s+(\d+)\s/ or die "$_ ?"; my $flow= $flows[$ix] or die; - $flow->{Opt_qty}= $qty; - $flow->{Opt_profit}= $flow->{'unitprofit'} * $qty; + $flow->{OptQty}= $qty; + $flow->{OptProfit}= $flow->{'unitprofit'} * $qty; + $flow->{OptCapital}= $flow->{OptQty} * $flow->{'org_price'}; } print "\n" if $qa->{'debug'}; pipethrough_run_finish($output, 'glpsol'); @@ -252,13 +313,63 @@ if ($qa->{'debug'}) { print join ' ', map { $_->{Optimal} } @flows; -push @columns, qw(Opt_qty Opt_profit); +push @columns, qw(OptQty OptCapital OptProfit); -<% join ' ', @columns %> % } # ========== OPTIMISATION ========== +% { +% my $cdspan= $qa->{ShowStalls} ? ' colspan=2' : ''; +% my $cdstall= $qa->{ShowStalls} ? 'Stall' : ''; + + + + +
+>Collect +>Deliver + +Collect +Deliver +Profit +Max +% if ($optimise) { +Suggested +% } + +
+Island <% $cdstall %> +Island <% $cdstall %> +Commodity +Price +Qty +Price +Qty +Unit +Margin +Qty +Capital +Profit +% if ($optimise) { +Qty +Capital +Profit +% } +% } + +% foreach my $flow (@flows) { +
{Uid} %> value=1> + {Uid} %> value=1 + <% $flow->{Suppress} ? '' : 'checked' %> > +% foreach my $col (@columns) { +<% $flow->{$col} |h %> +% } +% } +
+ + + <%init> use CommodsWeb; use Commods;