Add all-balance summing and listing of retired balances
authoradamch <adamch>
Thu, 13 Sep 2007 18:29:32 +0000 (18:29 +0000)
committeradamch <adamch>
Thu, 13 Sep 2007 18:29:32 +0000 (18:29 +0000)
balance.sig
balance.sml
money.mlt

index 6eb3349..06726a5 100644 (file)
@@ -8,8 +8,11 @@ sig
     val deleteBalance : int -> unit
     val listBalances : unit -> balance list
     val listOwnedBalances : unit -> balance list
+    val listUnownedBalances : unit -> balance list
     val listBalanceUsers : int -> Init.user list
 
     val validBalanceName : string -> bool
     val balanceNameToId : string -> int option
+
+    val sumOwnedBalances : unit -> real
 end
index 9a1f8b6..9943944 100644 (file)
@@ -54,6 +54,14 @@ fun listOwnedBalances () =
                                           AND WebUser.bal = Balance.id
                                     ORDER BY Balance.name`)
 
+fun listUnownedBalances () =
+    C.map (getDb ()) mkBalanceRow ($`SELECT Balance.id, Balance.name, amount
+                                    FROM Balance LEFT OUTER JOIN WebUser
+                                       ON Balance.name = WebUser.name
+                                          AND WebUser.bal = Balance.id
+                                    WHERE WebUser.id IS NULL
+                                    ORDER BY Balance.name`)
+
 fun validBalanceName name =
     size name <= 20
     andalso CharVector.all (fn ch => Char.isAlpha ch orelse ch = #"+") name
@@ -69,4 +77,12 @@ fun listBalanceUsers bal =
                                  WHERE bal = ^(C.intToSql bal)
                                  ORDER BY name`)
 
+fun sumOwnedBalances () =
+    case C.oneRow (getDb ()) ($`SELECT SUM(amount)
+                               FROM Balance JOIN WebUser
+                                 ON Balance.name = WebUser.name
+                                   AND WebUser.bal = Balance.id`) of
+       [amt] => C.realFromSql amt
+      | _ => raise Fail "sumOwnedBalance: no rows"
+
 end
index 08aaa4b..bd532be 100644 (file)
--- a/money.mlt
+++ b/money.mlt
@@ -37,7 +37,7 @@ end %>
 <% elseif $"cmd" = "bals" then
        showNormal := false %>
 
-<h3>Balances</h3>
+<h3>Active Balances</h3>
 
 <table>
 <% foreach bal in Balance.listOwnedBalances () do %>
@@ -53,6 +53,25 @@ end %></td> </tr>
 <% end %>
 </table>
 
+<% elseif $"cmd" = "deadbals" then
+       showNormal := false %>
+
+<h3>Retired Balances</h3>
+
+<table>
+<% foreach bal in Balance.listUnownedBalances () do %>
+<tr><td><% #name bal %></td> <td><% #amount bal %></td> <td>
+<% switch Balance.listBalanceUsers (#id bal) of
+         [] =>
+       | (user :: users) =>
+               %><a href="money?hist=<% #id user %>"><% Web.html (#name user) %></a><%
+               foreach user in users do
+                       %>, <a href="money?hist=<% # id user %>"><% Web.html (#name user) %></a><%
+               end
+end %></td> </tr>
+<% end %>
+</table>
+
 <% elseif $"cmd" = "hosting" then
        Group.requireGroupName "money";
        showNormal := false %>
@@ -307,10 +326,12 @@ end %>
 
 <% if showNormal then %>
 
-<h3>Your balance:</b> $<% #amount (Balance.lookupBalance (#bal (Init.getUser ()))) %></h3>
+<h3>Your balance: $<% #amount (Balance.lookupBalance (#bal (Init.getUser ()))) %></h3>
+<% if Group.inGroupName "money" then %><h3>Sum of all active balances: $<% Balance.sumOwnedBalances () %></h3><% end %>
 
 <a href="money?cmd=list">List all transactions</a><br>
-<a href="money?cmd=bals">List all balances</a><br>
+<a href="money?cmd=bals">List active balances</a><br>
+<a href="money?cmd=deadbals">List retired balances</a><br>
 
 <% if Group.inGroupName "money" then %>