BalanceView system implemented for incomestatement and cashflow
This commit is contained in:
		
							parent
							
								
									a6f98f1170
								
							
						
					
					
						commit
						321ace107e
					
				| @ -2,22 +2,39 @@ | |||||||
| 
 | 
 | ||||||
| module Hledger.Cli.BalanceView ( | module Hledger.Cli.BalanceView ( | ||||||
|   BalanceView(..) |   BalanceView(..) | ||||||
|  |  ,balanceviewmode | ||||||
|  ,balanceviewReport |  ,balanceviewReport | ||||||
| ) where | ) where | ||||||
| 
 | 
 | ||||||
| import Data.Time.Calendar | import Data.Time.Calendar (Day) | ||||||
| import Data.List | import Data.List (intercalate) | ||||||
| import Data.Monoid (Sum(..), (<>)) | import Data.Monoid (Sum(..), (<>)) | ||||||
| import qualified Data.Text as T | import System.Console.CmdArgs.Explicit | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
| import Hledger.Cli.Balance | import Hledger.Cli.Balance | ||||||
| import Hledger.Cli.CliOptions | import Hledger.Cli.CliOptions | ||||||
| 
 | 
 | ||||||
| data BalanceView = BV { bvname    :: String | data BalanceView = BV { bvmode    :: String | ||||||
|  |                       , bvaliases :: [String] | ||||||
|  |                       , bvhelp    :: String | ||||||
|  |                       , bvname    :: String | ||||||
|                       , bvqueries :: [(String, Journal -> Query)] |                       , bvqueries :: [(String, Journal -> Query)] | ||||||
|                       } |                       } | ||||||
| 
 | 
 | ||||||
|  | balanceviewmode :: BalanceView -> Mode RawOpts | ||||||
|  | balanceviewmode bv@BV{..} = (defCommandMode $ bvmode : bvaliases) { | ||||||
|  |   modeHelp = bvhelp `withAliases` bvaliases | ||||||
|  |  ,modeGroupFlags = Group { | ||||||
|  |      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" | ||||||
|  |      ] | ||||||
|  |     ,groupHidden = [] | ||||||
|  |     ,groupNamed = [generalflagsgroup1] | ||||||
|  |     } | ||||||
|  |  } | ||||||
|  | 
 | ||||||
| balanceviewQueryReport | balanceviewQueryReport | ||||||
|     :: ReportOpts |     :: ReportOpts | ||||||
|     -> Day |     -> Day | ||||||
|  | |||||||
| @ -11,38 +11,26 @@ module Hledger.Cli.Balancesheet ( | |||||||
|  ,tests_Hledger_Cli_Balancesheet |  ,tests_Hledger_Cli_Balancesheet | ||||||
| ) where | ) where | ||||||
| 
 | 
 | ||||||
| import qualified Data.Text.Lazy.IO as LT |  | ||||||
| import System.Console.CmdArgs.Explicit | import System.Console.CmdArgs.Explicit | ||||||
| import Test.HUnit | import Test.HUnit | ||||||
| import Text.Shakespeare.Text |  | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
| import Hledger.Cli.CliOptions | import Hledger.Cli.CliOptions | ||||||
| import Hledger.Cli.Balance |  | ||||||
| import Hledger.Cli.BalanceView | import Hledger.Cli.BalanceView | ||||||
| 
 | 
 | ||||||
|  | bsBV = BV "balancesheet" | ||||||
|  |           ["bs"] | ||||||
|  |           "show a balance sheet" | ||||||
|  |           "Balance Sheet" | ||||||
|  |           [ ("Assets", journalAssetAccountQuery) | ||||||
|  |           , ("Liabilities", journalLiabilityAccountQuery) | ||||||
|  |           ] | ||||||
| 
 | 
 | ||||||
