payment: note that Stripe has instituted an additional 1% fee for non-US cards
[hcoop/portal.git] / money.mlt
index cc27889..d92969b 100644 (file)
--- a/money.mlt
+++ b/money.mlt
@@ -1,11 +1,13 @@
 <% @header [("title", ["MoneyMatters"])];
 
+val root = Group.inGroupNum 0;
+
 ref showNormal = true;
 
 if $"hist" <> "" then
        showNormal := false;
        val id = Web.stoi ($"hist") %>
-<table>
+<table class="data">
 <tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> </tr>
 <% foreach (amount, trn) in Money.listUserTransactions id do %>
 <tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td><% amount %>/<% #amount trn %></td> </tr>
@@ -18,7 +20,7 @@ if $"hist" <> "" then
 
 <h3>Transactions</h3>
 
-<table>
+<table class="data">
 <tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> <td><b>Last modified</b></td> <td><b>Participants</b></td> </tr>
 <% foreach trn in Money.listTransactions () do %>
 <tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td><% #amount trn %></td> <td><% #stamp trn %></td>
@@ -27,7 +29,29 @@ if $"hist" <> "" then
        | _ => %><td><i>multi</i></td><%
 end;
 if admin then
-       %><td><a href="money?modHosting=<% #id trn %>">[Hosting]</a> <a href="money?modPay=<% #id trn %>">[Payment]</a> <a href="money?modEven=<% #id trn %>">[Other]</a></td>
+       %><td><a href="money?modBill=<% #id trn %>">[Bill]</a> <a href="money?modPay=<% #id trn %>">[Payment]</a> <a href="money?modEven=<% #id trn %>">[Other]</a></td>
+       <td><a href="money?del=<% #id trn %>">[Delete]</a></td><%
+end
+%></tr><%
+end %>
+</table>
+
+<% elseif $"cmd" = "primary" then
+       val admin = Group.inGroupName "money";
+       showNormal := false %>
+
+<h3>Primary Transactions</h3>
+
+<table class="data">
+<tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> <td><b>Last modified</b></td> <td><b>Participants</b></td> </tr>
+<% foreach (amt, trn) in Money.listUserTransactions (valOf (Init.userNameToId "hcoop")) do %>
+<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td style="text-align: right"><% #amount trn %></td> <td><% #stamp trn %></td>
+<% switch Money.listChargesWithNames (#id trn) of
+         [(name, cha)] => %><td><a href="user?id=<% #usr cha %>"><% name %></a></td><%
+       | _ => %><td><i>multi</i></td><%
+end;
+if admin then
+       %><td><a href="money?modBill=<% #id trn %>">[Bill]</a> <a href="money?modPay=<% #id trn %>">[Payment]</a> <a href="money?modEven=<% #id trn %>">[Other]</a></td>
        <td><a href="money?del=<% #id trn %>">[Delete]</a></td><%
 end
 %></tr><%
@@ -37,15 +61,15 @@ end %>
 <% elseif $"cmd" = "bals" then
        showNormal := false %>
 
-<h3>Balances</h3>
+<h3>Active Balances</h3>
 
-<table>
+<table class="data">
 <% foreach bal in Balance.listOwnedBalances () 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><%
+               %><a href="money?hist=<% #id user %>"><% Web.html (#name user) %></a> <a href="money?hist=<% #id user %>">[History]</a><%
                foreach user in users do
                        %>, <a href="money?hist=<% # id user %>"><% Web.html (#name user) %></a><%
                end
@@ -53,6 +77,45 @@ end %></td> </tr>
 <% end %>
 </table>
 
+<% elseif $"cmd" = "deadbals" then
+       showNormal := false %>
+
+<h3>Retired Balances</h3>
+
+<table class="data">
+<% 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="user?id=<% #id user %>"><% Web.html (#name user) %></a> <a href="money?hist=<% #id user %>">[History]</a><%
+               foreach user in users do
+                       %>, <a href="user?id=<% #id user %>"><% Web.html (#name user) %></a> <a href="money?hist=<% #id user %>">[History]</a><%
+               end
+end %></td> </tr>
+<% end %>
+</table>
+
+<% elseif $"cmd" = "nbals" then
+       showNormal := false;
+       val bals =  Balance.listNegativeOwnedBalances () %>
+
+<h3>Negative Active Balances (<% length bals %>)</h3>
+
+<table class="data">
+<% foreach bal in bals do %>
+<tr><td><% #name bal %></td> <td><% #amount bal %></td> <td>
+<% switch Balance.listBalanceUsers (#id bal) of
+         [] =>
+       | (user :: users) =>
+               %><a href="user?id=<% #id user %>"><% Web.html (#name user) %></a> <a href="money?hist=<% #id user %>">[History]</a><%
+               foreach user in users do
+                       %>, <a href="user?id=<% #id user %>"><% Web.html (#name user) %></a> <a href="money?hist=<% #id user %>">[History]</a><%
+               end
+end %></td> </tr>
+<% end %>
+</table>
+
 <% elseif $"cmd" = "hosting" then
        Group.requireGroupName "money";
        showNormal := false %>
@@ -112,6 +175,58 @@ end %></textarea></td> </tr>
 
        %><h3>Hosting transaction modified.</h3>
 
+<% elseif $"cmd" = "bill" then
+       Group.requireGroupName "money";
+       showNormal := false %>
+
+<h3>New bill for the co-op</h3>
+
+<form action="money" method="post">
+<input type="hidden" name="cmd" value="bill2">
+<table class="blanks">
+<tr> <td>Description:</td> <td><input name="descr"></td> </tr>
+<tr> <td>Date:</td> <td><input name="d"></td> </tr>
+<tr> <td>Amount:</td> <td><input name="amount"></td> </tr>
+<tr> <td><input type="submit" value="Add"></td> </tr>
+</table>
+
+<% elseif $"cmd" = "bill2" then
+       Group.requireGroupName "money";
+       val id = Money.addTransaction ($"descr", Util.neg (Web.stor ($"amount")), $"d");
+       Money.addCharge {trn = id, usr = valOf (Init.userNameToId "hcoop"), amount = Util.neg (Web.stor ($"amount"))};
+       Money.applyCharges id;
+
+       %><h3>Bill added.</h3>
+
+<% elseif $"modBill" <> "" then
+       Group.requireGroupName "money";
+       showNormal := false;
+       val id = Web.stoi ($"modBill");
+       val trn = Money.lookupTransaction id %>
+
+<h3>Modify bill</h3>
+
+<form action="money" method="post">
+<input type="hidden" name="saveBill" value="<% id %>">
+<table class="blanks">
+<tr> <td>Description:</td> <td><input name="descr" value="<% Web.html (#descr trn) %>"></td> </tr>
+<tr> <td>Date:</td> <td><input name="d" value="<% Web.html (#d trn) %>"></td> </tr>
+<tr> <td>Amount:</td> <td><input name="amount" value="<% Util.neg (#amount trn) %>"></td> </tr>
+<tr> <td><input type="submit" value="Save"></td> </tr>
+</table>
+
+<% elseif $"saveBill" <> "" then
+       Group.requireGroupName "money";
+       val id = Web.stoi ($"saveBill");
+       val trn = Money.lookupTransaction id;
+
+       Money.clearCharges id;
+       Money.modTransaction {trn with descr = $"descr", d = $"d", amount = Util.neg (Web.stor ($"amount"))};
+       Money.addCharge {trn = id, usr = valOf (Init.userNameToId "hcoop"), amount = Util.neg (Web.stor ($"amount"))};
+       Money.applyCharges id;
+
+       %><h3>Bill modified.</h3>
+
 <% elseif $"cmd" = "evenForm" then
        Group.requireGroupName "money";
        showNormal := false %>
@@ -133,6 +248,7 @@ end %></textarea></td> </tr>
 
 <% elseif $"cmd" = "pay" then
        Group.requireGroupName "money";
+       val uid = (case $"user" of "" => ~1 | s => Web.stoi s);
        showNormal := false %>
 
 <h3>New member payment</h3>
@@ -140,12 +256,17 @@ end %></textarea></td> </tr>
 <form action="money" method="post">
 <input type="hidden" name="cmd" value="pay2">
 <table class="blanks">
-<tr> <td>Description:</td> <td><input name="descr"></td> </tr>
-<tr> <td>Date:</td> <td><input name="d"></td> </tr>
+<tr> <td>Description:</td> <td><select name="descr">
+       <option<% if $"checkout" = "" then %> selected<% end %>>PayPal</option>
+       <option>Check</option>
+       <option>Direct transfer</option>
+       <option value="">Other:</option>
+</select> <input name="descr2"></td> </tr>
+<tr> <td>Date:</td> <td><input name="d" value="<% Web.html (Date.fmt "%B %d, %Y" (Date.fromTimeLocal (Time.now ()))) %>"></td> </tr>
 <tr> <td>Amount:</td> <td><input name="amount"></td> </tr>
 <tr> <td>Member:</td> <td><select name="usr">
 <% foreach usr in Init.listUsers () do %>
-       <option value="<% #id usr %>"><% #name usr %></option>
+       <option value="<% #id usr %>"<% if #id usr = uid then %> selected<% end %>><% #name usr %></option>
 <% end %>
 </select></td> </tr>
 <tr> <td><input type="submit" value="Add"></td> </tr>
@@ -155,12 +276,34 @@ end %></textarea></td> </tr>
 <% elseif $"cmd" = "pay2" then
        Group.requireGroupName "money";
        val amount = Web.stor ($"amount");
-       val id = Money.addTransaction ($"descr", amount, $"d");
+       val descr = $"descr";
+       val descr = iff descr = "" then $"descr2" else descr;
+       val id = Money.addTransaction (descr, amount, $"d");
        Money.addCharge {trn = id, usr = Web.stoi ($"usr"), amount = amount};
        Money.applyCharges id;
 
        %><h3>Payment transaction added.</h3>
 
+<% elseif $"cmd" = "stripeApply" then
+        val stripePmt = Money.lookupStripePayment ($"stripeId");
+        val txid = Money.applyStripePayment stripePmt;
+%><h3>Stripe Payment Processed (Transaction <% txid %>)</h3>
+
+<% elseif $"cmd" = "stripeRejected" then
+%><h3>Stripe Payment Rejected!</h3>
+
+<% elseif $"cmd" = "stripeReject" then
+        Group.requireGroupName "money";
+        showNormal := false;
+%>
+<h3>Are you sure you want to reject Stripe Payment <% $"stripeChargeId" %>?</h3>
+<form method="post" action="/stripe/stripe-admin.cgi">
+        <input type="hidden" name="cmd" value="reject_member_payment" />
+        <input type="hidden" name="stripeChargeId" value="<% $"stripeChargeId" %>" />
+       <label>Reason for rejection <input type="text" name="reason" /></label>
+        <input type="submit" value="Really Reject Payment" />
+      </form>
+
 <% elseif $"modPay" <> "" then
        Group.requireGroupName "money";
        showNormal := false;
@@ -215,6 +358,27 @@ end %></textarea></td> </tr>
 </table>
 </form>
 
+<% elseif $"cmd" = "dues" then
+       Group.requireGroupName "money";
+       showNormal := false %>
+
+<h3>Monthly dues</h3>
+
+<form action="money" method="post">
+<input type="hidden" name="cmd" value="dues2">
+<table class="blanks">
+<tr> <td>Description:</td> <td><input name="descr" value="Dues"></td> </tr>
+<tr> <td>Date:</td> <td><input name="d"></td> </tr>
+<tr> <td>Amount/pledge:</td> <td><input name="base"></td> </tr>
+<tr> <td><input type="submit" value="Add"></td> </tr>
+</table>
+
+<% elseif $"cmd" = "dues2" then
+       Group.requireGroupName "money";
+       Money.billDues {descr = $"descr", base = Web.stor ($"base"), date = $"d"};
+
+       %><h3>Dues debits added.</h3>
+
 <% elseif $"cmd" = "even" then
        Group.requireGroupName "money";
        val id = Money.addTransaction ($"descr", Web.stor ($"amount"), $"d");
@@ -295,49 +459,316 @@ foreach (name, cha) in Money.listChargesWithNames id do
 end %>
 </table>
 
+<% elseif $"cmd" = "paypal" then
+   showNormal := false;
+   val apps = App.searchPaypal ($"email");
+   val users = Init.searchPaypal ($"email");
+
+   switch apps of
+     _ :: _ =>
+     %><h3>Approved applications</h3>
+
+     <% foreach appl in apps do %>
+               <br><hr><br>
+               <table class="blanks">
+               <tr> <td>Received:</td> <td><% #applied appl %></td> </tr>
+               <tr> <td>Approved by:</td> <td><%
+                       ref first = true;
+                       ref found = false;
+                       foreach (id, name) in App.votes (#id appl) do
+                               if first then
+                                       first := false
+                               else
+                                       %>, <%
+                               end
+                               %><a href="user?id=<% id %>"><% name %></a><%
+                       end %> </td> </tr>
+       <tr> <td>Username:</td> <td><% #name appl %></td> </tr>
+       <tr> <td>Real name:</td> <td><% Web.html (#rname appl) %></td> </tr>
+       <tr> <td>E-mail address:</td> <td><a href="mailto:<% #email appl %>"><% #email appl %></a></td> </tr>
+       <% switch #paypal appl of
+         SOME s => %><tr> <td>PayPal:</td> <td><a href="mailto:<% s %>"><% s %></a></td> </tr>
+       <% end %>
+       <tr> <td>Forward e-mail?</td> <td><% if #forward appl then %>yes<% else %>no<% end %></td> </tr>
+       <tr> <td>Proposed uses:</td> <td><% Web.htmlNl (#uses appl) %></td> </tr>
+       <tr> <td>Other information:</td> <td><% Web.htmlNl (#other appl) %></td> </tr>
+       </table>
+
+       <% if root then %>
+               <a href="apps?add=<% #id appl %>">Add this member.</a><br>
+       <% end
+       end
+   end;
+
+   switch users of
+     _ :: _ =>
+     %><h3>Members</h3>
+
+     <% foreach user in users do %>
+       <li> <a href="user?id=<% #id user %>"><% #name user %></a> <a href="?cmd=pay&user=<% #id user %>">[add payment]</a></li>
+     <% end
+   end;
+
+   switch (apps, users) of
+     (nil, nil) => %>No matches.<%
+   end
+
+elseif $"cmd" = "realname" then
+   showNormal := false;
+   val users = Init.searchRealName ($"rname");
+
+   switch users of
+     _ :: _ =>
+     %><h3>Members</h3>
+
+     <% foreach user in users do %>
+       <li> <a href="user?id=<% #id user %>"><% #name user %></a> <a href="?cmd=pay&user=<% #id user %>">[add payment]</a></li>
+     <% end
+   end;
+
+   switch users of
+     nil => %>No matches.<%
+   end
+
+elseif $"cmd" = "checkout" then
+   showNormal := false;
+   val apps = App.searchCheckout ($"email");
+   val users = Init.searchCheckout ($"email");
+
+   switch apps of
+     _ :: _ =>
+     %><h3>Approved applications</h3>
+
+     <% foreach appl in apps do %>
+               <br><hr><br>
+               <table class="blanks">
+               <tr> <td>Received:</td> <td><% #applied appl %></td> </tr>
+               <tr> <td>Approved by:</td> <td><%
+                       ref first = true;
+                       ref found = false;
+                       foreach (id, name) in App.votes (#id appl) do
+                               if first then
+                                       first := false
+                               else
+                                       %>, <%
+                               end
+                               %><a href="user?id=<% id %>"><% name %></a><%
+                       end %> </td> </tr>
+       <tr> <td>Username:</td> <td><% #name appl %></td> </tr>
+       <tr> <td>Real name:</td> <td><% Web.html (#rname appl) %></td> </tr>
+       <tr> <td>E-mail address:</td> <td><a href="mailto:<% #email appl %>"><% #email appl %></a></td> </tr>
+       <% switch #paypal appl of
+         SOME s => %><tr> <td>PayPal:</td> <td><a href="mailto:<% s %>"><% s %></a></td> </tr>
+       <% end %>
+       <tr> <td>Forward e-mail?</td> <td><% if #forward appl then %>yes<% else %>no<% end %></td> </tr>
+       <tr> <td>Proposed uses:</td> <td><% Web.htmlNl (#uses appl) %></td> </tr>
+       <tr> <td>Other information:</td> <td><% Web.htmlNl (#other appl) %></td> </tr>
+       </table>
+
+       <% if root then %>
+               <a href="apps?add=<% #id appl %>">Add this member.</a><br>
+       <% end
+       end
+   end;
+
+   switch users of
+     _ :: _ =>
+     %><h3>Members</h3>
+
+     <% foreach user in users do %>
+       <li> <a href="user?id=<% #id user %>"><% #name user %></a> <a href="?cmd=pay&user=<% #id user %>&checkout=1">[add payment]</a></li>
+     <% end
+   end;
+
+   switch (apps, users) of
+     (nil, nil) => %>No matches.<%
+   end
+
+elseif $"cmd" = "delinq" then
+       showNormal := false;
+       val dqs = Money.delinquentPledgers () %>
+<table class="data">
+<tr> <th>Member</th> <th>Pledge</th> <th>Balance</th> </tr>
+<% foreach dq in dqs do %>
+<tr> <td><a href="user?id=<% #id dq %>"><% #name dq %></a></td> <td><% #shares dq %></td> <td>$<% #balance dq %></td> </tr>
 <% end %>
+</table>
 
-<% if showNormal then %>
+<a href="?lower=<% String.concatWith "," (List.map (fn dq => Int.toString (#id dq)) dqs) %>">Lower these pledges to 1</a>
 
-<h3>Your balance:</b> $<% #amount (Balance.lookupBalance (#bal (Init.getUser ()))) %></h3>
+<% elseif $"lower" <> "" then
+   Money.resetPledges (List.map Web.stoi (String.tokens (fn ch => ch = #",") ($"lower")))
 
+   %><h3>Pledges reset.</h3><%
+
+elseif $"cmd" = "freezeworthy" then
+       showNormal := false;
+       val dqs = Money.freezeworthyPledgers () %>
+<table class="data">
+<tr> <th>Member</th> <th>Balance</th> </tr>
+<% foreach dq in dqs do %>
+<tr> <td><a href="user?id=<% #id dq %>"><% #name dq %></a></td> <td style="text-align: right">$<% #balance dq %></td>
+<td><a href="money?hist=<% #id dq %>">[History]</a></td>
+<% if #joinedThisMonth dq then %><td><i>(joined this month)</i></td><% end %></tr>
+<% end %>
+</table>
+
+<% elseif $"cmd" = "bootworthy" then
+       showNormal := false;
+       val dqs = Money.bootworthyPledgers () %>
+<table class="data">
+<tr> <th>Member</th> <th>Balance</th> </tr>
+<% foreach dq in dqs do %>
+<tr> <td><a href="user?id=<% #id dq %>"><% #name dq %></a></td> <td style="text-align: right">$<% #balance dq %></td><td><a href="money?hist=<% #id dq %>">[History]</a></td>
+<% if #joinedThisMonth dq then %><td><i>(joined this month)</i></td><% end %></tr>
+<% end %>
+</table>
+
+<% end %>
+
+<% if showNormal then
+   val you = Init.getUser();
+   val bal = Balance.lookupBalance (#bal you);
+   val deposit = Balance.depositAmount (#id bal);
+   val showBal = Util.sub (#amount bal, deposit) %>
+
+<h3>Your balance: $<% showBal %><br>
+Deposit: $<% deposit %> (3 months of dues at the minimal <a href="pledge">pledge level</a>)<br>
+<!--Co-op balance: $<% #amount (Balance.lookupBalance (valOf (Balance.balanceNameToId "hcoop"))) %>--></h3>
+
+<% if (iff Group.inGroupName "money" then $"lookback" = "" else $"audit" <> "") then %><h3>Sum of all active balances: $<% Balance.sumOwnedBalances () %></h3><% end %>
+<div class="payment-tile">
+  <h3>Financial Information</h3>
 <a href="money?cmd=list">List all transactions</a><br>
-<a href="money?cmd=bals">List all balances</a><br>
+<a href="money?cmd=primary">List primary transactions</a><br>
+<a href="money?cmd=bals">List active balances</a><br>
+<a href="money?cmd=nbals">List negative active balances</a><br>
+<a href="money?cmd=freezeworthy">List members who deserve account freezing</a><br>
+<a href="money?cmd=bootworthy">List members who deserve to be kicked out</a><br>
+<a href="money?cmd=deadbals">List retired balances</a><br>
+
+<% if (Group.inGroupName "money" and $"lookback" = "") or $"audit" <> "" then %>
+
+<a href="?cmd=delinq">Drop pledges of delinquent members</a><br>
+<a href="?lookback=20">Switch to regular member view</a><br>
 
-<% if Group.inGroupName "money" then %>
+<% end %>
+</div>
 
-<br><b><u>New transaction:</u></b><br>
-<a href="money?cmd=hosting">Hosting bill</a><br>
+<% if (Group.inGroupName "money" and $"lookback" = "") or $"audit" <> "" then %>
+<div class="payment-tile">
+  <h3>New Transaction</h3>
+<a href="money?cmd=bill">Bill for the co-op</a><br>
 <a href="money?cmd=pay">Payment from member</a><br>
+<a href="money?cmd=dues">Monthly dues</a><br>
+<a href="money?cmd=hosting">New hosting bill (old style)</a><br>
 <a href="money?cmd=evenForm">Generic/even</a><br>
 <br>
-<a href="money?cmd=equalize">Equalize balances</a><br>
+<!-- <a href="money?cmd=equalize">Equalize balances</a><br> -->
+<br>
+</div>
+
+<div>
+<div class="payment-tile">
+<h3>Look up a PayPal e-mail address</h3>
+
+<form method="post">
+<input type="hidden" name="cmd" value="paypal">
+<input name="email"> <input type="submit" value="Look up">
+</form>
+</div>
+
+
+<div class="payment-tile">
+<h3>Look up a Real Name</h3>
+
+<form method="post">
+<input type="hidden" name="cmd" value="realname">
+<input name="rname"> <input type="submit" value="Look up">
+</form>
+</div>
+</div>
+
+<h3>Apply Stripe Payments</h3>
+
+<table class="data">
+<tr><td><strong>Date</strong></td><td><strong>Member</strong></td>
+    <td><strong>Name on Card</strong></td>
+    <td><strong>Amount</strong> (After Fees)</td><td><td></td>
+</tr>
+<% foreach stripePmt in Money.listAllPendingStripePayments () do %>
+
+<tr>
+  <td><% #name (Init.lookupUser (#webuser_id stripePmt)) %></td>
+  <td><% #paid_on stripePmt %></td>
+  <td><% #card_name stripePmt %></td>
+  <td style="text-align: right">$<% #net stripePmt %></td>
+  <td><form method="post">
+        <input type="hidden" name="cmd" value="stripeApply" />
+        <input type="hidden" name="stripeId" value="<% #charge_id stripePmt %>" />
+        <input type="submit" value="Apply to Balance" /> <!-- also, refund? -->
+      </form>
+  </td>
+  <td><form method="post">
+        <input type="hidden" name="cmd" value="stripeReject" />
+        <input type="hidden" name="stripeChargeId" value="<% #charge_id stripePmt %>" />
+        <input type="submit" value="Reject Payment" /> <!-- also, refund? -->
+      </form>
+  </td>
+</tr>
+<% end %>
+</table>
 
 <h3>Most recent transactions</h3>
 
-<table>
+
+
+<table class="data">
 <tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> <td><b>Participants</b></td> <td><b>Replace</b></td> <td><b>Delete</b></td> </tr>
 <% foreach trn in Money.listTransactionsLimit 20 do %>
-<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td><% #amount trn %></td>
+<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td style="text-align: right"><% #amount trn %></td>
 <% switch Money.listChargesWithNames (#id trn) of
          [(name, cha)] => %><td><a href="user?id=<% #usr cha %>"><% name %></a></td><%
        | _ => %><td><i>multi</i></td><%
 end %>
-<td><a href="money?modHosting=<% #id trn %>">[Hosting]</a> <a href="money?modPay=<% #id trn %>">[Payment]</a> <a href="money?modEven=<% #id trn %>">[Other]</a></td>
+<td><a href="money?modBill=<% #id trn %>">[Bill]</a> <a href="money?modPay=<% #id trn %>">[Payment]</a> <a href="money?modEven=<% #id trn %>">[Other]</a></td>
 <td><a href="money?del=<% #id trn %>">[Delete]</a></td> </tr>
 <% end %>
 </table>
 
 <% else %>
+<div class="payment-tile">
+<p>
+<a href="?audit=1">Switch to audit view</a><br>
+</p></div>
+<% val lookback = case $"lookback" of "" => 20 | "inf" => 0 | lb => Web.stoi lb;
+   @payment [] %>
 
 <h3>Your recent account activity</h3>
 
-<table>
-<tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> </tr>
-<% foreach (amount, trn) in Money.listUserTransactionsLimit (Init.getUserId (), 20) do %>
-<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td><% amount %>/<% #amount trn %></td> </tr>
-<% end %>
+<form>
+      Show <input name="lookback" size="5" value="<% iff lookback = 0 then 20 else lookback %>"> most recent transactions.
+      <input type="submit" value="Show">
+</form>
+
+<% ref running = showBal;
+val trans = iff lookback = 0 then
+        Money.listUserTransactions (Init.getUserId ())
+    else
+       Money.listUserTransactionsLimit (Init.getUserId (), lookback) %>
+<table class="data">
+<tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Debit</b></td> <td><b>Credit</b></td> <td><b>Balance</b></td></tr>
+<% foreach (amount, trn) in trans do %>
+<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td>
+<td style="text-align: right"><% if Util.lt (amount, 0.0) then %><% amount %><% end %></td>
+<td style="text-align: right"><% if Util.ge (amount, 0.0) then %><% amount %><% end %></td>
+<td style="text-align: right"><% running %></tr>
+<% running := Util.sub (running, amount)
+end %>
 </table>
+<% if lookback <> 0 then %>
+<a href="?lookback=inf">Show all transactions</a><br>
+<% end %>
 
 <% end
 end %>