From 47b1142d49f77e689f4e2bf0036c866eef026705 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 18 Jul 2011 23:30:40 +0000 Subject: [PATCH] move reports back to Hledger.Cli for now, forgot they still depend on cli options --- hledger-lib/Hledger.hs | 2 - hledger/Hledger/Cli.hs | 3 + hledger/Hledger/Cli/Add.hs | 1 + hledger/Hledger/Cli/Balance.hs | 1 + hledger/Hledger/Cli/Print.hs | 1 + hledger/Hledger/Cli/Register.hs | 1 + .../Hledger/Cli/Reports.hs | 136 +++++++++--------- hledger/hledger.cabal | 2 + 8 files changed, 80 insertions(+), 67 deletions(-) rename hledger-lib/Hledger/Report.hs => hledger/Hledger/Cli/Reports.hs (96%) diff --git a/hledger-lib/Hledger.hs b/hledger-lib/Hledger.hs index 722d4fa9a..7d6e1a81e 100644 --- a/hledger-lib/Hledger.hs +++ b/hledger-lib/Hledger.hs @@ -1,11 +1,9 @@ module Hledger ( module Hledger.Data ,module Hledger.Read - ,module Hledger.Report ,module Hledger.Utils ) where import Hledger.Data import Hledger.Read -import Hledger.Report import Hledger.Utils diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index 2cd8f96e5..c922360a9 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -10,6 +10,7 @@ module Hledger.Cli ( module Hledger.Cli.Histogram, module Hledger.Cli.Print, module Hledger.Cli.Register, + module Hledger.Cli.Reports, module Hledger.Cli.Stats, module Hledger.Cli.Options, module Hledger.Cli.Utils, @@ -30,6 +31,7 @@ import Hledger.Cli.Convert import Hledger.Cli.Histogram import Hledger.Cli.Print import Hledger.Cli.Register +import Hledger.Cli.Reports import Hledger.Cli.Stats import Hledger.Cli.Options import Hledger.Cli.Utils @@ -50,6 +52,7 @@ tests_Hledger_Cli = TestList ,tests_Hledger_Cli_Options -- ,tests_Hledger_Cli_Print ,tests_Hledger_Cli_Register + ,tests_Hledger_Cli_Reports -- ,tests_Hledger_Cli_Stats diff --git a/hledger/Hledger/Cli/Add.hs b/hledger/Hledger/Cli/Add.hs index 3f5f63439..c5d5cf991 100644 --- a/hledger/Hledger/Cli/Add.hs +++ b/hledger/Hledger/Cli/Add.hs @@ -34,6 +34,7 @@ import Hledger.Utils.UTF8 (putStr, putStrLn, appendFile) import Hledger.Cli.Options import Hledger.Cli.Register (postingRegisterReportAsText) import Hledger.Cli.Utils +import Hledger.Cli.Reports {- | Information used as the basis for suggested account names, amounts, diff --git a/hledger/Hledger/Cli/Balance.hs b/hledger/Hledger/Cli/Balance.hs index 14b97a260..944eeec76 100644 --- a/hledger/Hledger/Cli/Balance.hs +++ b/hledger/Hledger/Cli/Balance.hs @@ -111,6 +111,7 @@ import Hledger.Utils.UTF8 (putStr) import Hledger.Cli.Format import qualified Hledger.Cli.Format as Format import Hledger.Cli.Options +import Hledger.Cli.Reports -- | Print a balance report. diff --git a/hledger/Hledger/Cli/Print.hs b/hledger/Hledger/Cli/Print.hs index f0779f343..29fd37233 100644 --- a/hledger/Hledger/Cli/Print.hs +++ b/hledger/Hledger/Cli/Print.hs @@ -15,6 +15,7 @@ import Hledger import Prelude hiding (putStr) import Hledger.Utils.UTF8 (putStr) import Hledger.Cli.Options +import Hledger.Cli.Reports -- | Print journal transactions in standard format. print' :: [Opt] -> [String] -> Journal -> IO () diff --git a/hledger/Hledger/Cli/Register.hs b/hledger/Hledger/Cli/Register.hs index 0bc93f4c3..a78d6958d 100644 --- a/hledger/Hledger/Cli/Register.hs +++ b/hledger/Hledger/Cli/Register.hs @@ -21,6 +21,7 @@ import Hledger import Prelude hiding (putStr) import Hledger.Utils.UTF8 (putStr) import Hledger.Cli.Options +import Hledger.Cli.Reports -- | Print a (posting) register report. diff --git a/hledger-lib/Hledger/Report.hs b/hledger/Hledger/Cli/Reports.hs similarity index 96% rename from hledger-lib/Hledger/Report.hs rename to hledger/Hledger/Cli/Reports.hs index d835911d4..384bbdd02 100644 --- a/hledger-lib/Hledger/Report.hs +++ b/hledger/Hledger/Cli/Reports.hs @@ -1,31 +1,37 @@ {-| -Generate several common kinds of report from a journal, as "Reports" - +Generate several common kinds of report from a journal, as \"*Report\" - simple intermediate data structures intended to be easily rendered as text, html, json, csv etc. by hledger commands, hamlet templates, -javascript, or whatever. +javascript, or whatever. This is under Hledger.Cli since it depends +on the command-line options, should move to hledger-lib later. -} -module Hledger.Report ( - tests_Hledger_Report - ,JournalReport - ,JournalReportItem - ,PostingRegisterReport - ,PostingRegisterReportItem - ,AccountRegisterReport - ,AccountRegisterReportItem - ,BalanceReport - ,BalanceReportItem - ,ariDate - ,ariBalance - ,journalReport - ,postingRegisterReport - ,accountRegisterReport - ,journalRegisterReport - ,mkpostingRegisterItem -- for silly showPostingWithBalanceForVty in Hledger.Cli.Register - ,balanceReport - ,balanceReport2 +module Hledger.Cli.Reports ( + -- * Journal report + JournalReport, + JournalReportItem, + journalReport, + -- * Posting register report + PostingRegisterReport, + PostingRegisterReportItem, + postingRegisterReport, + mkpostingRegisterItem, -- for silly showPostingWithBalanceForVty in Hledger.Cli.Register + journalRegisterReport, + -- * Account register report + AccountRegisterReport, + AccountRegisterReportItem, + ariDate, + ariBalance, + accountRegisterReport, + -- * Balance report + BalanceReport, + BalanceReportItem, + balanceReport, + balanceReport2, + -- * Tests + tests_Hledger_Cli_Reports ) where @@ -40,16 +46,26 @@ import Test.HUnit import Text.ParserCombinators.Parsec import Text.Printf -import Hledger.Cli.Options -import Hledger.Cli.Utils import Hledger.Data import Hledger.Utils +import Hledger.Cli.Options +import Hledger.Cli.Utils + +------------------------------------------------------------------------------- -- | A "journal report" is just a list of transactions. type JournalReport = [JournalReportItem] type JournalReportItem = Transaction +-- | Select transactions, as in the print command. +journalReport :: [Opt] -> FilterSpec -> Journal -> JournalReport +journalReport opts fspec j = sortBy (comparing tdate) $ jtxns $ filterJournalTransactions fspec j' + where + j' = journalSelectingDateFromOpts opts $ journalSelectingAmountFromOpts opts j + +------------------------------------------------------------------------------- + -- | A posting register report lists postings to one or more accounts, -- with a running total. Postings may be actual postings, or aggregate -- postings corresponding to a reporting interval. @@ -62,46 +78,6 @@ type PostingRegisterReportItem = (Maybe (Day, String) -- transaction date and de ,MixedAmount -- the running total after this posting ) --- | An account register report lists transactions to a single account (or --- possibly subs as well), with the accurate running account balance when --- possible (otherwise, a running total.) -type AccountRegisterReport = (String -- label for the balance column, eg "balance" or "total" - ,[AccountRegisterReportItem] -- line items, one per transaction - ) - -type AccountRegisterReportItem = (Transaction -- the corresponding transaction - ,Transaction -- the transaction with postings to the focussed account removed - ,Bool -- is this a split (more than one other-account posting) ? - ,String -- the (possibly aggregated) account info to display - ,MixedAmount -- the (possibly aggregated) amount to display (sum of the other-account postings) - ,MixedAmount -- the running balance for the focussed account after this transaction - ) - -ariDate (t,_,_,_,_,_) = tdate t -ariBalance (_,_,_,_,_,Mixed a) = case a of [] -> "0" - (Amount{quantity=q}):_ -> show q - --- | A balance report is a chart of accounts with balances, and their grand total. -type BalanceReport = ([BalanceReportItem] -- line items, one per account - ,MixedAmount -- total balance of all accounts - ) - -type BalanceReportItem = (AccountName -- full account name - ,AccountName -- account name elided for display: the leaf name, - -- prefixed by any boring parents immediately above - ,Int -- how many steps to indent this account (0-based account depth excluding boring parents) - ,MixedAmount) -- account balance, includes subs unless --flat is present - -------------------------------------------------------------------------------- - --- | Select transactions, as in the print command. -journalReport :: [Opt] -> FilterSpec -> Journal -> JournalReport -journalReport opts fspec j = sortBy (comparing tdate) $ jtxns $ filterJournalTransactions fspec j' - where - j' = journalSelectingDateFromOpts opts $ journalSelectingAmountFromOpts opts j - -------------------------------------------------------------------------------- - -- | Select postings from the journal and get their running balance, as in -- the register command. postingRegisterReport :: [Opt] -> FilterSpec -> Journal -> PostingRegisterReport @@ -250,6 +226,25 @@ journalRegisterReport _ Journal{jtxns=ts} m = (totallabel, items) ------------------------------------------------------------------------------- +-- | An account register report lists transactions to a single account (or +-- possibly subs as well), with the accurate running account balance when +-- possible (otherwise, a running total.) +type AccountRegisterReport = (String -- label for the balance column, eg "balance" or "total" + ,[AccountRegisterReportItem] -- line items, one per transaction + ) + +type AccountRegisterReportItem = (Transaction -- the corresponding transaction + ,Transaction -- the transaction with postings to the focussed account removed + ,Bool -- is this a split (more than one other-account posting) ? + ,String -- the (possibly aggregated) account info to display + ,MixedAmount -- the (possibly aggregated) amount to display (sum of the other-account postings) + ,MixedAmount -- the running balance for the focussed account after this transaction + ) + +ariDate (t,_,_,_,_,_) = tdate t +ariBalance (_,_,_,_,_,Mixed a) = case a of [] -> "0" + (Amount{quantity=q}):_ -> show q + -- | Select transactions within one (or more) specified accounts, and get -- their running balance within that (those) account(s). Used for a -- conventional quicker/gnucash/bank-style account register. Specifically, @@ -338,6 +333,17 @@ filterTransactionPostings m t@Transaction{tpostings=ps} = t{tpostings=filter (m ------------------------------------------------------------------------------- +-- | A balance report is a chart of accounts with balances, and their grand total. +type BalanceReport = ([BalanceReportItem] -- line items, one per account + ,MixedAmount -- total balance of all accounts + ) + +type BalanceReportItem = (AccountName -- full account name + ,AccountName -- account name elided for display: the leaf name, + -- prefixed by any boring parents immediately above + ,Int -- how many steps to indent this account (0-based account depth excluding boring parents) + ,MixedAmount) -- account balance, includes subs unless --flat is present + -- | Select accounts, and get their balances at the end of the selected -- period, as in the balance command. balanceReport :: [Opt] -> FilterSpec -> Journal -> BalanceReport @@ -408,8 +414,8 @@ isInterestingIndented opts l a ------------------------------------------------------------------------------- -tests_Hledger_Report :: Test -tests_Hledger_Report = TestList +tests_Hledger_Cli_Reports :: Test +tests_Hledger_Cli_Reports = TestList [ "summarisePostingsByInterval" ~: do diff --git a/hledger/hledger.cabal b/hledger/hledger.cabal index 2a960f3de..9d5180af6 100644 --- a/hledger/hledger.cabal +++ b/hledger/hledger.cabal @@ -53,6 +53,7 @@ library Hledger.Cli.Histogram Hledger.Cli.Print Hledger.Cli.Register + Hledger.Cli.Reports Hledger.Cli.Stats -- should be the same as below build-depends: @@ -99,6 +100,7 @@ executable hledger Hledger.Cli.Histogram Hledger.Cli.Print Hledger.Cli.Register + Hledger.Cli.Reports Hledger.Cli.Stats -- XXX should set patchlevel here as in Makefile cpp-options: -DPATCHLEVEL=0