rename NormalBalance -> NormalSign

This commit is contained in:
Simon Michael 2018-01-15 13:05:20 -08:00
parent 02516ef987
commit 79812f974a
10 changed files with 31 additions and 29 deletions

View File

@ -189,14 +189,14 @@ filterAccounts p a
-- so that the accounts with largest normal balances are listed first. -- so that the accounts with largest normal balances are listed first.
-- The provided normal balance sign determines whether normal balances -- The provided normal balance sign determines whether normal balances
-- are negative or positive. -- are negative or positive.
sortAccountTreeByAmount :: NormalBalance -> Account -> Account sortAccountTreeByAmount :: NormalSign -> Account -> Account
sortAccountTreeByAmount normalsign a sortAccountTreeByAmount normalsign a
| null $ asubs a = a | null $ asubs a = a
| otherwise = a{asubs= | otherwise = a{asubs=
sortBy (maybeflip $ comparing aibalance) $ sortBy (maybeflip $ comparing aibalance) $
map (sortAccountTreeByAmount normalsign) $ asubs a} map (sortAccountTreeByAmount normalsign) $ asubs a}
where where
maybeflip | normalsign==NormalNegative = id maybeflip | normalsign==NormallyNegative = id
| otherwise = flip | otherwise = flip
-- | Search an account list by name. -- | Search an account list by name.

View File

@ -362,13 +362,14 @@ data Account = Account {
aboring :: Bool -- ^ used in the accounts report to label elidable parents aboring :: Bool -- ^ used in the accounts report to label elidable parents
} deriving (Typeable, Data, Generic) } deriving (Typeable, Data, Generic)
-- | Whether an account's balance is normally a positive number (in accounting terms, -- | Whether an account's balance is normally a positive number (in
-- normally a debit balance), as for asset and expense accounts, or a negative number -- accounting terms, a debit balance) or a negative number (credit balance).
-- (in accounting terms, normally a credit balance), as for liability, equity and -- Assets and expenses are normally positive (debit), while liabilities, equity
-- income accounts. Cf https://en.wikipedia.org/wiki/Normal_balance . -- and income are normally negative (credit).
data NormalBalance = -- https://en.wikipedia.org/wiki/Normal_balance
NormalPositive -- ^ normally debit - assets, expenses... data NormalSign =
| NormalNegative -- ^ normally credit - liabilities, equity, income... NormallyPositive -- ^ normally debit - assets, expenses...
| NormallyNegative -- ^ normally credit - liabilities, equity, income...
deriving (Show, Data, Eq) deriving (Show, Data, Eq)
-- | A Ledger has the journal it derives from, and the accounts -- | A Ledger has the journal it derives from, and the accounts

View File

@ -108,8 +108,8 @@ balanceReport opts q j = (items, total)
maybesortflat | sort_amount_ opts = sortBy (maybeflip $ comparing balance) maybesortflat | sort_amount_ opts = sortBy (maybeflip $ comparing balance)
| otherwise = id | otherwise = id
where where
maybeflip = if normalbalance_ opts == Just NormalNegative then id else flip maybeflip = if normalbalance_ opts == Just NormallyNegative then id else flip
maybesorttree | sort_amount_ opts = sortAccountTreeByAmount (fromMaybe NormalPositive $ normalbalance_ opts) maybesorttree | sort_amount_ opts = sortAccountTreeByAmount (fromMaybe NormallyPositive $ normalbalance_ opts)
| otherwise = id | otherwise = id
items = dbg1 "items" $ map (balanceReportItem opts q) accts' items = dbg1 "items" $ map (balanceReportItem opts q) accts'
total | not (flat_ opts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0] total | not (flat_ opts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0]

View File

@ -192,7 +192,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, tot
-- Sort the report rows, representing a flat account list, by row total. -- Sort the report rows, representing a flat account list, by row total.
sortFlatMultiBalanceReportRowsByAmount = sortBy (maybeflip $ comparing fifth6) sortFlatMultiBalanceReportRowsByAmount = sortBy (maybeflip $ comparing fifth6)
where where
maybeflip = if normalbalance_ opts == Just NormalNegative then id else flip maybeflip = if normalbalance_ opts == Just NormallyNegative then id else flip
-- Sort the report rows, representing a tree of accounts, by row total at each level. -- Sort the report rows, representing a tree of accounts, by row total at each level.
-- To do this we recreate an Account tree with the row totals as balances, -- To do this we recreate an Account tree with the row totals as balances,
@ -209,7 +209,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, tot
where where
-- this error should not happen, but it's ugly TODO -- this error should not happen, but it's ugly TODO
setibalance a = a{aibalance=fromMaybe (error "sortTreeMultiBalanceReportRowsByAmount 1") $ lookup (aname a) atotals} setibalance a = a{aibalance=fromMaybe (error "sortTreeMultiBalanceReportRowsByAmount 1") $ lookup (aname a) atotals}
sortedaccounttree = sortAccountTreeByAmount (fromMaybe NormalPositive $ normalbalance_ opts) accounttreewithbals sortedaccounttree = sortAccountTreeByAmount (fromMaybe NormallyPositive $ normalbalance_ opts) accounttreewithbals
sortedaccounts = drop 1 $ flattenAccounts sortedaccounttree sortedaccounts = drop 1 $ flattenAccounts sortedaccounttree
sortedrows = [ sortedrows = [
-- this error should not happen, but it's ugly TODO -- this error should not happen, but it's ugly TODO

View File

@ -102,10 +102,11 @@ data ReportOpts = ReportOpts {
,value_ :: Bool ,value_ :: Bool
,pretty_tables_ :: Bool ,pretty_tables_ :: Bool
,sort_amount_ :: Bool ,sort_amount_ :: Bool
,normalbalance_ :: Maybe NormalBalance ,normalbalance_ :: Maybe NormalSign
-- ^ when running a balance report on accounts of the same normal balance type, -- ^ This can be set when running balance reports on a set of accounts
-- eg in the income section of an income statement, this helps --sort-amount know -- with the same normal balance type (eg all assets, or all incomes).
-- how to sort negative numbers. -- It helps --sort-amount know how to sort negative numbers
-- (eg in the income section of an income statement)
,color_ :: Bool ,color_ :: Bool
,forecast_ :: Bool ,forecast_ :: Bool
,auto_ :: Bool ,auto_ :: Bool

View File

@ -29,8 +29,8 @@ It assumes that these accounts are under a top-level `asset` or `liability`
account (case insensitive, plural forms also allowed). account (case insensitive, plural forms also allowed).
|], |],
cbctitle = "Balance Sheet", cbctitle = "Balance Sheet",
cbcqueries = [ ("Assets" , journalAssetAccountQuery, Just NormalPositive), cbcqueries = [ ("Assets" , journalAssetAccountQuery, Just NormallyPositive),
("Liabilities", journalLiabilityAccountQuery, Just NormalNegative) ("Liabilities", journalLiabilityAccountQuery, Just NormallyNegative)
], ],
cbctype = HistoricalBalance cbctype = HistoricalBalance
} }

