diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index e76c2e497..8f18c535d 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -105,6 +105,7 @@ data ReportOpts = ReportOpts { -- how to sort negative numbers. ,color_ :: Bool ,forecast_ :: Bool + ,auto_ :: Bool } deriving (Show, Data, Typeable) instance Default ReportOpts where def = defreportopts @@ -136,6 +137,7 @@ defreportopts = ReportOpts def def def + def rawOptsToReportOpts :: RawOpts -> IO ReportOpts rawOptsToReportOpts rawopts = checkReportOpts <$> do @@ -167,6 +169,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do ,pretty_tables_ = boolopt "pretty-tables" rawopts' ,color_ = color ,forecast_ = boolopt "forecast" rawopts' + ,auto_ = boolopt "auto" rawopts' } -- | Do extra validation of raw option values, raising an error if there's a problem. diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 11292f55f..fb05d0687 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -155,7 +155,8 @@ reportflags = [ ,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 ["forecast"] (\opts -> setboolopt "forecast" opts) "generate forecast transactions" + ,flagNone ["forecast"] (setboolopt "forecast") "generate forecast transactions" + ,flagNone ["auto"] (setboolopt "auto") "generate automated postings" ] -- | Common output-related flags: --output-file, --output-format... diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index 813a6d38b..9a272ad04 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -54,6 +54,7 @@ import Hledger.Data import Hledger.Read import Hledger.Reports import Hledger.Utils +import Hledger.Query (Query(Any)) -- | Parse the user's specified journal file, maybe apply some transformations @@ -71,6 +72,7 @@ withJournalDo opts cmd = do . journalApplyAliases (aliasesFromOpts opts) <=< journalApplyValue (reportopts_ opts) <=< journalAddForecast opts + . generateAutomaticPostings (reportopts_ opts) either error' f ej -- | Apply the pivot transformation on a journal, if option is present. @@ -141,6 +143,15 @@ journalAddForecast opts j = do in either error' id $ journalBalanceTransactions assrt j +-- | Generate Automatic postings and add them to the current journal. +generateAutomaticPostings :: ReportOpts -> Journal -> Journal +generateAutomaticPostings ropts j = + if auto_ ropts then j { jtxns = map modifier $ jtxns j } else j + where + modifier = foldr (flip (.) . runModifierTransaction') id mtxns + runModifierTransaction' = fmap txnTieKnot . runModifierTransaction Any + mtxns = jmodifiertxns j + -- | Write some output to stdout or to a file selected by --output-file. -- If the file exists it will be overwritten. writeOutput :: CliOpts -> String -> IO () diff --git a/tests/budget/auto.test b/tests/budget/auto.test new file mode 100644 index 000000000..daf1c07ca --- /dev/null +++ b/tests/budget/auto.test @@ -0,0 +1,78 @@ +# Add proportional income tax (from documentation) +hledger print -f- --auto +<<< +2016/1/1 paycheck + income:remuneration $-100 + income:donations $-15 + assets:bank + +2016/1/1 withdraw + assets:cash $20 + assets:bank + += ^income + (liabilities:tax) *.33 ; income tax +>>> +2016/01/01 paycheck + income:remuneration $-100 + income:donations $-15 + assets:bank + (liabilities:tax) $-33 ; income tax + (liabilities:tax) $-5 ; income tax + +2016/01/01 withdraw + assets:cash $20 + assets:bank + +>>>2 +>>>=0 + +hledger register -f- --auto +<<< +2016/1/1 paycheck + income:remuneration $-100 + income:donations $-15 + assets:bank + +2016/1/1 withdraw + assets:cash $20 + assets:bank + += ^income + (liabilities:tax) *.33 ; income tax +>>> +2016/01/01 paycheck income:remuneration $-100 $-100 + income:donations $-15 $-115 + assets:bank $115 0 + (liabilities:tax) $-33 $-33 + (liabilities:tax) $-5 $-38 +2016/01/01 withdraw assets:cash $20 $-18 + assets:bank $-20 $-38 +>>>2 +>>>=0 + +hledger balance -f- --auto +<<< +2016/1/1 paycheck + income:remuneration $-100 + income:donations $-15 + assets:bank + +2016/1/1 withdraw + assets:cash $20 + assets:bank + += ^income + (liabilities:tax) *.33 ; income tax +>>> + $115 assets + $95 bank + $20 cash + $-115 income + $-15 donations + $-100 remuneration + $-38 liabilities:tax +-------------------- + $-38 +>>>2 +>>>=0