Change granter.sh to give permissions to user specified on command line
[hcoop/zz_old/portal.git] / money.sml
index 056b051..4689491 100644 (file)
--- a/money.sml
+++ b/money.sml
@@ -86,7 +86,7 @@ val mkUserRow' =
      | row => Init.rowError ("listUsers", row)
 
 fun listUsers trn =
-    C.map (getDb ()) mkUserRow' ($`SELECT trn, id, name, rname, bal, joined
+    C.map (getDb ()) mkUserRow' ($`SELECT trn, id, name, rname, bal, joined, app, shares
                                   FROM WebUser LEFT OUTER JOIN Charge ON usr = id AND trn = ^(C.intToSql trn)
                                   ORDER BY name`)
 
@@ -204,7 +204,7 @@ fun addHostingCharges {trn, cutoff, cost, usage} =
                        in
                            walkNvs (rest,
                                     SM.insert (umap, name, extra),
-                                    amount - extra)
+                                    amount + extra)
                        end
                    else
                        walkNvs (rest, umap, amount)
@@ -221,15 +221,42 @@ fun addHostingCharges {trn, cutoff, cost, usage} =
                val (charge, umap) =
                    case SM.find (umap, #name usr) of
                        NONE => (even, umap)
-                     | SOME extra => (even + extra, #1 (SM.remove (umap, #name usr)))
+                     | SOME extra => (even - extra, #1 (SM.remove (umap, #name usr)))
            in
                addCharge {trn = trn, usr = #id usr, amount = charge};
                umap
            end
+
+       val _ = if SM.numItems (foldl doUser umap payers) = 0 then
+                   applyCharges trn
+               else
+                   raise Fail "Usage description contains an unknown username"
+
+       val usageFile = TextIO.openOut (Init.scratchDir ^ "/usage/" ^ Int.toString trn)
     in
-       if SM.numItems (foldl doUser umap payers) = 0 then
-           ()
-       else
-           raise Fail "Usage description contains an unknown username"
+       TextIO.output (usageFile, usage);
+       TextIO.closeOut usageFile
     end
-end
\ No newline at end of file
+
+fun equalizeBalances () =
+    ignore (C.dml (getDb ()) ($`UPDATE Balance SET amount = (SELECT SUM(amount) FROM Charge JOIN WebUser ON usr = WebUser.id WHERE bal = Balance.id)`))
+
+fun lookupHostingUsage trn =
+    let
+       val usageFile = TextIO.openIn (Init.scratchDir ^ "/usage/" ^ Int.toString trn)
+
+       fun loop acc =
+           case TextIO.inputLine usageFile of
+               NONE => String.concat (List.rev acc)
+             | SOME line => loop (line :: acc)
+    in
+       SOME (loop [])
+       before TextIO.closeIn usageFile
+    end handle _ => NONE
+
+fun costBase amt =
+    case C.oneRow (getDb ()) ($`SELECT ^(C.realToSql amt) / SUM(shares) FROM WebUserPaying`) of
+       [share] => C.realFromSql share
+      | row => Init.rowError ("Bad costBase result", row)
+
+end