Show running balances on transaction summaries
[bpt/portal.git] / money.mlt
index 2af1635..5977d09 100644 (file)
--- a/money.mlt
+++ b/money.mlt
@@ -29,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>
+<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><% #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><%
@@ -47,7 +69,7 @@ end %>
 <% 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
@@ -153,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 %>
@@ -205,7 +279,7 @@ end %></textarea></td> </tr>
        val amount = Web.stor ($"amount");
        val descr = $"descr";
        val descr = iff descr = "" then $"descr2" else descr;
-       val id = Money.addTransaction ($"descr", amount, $"d");
+       val id = Money.addTransaction (descr, amount, $"d");
        Money.addCharge {trn = id, usr = Web.stoi ($"usr"), amount = amount};
        Money.applyCharges id;
 
@@ -265,6 +339,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");
@@ -459,32 +554,79 @@ elseif $"cmd" = "checkout" then
      (nil, nil) => %>No matches.<%
    end
 
-end %>
+elseif $"cmd" = "delinq" then
+       showNormal := false;
+       val dqs = Money.delinquentPledgers () %>
+<table>
+<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>
+
+<a href="?lower=<% String.concatWith "," (List.map (fn dq => Int.toString (#id dq)) dqs) %>">Lower these pledges to 1</a>
+
+<% 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>
+<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>$<% #balance dq %></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>
+<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>$<% #balance dq %></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 deposit = Balance.depositAmount (#id bal);
+   val showBal = Util.sub (#amount bal, deposit) %>
 
-<!--h3>Your balance: $<% Util.sub (#amount bal, deposit) %><br>
-Deposit: $<% deposit %></b> (3 months of dues at your current <a href="pledge">pledge level</a>)</h3-->
+<h3>Your balance: $<% showBal %><br>
+Deposit: $<% deposit %></b> (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>
 
-<h3>Your balance: $<% #amount bal %></h3>
+<!--h3>Your balance: $<% #amount bal %></h3-->
 
 <% if (iff Group.inGroupName "money" then $"lookback" = "" else $"audit" <> "") 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=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>
 
 <br><b><u>New transaction:</u></b><br>
-<a href="money?cmd=hosting">Hosting bill</a><br>
+<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>
@@ -514,7 +656,7 @@ Deposit: $<% deposit %></b> (3 months of dues at your current <a href="pledge">p
          [(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>
@@ -531,11 +673,14 @@ end %>
       <input type="submit" value="Show">
 </form>
 
+<% ref running = showBal %>
 <table>
-<tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> </tr>
+<tr> <td><b>Date</b></td> <td><b>Description</b></td> <td><b>Amount</b></td> <td><b>Balance</b></td></tr>
 <% foreach (amount, trn) in Money.listUserTransactionsLimit (Init.getUserId (), lookback) 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 %>
+<tr> <td><% #d trn %></td> <td><a href="money?trn=<% #id trn %>"><% Web.html (#descr trn) %></a></td> <td><% amount %>/<% #amount trn %></td>
+<td><% running %></tr>
+<% running := Util.sub (running, amount)
+end %>
 </table>
 
 <% end