| balancesheetmode :: Mode RawOpts | balancesheetmode :: Mode RawOpts | ||||||
| balancesheetmode = (defCommandMode $ ["balancesheet"]++aliases) { | balancesheetmode = balanceviewmode bsBV | ||||||
|   modeHelp = "show a balance sheet" `withAliases` aliases |  | ||||||
|  ,modeGroupFlags = Group { |  | ||||||
|      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" |  | ||||||
|      ] |  | ||||||
|     ,groupHidden = [] |  | ||||||
|     ,groupNamed = [generalflagsgroup1] |  | ||||||
|     } |  | ||||||
|  } |  | ||||||
|   where aliases = ["bs"] |  | ||||||
| 
 | 
 | ||||||
| balancesheet :: CliOpts -> Journal -> IO () | balancesheet :: CliOpts -> Journal -> IO () | ||||||
| balancesheet = balanceviewReport bv | balancesheet = balanceviewReport bsBV | ||||||
|   where |  | ||||||
|     bv = BV "Balance Sheet" |  | ||||||
|             [ ("Assets", journalAssetAccountQuery) |  | ||||||
|             , ("Liabilities", journalLiabilityAccountQuery) |  | ||||||
|             ] |  | ||||||
| 
 | 
 | ||||||
| tests_Hledger_Cli_Balancesheet :: Test | tests_Hledger_Cli_Balancesheet :: Test | ||||||
| tests_Hledger_Cli_Balancesheet = TestList | tests_Hledger_Cli_Balancesheet = TestList | ||||||
|  | |||||||
| @ -14,48 +14,25 @@ module Hledger.Cli.Cashflow ( | |||||||
|  ,tests_Hledger_Cli_Cashflow |  ,tests_Hledger_Cli_Cashflow | ||||||
| ) where | ) where | ||||||
| 
 | 
 | ||||||
| import qualified Data.Text.Lazy.IO as LT |  | ||||||
| import System.Console.CmdArgs.Explicit | import System.Console.CmdArgs.Explicit | ||||||
| import Test.HUnit | import Test.HUnit | ||||||
| import Text.Shakespeare.Text |  | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
| import Hledger.Cli.CliOptions | import Hledger.Cli.CliOptions | ||||||
| import Hledger.Cli.Balance | import Hledger.Cli.BalanceView | ||||||
| 
 | 
 | ||||||
|  | cfBV = BV "cashflow" | ||||||
|  |           ["cf"] | ||||||
|  |           "show a cashflow statement statement" | ||||||
|  |           "Cashflow Statement" | ||||||
|  |           [ ("Cash flows", journalCashAccountQuery) | ||||||
|  |           ] | ||||||
| 
 | 
 | ||||||
| cashflowmode :: Mode RawOpts | cashflowmode :: Mode RawOpts | ||||||
| cashflowmode = (defCommandMode ["cashflow","cf"]) { | cashflowmode = balanceviewmode cfBV | ||||||
|   modeHelp = "show a cashflow statement" `withAliases` ["cf"] |  | ||||||
|  ,modeGroupFlags = Group { |  | ||||||
|      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" |  | ||||||
|      ] |  | ||||||
|     ,groupHidden = [] |  | ||||||
|     ,groupNamed = [generalflagsgroup1] |  | ||||||
|     } |  | ||||||
|  } |  | ||||||
| 
 | 
 | ||||||
| -- | Print a simple cashflow statement. |  | ||||||
| cashflow :: CliOpts -> Journal -> IO () | cashflow :: CliOpts -> Journal -> IO () | ||||||
| cashflow CliOpts{reportopts_=ropts} j = do | cashflow = balanceviewReport cfBV | ||||||
|   -- let lines = case lineFormatFromOpts ropts of Left err, Right ... |  | ||||||
|   d <- getCurrentDay |  | ||||||
|   let q = queryFromOpts d ropts |  | ||||||
|       cashreport@(_,total) = balanceReport ropts (And [q, journalCashAccountQuery j]) j |  | ||||||
|       -- operatingreport@(_,operating) = balanceReport ropts (And [q, journalOperatingAccountMatcher j]) j |  | ||||||
|       -- investingreport@(_,investing) = balanceReport ropts (And [q, journalInvestingAccountMatcher j]) j |  | ||||||
|       -- financingreport@(_,financing) = balanceReport ropts (And [q, journalFinancingAccountMatcher j]) j |  | ||||||
|       -- total = operating + investing + financing |  | ||||||
|   LT.putStr $ [lt|Cashflow Statement |  | ||||||
| 
 |  | ||||||
| Cash flows: |  | ||||||
| #{balanceReportAsText ropts cashreport} |  | ||||||
| Total: |  | ||||||
| -------------------- |  | ||||||
| #{padleft 20 $ showMixedAmountWithoutPrice total} |  | ||||||
| |] |  | ||||||
| 
 | 
 | ||||||
