bs/bse/cf/is: use account type declarations if any
These commands now detect the account types declared by account directives. Whenever such declarations are not present, built-in regular expressions are used, as before.
This commit is contained in:
parent
c1236fa6e9
commit
605a082d77
@ -53,7 +53,7 @@ module Hledger.Data.Journal (
|
|||||||
-- * Standard account types
|
-- * Standard account types
|
||||||
journalBalanceSheetAccountQuery,
|
journalBalanceSheetAccountQuery,
|
||||||
journalProfitAndLossAccountQuery,
|
journalProfitAndLossAccountQuery,
|
||||||
journalIncomeAccountQuery,
|
journalRevenueAccountQuery,
|
||||||
journalExpenseAccountQuery,
|
journalExpenseAccountQuery,
|
||||||
journalAssetAccountQuery,
|
journalAssetAccountQuery,
|
||||||
journalLiabilityAccountQuery,
|
journalLiabilityAccountQuery,
|
||||||
@ -279,24 +279,65 @@ journalAccountNames = journalAccountNamesDeclaredOrImplied
|
|||||||
journalAccountNameTree :: Journal -> Tree AccountName
|
journalAccountNameTree :: Journal -> Tree AccountName
|
||||||
journalAccountNameTree = accountNameTreeFrom . journalAccountNames
|
journalAccountNameTree = accountNameTreeFrom . journalAccountNames
|
||||||
|
|
||||||
-- standard account types
|
-- queries for standard account types
|
||||||
|
|
||||||
-- | A query for Profit & Loss accounts in this journal.
|
-- | Get a query for accounts of a certain type (Asset, Liability..) in this journal.
|
||||||
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Profit_.26_Loss_accounts>.
|
-- The query will match all accounts which were declared as that type by account directives,
|
||||||
journalProfitAndLossAccountQuery :: Journal -> Query
|
-- plus all their subaccounts which have not been declared as a different type.
|
||||||
journalProfitAndLossAccountQuery j = Or [journalIncomeAccountQuery j
|
-- If no accounts were declared as this type, the query will instead match accounts
|
||||||
,journalExpenseAccountQuery j
|
-- with names matched by the provided case-insensitive regular expression.
|
||||||
]
|
journalAccountTypeQuery :: AccountType -> Regexp -> Journal -> Query
|
||||||
|
journalAccountTypeQuery atype fallbackregex j =
|
||||||
|
case M.lookup atype (jdeclaredaccounttypes j) of
|
||||||
|
Nothing -> Acct fallbackregex
|
||||||
|
Just as ->
|
||||||
|
-- XXX Query isn't able to match account type since that requires extra info from the journal.
|
||||||
|
-- So we do a hacky search by name instead.
|
||||||
|
And [
|
||||||
|
Or $ map (Acct . accountNameToAccountRegex) as
|
||||||
|
,Not $ Or $ map (Acct . accountNameToAccountRegex) differentlytypedsubs
|
||||||
|
]
|
||||||
|
where
|
||||||
|
differentlytypedsubs = concat
|
||||||
|
[subs | (t,bs) <- M.toList (jdeclaredaccounttypes j)
|
||||||
|
, t /= atype
|
||||||
|
, let subs = [b | b <- bs, any (`isAccountNamePrefixOf` b) as]
|
||||||
|
]
|
||||||
|
|
||||||
-- | A query for Income (Revenue) accounts in this journal.
|
-- | A query for accounts in this journal which have been
|
||||||
-- This is currently hard-coded to the case-insensitive regex @^(income|revenue)s?(:|$)@.
|
-- declared as Asset by account directives, or otherwise for
|
||||||
journalIncomeAccountQuery :: Journal -> Query
|
-- accounts with names matched by the case-insensitive regular expression
|
||||||
journalIncomeAccountQuery _ = Acct "^(income|revenue)s?(:|$)"
|
-- @^assets?(:|$)@.
|
||||||
|
journalAssetAccountQuery :: Journal -> Query
|
||||||
|
journalAssetAccountQuery = journalAccountTypeQuery Asset "^assets?(:|$)"
|
||||||
|
|
||||||
-- | A query for Expense accounts in this journal.
|
-- | A query for accounts in this journal which have been
|
||||||
-- This is currently hard-coded to the case-insensitive regex @^expenses?(:|$)@.
|
-- declared as Liability by account directives, or otherwise for
|
||||||
|
-- accounts with names matched by the case-insensitive regular expression
|
||||||
|
-- @^(debts?|liabilit(y|ies))(:|$)@.
|
||||||
|
journalLiabilityAccountQuery :: Journal -> Query
|
||||||
|
journalLiabilityAccountQuery = journalAccountTypeQuery Liability "^(debts?|liabilit(y|ies))(:|$)"
|
||||||
|
|
||||||
|
-- | A query for accounts in this journal which have been
|
||||||
|
-- declared as Equity by account directives, or otherwise for
|
||||||
|
-- accounts with names matched by the case-insensitive regular expression
|
||||||
|
-- @^equity(:|$)@.
|
||||||
|
journalEquityAccountQuery :: Journal -> Query
|
||||||
|
journalEquityAccountQuery = journalAccountTypeQuery Equity "^equity(:|$)"
|
||||||
|
|
||||||
|
-- | A query for accounts in this journal which have been
|
||||||
|
-- declared as Revenue by account directives, or otherwise for
|
||||||
|
-- accounts with names matched by the case-insensitive regular expression
|
||||||
|
-- @^(income|revenue)s?(:|$)@.
|
||||||
|
journalRevenueAccountQuery :: Journal -> Query
|
||||||
|
journalRevenueAccountQuery = journalAccountTypeQuery Revenue "^(income|revenue)s?(:|$)"
|
||||||
|
|
||||||
|
-- | A query for accounts in this journal which have been
|
||||||
|
-- declared as Expense by account directives, or otherwise for
|
||||||
|
-- accounts with names matched by the case-insensitive regular expression
|
||||||
|
-- @^(income|revenue)s?(:|$)@.
|
||||||
journalExpenseAccountQuery :: Journal -> Query
|
journalExpenseAccountQuery :: Journal -> Query
|
||||||
journalExpenseAccountQuery _ = Acct "^expenses?(:|$)"
|
journalExpenseAccountQuery = journalAccountTypeQuery Expense "^expenses?(:|$)"
|
||||||
|
|
||||||
-- | A query for Asset, Liability & Equity accounts in this journal.
|
-- | A query for Asset, Liability & Equity accounts in this journal.
|
||||||
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Balance_Sheet_Accounts>.
|
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Balance_Sheet_Accounts>.
|
||||||
@ -306,25 +347,17 @@ journalBalanceSheetAccountQuery j = Or [journalAssetAccountQuery j
|
|||||||
,journalEquityAccountQuery j
|
,journalEquityAccountQuery j
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | A query for Asset accounts in this journal.
|
-- | A query for Profit & Loss accounts in this journal.
|
||||||
-- This is currently hard-coded to the case-insensitive regex @^assets?(:|$)@.
|
-- Cf <http://en.wikipedia.org/wiki/Chart_of_accounts#Profit_.26_Loss_accounts>.
|
||||||
journalAssetAccountQuery :: Journal -> Query
|
journalProfitAndLossAccountQuery :: Journal -> Query
|
||||||
journalAssetAccountQuery _ = Acct "^assets?(:|$)"
|
journalProfitAndLossAccountQuery j = Or [journalRevenueAccountQuery j
|
||||||
|
,journalExpenseAccountQuery j
|
||||||
-- | A query for Liability accounts in this journal.
|
]
|
||||||
-- This is currently hard-coded to the case-insensitive regex @^(debts?|liabilit(y|ies))(:|$)@.
|
|
||||||
journalLiabilityAccountQuery :: Journal -> Query
|
|
||||||
journalLiabilityAccountQuery _ = Acct "^(debts?|liabilit(y|ies))(:|$)"
|
|
||||||
|
|
||||||
-- | A query for Equity accounts in this journal.
|
|
||||||
-- This is currently hard-coded to the case-insensitive regex @^equity(:|$)@.
|
|
||||||
journalEquityAccountQuery :: Journal -> Query
|
|
||||||
journalEquityAccountQuery _ = Acct "^equity(:|$)"
|
|
||||||
|
|
||||||
-- | A query for Cash (-equivalent) accounts in this journal (ie,
|
-- | A query for Cash (-equivalent) accounts in this journal (ie,
|
||||||
-- accounts which appear on the cashflow statement.) This is currently
|
-- accounts which appear on the cashflow statement.) This is currently
|
||||||
-- hard-coded to be all the Asset accounts except for those containing the
|
-- hard-coded to be all the Asset accounts except for those with names
|
||||||
-- case-insensitive regex @(receivable|:A/R|:fixed)@.
|
-- containing the case-insensitive regular expression @(receivable|:A/R|:fixed)@.
|
||||||
journalCashAccountQuery :: Journal -> Query
|
journalCashAccountQuery :: Journal -> Query
|
||||||
journalCashAccountQuery j = And [journalAssetAccountQuery j, Not $ Acct "(receivable|:A/R|:fixed)"]
|
journalCashAccountQuery j = And [journalAssetAccountQuery j, Not $ Acct "(receivable|:A/R|:fixed)"]
|
||||||
|
|
||||||
@ -1089,7 +1122,7 @@ tests_Journal = tests "Journal" [
|
|||||||
test "assets" $ expectEq (namesfrom journalAssetAccountQuery) ["assets","assets:bank","assets:bank:checking","assets:bank:saving","assets:cash"]
|
test "assets" $ expectEq (namesfrom journalAssetAccountQuery) ["assets","assets:bank","assets:bank:checking","assets:bank:saving","assets:cash"]
|
||||||
,test "liabilities" $ expectEq (namesfrom journalLiabilityAccountQuery) ["liabilities","liabilities:debts"]
|
,test "liabilities" $ expectEq (namesfrom journalLiabilityAccountQuery) ["liabilities","liabilities:debts"]
|
||||||
,test "equity" $ expectEq (namesfrom journalEquityAccountQuery) []
|
,test "equity" $ expectEq (namesfrom journalEquityAccountQuery) []
|
||||||
,test "income" $ expectEq (namesfrom journalIncomeAccountQuery) ["income","income:gifts","income:salary"]
|
,test "income" $ expectEq (namesfrom journalRevenueAccountQuery) ["income","income:gifts","income:salary"]
|
||||||
,test "expenses" $ expectEq (namesfrom journalExpenseAccountQuery) ["expenses","expenses:food","expenses:supplies"]
|
,test "expenses" $ expectEq (namesfrom journalExpenseAccountQuery) ["expenses","expenses:food","expenses:supplies"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,7 @@ Note this report shows all account balances with normal positive sign
|
|||||||
cbcqueries = [
|
cbcqueries = [
|
||||||
CBCSubreportSpec{
|
CBCSubreportSpec{
|
||||||
cbcsubreporttitle="Revenues"
|
cbcsubreporttitle="Revenues"
|
||||||
,cbcsubreportquery=journalIncomeAccountQuery
|
,cbcsubreportquery=journalRevenueAccountQuery
|
||||||
,cbcsubreportnormalsign=NormallyNegative
|
,cbcsubreportnormalsign=NormallyNegative
|
||||||
,cbcsubreportincreasestotal=True
|
,cbcsubreportincreasestotal=True
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# account names
|
# account names
|
||||||
|
|
||||||
# 1. account directives are affected by aliases and apply account.
|
# 1. "apply account" and "alias" affect "account" directives.
|
||||||
<
|
<
|
||||||
apply account c
|
apply account c
|
||||||
alias c:a=b
|
alias c:a=b
|
||||||
@ -8,7 +8,62 @@ account a
|
|||||||
$ hledger -f - accounts
|
$ hledger -f - accounts
|
||||||
b
|
b
|
||||||
|
|
||||||
|
# 2. account directives can declare account type.
|
||||||
|
# Here "asset" is a liability, despite the name. So are its subaccounts.
|
||||||
|
# "b" is a liability. "b:bb" is an asset.
|
||||||
|
<
|
||||||
|
; a liability
|
||||||
|
account asset L
|
||||||
|
; an asset
|
||||||
|
account b:bb A
|
||||||
|
; a liability
|
||||||
|
account b L
|
||||||
|
|
||||||
|
2018/1/1
|
||||||
|
(asset:a) 1
|
||||||
|
(b) 2
|
||||||
|
(b:bb) 3
|
||||||
|
|
||||||
|
$ hledger -f - bs -N --flat
|
||||||
|
Balance Sheet 2018/01/01
|
||||||
|
|
||||||
|
|| 2018/01/01
|
||||||
|
=============++============
|
||||||
|
Assets ||
|
||||||
|
-------------++------------
|
||||||
|
b:bb || 3
|
||||||
|
-------------++------------
|
||||||
|
|| 3
|
||||||
|
=============++============
|
||||||
|
Liabilities ||
|
||||||
|
-------------++------------
|
||||||
|
asset:a || -1
|
||||||
|
b || -2
|
||||||
|
-------------++------------
|
||||||
|
|| -3
|
||||||
|
|
||||||
|
# 3. Tree mode. A little weird, b appears twice.
|
||||||
|
# It must be shown above bb, but since not an asset, its balance is excluded there.
|
||||||
|
# It is shown again in the liabilities section, this time with balance.
|
||||||
|
$ hledger -f - bs -N
|
||||||
|
Balance Sheet 2018/01/01
|
||||||
|
|
||||||
|
|| 2018/01/01
|
||||||
|
=============++============
|
||||||
|
Assets ||
|
||||||
|
-------------++------------
|
||||||
|
b || 3
|
||||||
|
bb || 3
|
||||||
|
-------------++------------
|
||||||
|
|| 3
|
||||||
|
=============++============
|
||||||
|
Liabilities ||
|
||||||
|
-------------++------------
|
||||||
|
asset || -1
|
||||||
|
a || -1
|
||||||
|
b || -2
|
||||||
|
-------------++------------
|
||||||
|
|| -3
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
# a trailing : should give a clear error
|
# a trailing : should give a clear error
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user