cli: make equity a builtin command
This commit is contained in:
		
							parent
							
								
									fe9cd2a186
								
							
						
					
					
						commit
						4e6aa06b62
					
				| @ -1,93 +0,0 @@ | |||||||
| #!/usr/bin/env stack |  | ||||||
| {- stack runghc --verbosity info |  | ||||||
|    --package hledger-lib |  | ||||||
|    --package hledger |  | ||||||
|    --package here |  | ||||||
|    --package time |  | ||||||
| -} |  | ||||||
| 
 |  | ||||||
| {-# LANGUAGE OverloadedStrings #-} |  | ||||||
| {-# LANGUAGE QuasiQuotes #-} |  | ||||||
| 
 |  | ||||||
| import Data.Maybe |  | ||||||
| import Data.String.Here |  | ||||||
| import Data.Time.Calendar |  | ||||||
| import Hledger.Cli |  | ||||||
| 
 |  | ||||||
| ------------------------------------------------------------------------------ |  | ||||||
| cmdmode :: Mode RawOpts |  | ||||||
| cmdmode = hledgerCommandMode |  | ||||||
|   [here| equity |  | ||||||
| Print a "closing balances" transaction that brings all accounts (or with |  | ||||||
| query arguments, just the matched accounts) to a zero balance, followed by an |  | ||||||
| opposite "opening balances" transaction that restores the balances from zero. |  | ||||||
| Such transactions can be useful, eg, for bringing account balances across |  | ||||||
| file boundaries. |  | ||||||
| 
 |  | ||||||
| FLAGS |  | ||||||
| 
 |  | ||||||
| The opening balances transaction is useful to carry over |  | ||||||
| asset/liability balances if you choose to start a new journal file, |  | ||||||
| eg at the beginning of the year. |  | ||||||
| 
 |  | ||||||
| The closing balances transaction is useful to zero out balances in |  | ||||||
| the old file, which gives you the option of reporting on both files |  | ||||||
| at once while still seeing correct balances. |  | ||||||
| 
 |  | ||||||
| Balances are calculated, and the opening transaction is dated, as of |  | ||||||
| the report end date, which you should specify with -e or date: (and |  | ||||||
| the closing transaction is dated one day earlier). If a report end |  | ||||||
| date is not specified, it defaults to today. |  | ||||||
| 
 |  | ||||||
| Example: |  | ||||||
| ```shell |  | ||||||
| $ hledger equity -f 2015.journal -e 2016/1/1 assets liabilities >>2015.journal |  | ||||||
| # move the opening balances transaction to 2016.journal |  | ||||||
| $ hledger -f 2015.journal bal assets liabilities not:desc:closing # shows correct 2015 balances |  | ||||||
| $ hledger -f 2016.journal bal assets liabilities                  # shows correct 2016 balances |  | ||||||
| $ hledger -f 2015.journal -f 2016.journal bal assets liabilities  # still shows correct 2016 balances |  | ||||||
| ``` |  | ||||||
| Open question: how to handle txns spanning a file boundary ? Eg: |  | ||||||
| ```journal |  | ||||||
| 2015/12/30 * food |  | ||||||
|     expenses:food:dining   $10 |  | ||||||
|     assets:bank:checking  -$10  ; date:2016/1/4 |  | ||||||
| ``` |  | ||||||
| This command might or might not have some connection to the concept of |  | ||||||
| "closing the books" in accounting. |  | ||||||
|   |] |  | ||||||
|   [] |  | ||||||
|   [generalflagsgroup1] |  | ||||||
|   [] |  | ||||||
|   ([], Just $ argsFlag "[QUERY]") |  | ||||||
| ------------------------------------------------------------------------------ |  | ||||||
| 
 |  | ||||||
| main :: IO () |  | ||||||
| main = do |  | ||||||
|   opts <- getHledgerCliOpts cmdmode |  | ||||||
|   withJournalDo opts $ |  | ||||||
|    \CliOpts{reportopts_=ropts} j -> do |  | ||||||
|         today <- getCurrentDay |  | ||||||
|         let ropts_ = ropts{accountlistmode_=ALFlat} |  | ||||||
|             q = queryFromOpts today ropts_ |  | ||||||
|             (acctbals,_) = balanceReport ropts_ q j |  | ||||||
|             balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals |  | ||||||
|             ps = [posting{paccount=a |  | ||||||
|                          ,pamount=mixed [b] |  | ||||||
|                          ,pbalanceassertion=Just b |  | ||||||
|                          } |  | ||||||
|                  |(a,_,_,mb) <- acctbals |  | ||||||
|                  ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb |  | ||||||
|                  ] |  | ||||||
|                  ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] |  | ||||||
|             enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q |  | ||||||
|             nps = [posting{paccount=a |  | ||||||
|                           ,pamount=mixed [negate b] |  | ||||||
|                           ,pbalanceassertion=Just b{aquantity=0} |  | ||||||
|                           } |  | ||||||
|                   |(a,_,_,mb) <- acctbals |  | ||||||
|                   ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb |  | ||||||
|                   ] |  | ||||||
|                  ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}] |  | ||||||
|         putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps}) |  | ||||||
|         putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps}) |  | ||||||
| @ -20,6 +20,7 @@ module Hledger.Cli.Commands ( | |||||||
|   ,module Hledger.Cli.Commands.Cashflow |   ,module Hledger.Cli.Commands.Cashflow | ||||||
|   ,module Hledger.Cli.Commands.Checkdates |   ,module Hledger.Cli.Commands.Checkdates | ||||||
|   ,module Hledger.Cli.Commands.Checkdupes |   ,module Hledger.Cli.Commands.Checkdupes | ||||||
|  |   ,module Hledger.Cli.Commands.Equity | ||||||
|   ,module Hledger.Cli.Commands.Help |   ,module Hledger.Cli.Commands.Help | ||||||
|   ,module Hledger.Cli.Commands.Incomestatement |   ,module Hledger.Cli.Commands.Incomestatement | ||||||
|   ,module Hledger.Cli.Commands.Print |   ,module Hledger.Cli.Commands.Print | ||||||
| @ -51,6 +52,7 @@ import Hledger.Cli.Commands.Balancesheetequity | |||||||
| import Hledger.Cli.Commands.Cashflow | import Hledger.Cli.Commands.Cashflow | ||||||
| import Hledger.Cli.Commands.Checkdates | import Hledger.Cli.Commands.Checkdates | ||||||
| import Hledger.Cli.Commands.Checkdupes | import Hledger.Cli.Commands.Checkdupes | ||||||
|  | import Hledger.Cli.Commands.Equity | ||||||
| import Hledger.Cli.Commands.Help | import Hledger.Cli.Commands.Help | ||||||
| import Hledger.Cli.Commands.Incomestatement | import Hledger.Cli.Commands.Incomestatement | ||||||
| import Hledger.Cli.Commands.Print | import Hledger.Cli.Commands.Print | ||||||
|  | |||||||
							
								
								
									
										85
									
								
								hledger/Hledger/Cli/Commands/Equity.hs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										85
									
								
								hledger/Hledger/Cli/Commands/Equity.hs
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,85 @@ | |||||||
|  | {-# LANGUAGE OverloadedStrings #-} | ||||||
|  | {-# LANGUAGE QuasiQuotes #-} | ||||||
|  | 
 | ||||||
|  | module Hledger.Cli.Commands.Equity ( | ||||||
|  |   equitymode | ||||||
|  |  ,equity | ||||||
|  | )  | ||||||
|  | where | ||||||
|  | 
 | ||||||
|  | import Data.Maybe | ||||||
|  | import Data.String.Here | ||||||
|  | import Data.Time.Calendar | ||||||
|  | import Hledger | ||||||
|  | import Hledger.Cli.CliOptions | ||||||
|  | 
 | ||||||
|  | equitymode = hledgerCommandMode | ||||||
|  |   [here| equity | ||||||
|  | Print a "closing balances" transaction that brings all accounts (or with | ||||||
|  | query arguments, just the matched accounts) to a zero balance, followed by an | ||||||
|  | opposite "opening balances" transaction that restores the balances from zero. | ||||||
|  | Such transactions can be useful, eg, for bringing account balances across | ||||||
|  | file boundaries. | ||||||
|  | 
 | ||||||
|  | FLAGS | ||||||
|  | 
 | ||||||
|  | The opening balances transaction is useful to carry over | ||||||
|  | asset/liability balances if you choose to start a new journal file, | ||||||
|  | eg at the beginning of the year. | ||||||
|  | 
 | ||||||
|  | The closing balances transaction is useful to zero out balances in | ||||||
|  | the old file, which gives you the option of reporting on both files | ||||||
|  | at once while still seeing correct balances. | ||||||
|  | 
 | ||||||
|  | Balances are calculated, and the opening transaction is dated, as of | ||||||
|  | the report end date, which you should specify with -e or date: (and | ||||||
|  | the closing transaction is dated one day earlier). If a report end | ||||||
|  | date is not specified, it defaults to today. | ||||||
|  | 
 | ||||||
|  | Example: | ||||||
|  | ```shell | ||||||
|  | $ hledger equity -f 2015.journal -e 2016/1/1 assets liabilities >>2015.journal | ||||||
|  | # move the opening balances transaction to 2016.journal | ||||||
|  | $ hledger -f 2015.journal bal assets liabilities not:desc:closing # shows correct 2015 balances | ||||||
|  | $ hledger -f 2016.journal bal assets liabilities                  # shows correct 2016 balances | ||||||
|  | $ hledger -f 2015.journal -f 2016.journal bal assets liabilities  # still shows correct 2016 balances | ||||||
|  | ``` | ||||||
|  | Open question: how to handle txns spanning a file boundary ? Eg: | ||||||
|  | ```journal | ||||||
|  | 2015/12/30 * food | ||||||
|  |     expenses:food:dining   $10 | ||||||
|  |     assets:bank:checking  -$10  ; date:2016/1/4 | ||||||
|  | ``` | ||||||
|  | This command might or might not have some connection to the concept of | ||||||
|  | "closing the books" in accounting. | ||||||
|  |   |] | ||||||
|  |   [] | ||||||
|  |   [generalflagsgroup1] | ||||||
|  |   [] | ||||||
|  |   ([], Just $ argsFlag "[QUERY]") | ||||||
|  | 
 | ||||||
|  | equity CliOpts{reportopts_=ropts} j = do | ||||||
|  |   today <- getCurrentDay | ||||||
|  |   let ropts_ = ropts{accountlistmode_=ALFlat} | ||||||
|  |       q = queryFromOpts today ropts_ | ||||||
|  |       (acctbals,_) = balanceReport ropts_ q j | ||||||
|  |       balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals | ||||||
|  |       ps = [posting{paccount=a | ||||||
|  |                    ,pamount=mixed [b] | ||||||
|  |                    ,pbalanceassertion=Just b | ||||||
|  |                    } | ||||||
|  |            |(a,_,_,mb) <- acctbals | ||||||
|  |            ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb | ||||||
|  |            ] | ||||||
|  |            ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] | ||||||
|  |       enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q | ||||||
|  |       nps = [posting{paccount=a | ||||||
|  |                     ,pamount=mixed [negate b] | ||||||
|  |                     ,pbalanceassertion=Just b{aquantity=0} | ||||||
|  |                     } | ||||||
|  |             |(a,_,_,mb) <- acctbals | ||||||
|  |             ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb | ||||||
|  |             ] | ||||||
|  |            ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}] | ||||||
|  |   putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps}) | ||||||
|  |   putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps}) | ||||||
| @ -89,11 +89,6 @@ is an old pie chart generator, in need of some love. | |||||||
| [hledger-check.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-check.hs) | [hledger-check.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-check.hs) | ||||||
| checks more powerful account balance assertions. | checks more powerful account balance assertions. | ||||||
| 
 | 
 | ||||||
