Debian Codegraph @

Hledger/Web/Handler/RegisterR.hs

raw
1 -- | /register handlers.
2
3 {-# LANGUAGE NamedFieldPuns #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE QuasiQuotes #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TupleSections #-}
9
10 module Hledger.Web.Handler.RegisterR where
11
12 import Data.List (intersperse, nub, partition)
13 import Data.Text qualified as T
14 import Safe (tailSafe)
15 import Text.Hamlet (hamletFile)
16
17 import Hledger
18 import Hledger.Cli.CliOptions
19 import Hledger.Web.Import
20 import Hledger.Web.WebOptions
21 import Hledger.Web.Widget.AddForm (addModal)
22 import Hledger.Web.Widget.Common
23 (accountQuery, mixedAmountAsHtml,
24 transactionFragment, removeDates, removeInacct, replaceInacct)
25
26 -- | The main journal/account register view, with accounts sidebar.
27 getRegisterR :: Handler Html
28 getRegisterR = do
29 checkServerSideUiEnabled
30 VD{perms, j, q, opts, qparam, qopts, today} <- getViewData
31 require ViewPermission
32
33 let (a,inclsubs) = fromMaybe ("all accounts",True) $ inAccount qopts
34 s1 = if inclsubs then "" else " (excluding subaccounts)"
35 s2 = if q /= Any then ", filtered" else ""
36 header = a <> s1 <> s2
37
38 let rspec = reportspec_ (cliopts_ opts)
39 acctQuery = fromMaybe Any (inAccountQuery qopts)
40 acctlink acc = (RegisterR, [("q", replaceInacct qparam $ accountQuery acc)])
41 otherTransAccounts =
42 map (\(acct,(name,comma)) -> (acct, (T.pack name, T.pack comma))) .
43 undecorateLinks . elideRightDecorated 40 . decorateLinks .
44 addCommas . preferReal . otherTransactionAccounts q acctQuery
45 addCommas xs =
46 zip xs $
47 zip (map (T.unpack . accountSummarisedName . paccount) xs) $
48 tailSafe (", "<$xs) ++ [""]
49 items =
50 styleAmounts (journalCommodityStylesWith HardRounding j) $
51 accountTransactionsReport rspec{_rsQuery=q} j acctQuery
52 balancelabel
53 | isJust (inAccount qopts), balanceaccum_ (_rsReportOpts rspec) == Historical = "Historical Total"
54 | isJust (inAccount qopts) = "Period Total"
55 | otherwise = "Total"
56 transactionFrag = transactionFragment j
57 defaultLayout $ do
58 setTitle "register - hledger-web"
59 $(widgetFile "register")
60
61 -- cf. Hledger.Reports.AccountTransactionsReport.accountTransactionsReportItems
62 otherTransactionAccounts :: Query -> Query -> Transaction -> [Posting]
63 otherTransactionAccounts reportq thisacctq torig
64 -- no current account ? summarise all matched postings
65 | thisacctq == None = reportps
66 -- only postings to current account ? summarise those
67 | null otheraccts = thisacctps
68 -- summarise matched postings to other account(s)
69 | otherwise = otheracctps
70 where
71 reportps = tpostings $ filterTransactionPostings reportq torig
72 (thisacctps, otheracctps) = partition (matchesPosting thisacctq) reportps
73 otheraccts = nub $ map paccount otheracctps
74
75 -- cf. Hledger.Reports.AccountTransactionsReport.summarisePostingAccounts
76 preferReal :: [Posting] -> [Posting]
77 preferReal ps
78 | null realps = ps
79 | otherwise = realps
80 where realps = filter isReal ps
81
82 elideRightDecorated :: Int -> [(Maybe d, Char)] -> [(Maybe d, Char)]
83 elideRightDecorated width s =
84 if length s > width
85 then take (width - 2) s ++ map (Nothing,) ".."
86 else s
87
88 undecorateLinks :: [(Maybe acct, char)] -> [(acct, ([char], [char]))]
89 undecorateLinks [] = []
90 undecorateLinks xs0@(x:_) =
91 case x of
92 (Just acct, _) ->
93 let (link, xs1) = span (isJust . fst) xs0
94 (comma, xs2) = span (isNothing . fst) xs1
95 in (acct, (map snd link, map snd comma)) : undecorateLinks xs2
96 _ -> error' "link name not decorated with account" -- PARTIAL:
97
98 decorateLinks :: [(acct, ([char], [char]))] -> [(Maybe acct, char)]
99 decorateLinks = concatMap $ \(acct, (name, comma)) ->
100 map (Just acct,) name ++ map (Nothing,) comma
101
102 -- | Generate javascript/html for a register balance line chart based on
103 -- the provided "AccountTransactionsReportItem"s.
104 registerChartHtml :: Text -> String -> [(CommoditySymbol, [AccountTransactionsReportItem])] -> HtmlUrl AppRoute
105 registerChartHtml q title percommoditytxnreports = $(hamletFile "templates/chart.hamlet")
106 -- have to make sure plot is not called when our container (maincontent)
107 -- is hidden, eg with add form toggled
108 where
109 charttitle = if null title then "" else title ++ ":"
110 colorForCommodity = fromMaybe 0 . flip lookup commoditiesIndex
111 commoditiesIndex = zip (map fst percommoditytxnreports) [0..] :: [(CommoditySymbol,Int)]
112 simpleMixedAmountQuantity = maybe 0 aquantity . listToMaybe . amounts . mixedAmountStripCosts
113 showZeroCommodity = wbUnpack . showMixedAmountB oneLineNoCostFmt{displayCost=False,displayZeroCommodity=True}
114 shownull c = if null c then " " else c
115 nodatelink = (RegisterR, [("q", T.unwords $ removeDates q)])
116
117 -- | Makes a unix timestamp (milliseconds since epoch) corresponding to noon on the given date in UTC.
118 dayToUtcNoonTimestamp :: Day -> Integer
119 dayToUtcNoonTimestamp d =
120 read (formatTime defaultTimeLocale "%s" t) * 1000 -- XXX read
121 where
122 t = UTCTime d (secondsToDiffTime $ 12 * 60 * 60)