From b4b2b6723698c31b6e9420c5b0b810efa21b14ad Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 15 Apr 2012 00:05:10 +0000 Subject: [PATCH] incomestatement: show revenue and expense accounts separately --- hledger-lib/Hledger/Data/Journal.hs | 49 ++++++++++++++++++++------ hledger/Hledger/Cli/Incomestatement.hs | 12 +++++-- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 4adc302e9..ceb1c6ee9 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -37,6 +37,11 @@ module Hledger.Data.Journal ( -- * Standard account types journalBalanceSheetAccountQuery, journalProfitAndLossAccountQuery, + journalIncomeAccountQuery, + journalExpenseAccountQuery, + journalAssetAccountQuery, + journalLiabilityAccountQuery, + journalEquityAccountQuery, -- * Misc groupPostings, matchpats, @@ -159,21 +164,45 @@ journalAccountNameTree = accountNameTreeFrom . journalAccountNames -- standard account types -balanceSheetAccountRegex, profitAndLossAccountRegex :: String -balanceSheetAccountRegex = "^(assets?|liabilit(y|ies)|equity)(:|$)" -profitAndLossAccountRegex = "^(income|expenses?|profits?|loss(es)?)(:|$)" +-- | A query for Profit & Loss accounts in this journal. +-- Cf . +journalProfitAndLossAccountQuery :: Journal -> Matcher +journalProfitAndLossAccountQuery j = MatchOr [journalIncomeAccountQuery j + ,journalExpenseAccountQuery j + ] + +-- | A query for Income (Revenue) accounts in this journal. +-- This is currently hard-coded to the case-insensitive regex @^(income|revenue)s?(:|$)@. +journalIncomeAccountQuery :: Journal -> Matcher +journalIncomeAccountQuery _ = MatchAcct "^(income|revenue)s?(:|$)" + +-- | A query for Expense accounts in this journal. +-- This is currently hard-coded to the case-insensitive regex @^expenses?(:|$)@. +journalExpenseAccountQuery :: Journal -> Matcher +journalExpenseAccountQuery _ = MatchAcct "^expenses?(:|$)" -- | A query for Asset, Liability & Equity accounts in this journal. -- Cf . --- This is currently hard-coded to the case-insensitive regex @^(assets?|liabilit(y|ies)|equity)(:|$)@. journalBalanceSheetAccountQuery :: Journal -> Matcher -journalBalanceSheetAccountQuery _ = MatchAcct balanceSheetAccountRegex +journalBalanceSheetAccountQuery j = MatchOr [journalAssetAccountQuery j + ,journalLiabilityAccountQuery j + ,journalEquityAccountQuery j + ] --- | A query for Profit & Loss accounts in this journal. --- Cf . --- This is currently hard-coded to the case-insensitive regex @^(income|expenses?|profits?|loss(es)?)(:|$)@. -journalProfitAndLossAccountQuery :: Journal -> Matcher -journalProfitAndLossAccountQuery _ = MatchAcct profitAndLossAccountRegex +-- | A query for Asset accounts in this journal. +-- This is currently hard-coded to the case-insensitive regex @^assets?(:|$)@. +journalAssetAccountQuery :: Journal -> Matcher +journalAssetAccountQuery _ = MatchAcct "^assets?(:|$)" + +-- | A query for Liability accounts in this journal. +-- This is currently hard-coded to the case-insensitive regex @^liabilit(y|ies)(:|$)@. +journalLiabilityAccountQuery :: Journal -> Matcher +journalLiabilityAccountQuery _ = MatchAcct "^liabilit(y|ies)(:|$)" + +-- | A query for Equity accounts in this journal. +-- This is currently hard-coded to the case-insensitive regex @^equity(:|$)@. +journalEquityAccountQuery :: Journal -> Matcher +journalEquityAccountQuery _ = MatchAcct "^equity(:|$)" -- Various kinds of filtering on journals. We do it differently depending -- on the command. diff --git a/hledger/Hledger/Cli/Incomestatement.hs b/hledger/Hledger/Cli/Incomestatement.hs index 85d05fd43..dcd49376b 100644 --- a/hledger/Hledger/Cli/Incomestatement.hs +++ b/hledger/Hledger/Cli/Incomestatement.hs @@ -21,10 +21,18 @@ import Hledger.Cli.Balance -- | Print a standard income statement. incomestatement :: CliOpts -> Journal -> IO () incomestatement CliOpts{reportopts_=ropts} j = do - let report = accountsReport2 ropts (journalProfitAndLossAccountQuery j) j + let incomereport@(_,income) = accountsReport2 ropts (journalIncomeAccountQuery j) j + expensereport@(_,expenses) = accountsReport2 ropts (journalExpenseAccountQuery j) j + total = income + expenses LT.putStr $ [lt|Income Statement -#{unlines $ accountsReportAsText ropts report}|] +Revenues: +#{unlines $ accountsReportAsText ropts incomereport} +Expenses: +#{unlines $ accountsReportAsText ropts expensereport} + +Total: #{show total} +|] tests_Hledger_Cli_Incomestatement :: Test tests_Hledger_Cli_Incomestatement = TestList