| 1 |
{-| |
| 2 |
|
| 3 |
A ledger-compatible @register@ command. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE CPP #-} |
| 8 |
{-# LANGUAGE OverloadedStrings #-} |
| 9 |
{-# LANGUAGE RecordWildCards #-} |
| 10 |
{-# LANGUAGE TemplateHaskell #-} |
| 11 |
|
| 12 |
module Hledger.Cli.Commands.Register ( |
| 13 |
registermode |
| 14 |
,register |
| 15 |
,postingsReportAsText |
| 16 |
,postingsReportItemAsText |
| 17 |
-- ,showPostingWithBalanceForVty |
| 18 |
,tests_Register |
| 19 |
) where |
| 20 |
|
| 21 |
import Data.Default (def) |
| 22 |
import Data.Maybe (fromMaybe, isJust) |
| 23 |
import Data.Text (Text) |
| 24 |
import Data.Map qualified as Map |
| 25 |
import Data.Text qualified as T |
| 26 |
import Data.Text.Lazy qualified as TL |
| 27 |
import Data.Text.Lazy.IO qualified as TL |
| 28 |
import Data.Text.Lazy.Builder qualified as TB |
| 29 |
import Safe (readMay) |
| 30 |
import System.Console.CmdArgs.Explicit (flagNone, flagReq) |
| 31 |
|
| 32 |
import Hledger hiding (per) |
| 33 |
import Hledger.Write.Csv (CSV, printCSV, printTSV) |
| 34 |
import Hledger.Write.Ods (printFods) |
| 35 |
import Hledger.Write.Html.Lucid (styledTableHtml) |
| 36 |
import Hledger.Write.Spreadsheet qualified as Spr |
| 37 |
import Hledger.Cli.CliOptions |
| 38 |
import Hledger.Cli.Utils |
| 39 |
import Hledger.Cli.Anchor (setAccountAnchor, dateCell) |
| 40 |
import Text.Tabular.AsciiWide (Cell(..), Align(..), Properties(..), Header(Header, Group), renderRowB, textCell, tableBorders, borderSpaces) |
| 41 |
import Lucid qualified |
| 42 |
import Data.List (sortBy) |
| 43 |
import Data.Char (toUpper) |
| 44 |
import Data.List.Extra (intersect) |
| 45 |
import System.IO qualified as IO |
| 46 |
|
| 47 |
registermode = hledgerCommandMode |
| 48 |
$(embedFileRelative "Hledger/Cli/Commands/Register.txt") |
| 49 |
([flagNone ["cumulative"] (setboolopt "cumulative") |
| 50 |
(accumprefix ++ "show running total from report start date (default)") |
| 51 |
,flagNone ["historical","H"] (setboolopt "historical") |
| 52 |
(accumprefix ++ "show historical running total/balance (includes postings before report start date)") |
| 53 |
,flagNone ["average","A"] (setboolopt "average") |
| 54 |
"show running average of posting amounts instead of total (implies --empty)" |
| 55 |
,let arg = "DESC" in |
| 56 |
flagReq ["match","m"] (\s opts -> Right $ setopt "match" s opts) arg |
| 57 |
("fuzzy search for one recent posting with description closest to "++arg) |
| 58 |
,flagNone ["related","r"] (setboolopt "related") "show postings' siblings instead" |
| 59 |
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" |
| 60 |
,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "omit N leading account name parts" |
| 61 |
,flagReq ["sort"] (\s opts -> Right $ setopt "sort" s opts) "FIELDS" |
| 62 |
("sort by: " <> sortKeysDescription |
| 63 |
<> ", or a comma-separated combination of these. For a descending sort, prefix with -. (Default: date)") |
| 64 |
,flagReq ["width","w"] (\s opts -> Right $ setopt "width" s opts) "N" |
| 65 |
("set output width (default: " ++ |
| 66 |
#ifdef mingw32_HOST_OS |
| 67 |
show defaultWidth |
| 68 |
#else |
| 69 |
"terminal width" |
| 70 |
#endif |
| 71 |
++ "). -wN,M sets description width as well." |
| 72 |
) |
| 73 |
,flagNone ["align-all"] (setboolopt "align-all") "guarantee alignment across all lines (slower)" |
| 74 |
,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "URLPREFIX" "in html output, generate links to hledger-web, with this prefix. (Usually the base url shown by hledger-web; can also be relative.)" |
| 75 |
,outputFormatFlag ["txt","csv","tsv","html","fods","json"] |
| 76 |
,outputFileFlag |
| 77 |
]) |
| 78 |
cligeneralflagsgroups1 |
| 79 |
hiddenflags |
| 80 |
([], Just $ argsFlag "[QUERY]") |
| 81 |
where |
| 82 |
accumprefix = "accumulation mode: " |
| 83 |
|
| 84 |
-- | Print a (posting) register report. |
| 85 |
register :: CliOpts -> Journal -> IO () |
| 86 |
register opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j |
| 87 |
-- match mode, print one recent posting most similar to given description, if any |
| 88 |
-- XXX should match similarly to print --match |
| 89 |
| Just desc <- maybestringopt "match" rawopts = do |
| 90 |
let ps = [p | (_,_,_,p,_) <- rpt] |
| 91 |
case similarPosting ps desc of |
| 92 |
Nothing -> error' $ "no postings found with description like " <> show desc |
| 93 |
Just p -> TL.putStr $ postingsReportAsText opts [pri] |
| 94 |
where pri = (Just (postingDate p) |
| 95 |
,Nothing |
| 96 |
,tdescription <$> ptransaction p |
| 97 |
,styleAmounts styles p |
| 98 |
,styleAmounts styles nullmixedamt) |
| 99 |
-- normal register report, list postings |
| 100 |
| otherwise = writeOutputLazyText opts $ render $ styleAmounts styles rpt |
| 101 |
where |
| 102 |
styles = journalCommodityStylesWith HardRounding j |
| 103 |
rpt = postingsReport rspec j |
| 104 |
render | fmt=="txt" = postingsReportAsText opts |
| 105 |
| fmt=="json" = toJsonText |
| 106 |
| fmt=="csv" = printCSV . postingsReportAsCsv opts |
| 107 |
| fmt=="tsv" = printTSV . postingsReportAsCsv opts |
| 108 |
| fmt=="html" = |
| 109 |
(<>"\n") . Lucid.renderText . styledTableHtml . |
| 110 |
map (map (fmap Lucid.toHtml)) . |
| 111 |
postingsReportAsSpreadsheet opts oneLineNoCostFmt baseUrl query |
| 112 |
| fmt=="fods" = |
| 113 |
printFods IO.localeEncoding . Map.singleton "Register" . |
| 114 |
(,) (1,0) . |
| 115 |
postingsReportAsSpreadsheet opts oneLineNoCostFmt baseUrl query |
| 116 |
| otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL: |
| 117 |
where fmt = outputFormatFromOpts opts |
| 118 |
baseUrl = balance_base_url_ $ _rsReportOpts rspec |
| 119 |
query = querystring_ $ _rsReportOpts rspec |
| 120 |
|
| 121 |
postingsReportAsCsv :: CliOpts -> PostingsReport -> CSV |
| 122 |
postingsReportAsCsv opts = |
| 123 |
Spr.rawTableContent . postingsReportAsSpreadsheet opts machineFmt Nothing [] |
| 124 |
|
| 125 |
-- ToDo: --layout=bare etc. |
| 126 |
-- ToDo: Text output does not show headers, but Spreadsheet does |
| 127 |
postingsReportAsSpreadsheet :: |
| 128 |
CliOpts -> AmountFormat -> Maybe Text -> [Text] -> |
| 129 |
PostingsReport -> [[Spr.Cell Spr.NumLines Text]] |
| 130 |
postingsReportAsSpreadsheet opts fmt baseUrl query is = |
| 131 |
Spr.addHeaderBorders |
| 132 |
(map Spr.headerCell |
| 133 |
["txnidx","date","code","description","account","amount","total"]) |
| 134 |
: |
| 135 |
map (postingsReportItemAsRecord opts fmt baseUrl query) is |
| 136 |
|
| 137 |
{- ToDo: |
| 138 |
link txnidx to journal URL, |
| 139 |
however, requires Web.Widget.Common.transactionFragment |
| 140 |
-} |
| 141 |
postingsReportItemAsRecord :: |
| 142 |
(Spr.Lines border) => |
| 143 |
CliOpts -> AmountFormat -> Maybe Text -> [Text] -> |
| 144 |
PostingsReportItem -> [Spr.Cell border Text] |
| 145 |
postingsReportItemAsRecord opts@CliOpts{reportspec_=rspec} fmt baseUrl query (_, _, _, p, b) = |
| 146 |
[idx, |
| 147 |
(dateCell baseUrl query (paccount p) date) {Spr.cellType = Spr.TypeDate}, |
| 148 |
cell code, cell desc, |
| 149 |
setAccountAnchor baseUrl query (paccount p) $ cell acct, |
| 150 |
amountCell (pamount p), |
| 151 |
amountCell b] |
| 152 |
where |
| 153 |
cell = Spr.defaultCell |
| 154 |
idx = Spr.integerCell . maybe 0 tindex $ ptransaction p |
| 155 |
date = postingDate p -- XXX csv should show date2 with --date2 |
| 156 |
code = maybe "" tcode $ ptransaction p |
| 157 |
desc = maybe "" tdescription $ ptransaction p |
| 158 |
acct = bracket . dropAcct . clipAcct $ paccount p |
| 159 |
where |
| 160 |
clipAcct = clipOrEllipsifyAccountName (depth_ $ _rsReportOpts rspec) |
| 161 |
dropAcct = accountNameDrop (fromMaybe 0 $ readMay =<< maybestringopt "drop" (rawopts_ opts)) |
| 162 |
bracket = case ptype p of |
| 163 |
BalancedVirtualPosting -> wrap "[" "]" |
| 164 |
VirtualPosting -> wrap "(" ")" |
| 165 |
_ -> id |
| 166 |
-- Since postingsReport strips prices from all Amounts when not used, we can display prices. |
| 167 |
amountCell amt = |
| 168 |
wbToText <$> Spr.cellFromMixedAmount fmt (Spr.Class "amount", amt) |
| 169 |
|
| 170 |
-- | Render a register report as plain text suitable for console output. |
| 171 |
postingsReportAsText :: CliOpts -> PostingsReport -> TL.Text |
| 172 |
postingsReportAsText opts = TB.toLazyText . |
| 173 |
postingsOrTransactionsReportAsText alignAll opts (postingsReportItemAsText opts) itemamt itembal |
| 174 |
where |
| 175 |
alignAll = boolopt "align-all" $ rawopts_ opts |
| 176 |
itemamt (_,_,_,Posting{pamount=a},_) = a |
| 177 |
itembal (_,_,_,_,a) = a |
| 178 |
|
| 179 |
-- | Render one register report line item as plain text. Layout is like so: |
| 180 |
-- @ |
| 181 |
-- <---------------- width (specified, terminal width, or 80) --------------------> |
| 182 |
-- date (10) description account amount (12) balance (12) |
| 183 |
-- DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaa AAAAAAAAAAAA AAAAAAAAAAAA |
| 184 |
-- @ |
| 185 |
-- If description's width is specified, account will use the remaining space. |
| 186 |
-- Otherwise, description and account divide up the space equally. |
| 187 |
-- |
| 188 |
-- With a report interval, the layout is like so: |
| 189 |
-- @ |
| 190 |
-- <---------------- width (specified, terminal width, or 80) --------------------> |
| 191 |
-- date (21) account amount (12) balance (12) |
| 192 |
-- DDDDDDDDDDDDDDDDDDDDD aaaaaaaaaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAAA AAAAAAAAAAAA |
| 193 |
-- @ |
| 194 |
-- |
| 195 |
-- date and description are shown for the first posting of a transaction only. |
| 196 |
-- |
| 197 |
-- Returns a string which can be multi-line, eg if the running balance |
| 198 |
-- has multiple commodities. Does not yet support formatting control |
| 199 |
-- like balance reports. |
| 200 |
-- |
| 201 |
-- Also returns the natural width (without padding) of the amount and balance |
| 202 |
-- fields. |
| 203 |
postingsReportItemAsText :: CliOpts -> Int -> Int |
| 204 |
-> (PostingsReportItem, [WideBuilder], [WideBuilder]) |
| 205 |
-> TB.Builder |
| 206 |
postingsReportItemAsText opts@CliOpts{reportspec_=rspec} preferredamtwidth preferredbalwidth ((mdate, mperiod, mdesc, p, _), amt, bal) = |
| 207 |
table <> TB.singleton '\n' |
| 208 |
where |
| 209 |
table = renderRowB def{tableBorders=False, borderSpaces=False} . Group NoLine $ map Header |
| 210 |
[ textCell TopLeft $ fitText (Just datewidth) (Just datewidth) True True date |
| 211 |
, spacerCell |
| 212 |
, textCell TopLeft $ fitText (Just descwidth) (Just descwidth) True True desc |
| 213 |
, spacerCell2 |
| 214 |
, textCell TopLeft $ fitText (Just acctwidth) (Just acctwidth) True True acct |
| 215 |
, spacerCell2 |
| 216 |
, Cell TopRight $ map (pad amtwidth) amt |
| 217 |
, spacerCell2 |
| 218 |
, Cell BottomRight $ map (pad balwidth) bal |
| 219 |
] |
| 220 |
spacerCell = Cell BottomLeft [WideBuilder (TB.singleton ' ') 1] |
| 221 |
spacerCell2 = Cell BottomLeft [WideBuilder (TB.fromString " ") 2] |
| 222 |
pad fullwidth amt' = WideBuilder (TB.fromText $ T.replicate w " ") w <> amt' |
| 223 |
where w = fullwidth - wbWidth amt' |
| 224 |
-- calculate widths |
| 225 |
(totalwidth,mdescwidth) = registerWidthsFromOpts opts |
| 226 |
datewidth = maybe 10 periodTextWidth mperiod |
| 227 |
date = case mperiod of |
| 228 |
Just per -> if isJust mdate then showPeriod per else "" |
| 229 |
Nothing -> maybe "" showDate mdate |
| 230 |
(amtwidth, balwidth) |
| 231 |
| shortfall <= 0 = (preferredamtwidth, preferredbalwidth) |
| 232 |
| otherwise = (adjustedamtwidth, adjustedbalwidth) |
| 233 |
where |
| 234 |
mincolwidth = 2 -- columns always show at least an ellipsis |
| 235 |
maxamtswidth = max 0 (totalwidth - (datewidth + 1 + mincolwidth + 2 + mincolwidth + 2 + 2)) |
| 236 |
shortfall = (preferredamtwidth + preferredbalwidth) - maxamtswidth |
| 237 |
amtwidthproportion = fromIntegral preferredamtwidth / fromIntegral (preferredamtwidth + preferredbalwidth) |
| 238 |
adjustedamtwidth = round $ amtwidthproportion * fromIntegral maxamtswidth |
| 239 |
adjustedbalwidth = maxamtswidth - adjustedamtwidth |
| 240 |
|
| 241 |
remaining = totalwidth - (datewidth + 1 + 2 + amtwidth + 2 + balwidth) |
| 242 |
(descwidth, acctwidth) |
| 243 |
| isJust mperiod = (0, remaining - 2) |
| 244 |
| otherwise = (w, remaining - 2 - w) |
| 245 |
where |
| 246 |
w = fromMaybe ((remaining - 2) `div` 2) mdescwidth |
| 247 |
|
| 248 |
-- gather content |
| 249 |
desc = fromMaybe "" mdesc |
| 250 |
acct = parenthesise . elideAccountName awidth . dropAcct . clipAcct $ paccount p |
| 251 |
where |
| 252 |
clipAcct = clipOrEllipsifyAccountName (depth_ $ _rsReportOpts rspec) |
| 253 |
dropAcct = accountNameDrop (fromMaybe 0 $ readMay =<< maybestringopt "drop" (rawopts_ opts)) |
| 254 |
(parenthesise, awidth) = case ptype p of |
| 255 |
BalancedVirtualPosting -> (wrap "[" "]", acctwidth-2) |
| 256 |
VirtualPosting -> (wrap "(" ")", acctwidth-2) |
| 257 |
_ -> (id,acctwidth) |
| 258 |
|
| 259 |
-- for register --match: |
| 260 |
|
| 261 |
-- Identify the closest recent match for this description in the given date-sorted postings. |
| 262 |
similarPosting :: [Posting] -> String -> Maybe Posting |
| 263 |
similarPosting ps desc = |
| 264 |
let matches = |
| 265 |
sortBy compareRelevanceAndRecency |
| 266 |
$ filter ((> threshold).fst) |
| 267 |
[(maybe 0 (\t -> compareDescriptions desc (T.unpack $ tdescription t)) (ptransaction p), p) | p <- ps] |
| 268 |
where |
| 269 |
compareRelevanceAndRecency (n1,p1) (n2,p2) = compare (n2,postingDate p2) (n1,postingDate p1) |
| 270 |
threshold = 0 |
| 271 |
in case matches of [] -> Nothing |
| 272 |
m:_ -> Just $ snd m |
| 273 |
|
| 274 |
-- -- Identify the closest recent match for this description in past transactions. |
| 275 |
-- similarTransaction :: Journal -> Query -> String -> Maybe Transaction |
| 276 |
-- similarTransaction j q desc = |
| 277 |
-- case historymatches = transactionsSimilarTo j q desc of |
| 278 |
-- ((,t):_) = Just t |
| 279 |
-- [] = Nothing |
| 280 |
|
| 281 |
compareDescriptions :: String -> String -> Double |
| 282 |
compareDescriptions s t = compareStrings s' t' |
| 283 |
where s' = simplify s |
| 284 |
t' = simplify t |
| 285 |
simplify = filter (not . (`elem` ("0123456789"::String))) |
| 286 |
|
| 287 |
-- | Return a similarity measure, from 0 to 1, for two strings. |
| 288 |
-- This is Simon White's letter pairs algorithm from |
| 289 |
-- http://www.catalysoft.com/articles/StrikeAMatch.html |
| 290 |
-- with a modification for short strings. |
| 291 |
compareStrings :: String -> String -> Double |
| 292 |
compareStrings "" "" = 1 |
| 293 |
compareStrings [_] "" = 0 |
| 294 |
compareStrings "" [_] = 0 |
| 295 |
compareStrings [a] [b] = if toUpper a == toUpper b then 1 else 0 |
| 296 |
compareStrings s1 s2 = 2.0 * fromIntegral i / fromIntegral u |
| 297 |
where |
| 298 |
i = length $ intersect pairs1 pairs2 |
| 299 |
u = length pairs1 + length pairs2 |
| 300 |
pairs1 = wordLetterPairs $ uppercase s1 |
| 301 |
pairs2 = wordLetterPairs $ uppercase s2 |
| 302 |
|
| 303 |
wordLetterPairs = concatMap letterPairs . words |
| 304 |
|
| 305 |
letterPairs (a:b:rest) = [a,b] : letterPairs (b:rest) |
| 306 |
letterPairs _ = [] |
| 307 |
|
| 308 |
-- tests |
| 309 |
|
| 310 |
tests_Register = testGroup "Register" [ |
| 311 |
|
| 312 |
testGroup "postingsReportAsText" [ |
| 313 |
testCase "unicode in register layout" $ do |
| 314 |
j <- readJournal'' "2009/01/01 * медвежья шкура\n расходы:покупки 100\n актив:наличные\n" |
| 315 |
let rspec = defreportspec |
| 316 |
(TL.unpack . postingsReportAsText defcliopts $ postingsReport rspec j) |
| 317 |
@?= |
| 318 |
unlines |
| 319 |
["2009-01-01 медвежья шкура расходы:покупки 100 100" |
| 320 |
," актив:наличные -100 0"] |
| 321 |
] |
| 322 |
|
| 323 |
] |