X-Git-Url: https://git.hcoop.net/hcoop/zz_old/portal.git/blobdiff_plain/d0dd06fa53e29eb65768852e271a1ad35480a28c..HEAD:/money.sml diff --git a/money.sml b/money.sml index ea9255a..f4423ac 100644 --- 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, app + C.map (getDb ()) mkUserRow' ($`SELECT trn, id, name, rname, bal, joined, app, shares, paypal, checkout FROM WebUser LEFT OUTER JOIN Charge ON usr = id AND trn = ^(C.intToSql trn) ORDER BY name`) @@ -213,27 +213,64 @@ fun addHostingCharges {trn, cutoff, cost, usage} = val (umap, amount) = walkNvs (nvs, SM.empty, #amount tran) - val payers = Group.groupMembers paying - val even = amount / real (length payers) + val db = getDb () + + val shares = + case C.oneRow db ($`SELECT SUM(shares) + FROM WebUser JOIN Membership ON usr = WebUser.id AND grp = ^(C.intToSql paying)`) of + [n] => C.intFromSql n + | row => Init.rowError ("Bad addHostingCharges share count result", row) + + val even = amount / real shares - fun doUser (usr : Init.user, umap) = + fun doUser ([uid, uname, shares], umap) = let + val uid = C.intFromSql uid + val uname = C.stringFromSql uname + val shares = C.intFromSql shares + val (charge, umap) = - case SM.find (umap, #name usr) of - NONE => (even, umap) - | SOME extra => (even - extra, #1 (SM.remove (umap, #name usr))) + case SM.find (umap, uname) of + NONE => (even * real shares, umap) + | SOME extra => (even * real shares - extra, #1 (SM.remove (umap, uname))) in - addCharge {trn = trn, usr = #id usr, amount = charge}; + addCharge {trn = trn, usr = uid, amount = charge}; umap end + + val _ = if SM.numItems (C.fold db doUser umap + ($`SELECT id, name, shares + FROM WebUser JOIN Membership ON usr = WebUser.id AND grp = ^(C.intToSql paying)`)) + = 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 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)`)) -end \ No newline at end of file +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