chiark / gitweb /
can compute profit/loss
[ypp-sc-tools.db-test.git] / ypp-restock-rum
index 0fe47f98ebd289b23c5805941d1ec31002a84f28..b76d1c4ccfd349fd6331b2f126dadce9d108cf1b 100755 (executable)
@@ -121,7 +121,10 @@ sub pr1 ($$) {
     my ($k,$v) = @_;
     printf "%-20s %s\n", "$k:", $v;
 }
-
+sub fmt_stock_index ($) {
+    my ($si) = @_;
+    @have==1 ? '' : ' #'.$si;
+}
 
 our @norm_price;
 our ($best, $best_norm_price);
@@ -132,8 +135,8 @@ sub print_inputs () {
     print "\n\n";
     pr('prices', @price, 'poe ea.') if valid(@price);
     pr('target stocks', @want, 'units') if valid(@want);
-    my $i=0; for my $stocks (@have) {
-       pr('actual stocks'.(@have==1 ? '' : ' #'.++$i),
+    my $si=0; for my $stocks (@have) {
+       pr('actual stocks'.fmt_stock_index(++$si),
           @$stocks, 'units');
     }
     print "\n";
@@ -146,7 +149,7 @@ sub compute_cheapest_rum() {
     prf('equiv. ordering price', @perorder, 'poe/order');
 
     $best= undef;
-    my $best_norm_price= 1e5;
+    $best_norm_price= 1e5;
     for my $i (qw(0 1 2)) {
        next unless $price[$i];
        $norm_price[$i] = $price[$i] * 100 / $proofs[$i];
@@ -170,9 +173,61 @@ sub compute_cheapest_rum() {
     print "\n";
 }
 
+sub pr1s ($) {
+    my ($x) = @_;
+    if (defined $x and $x) {
+       printf ' %9.1f', $x;
+    } else {
+       printf "          ";
+    }
+}
+
+sub compute_stock_values() {
+    return unless @have;
+    print <<END
+                 Rum      Rum     Shot    Shot     total      Profit    Profit
+                equiv.   value   stocks   value    value      this leg   total
+END
+;
+
+    my $initial_value;
+    my $last_value;
+    my $si=0; for my $stocks (@have) {
+       my $stock_rum= 0;
+       foreach my $i (qw(0 1 2)) {
+           $stock_rum += $stocks->[$i] * $proofs[$i] / 100;
+       }
+       my $rum_value= defined($best) ? $stock_rum * $best_norm_price : 0;
+       my $shot_value= valid($price[3]) ? $stocks->[3] * $price[3] : 0;
+       my $total_value= $rum_value + $shot_value;
+
+       printf "%-10s ", 'stocks'.fmt_stock_index(++$si).':';
+       pr1s($stock_rum);
+       pr1s($rum_value);
+       printf "%6d", $stocks->[3];
+       pr1s($shot_value);
+       pr1s($total_value);
+       
+       if (defined $last_value) {
+           printf(" %10.1f %10.1f",
+                  $total_value - $last_value,
+                  $total_value - $initial_value);
+       }
+       $initial_value= $total_value unless defined $initial_value;
+       $last_value= $total_value;
+       print "\n";
+    }
+    print <<END
+                 fine      poe    units    poe       poe     delta-poe     poe
+
+END
+;
+}
+
 parse_args();
 print_inputs();
 compute_cheapest_rum();
+compute_stock_values();
 
 __DATA__
 
@@ -224,6 +279,7 @@ sub compute_restock_requirements () {
            pr1("vessel",$the_ship);
        }
     }
+
     pr1('desired stock level', sprintf("%4d fine rum", $need{Fine}));
     $need_proof= $need{Fine} * $proof{Fine} - $have_proof;
     map {