View File

@ -26,9 +26,9 @@ It assumes that these accounts are under a top-level `asset`, `liability` and `e
account (plural forms also allowed). account (plural forms also allowed).
|], |],
cbctitle = "Balance Sheet With Equity", cbctitle = "Balance Sheet With Equity",
cbcqueries = [("Assets", journalAssetAccountQuery, Just NormalPositive), cbcqueries = [("Assets", journalAssetAccountQuery, Just NormallyPositive),
("Liabilities", journalLiabilityAccountQuery, Just NormalNegative), ("Liabilities", journalLiabilityAccountQuery, Just NormallyNegative),
("Equity", journalEquityAccountQuery, Just NormalNegative) ("Equity", journalEquityAccountQuery, Just NormallyNegative)
], ],
cbctype = HistoricalBalance cbctype = HistoricalBalance
} }

View File

@ -32,7 +32,7 @@ in "cash" accounts. It assumes that these accounts are under a top-level
contain `receivable` or `A/R` in their name. contain `receivable` or `A/R` in their name.
|], |],
cbctitle = "Cashflow Statement", cbctitle = "Cashflow Statement",
cbcqueries = [("Cash flows", journalCashAccountQuery, Just NormalPositive)], cbcqueries = [("Cash flows", journalCashAccountQuery, Just NormallyPositive)],
cbctype = PeriodChange cbctype = PeriodChange
} }

View File

@ -29,8 +29,8 @@ top-level `revenue` or `income` or `expense` account (case insensitive,
plural forms also allowed). plural forms also allowed).
|], |],
cbctitle = "Income Statement", cbctitle = "Income Statement",
cbcqueries = [ ("Revenues", journalIncomeAccountQuery, Just NormalNegative), cbcqueries = [ ("Revenues", journalIncomeAccountQuery, Just NormallyNegative),
("Expenses", journalExpenseAccountQuery, Just NormalPositive) ("Expenses", journalExpenseAccountQuery, Just NormallyPositive)
], ],
cbctype = PeriodChange cbctype = PeriodChange
} }

View File

@ -39,8 +39,8 @@ data CompoundBalanceCommandSpec = CompoundBalanceCommandSpec {
cbcaliases :: [String], -- ^ command aliases cbcaliases :: [String], -- ^ command aliases
cbchelp :: String, -- ^ command line help cbchelp :: String, -- ^ command line help
cbctitle :: String, -- ^ overall report title cbctitle :: String, -- ^ overall report title
cbcqueries :: [(String, Journal -> Query, Maybe NormalBalance)], cbcqueries :: [(String, Journal -> Query, Maybe NormalSign)],
-- ^ title, journal-parameterised query, and expected normal balance for each subreport. -- ^ title, journal-parameterised query, and normal balance sign for each subreport.
-- The normal balance helps --sort-amount know how to sort negative amounts. -- The normal balance helps --sort-amount know how to sort negative amounts.
cbctype :: BalanceType -- ^ the type of "balance" this report shows (overrides command line flags) cbctype :: BalanceType -- ^ the type of "balance" this report shows (overrides command line flags)
} }
@ -197,7 +197,7 @@ compoundBalanceCommandSingleColumnReport
-> Journal -> Journal
-> String -> String
-> (Journal -> Query) -> (Journal -> Query)
-> Maybe NormalBalance -> Maybe NormalSign
-> ([String], Sum MixedAmount) -> ([String], Sum MixedAmount)
compoundBalanceCommandSingleColumnReport ropts userq j subreporttitle subreportqfn subreportnormalsign = compoundBalanceCommandSingleColumnReport ropts userq j subreporttitle subreportqfn subreportnormalsign =
([subreportstr], Sum total) ([subreportstr], Sum total)
@ -233,7 +233,7 @@ type CompoundBalanceReport =
-- | Run one subreport for a compound balance command in multi-column mode. -- | Run one subreport for a compound balance command in multi-column mode.
-- This returns a MultiBalanceReport. -- This returns a MultiBalanceReport.
compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> Maybe NormalBalance -> MultiBalanceReport compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> Maybe NormalSign -> MultiBalanceReport
compoundBalanceSubreport ropts userq j subreportqfn subreportnormalsign = r' compoundBalanceSubreport ropts userq j subreportqfn subreportnormalsign = r'
where where
-- force --empty to ensure same columns in all sections -- force --empty to ensure same columns in all sections