| tests_Hledger_Cli_Cashflow :: Test | tests_Hledger_Cli_Cashflow :: Test | ||||||
| tests_Hledger_Cli_Cashflow = TestList | tests_Hledger_Cli_Cashflow = TestList | ||||||
|  | |||||||
| @ -11,48 +11,26 @@ module Hledger.Cli.Incomestatement ( | |||||||
|  ,tests_Hledger_Cli_Incomestatement |  ,tests_Hledger_Cli_Incomestatement | ||||||
| ) where | ) where | ||||||
| 
 | 
 | ||||||
| import qualified Data.Text.Lazy.IO as LT |  | ||||||
| import System.Console.CmdArgs.Explicit | import System.Console.CmdArgs.Explicit | ||||||
| import Test.HUnit | import Test.HUnit | ||||||
| import Text.Shakespeare.Text |  | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
| import Hledger.Cli.CliOptions | import Hledger.Cli.CliOptions | ||||||
| import Hledger.Cli.Balance | import Hledger.Cli.BalanceView | ||||||
| 
 | 
 | ||||||
|  | isBV = BV "incomestatement" | ||||||
|  |           ["is"] | ||||||
|  |           "show an income statement" | ||||||
|  |           "Income Statement" | ||||||
|  |           [ ("Revenues", journalIncomeAccountQuery) | ||||||
|  |           , ("Expenses", journalExpenseAccountQuery) | ||||||
|  |           ] | ||||||
| 
 | 
 | ||||||
| incomestatementmode :: Mode RawOpts | incomestatementmode :: Mode RawOpts | ||||||
| incomestatementmode = (defCommandMode $ ["incomestatement"]++aliases) { | incomestatementmode = balanceviewmode isBV | ||||||
|   modeHelp = "show an income statement" `withAliases` aliases |  | ||||||
|  ,modeGroupFlags = Group { |  | ||||||
|      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" |  | ||||||
|      ] |  | ||||||
|     ,groupHidden = [] |  | ||||||
|     ,groupNamed = [generalflagsgroup1] |  | ||||||
|     } |  | ||||||
|  } |  | ||||||
|   where aliases = ["is"] |  | ||||||
| 
 | 
 | ||||||
| -- | Print a simple income statement. |  | ||||||
| incomestatement :: CliOpts -> Journal -> IO () | incomestatement :: CliOpts -> Journal -> IO () | ||||||
| incomestatement CliOpts{reportopts_=ropts} j = do | incomestatement = balanceviewReport isBV | ||||||
|   d <- getCurrentDay |  | ||||||
|   let q = queryFromOpts d ropts |  | ||||||
|       incomereport@(_,income)    = balanceReport ropts (And [q, journalIncomeAccountQuery j]) j |  | ||||||
|       expensereport@(_,expenses) = balanceReport ropts (And [q, journalExpenseAccountQuery j]) j |  | ||||||
|       total = income + expenses |  | ||||||
|   LT.putStr $ [lt|Income Statement |  | ||||||
| 
 |  | ||||||
| Revenues: |  | ||||||
| #{balanceReportAsText ropts incomereport} |  | ||||||
| Expenses: |  | ||||||
| #{balanceReportAsText ropts expensereport} |  | ||||||
| Total: |  | ||||||
| -------------------- |  | ||||||
| #{padleft 20 $ showMixedAmountWithoutPrice total} |  | ||||||
| |] |  | ||||||
| 
 | 
 | ||||||
| tests_Hledger_Cli_Incomestatement :: Test | tests_Hledger_Cli_Incomestatement :: Test | ||||||
| tests_Hledger_Cli_Incomestatement = TestList | tests_Hledger_Cli_Incomestatement = TestList | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user