From baf232d3d989da3dd7309ed9f1ff18f174d59593 Mon Sep 17 00:00:00 2001 From: Justin Le Date: Sat, 4 Feb 2017 00:17:37 -0800 Subject: [PATCH] moved -V/--value to be a global report option, so it works with balance, print, register, balancesheet, incomestatement, cashflow, etc. --- hledger/Hledger/Cli/Balance.hs | 9 ++------- hledger/Hledger/Cli/BalanceView.hs | 5 +---- hledger/Hledger/Cli/CliOptions.hs | 1 + hledger/Hledger/Cli/Utils.hs | 19 ++++++++++++++++++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/hledger/Hledger/Cli/Balance.hs b/hledger/Hledger/Cli/Balance.hs index 33f8cd138..a441ab211 100644 --- a/hledger/Hledger/Cli/Balance.hs +++ b/hledger/Hledger/Cli/Balance.hs @@ -275,7 +275,6 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don "show historical ending balance in each period (includes postings before report start date)\n " ,flagNone ["tree"] (\opts -> setboolopt "tree" opts) "show accounts as a tree; amounts include subaccounts (default in simple reports)" ,flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list; amounts exclude subaccounts except when account is depth-clipped (default in multicolumn reports)\n " - ,flagNone ["value","V"] (setboolopt "value") "convert amounts to their market value on the report end date (using the most recent applicable market price, if any)" ,flagNone ["average","A"] (\opts -> setboolopt "average" opts) "show a row average column (in multicolumn reports)" ,flagNone ["row-total","T"] (\opts -> setboolopt "row-total" opts) "show a row total column (in multicolumn reports)" ,flagNone ["no-total","N"] (\opts -> setboolopt "no-total" opts) "omit the final total row" @@ -314,23 +313,19 @@ balance opts@CliOpts{reportopts_=ropts} j = do | otherwise = ropts{accountlistmode_=ALTree} in singleBalanceReport ropts' (queryFromOpts d ropts) j | otherwise = balanceReport ropts (queryFromOpts d ropts) j - convert | value_ ropts = maybe id (balanceReportValue j) mvaluedate - | otherwise = id render = case format of "csv" -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r _ -> balanceReportAsText - writeOutput opts $ render ropts $ convert report + writeOutput opts $ render ropts report _ -> do let report = multiBalanceReport ropts (queryFromOpts d ropts) j - convert | value_ ropts = maybe id (multiBalanceReportValue j) mvaluedate - | otherwise = id render = case format of "csv" -> \ropts r -> (++ "\n") $ printCSV $ multiBalanceReportAsCsv ropts r _ -> case baltype of PeriodChange -> periodChangeReportAsText CumulativeChange -> cumulativeChangeReportAsText HistoricalBalance -> historicalBalanceReportAsText - writeOutput opts $ render ropts $ convert report + writeOutput opts $ render ropts report -- single-column balance reports diff --git a/hledger/Hledger/Cli/BalanceView.hs b/hledger/Hledger/Cli/BalanceView.hs index 27915b684..d4daf7f1a 100644 --- a/hledger/Hledger/Cli/BalanceView.hs +++ b/hledger/Hledger/Cli/BalanceView.hs @@ -30,7 +30,6 @@ balanceviewmode bv@BV{..} = (defCommandMode $ bvmode : bvaliases) { groupUnnamed = [ flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list" ,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts" - ,flagNone ["value","V"] (setboolopt "value") "convert amounts to their market value on the report end date (using the most recent applicable market price, if any)" ,flagNone ["no-total","N"] (\opts -> setboolopt "no-total" opts) "omit the final total row" ,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "don't squash boring parent accounts (in tree mode)" ,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)" @@ -51,9 +50,7 @@ balanceviewQueryReport balanceviewQueryReport ropts currDay reportEnd j t q = ([view], Sum amt) where q' = And [queryFromOpts currDay (withoutBeginDate ropts), q j] - convert | value_ ropts = maybe id (balanceReportValue j) reportEnd - | otherwise = id - rep@(_ , amt) = convert $ balanceReport ropts q' j + rep@(_ , amt) = balanceReport ropts q' j view = intercalate "\n" [t <> ":", balanceReportAsText ropts rep] balanceviewReport :: BalanceView -> CliOpts -> Journal -> IO () diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index b05ea5dca..0aea4c2ef 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -144,6 +144,7 @@ reportflags = [ ,flagReq ["depth"] (\s opts -> Right $ setopt "depth" s opts) "N" "hide accounts/postings deeper than N" ,flagNone ["empty","E"] (setboolopt "empty") "show items with zero amount, normally hidden" ,flagNone ["cost","B"] (setboolopt "cost") "convert amounts to their cost at transaction time (using the transaction price, if any)" + ,flagNone ["value","V"] (setboolopt "value") "convert amounts to their market value on the report end date (using the most recent applicable market price, if any)" ,flagNone ["anon"] (setboolopt "anon") "output ledger with anonymized accounts and payees." ] diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index ddb1e693b..cb9e41dd8 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -23,6 +23,7 @@ module Hledger.Cli.Utils ) where import Control.Exception as C +import Control.Monad ((<=<)) import Data.Hashable (hash) import Data.List import Data.Maybe @@ -63,6 +64,7 @@ import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds) import Hledger.Cli.CliOptions import Hledger.Data import Hledger.Read +import Hledger.Reports import Hledger.Utils @@ -76,7 +78,12 @@ withJournalDo opts cmd = do rulespath <- rulesFilePathFromOpts opts journalpaths <- journalFilePathFromOpts opts ej <- readJournalFiles Nothing rulespath (not $ ignore_assertions_ opts) journalpaths - either error' (cmd opts . pivotByOpts opts . anonymiseByOpts opts . journalApplyAliases (aliasesFromOpts opts)) ej + let f = cmd opts + . pivotByOpts opts + . anonymiseByOpts opts + . journalApplyAliases (aliasesFromOpts opts) + <=< journalApplyValue (reportopts_ opts) + either error' f ej -- | Apply the pivot transformation on a journal, if option is present. pivotByOpts :: CliOpts -> Journal -> Journal @@ -119,6 +126,16 @@ anonymise j where anon = T.pack . flip showHex "" . (fromIntegral :: Int -> Word32) . hash +journalApplyValue :: ReportOpts -> Journal -> IO Journal +journalApplyValue ropts j = do + mvaluedate <- reportEndDate j ropts + let convert | value_ ropts + , Just d <- mvaluedate + = overJournalAmounts (amountValue j d) + | otherwise + = id + return $ convert j + -- | Write some output to stdout or to a file selected by --output-file. writeOutput :: CliOpts -> String -> IO () writeOutput opts s = do