| 1 |
{-| |
| 2 |
|
| 3 |
A ledger-compatible @print@ command. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE OverloadedStrings #-} |
| 8 |
{-# LANGUAGE TemplateHaskell #-} |
| 9 |
|
| 10 |
module Hledger.Cli.Commands.Print ( |
| 11 |
printmode |
| 12 |
,print' |
| 13 |
-- ,entriesReportAsText |
| 14 |
,roundFlag |
| 15 |
,roundFromRawOpts |
| 16 |
,amountStylesSetRoundingFromRawOpts |
| 17 |
,transactionWithMostlyOriginalPostings |
| 18 |
) |
| 19 |
where |
| 20 |
|
| 21 |
|
| 22 |
import Data.Function ((&)) |
| 23 |
import Data.List (intersperse, intercalate) |
| 24 |
import Data.List.Extra (nubSort) |
| 25 |
import Data.Text (Text) |
| 26 |
import Data.Map (Map) |
| 27 |
import Data.Map qualified as Map |
| 28 |
import Data.Text qualified as T |
| 29 |
import Data.Text.Lazy qualified as TL |
| 30 |
import Data.Text.Lazy.Builder qualified as TB |
| 31 |
import Lens.Micro ((^.), _Just, has) |
| 32 |
import Safe (lastMay, minimumDef) |
| 33 |
import System.Console.CmdArgs.Explicit |
| 34 |
|
| 35 |
import Hledger |
| 36 |
import Hledger.Write.Beancount (accountNameToBeancount, showTransactionBeancount, showBeancountMetadata) |
| 37 |
import Hledger.Write.Csv (CSV, printCSV, printTSV) |
| 38 |
import Hledger.Write.Ods (printFods) |
| 39 |
import Hledger.Write.Html.Lucid (styledTableHtml) |
| 40 |
import Hledger.Write.Spreadsheet qualified as Spr |
| 41 |
import Hledger.Cli.CliOptions |
| 42 |
import Hledger.Cli.Utils |
| 43 |
import Hledger.Cli.Anchor (setAccountAnchor) |
| 44 |
import Lucid qualified |
| 45 |
import System.IO qualified as IO |
| 46 |
import Data.Maybe (isJust, catMaybes, fromMaybe) |
| 47 |
import Hledger.Write.Beancount (commodityToBeancount, tagsToBeancountMetadata) |
| 48 |
|
| 49 |
printmode = hledgerCommandMode |
| 50 |
$(embedFileRelative "Hledger/Cli/Commands/Print.txt") |
| 51 |
([ |
| 52 |
flagNone ["explicit","x"] (setboolopt "explicit") "show all amounts explicitly" |
| 53 |
,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" |
| 54 |
,flagNone ["locations"] (setboolopt "locations") "add tags showing file paths and line numbers" |
| 55 |
,let arg = "DESC" in |
| 56 |
flagReq ["match","m"] (\s opts -> Right $ setopt "match" s opts) arg |
| 57 |
("fuzzy search for one recent transaction with description closest to "++arg) |
| 58 |
,flagNone ["new"] (setboolopt "new") "show only newer-dated transactions added in each file since last run" |
| 59 |
,roundFlag |
| 60 |
,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "URLPREFIX" |
| 61 |
"in html output, generate links to hledger-web, with this prefix. (Usually the base url shown by hledger-web; can also be relative.)" |
| 62 |
,outputFormatFlag ["txt","beancount","csv","tsv","html","fods","json","sql"] |
| 63 |
,outputFileFlag |
| 64 |
]) |
| 65 |
cligeneralflagsgroups1 |
| 66 |
(hiddenflags ++ |
| 67 |
-- this works already as a unique abbreviation, but declare here in case it ever becomes nonunique |
| 68 |
[flagNone ["location"] (setboolopt "locations") "deprecated, use --locations instead" |
| 69 |
]) |
| 70 |
([], Just $ argsFlag "[QUERY]") |
| 71 |
|
| 72 |
roundFlag = flagReq ["round"] (\s opts -> Right $ setopt "round" s opts) "TYPE" $ |
| 73 |
intercalate "\n" |
| 74 |
["how much rounding or padding should be done when displaying amounts ?" |
| 75 |
,"none - show original decimal digits," |
| 76 |
," as in journal (default)" |
| 77 |
,"soft - just add or remove decimal zeros" |
| 78 |
," to match precision" |
| 79 |
,"hard - round posting amounts to precision" |
| 80 |
," (can unbalance transactions)" |
| 81 |
,"all - also round cost amounts to precision" |
| 82 |
," (can unbalance transactions)" |
| 83 |
] |
| 84 |
|
| 85 |
-- | Get the --round option's value, if any. Can fail with a parse error. |
| 86 |
roundFromRawOpts :: RawOpts -> Maybe Rounding |
| 87 |
roundFromRawOpts = lastMay . collectopts roundfromrawopt |
| 88 |
where |
| 89 |
roundfromrawopt (n,v) |
| 90 |
| n=="round", v=="none" = Just NoRounding |
| 91 |
| n=="round", v=="soft" = Just SoftRounding |
| 92 |
| n=="round", v=="hard" = Just HardRounding |
| 93 |
| n=="round", v=="all" = Just AllRounding |
| 94 |
| n=="round" = error' $ "--round's value should be none, soft, hard or all; got: "++v |
| 95 |
| otherwise = Nothing |
| 96 |
|
| 97 |
-- | Set these amount styles' rounding strategy when they are being applied to amounts, |
| 98 |
-- according to the value of the --round option, if any. |
| 99 |
amountStylesSetRoundingFromRawOpts :: RawOpts -> Map CommoditySymbol AmountStyle -> Map CommoditySymbol AmountStyle |
| 100 |
amountStylesSetRoundingFromRawOpts rawopts styles = |
| 101 |
case roundFromRawOpts rawopts of |
| 102 |
Just r -> amountStylesSetRounding r styles |
| 103 |
Nothing -> styles |
| 104 |
|
| 105 |
-- | Print journal transactions in standard format. |
| 106 |
print' :: CliOpts -> Journal -> IO () |
| 107 |
print' opts@CliOpts{rawopts_=rawopts} j = do |
| 108 |
-- The print command should show all amounts with their original decimal places, |
| 109 |
-- but as part of journal reading the posting amounts have already been normalised |
| 110 |
-- according to commodity display styles, and currently it's not easy to avoid |
| 111 |
-- that. For now we try to reverse it by increasing all amounts' decimal places |
| 112 |
-- sufficiently to show the amount exactly. The displayed amounts may have minor |
| 113 |
-- differences from the originals, such as trailing zeroes added. |
| 114 |
let |
| 115 |
-- lbl = lbl_ "print'" |
| 116 |
j' = j |
| 117 |
-- & dbg9With (lbl "amounts before setting full precision".showJournalPostingAmountsDebug) |
| 118 |
& journalMapPostingAmounts mixedAmountSetFullPrecision |
| 119 |
-- & dbg9With (lbl "amounts after setting full precision: ".showJournalPostingAmountsDebug) |
| 120 |
& if boolopt "locations" rawopts then journalMapTransactions addLocationTag else id |
| 121 |
|
| 122 |
case maybestringopt "match" $ rawopts_ opts of |
| 123 |
Nothing -> printEntries opts j' |
| 124 |
Just desc -> |
| 125 |
-- match mode, prints one recent transaction most similar to given description |
| 126 |
-- XXX should match similarly to register --match |
| 127 |
case journalSimilarTransaction opts j' (dbg1 "finding best match for description" $ T.pack desc) of |
| 128 |
Just t -> printEntries opts j'{jtxns=[t]} |
| 129 |
Nothing -> error' $ "no transactions found with descriptions like " <> show desc |
| 130 |
|
| 131 |
printEntries :: CliOpts -> Journal -> IO () |
| 132 |
printEntries opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j = |
| 133 |
writeOutputLazyText opts $ render $ entriesReport rspec j |
| 134 |
where |
| 135 |
-- print does user-specified rounding or (by default) no rounding, in all output formats |
| 136 |
styles = amountStylesSetRoundingFromRawOpts rawopts $ journalCommodityStyles j |
| 137 |
|
| 138 |
fmt = outputFormatFromOpts opts |
| 139 |
baseUrl = balance_base_url_ $ _rsReportOpts rspec |
| 140 |
query = querystring_ $ _rsReportOpts rspec |
| 141 |
render | fmt=="txt" = entriesReportAsText . styleAmounts styles . map maybeoriginalamounts |
| 142 |
| fmt=="beancount" = entriesReportAsBeancount (jdeclaredaccounttags j) . styleAmounts styles . map maybeoriginalamounts |
| 143 |
| fmt=="csv" = printCSV . entriesReportAsCsv . styleAmounts styles |
| 144 |
| fmt=="tsv" = printTSV . entriesReportAsCsv . styleAmounts styles |
| 145 |
| fmt=="json" = toJsonText . styleAmounts styles |
| 146 |
| fmt=="sql" = entriesReportAsSql . styleAmounts styles |
| 147 |
| fmt=="html" = |
| 148 |
(<>"\n") . Lucid.renderText . styledTableHtml . |
| 149 |
map (map (fmap Lucid.toHtml)) . |
| 150 |
entriesReportAsSpreadsheet oneLineNoCostFmt baseUrl query . |
| 151 |
styleAmounts styles |
| 152 |
| fmt=="fods" = |
| 153 |
printFods IO.localeEncoding . Map.singleton "Print" . |
| 154 |
(,) (1,0) . |
| 155 |
entriesReportAsSpreadsheet oneLineNoCostFmt baseUrl query . |
| 156 |
styleAmounts styles |
| 157 |
| otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL: |
| 158 |
where |
| 159 |
maybeoriginalamounts |
| 160 |
-- Use the fully inferred and amount-styled/rounded transaction in the following situations: |
| 161 |
-- with -x/--explicit: |
| 162 |
| boolopt "explicit" (rawopts_ opts) = id |
| 163 |
-- with --infer-costs |
| 164 |
| opts ^. infer_costs = id |
| 165 |
-- with -B/-V/-X/--value ("because of #551, and because of print -V valuing only one posting when there's an implicit txn price.") |
| 166 |
| has (value . _Just) opts = id |
| 167 |
-- Otherwise, keep the transaction's amounts close to how they were written in the journal. |
| 168 |
| otherwise = transactionWithMostlyOriginalPostings |
| 169 |
|
| 170 |
-- | Replace this transaction's postings with the original postings if any, but keep the |
| 171 |
-- current possibly rewritten account names, and the inferred values of any auto postings. |
| 172 |
-- This is mainly for showing transactions with the amounts in their original journal format. |
| 173 |
transactionWithMostlyOriginalPostings :: Transaction -> Transaction |
| 174 |
transactionWithMostlyOriginalPostings = transactionMapPostings postingMostlyOriginal |
| 175 |
where |
| 176 |
postingMostlyOriginal p = orig |
| 177 |
{ paccount = paccount p |
| 178 |
, pamount = pamount $ if isGenerated then p else orig } |
| 179 |
where |
| 180 |
orig = originalPosting p |
| 181 |
isGenerated = "_generated-posting" `elem` map fst (ptags p) |
| 182 |
|
| 183 |
entriesReportAsText :: EntriesReport -> TL.Text |
| 184 |
entriesReportAsText = entriesReportAsTextHelper showTransaction |
| 185 |
|
| 186 |
entriesReportAsTextHelper :: (Transaction -> T.Text) -> EntriesReport -> TL.Text |
| 187 |
entriesReportAsTextHelper showtxn = TB.toLazyText . foldMap (TB.fromText . showtxn) |
| 188 |
|
| 189 |
-- | This generates Beancount-compatible journal output, transforming/encoding the data |
| 190 |
-- in various ways when necessary (see Beancount.hs). It renders: |
| 191 |
-- account open directives for each account used (on their earliest posting dates), |
| 192 |
-- operating_currency directives (based on currencies used in costs), |
| 193 |
-- and transaction entries. |
| 194 |
-- Transaction and posting tags are converted to metadata lines. |
| 195 |
-- Account tags are not propagated to the open directive, currently. |
| 196 |
entriesReportAsBeancount :: Map AccountName [Tag] -> EntriesReport -> TL.Text |
| 197 |
entriesReportAsBeancount atags ts = |
| 198 |
-- PERF: gathers and converts all account names, then repeats that work when showing each transaction |
| 199 |
TL.concat [ |
| 200 |
TL.fromStrict operatingcurrencydirectives |
| 201 |
,TL.fromStrict openaccountdirectives |
| 202 |
,"\n" |
| 203 |
,entriesReportAsTextHelper showTransactionBeancount ts3 |
| 204 |
] |
| 205 |
where |
| 206 |
-- Remove any virtual postings. |
| 207 |
ts2 = [t{tpostings=filter isReal $ tpostings t} | t <- ts] |
| 208 |
|
| 209 |
-- Remove any conversion postings that are redundant with costs. |
| 210 |
-- It would be easier to remove the costs instead, |
| 211 |
-- but those are more useful to Beancount than conversion postings. |
| 212 |
ts3 = |
| 213 |
[ t{tpostings=filter (not . isredundantconvp) $ tpostings t} |
| 214 |
| t <- ts2 |
| 215 |
-- XXX But conversion-posting tag is on non-redundant postings too, so how to do it ? |
| 216 |
-- Assume the simple case of no more than one cost + conversion posting group in each transaction. |
| 217 |
-- Actually that seems to be required by hledger right now. |
| 218 |
, let isredundantconvp p = |
| 219 |
matchesPosting (Tag (toRegex' "conversion-posting") Nothing) p |
| 220 |
&& any (any (isJust.acost) . amounts . pamount) (tpostings t) |
| 221 |
] |
| 222 |
|
| 223 |
-- https://fava.pythonanywhere.com/example-beancount-file/help/beancount_syntax |
| 224 |
-- https://fava.pythonanywhere.com/example-beancount-file/help/options |
| 225 |
-- "conversion-currencies |
| 226 |
-- When set, the currency conversion select dropdown in all charts will show the list of currencies specified in this option. |
| 227 |
-- By default, Fava lists all operating currencies and those currencies that match ISO 4217 currency codes." |
| 228 |
|
| 229 |
-- http://furius.ca/beancount/doc/syntax |
| 230 |
-- http://furius.ca/beancount/doc/options |
| 231 |
-- "This option may be supplied multiple times ... |
| 232 |
-- A list of currencies that we single out during reporting and create dedicated columns for ... |
| 233 |
-- we use this to display these values in table cells without their associated unit strings ... |
| 234 |
-- This is used to indicate the main currencies that you work with in real life" |
| 235 |
-- We use: all currencies used in costs. |
| 236 |
operatingcurrencydirectives |
| 237 |
| null basecurrencies = "" |
| 238 |
| otherwise = T.unlines (map (todirective . commodityToBeancount) basecurrencies) <> "\n" |
| 239 |
where |
| 240 |
todirective c = "option \"operating_currency\" \"" <> c <> "\"" |
| 241 |
basecurrencies = allcostcurrencies |
| 242 |
where |
| 243 |
allcostcurrencies = nubSort $ map acommodity costamounts |
| 244 |
where |
| 245 |
costamounts = |
| 246 |
map (\c -> case c of |
| 247 |
UnitCost a -> a |
| 248 |
TotalCost a -> a |
| 249 |
) $ |
| 250 |
catMaybes $ |
| 251 |
map acost $ |
| 252 |
concatMap (amounts . pamount) $ |
| 253 |
concatMap tpostings |
| 254 |
ts3 |
| 255 |
|
| 256 |
-- http://furius.ca/beancount/doc/syntax |
| 257 |
-- "there exists an “Open” directive that is used to provide the start date of each account. |
| 258 |
-- That can be located anywhere in the file, it does not have to appear in the file somewhere before you use an account name. |
| 259 |
-- You can just start using account names in transactions right away, |
| 260 |
-- though all account names that receive postings to them will eventually have to have |
| 261 |
-- a corresponding Open directive with a date that precedes all transactions posted to the account in the input file." |
| 262 |
openaccountdirectives |
| 263 |
| null ts = "" |
| 264 |
| otherwise = T.unlines [ |
| 265 |
T.intercalate "\n" $ |
| 266 |
firstdate <> " open " <> accountNameToBeancount a : |
| 267 |
mdlines |
| 268 |
| a <- nubSort $ concatMap (map paccount.tpostings) ts3 |
| 269 |
, let mds = tagsToBeancountMetadata $ fromMaybe [] $ Map.lookup a atags |
| 270 |
, let maxwidth = maximum' $ map (T.length . fst) mds |
| 271 |
, let mdlines = map (postingIndent . showBeancountMetadata (Just maxwidth)) mds |
| 272 |
] |
| 273 |
where |
| 274 |
firstdate = showDate $ minimumDef err $ map tdate ts3 |
| 275 |
where err = error' "entriesReportAsBeancount: should not happen" |
| 276 |
|
| 277 |
entriesReportAsSql :: EntriesReport -> TL.Text |
| 278 |
entriesReportAsSql txns = TB.toLazyText $ mconcat |
| 279 |
[ TB.fromText "create table if not exists postings(id serial,txnidx int,date1 date,date2 date,status text,code text,description text,comment text,account text,amount numeric,commodity text,credit numeric,debit numeric,posting_status text,posting_comment text);\n" |
| 280 |
, TB.fromText "insert into postings(txnidx,date1,date2,status,code,description,comment,account,amount,commodity,credit,debit,posting_status,posting_comment) values\n" |
| 281 |
, mconcat . intersperse (TB.fromText ",") $ map values csv |
| 282 |
, TB.fromText ";\n" |
| 283 |
] |
| 284 |
where |
| 285 |
values vs = TB.fromText "(" <> mconcat (intersperse (TB.fromText ",") $ map toSql vs) <> TB.fromText ")\n" |
| 286 |
toSql "" = TB.fromText "NULL" |
| 287 |
toSql s = TB.fromText "'" <> TB.fromText (T.replace "'" "''" s) <> TB.fromText "'" |
| 288 |
csv = |
| 289 |
Spr.rawTableContent . transactionToSpreadsheet machineFmt Nothing [] . |
| 290 |
transactionMapPostingAmounts (mapMixedAmount setDecimalPoint) |
| 291 |
=<< txns |
| 292 |
where |
| 293 |
setDecimalPoint a = a{astyle=(astyle a){asdecimalmark=Just '.'}} |
| 294 |
|
| 295 |
entriesReportAsCsv :: EntriesReport -> CSV |
| 296 |
entriesReportAsCsv = |
| 297 |
Spr.rawTableContent . entriesReportAsSpreadsheet machineFmt Nothing [] |
| 298 |
|
| 299 |
entriesReportAsSpreadsheet :: |
| 300 |
AmountFormat -> Maybe Text -> [Text] -> |
| 301 |
EntriesReport -> [[Spr.Cell Spr.NumLines Text]] |
| 302 |
entriesReportAsSpreadsheet fmt baseUrl query txns = |
| 303 |
Spr.addHeaderBorders |
| 304 |
(map Spr.headerCell |
| 305 |
["txnidx","date","date2","status","code","description","comment", |
| 306 |
"account","amount","commodity","credit","debit", |
| 307 |
"posting-status","posting-comment"]) |
| 308 |
: |
| 309 |
concatMap (transactionToSpreadsheet fmt baseUrl query) txns |
| 310 |
|
| 311 |
-- | Generate one record per posting, duplicating the common transaction fields. |
| 312 |
-- The txnidx field (transaction index) allows postings to be grouped back into transactions. |
| 313 |
transactionToSpreadsheet :: |
| 314 |
AmountFormat -> Maybe Text -> [Text] -> |
| 315 |
Transaction -> [[Spr.Cell Spr.NumLines Text]] |
| 316 |
transactionToSpreadsheet fmt baseUrl query t = |
| 317 |
addRowSpanHeader (idx:d:d2:status:code:description:comment:[]) |
| 318 |
(postingToSpreadsheet fmt baseUrl query =<< tpostings t) |
| 319 |
where |
| 320 |
cell = Spr.defaultCell |
| 321 |
idx = Spr.integerCell $ tindex t |
| 322 |
description = cell $ tdescription t |
| 323 |
dateCell date = |
| 324 |
(Spr.defaultCell $ showDate date) {Spr.cellType = Spr.TypeDate} |
| 325 |
d = dateCell $ tdate t |
| 326 |
d2 = maybe Spr.emptyCell dateCell $ tdate2 t |
| 327 |
status = cell $ T.pack . show $ tstatus t |
| 328 |
code = cell $ tcode t |
| 329 |
comment = cell $ T.strip $ tcomment t |
| 330 |
|
| 331 |
addRowSpanHeader :: |
| 332 |
[Spr.Cell border text] -> |
| 333 |
[[Spr.Cell border text]] -> [[Spr.Cell border text]] |
| 334 |
addRowSpanHeader common rows = |
| 335 |
case rows of |
| 336 |
[] -> [] |
| 337 |
[row] -> [common++row] |
| 338 |
_ -> |
| 339 |
let setSpan spn cell = cell{Spr.cellSpan = spn} in |
| 340 |
zipWith (++) |
| 341 |
(map (setSpan $ Spr.SpanVertical $ length rows) common : |
| 342 |
repeat (map (setSpan Spr.Covered) common)) |
| 343 |
rows |
| 344 |
|
| 345 |
postingToSpreadsheet :: |
| 346 |
(Spr.Lines border) => |
| 347 |
AmountFormat -> Maybe Text -> [Text] -> |
| 348 |
Posting -> [[Spr.Cell border Text]] |
| 349 |
postingToSpreadsheet fmt baseUrl query p = |
| 350 |
map (\(a@(Amount {aquantity=q,acommodity=c})) -> |
| 351 |
-- commodity goes into separate column, so we suppress it, along with digit group |
| 352 |
-- separators and prices |
| 353 |
let a_ = amountStripCost a{acommodity=""} in |
| 354 |
let credit = if q < 0 then amountCell $ negate a_ else Spr.emptyCell in |
| 355 |
let debit = if q >= 0 then amountCell a_ else Spr.emptyCell in |
| 356 |
[setAccountAnchor baseUrl query (paccount p) $ cell account, |
| 357 |
amountCell a_, cell c, |
| 358 |
credit, debit, cell status, cell comment]) |
| 359 |
. amounts $ pamount p |
| 360 |
where |
| 361 |
cell = Spr.defaultCell |
| 362 |
amountCell amt = |
| 363 |
Spr.cellFromAmount fmt |
| 364 |
(Spr.Class "amount", (wbToText $ showAmountB machineFmt amt, amt)) |
| 365 |
status = T.pack . show $ pstatus p |
| 366 |
account = showAccountName Nothing (ptype p) (paccount p) |
| 367 |
comment = T.strip $ pcomment p |
| 368 |
|
| 369 |
addLocationTag :: Transaction -> Transaction |
| 370 |
addLocationTag t = t{tcomment = tcomment t `commentAddTagNextLine` loctag} |
| 371 |
where |
| 372 |
loctag = ("location", T.pack . sourcePosPairPretty $ tsourcepos t) |