| ### equity |  | ||||||
| 
 |  | ||||||
| [hledger-equity.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-equity.hs#L17) |  | ||||||
| prints balance-resetting transactions, useful for bringing account balances across file boundaries.  |  | ||||||
| 
 |  | ||||||
| ### prices | ### prices | ||||||
| 
 | 
 | ||||||
| [hledger-prices.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-prices.hs) | [hledger-prices.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-prices.hs) | ||||||
|  | |||||||
| @ -346,9 +346,13 @@ Check that transactions are sorted by increasing date. | |||||||
| With a query, only matched transactions' dates are checked. | With a query, only matched transactions' dates are checked. | ||||||
| 
 | 
 | ||||||
| ## check-dupes | ## check-dupes | ||||||
| Reports account names having the same leaf but different prefixes.  | Report account names having the same leaf but different prefixes.  | ||||||
| An example: http://stefanorodighiero.net/software/hledger-dupes.html | An example: http://stefanorodighiero.net/software/hledger-dupes.html | ||||||
| 
 | 
 | ||||||
|  | ## equity | ||||||
|  | Print closing/opening transactions that bring some or all account balances to zero and back.  | ||||||
|  | Can be useful for bringing account balances across file boundaries.  | ||||||
|  | 
 | ||||||
| ## help | ## help | ||||||
| Show any of the hledger manuals. | Show any of the hledger manuals. | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -131,6 +131,7 @@ library | |||||||
|       Hledger.Cli.Commands.Cashflow |       Hledger.Cli.Commands.Cashflow | ||||||
|       Hledger.Cli.Commands.Checkdates |       Hledger.Cli.Commands.Checkdates | ||||||
|       Hledger.Cli.Commands.Checkdupes |       Hledger.Cli.Commands.Checkdupes | ||||||
|  |       Hledger.Cli.Commands.Equity | ||||||
|       Hledger.Cli.Commands.Help |       Hledger.Cli.Commands.Help | ||||||
|       Hledger.Cli.Commands.Incomestatement |       Hledger.Cli.Commands.Incomestatement | ||||||
|       Hledger.Cli.Commands.Print |       Hledger.Cli.Commands.Print | ||||||
|  | |||||||
| @ -112,6 +112,7 @@ library: | |||||||
|   - Hledger.Cli.Commands.Cashflow |   - Hledger.Cli.Commands.Cashflow | ||||||
|   - Hledger.Cli.Commands.Checkdates |   - Hledger.Cli.Commands.Checkdates | ||||||
|   - Hledger.Cli.Commands.Checkdupes |   - Hledger.Cli.Commands.Checkdupes | ||||||
|  |   - Hledger.Cli.Commands.Equity | ||||||
|   - Hledger.Cli.Commands.Help |   - Hledger.Cli.Commands.Help | ||||||
|   - Hledger.Cli.Commands.Incomestatement |   - Hledger.Cli.Commands.Incomestatement | ||||||
|   - Hledger.Cli.Commands.Print |   - Hledger.Cli.Commands.Print | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user