cli: --auto adds automated postings to reports
Ledger-style automated postings, previously supported only by hledger-budget, have landed as a first-class feature. The --auto flag activates them, so that any postings they generate are included in reports.
This commit is contained in:
		
							parent
							
								
									f101d5b515
								
							
						
					
					
						commit
						23f3da4e92
					
				| @ -105,6 +105,7 @@ data ReportOpts = ReportOpts { | |||||||
|       -- how to sort negative numbers. |       -- how to sort negative numbers. | ||||||
|     ,color_          :: Bool |     ,color_          :: Bool | ||||||
|     ,forecast_       :: Bool |     ,forecast_       :: Bool | ||||||
|  |     ,auto_           :: Bool | ||||||
|  } deriving (Show, Data, Typeable) |  } deriving (Show, Data, Typeable) | ||||||
| 
 | 
 | ||||||
| instance Default ReportOpts where def = defreportopts | instance Default ReportOpts where def = defreportopts | ||||||
| @ -136,6 +137,7 @@ defreportopts = ReportOpts | |||||||
|     def |     def | ||||||
|     def |     def | ||||||
|     def |     def | ||||||
|  |     def | ||||||
| 
 | 
 | ||||||
| rawOptsToReportOpts :: RawOpts -> IO ReportOpts | rawOptsToReportOpts :: RawOpts -> IO ReportOpts | ||||||
| rawOptsToReportOpts rawopts = checkReportOpts <$> do | rawOptsToReportOpts rawopts = checkReportOpts <$> do | ||||||
| @ -167,6 +169,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do | |||||||
|     ,pretty_tables_ = boolopt "pretty-tables" rawopts' |     ,pretty_tables_ = boolopt "pretty-tables" rawopts' | ||||||
|     ,color_       = color |     ,color_       = color | ||||||
|     ,forecast_    = boolopt "forecast" rawopts' |     ,forecast_    = boolopt "forecast" rawopts' | ||||||
|  |     ,auto_        = boolopt "auto" rawopts' | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| -- | Do extra validation of raw option values, raising an error if there's a problem. | -- | Do extra validation of raw option values, raising an error if there's a problem. | ||||||
|  | |||||||
| @ -155,7 +155,8 @@ reportflags = [ | |||||||
|  ,flagNone ["empty","E"]     (setboolopt "empty") "show items with zero amount, normally hidden" |  ,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 ["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 ["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... | -- | Common output-related flags: --output-file, --output-format... | ||||||
|  | |||||||
| @ -54,6 +54,7 @@ import Hledger.Data | |||||||
| import Hledger.Read | import Hledger.Read | ||||||
| import Hledger.Reports | import Hledger.Reports | ||||||
| import Hledger.Utils | import Hledger.Utils | ||||||
|  | import Hledger.Query (Query(Any)) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| -- | Parse the user's specified journal file, maybe apply some transformations | -- | Parse the user's specified journal file, maybe apply some transformations | ||||||
| @ -71,6 +72,7 @@ withJournalDo opts cmd = do | |||||||
|           . journalApplyAliases (aliasesFromOpts opts) |           . journalApplyAliases (aliasesFromOpts opts) | ||||||
|         <=< journalApplyValue (reportopts_ opts) |         <=< journalApplyValue (reportopts_ opts) | ||||||
|         <=< journalAddForecast opts |         <=< journalAddForecast opts | ||||||
|  |           . generateAutomaticPostings (reportopts_ opts) | ||||||
|   either error' f ej |   either error' f ej | ||||||
| 
 | 
 | ||||||
| -- | Apply the pivot transformation on a journal, if option is present. | -- | Apply the pivot transformation on a journal, if option is present. | ||||||
| @ -141,6 +143,15 @@ journalAddForecast opts j = do | |||||||
|       in |       in | ||||||
|        either error' id $ journalBalanceTransactions assrt j |        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. | -- | Write some output to stdout or to a file selected by --output-file. | ||||||
| -- If the file exists it will be overwritten. | -- If the file exists it will be overwritten. | ||||||
| writeOutput :: CliOpts -> String -> IO () | writeOutput :: CliOpts -> String -> IO () | ||||||
|  | |||||||
							
								
								
									
										78
									
								
								tests/budget/auto.test
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								tests/budget/auto.test
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user