{-# LANGUAGE NamedFieldPuns      #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE Rank2Types          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}

{-|

A 'Journal' is a set of transactions, plus optional related data.  This is
hledger's primary data object. It is usually parsed from a journal file or
other data format (see "Hledger.Read").

-}

module Hledger.Data.Journal (
  -- * Parsing helpers
  JournalParser,
  ErroringJournalParser,
  addPriceDirective,
  addTransactionModifier,
  addPeriodicTransaction,
  addTransaction,
  journalInferMarketPricesFromTransactions,
  journalApplyCommodityStyles,
  commodityStylesFromAmounts,
  journalCommodityStyles,
  journalToCost,
  journalAddInferredEquityPostings,
  journalAddPricesFromEquity,
  journalReverse,
  journalSetLastReadTime,
  journalRenumberAccountDeclarations,
  journalPivot,
  -- * Filtering
  filterJournalTransactions,
  filterJournalPostings,
  filterJournalRelatedPostings,
  filterJournalAmounts,
  filterTransactionAmounts,
  filterTransactionPostings,
  filterTransactionPostingsExtra,
  filterTransactionRelatedPostings,
  filterPostingAmount,
  -- * Mapping
  journalMapTransactions,
  journalMapPostings,
  journalMapPostingAmounts,
  -- * Querying
  journalAccountNamesUsed,
  journalAccountNamesImplied,
  journalAccountNamesDeclared,
  journalAccountNamesDeclaredOrUsed,
  journalAccountNamesDeclaredOrImplied,
  journalLeafAccountNamesDeclared,
  journalAccountNames,
  journalLeafAccountNames,
  journalAccountNameTree,
  journalAccountTags,
  journalInheritedAccountTags,
  -- journalAmountAndPriceCommodities,
  -- journalAmountStyles,
  -- overJournalAmounts,
  -- traverseJournalAmounts,
  -- journalCanonicalCommodities,
  journalPayeesDeclared,
  journalPayeesUsed,
  journalPayeesDeclaredOrUsed,
  journalCommoditiesDeclared,
  journalCommodities,
  journalDateSpan,
  journalDateSpanBothDates,
  journalStartDate,
  journalEndDate,
  journalLastDay,
  journalDescriptions,
  journalFilePath,
  journalFilePaths,
  journalTransactionAt,
  journalNextTransaction,
  journalPrevTransaction,
  journalPostings,
  journalTransactionsSimilarTo,
  -- * Account types
  journalAccountType,
  journalAccountTypes,
  journalAddAccountTypes,
  journalPostingsAddAccountTags,
  -- journalPrices,
  journalConversionAccount,
  -- * Misc
  canonicalStyleFrom,
  nulljournal,
  journalConcat,
  journalNumberTransactions,
  journalNumberAndTieTransactions,
  journalUntieTransactions,
  journalModifyTransactions,
  journalApplyAliases,
  dbgJournalAcctDeclOrder,
  -- * Tests
  samplejournal,
  samplejournalMaybeExplicit,
  tests_Journal
  --
)
where

import Control.Applicative ((<|>))
import Control.Monad.Except (ExceptT(..))
import Control.Monad.State.Strict (StateT)
import Data.Char (toUpper, isDigit)
import Data.Default (Default(..))
import Data.Foldable (toList)
import Data.List ((\\), find, foldl', sortBy, union, intercalate)
import Data.List.Extra (nubSort)
import qualified Data.Map.Strict as M
import Data.Maybe (catMaybes, fromMaybe, mapMaybe, maybeToList)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import Safe (headMay, headDef, maximumMay, minimumMay)
import Data.Time.Calendar (Day, addDays, fromGregorian)
import Data.Time.Clock.POSIX (POSIXTime)
import Data.Tree (Tree(..), flatten)
import Text.Printf (printf)
import Text.Megaparsec (ParsecT)
import Text.Megaparsec.Custom (FinalParseError)

import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.AccountName
import Hledger.Data.Amount
import Hledger.Data.Posting
import Hledger.Data.Transaction
import Hledger.Data.TransactionModifier
import Hledger.Data.Valuation
import Hledger.Query
import System.FilePath (takeFileName)


-- | A parser of text that runs in some monad, keeping a Journal as state.
type JournalParser m a = StateT Journal (ParsecT HledgerParseErrorData Text m) a

-- | A parser of text that runs in some monad, keeping a Journal as
-- state, that can throw an exception to end parsing, preventing
-- further parser backtracking.
type ErroringJournalParser m a =
  StateT Journal (ParsecT HledgerParseErrorData Text (ExceptT FinalParseError m)) a

-- deriving instance Show Journal
instance Show Journal where
  show :: Journal -> RegexError
show Journal
j
    | Int
debugLevel forall a. Ord a => a -> a -> Bool
< Int
3 = forall r. PrintfType r => RegexError -> r
printf RegexError
"Journal %s with %d transactions, %d accounts"
             (Journal -> RegexError
journalFilePath Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length [TagName]
accounts)
    | Int
debugLevel forall a. Ord a => a -> a -> Bool
< Int
6 = forall r. PrintfType r => RegexError -> r
printf RegexError
"Journal %s with %d transactions, %d accounts: %s"
             (Journal -> RegexError
journalFilePath Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length [TagName]
accounts)
             (forall a. Show a => a -> RegexError
show [TagName]
accounts)
    | Bool
otherwise = forall r. PrintfType r => RegexError -> r
printf RegexError
"Journal %s with %d transactions, %d accounts: %s, commodity styles: %s"
             (Journal -> RegexError
journalFilePath Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j)
             (forall (t :: * -> *) a. Foldable t => t a -> Int
length [TagName]
accounts)
             (forall a. Show a => a -> RegexError
show [TagName]
accounts)
             (forall a. Show a => a -> RegexError
show forall a b. (a -> b) -> a -> b
$ Journal -> Map TagName AmountStyle
jinferredcommodities Journal
j)
             -- ++ (show $ journalTransactions l)
             where accounts :: [TagName]
accounts = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/= TagName
"root") forall a b. (a -> b) -> a -> b
$ forall a. Tree a -> [a]
flatten forall a b. (a -> b) -> a -> b
$ Journal -> Tree TagName
journalAccountNameTree Journal
j

-- showJournalDebug j = unlines [
--                       show j
--                      ,show (jtxns j)
--                      ,show (jtxnmodifiers j)
--                      ,show (jperiodictxns j)
--                      ,show $ jparsetimeclockentries j
--                      ,show $ jpricedirectives j
--                      ,show $ jfinalcommentlines j
--                      ,show $ jparsestate j
--                      ,show $ map fst $ jfiles j
--                      ]

-- The semigroup instance for Journal is useful for two situations.
--
-- 1. concatenating finalised journals, eg with multiple -f options:
-- FIRST <> SECOND.
--
-- 2. merging a child parsed journal, eg with the include directive:
-- CHILD <> PARENT. A parsed journal's data is in reverse order, so
-- this gives what we want.
--
-- Note that (<>) is right-biased, so nulljournal is only a left identity.
-- In particular, this prevents Journal from being a monoid.
instance Semigroup Journal where Journal
j1 <> :: Journal -> Journal -> Journal
<> Journal
j2 = Journal
j1 Journal -> Journal -> Journal
`journalConcat` Journal
j2

-- | Merge two journals into one.
-- Transaction counts are summed, map fields are combined,
-- the second's list fields are appended to the first's,
-- the second's parse state is kept.
journalConcat :: Journal -> Journal -> Journal
journalConcat :: Journal -> Journal -> Journal
journalConcat Journal
j1 Journal
j2 =
  let
    f1 :: RegexError
f1 = ShowS
takeFileName forall a b. (a -> b) -> a -> b
$ Journal -> RegexError
journalFilePath Journal
j1
    f2 :: RegexError
f2 = forall b a. b -> (a -> b) -> Maybe a -> b
maybe RegexError
"(unknown)" ShowS
takeFileName forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe a
headMay forall a b. (a -> b) -> a -> b
$ Journal -> [RegexError]
jincludefilestack Journal
j2  -- XXX more accurate than journalFilePath for some reason
  in
    RegexError -> Journal -> Journal
dbgJournalAcctDeclOrder (RegexError
"journalConcat: " forall a. Semigroup a => a -> a -> a
<> RegexError
f1 forall a. Semigroup a => a -> a -> a
<> RegexError
" <> " forall a. Semigroup a => a -> a -> a
<> RegexError
f2 forall a. Semigroup a => a -> a -> a
<> RegexError
", acct decls renumbered: ") forall a b. (a -> b) -> a -> b
$
    Journal -> Journal
journalRenumberAccountDeclarations forall a b. (a -> b) -> a -> b
$
    RegexError -> Journal -> Journal
dbgJournalAcctDeclOrder (RegexError
"journalConcat: " forall a. Semigroup a => a -> a -> a
<> RegexError
f1 forall a. Semigroup a => a -> a -> a
<> RegexError
" <> " forall a. Semigroup a => a -> a -> a
<> RegexError
f2 forall a. Semigroup a => a -> a -> a
<> RegexError
", acct decls           : ") forall a b. (a -> b) -> a -> b
$
    Journal {
     jparsedefaultyear :: Maybe Integer
jparsedefaultyear          = Journal -> Maybe Integer
jparsedefaultyear          Journal
j2
    ,jparsedefaultcommodity :: Maybe (TagName, AmountStyle)
jparsedefaultcommodity     = Journal -> Maybe (TagName, AmountStyle)
jparsedefaultcommodity     Journal
j2
    ,jparsedecimalmark :: Maybe Char
jparsedecimalmark          = Journal -> Maybe Char
jparsedecimalmark          Journal
j2
    ,jparseparentaccounts :: [TagName]
jparseparentaccounts       = Journal -> [TagName]
jparseparentaccounts       Journal
j2
    ,jparsealiases :: [AccountAlias]
jparsealiases              = Journal -> [AccountAlias]
jparsealiases              Journal
j2
    -- ,jparsetransactioncount     = jparsetransactioncount     j1 +  jparsetransactioncount     j2
    ,jparsetimeclockentries :: [TimeclockEntry]
jparsetimeclockentries     = Journal -> [TimeclockEntry]
jparsetimeclockentries     Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [TimeclockEntry]
jparsetimeclockentries     Journal
j2
    ,jincludefilestack :: [RegexError]
jincludefilestack          = Journal -> [RegexError]
jincludefilestack Journal
j2
    ,jdeclaredpayees :: [(TagName, PayeeDeclarationInfo)]
jdeclaredpayees            = Journal -> [(TagName, PayeeDeclarationInfo)]
jdeclaredpayees            Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [(TagName, PayeeDeclarationInfo)]
jdeclaredpayees            Journal
j2
    ,jdeclaredaccounts :: [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts          = Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts          Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts          Journal
j2
    ,jdeclaredaccounttags :: Map TagName [Tag]
jdeclaredaccounttags       = Journal -> Map TagName [Tag]
jdeclaredaccounttags       Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map TagName [Tag]
jdeclaredaccounttags       Journal
j2
    ,jdeclaredaccounttypes :: Map AccountType [TagName]
jdeclaredaccounttypes      = Journal -> Map AccountType [TagName]
jdeclaredaccounttypes      Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map AccountType [TagName]
jdeclaredaccounttypes      Journal
j2
    ,jaccounttypes :: Map TagName AccountType
jaccounttypes              = Journal -> Map TagName AccountType
jaccounttypes              Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map TagName AccountType
jaccounttypes              Journal
j2
    ,jglobalcommoditystyles :: Map TagName AmountStyle
jglobalcommoditystyles     = Journal -> Map TagName AmountStyle
jglobalcommoditystyles     Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map TagName AmountStyle
jglobalcommoditystyles     Journal
j2
    ,jcommodities :: Map TagName Commodity
jcommodities               = Journal -> Map TagName Commodity
jcommodities               Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map TagName Commodity
jcommodities               Journal
j2
    ,jinferredcommodities :: Map TagName AmountStyle
jinferredcommodities       = Journal -> Map TagName AmountStyle
jinferredcommodities       Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> Map TagName AmountStyle
jinferredcommodities       Journal
j2
    ,jpricedirectives :: [PriceDirective]
jpricedirectives           = Journal -> [PriceDirective]
jpricedirectives           Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [PriceDirective]
jpricedirectives           Journal
j2
    ,jinferredmarketprices :: [MarketPrice]
jinferredmarketprices      = Journal -> [MarketPrice]
jinferredmarketprices      Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [MarketPrice]
jinferredmarketprices      Journal
j2
    ,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers              = Journal -> [TransactionModifier]
jtxnmodifiers              Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [TransactionModifier]
jtxnmodifiers              Journal
j2
    ,jperiodictxns :: [PeriodicTransaction]
jperiodictxns              = Journal -> [PeriodicTransaction]
jperiodictxns              Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [PeriodicTransaction]
jperiodictxns              Journal
j2
    ,jtxns :: [Transaction]
jtxns                      = Journal -> [Transaction]
jtxns                      Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [Transaction]
jtxns                      Journal
j2
    ,jfinalcommentlines :: TagName
jfinalcommentlines         = Journal -> TagName
jfinalcommentlines Journal
j2  -- XXX discards j1's ?
    ,jfiles :: [(RegexError, TagName)]
jfiles                     = Journal -> [(RegexError, TagName)]
jfiles                     Journal
j1 forall a. Semigroup a => a -> a -> a
<> Journal -> [(RegexError, TagName)]
jfiles                     Journal
j2
    ,jlastreadtime :: POSIXTime
jlastreadtime              = forall a. Ord a => a -> a -> a
max (Journal -> POSIXTime
jlastreadtime Journal
j1) (Journal -> POSIXTime
jlastreadtime Journal
j2)
    }

-- | Renumber all the account declarations. This is useful to call when
-- finalising or concatenating Journals, to give account declarations
-- a total order across files.
journalRenumberAccountDeclarations :: Journal -> Journal
journalRenumberAccountDeclarations :: Journal -> Journal
journalRenumberAccountDeclarations Journal
j = Journal
j{jdeclaredaccounts :: [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts=[(TagName, AccountDeclarationInfo)]
jdas'}
  where
    jdas' :: [(TagName, AccountDeclarationInfo)]
jdas' = [(TagName
a, AccountDeclarationInfo
adi{adideclarationorder :: Int
adideclarationorder=Int
n}) | (Int
n, (TagName
a,AccountDeclarationInfo
adi)) <- forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] forall a b. (a -> b) -> a -> b
$ Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j]
    -- the per-file declaration order saved during parsing is discarded,
    -- it seems unneeded except perhaps for debugging

-- | Debug log the ordering of a journal's account declarations
-- (at debug level 5+).
dbgJournalAcctDeclOrder :: String -> Journal -> Journal
dbgJournalAcctDeclOrder :: RegexError -> Journal -> Journal
dbgJournalAcctDeclOrder RegexError
prefix =
  forall a. Int -> (a -> RegexError) -> a -> a
traceOrLogAtWith Int
5 ((RegexError
prefixforall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(TagName, AccountDeclarationInfo)] -> RegexError
showAcctDeclsSummary forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts)
  where
    showAcctDeclsSummary :: [(AccountName,AccountDeclarationInfo)] -> String
    showAcctDeclsSummary :: [(TagName, AccountDeclarationInfo)] -> RegexError
showAcctDeclsSummary [(TagName, AccountDeclarationInfo)]
adis
      | forall (t :: * -> *) a. Foldable t => t a -> Int
length [(TagName, AccountDeclarationInfo)]
adis forall a. Ord a => a -> a -> Bool
< (Int
2forall a. Num a => a -> a -> a
*Int
nforall a. Num a => a -> a -> a
+Int
2) = RegexError
"[" forall a. Semigroup a => a -> a -> a
<> [(TagName, AccountDeclarationInfo)] -> RegexError
showadis [(TagName, AccountDeclarationInfo)]
adis forall a. Semigroup a => a -> a -> a
<> RegexError
"]"
      | Bool
otherwise =
          RegexError
"[" forall a. Semigroup a => a -> a -> a
<> [(TagName, AccountDeclarationInfo)] -> RegexError
showadis (forall a. Int -> [a] -> [a]
take Int
n [(TagName, AccountDeclarationInfo)]
adis) forall a. Semigroup a => a -> a -> a
<> RegexError
" ... " forall a. Semigroup a => a -> a -> a
<> [(TagName, AccountDeclarationInfo)] -> RegexError
showadis (forall a. Int -> [a] -> [a]
takelast Int
n [(TagName, AccountDeclarationInfo)]
adis) forall a. Semigroup a => a -> a -> a
<> RegexError
"]"
      where
        n :: Int
n = Int
3
        showadis :: [(TagName, AccountDeclarationInfo)] -> RegexError
showadis = forall a. [a] -> [[a]] -> [a]
intercalate RegexError
", " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (TagName, AccountDeclarationInfo) -> RegexError
showadi
        showadi :: (TagName, AccountDeclarationInfo) -> RegexError
showadi (TagName
a,AccountDeclarationInfo
adi) = RegexError
"("forall a. Semigroup a => a -> a -> a
<>forall a. Show a => a -> RegexError
show (AccountDeclarationInfo -> Int
adideclarationorder AccountDeclarationInfo
adi)forall a. Semigroup a => a -> a -> a
<>RegexError
","forall a. Semigroup a => a -> a -> a
<>TagName -> RegexError
T.unpack TagName
aforall a. Semigroup a => a -> a -> a
<>RegexError
")"
        takelast :: Int -> [a] -> [a]
takelast Int
n' = forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
n' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse

instance Default Journal where
  def :: Journal
def = Journal
nulljournal

nulljournal :: Journal
nulljournal :: Journal
nulljournal = Journal {
   jparsedefaultyear :: Maybe Integer
jparsedefaultyear          = forall a. Maybe a
Nothing
  ,jparsedefaultcommodity :: Maybe (TagName, AmountStyle)
jparsedefaultcommodity     = forall a. Maybe a
Nothing
  ,jparsedecimalmark :: Maybe Char
jparsedecimalmark          = forall a. Maybe a
Nothing
  ,jparseparentaccounts :: [TagName]
jparseparentaccounts       = []
  ,jparsealiases :: [AccountAlias]
jparsealiases              = []
  -- ,jparsetransactioncount     = 0
  ,jparsetimeclockentries :: [TimeclockEntry]
jparsetimeclockentries     = []
  ,jincludefilestack :: [RegexError]
jincludefilestack          = []
  ,jdeclaredpayees :: [(TagName, PayeeDeclarationInfo)]
jdeclaredpayees            = []
  ,jdeclaredaccounts :: [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts          = []
  ,jdeclaredaccounttags :: Map TagName [Tag]
jdeclaredaccounttags       = forall k a. Map k a
M.empty
  ,jdeclaredaccounttypes :: Map AccountType [TagName]
jdeclaredaccounttypes      = forall k a. Map k a
M.empty
  ,jaccounttypes :: Map TagName AccountType
jaccounttypes              = forall k a. Map k a
M.empty
  ,jglobalcommoditystyles :: Map TagName AmountStyle
jglobalcommoditystyles     = forall k a. Map k a
M.empty
  ,jcommodities :: Map TagName Commodity
jcommodities               = forall k a. Map k a
M.empty
  ,jinferredcommodities :: Map TagName AmountStyle
jinferredcommodities       = forall k a. Map k a
M.empty
  ,jpricedirectives :: [PriceDirective]
jpricedirectives           = []
  ,jinferredmarketprices :: [MarketPrice]
jinferredmarketprices      = []
  ,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers              = []
  ,jperiodictxns :: [PeriodicTransaction]
jperiodictxns              = []
  ,jtxns :: [Transaction]
jtxns                      = []
  ,jfinalcommentlines :: TagName
jfinalcommentlines         = TagName
""
  ,jfiles :: [(RegexError, TagName)]
jfiles                     = []
  ,jlastreadtime :: POSIXTime
jlastreadtime              = POSIXTime
0
  }

journalFilePath :: Journal -> FilePath
journalFilePath :: Journal -> RegexError
journalFilePath = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> (RegexError, TagName)
mainfile

journalFilePaths :: Journal -> [FilePath]
journalFilePaths :: Journal -> [RegexError]
journalFilePaths = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(RegexError, TagName)]
jfiles

mainfile :: Journal -> (FilePath, Text)
mainfile :: Journal -> (RegexError, TagName)
mainfile = forall a. a -> [a] -> a
headDef (RegexError
"(unknown)", TagName
"") forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(RegexError, TagName)]
jfiles

addTransaction :: Transaction -> Journal -> Journal
addTransaction :: Transaction -> Journal -> Journal
addTransaction Transaction
t Journal
j = Journal
j { jtxns :: [Transaction]
jtxns = Transaction
t forall a. a -> [a] -> [a]
: Journal -> [Transaction]
jtxns Journal
j }

addTransactionModifier :: TransactionModifier -> Journal -> Journal
addTransactionModifier :: TransactionModifier -> Journal -> Journal
addTransactionModifier TransactionModifier
mt Journal
j = Journal
j { jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers = TransactionModifier
mt forall a. a -> [a] -> [a]
: Journal -> [TransactionModifier]
jtxnmodifiers Journal
j }

addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
addPeriodicTransaction PeriodicTransaction
pt Journal
j = Journal
j { jperiodictxns :: [PeriodicTransaction]
jperiodictxns = PeriodicTransaction
pt forall a. a -> [a] -> [a]
: Journal -> [PeriodicTransaction]
jperiodictxns Journal
j }

addPriceDirective :: PriceDirective -> Journal -> Journal
addPriceDirective :: PriceDirective -> Journal -> Journal
addPriceDirective PriceDirective
h Journal
j = Journal
j { jpricedirectives :: [PriceDirective]
jpricedirectives = PriceDirective
h forall a. a -> [a] -> [a]
: Journal -> [PriceDirective]
jpricedirectives Journal
j }  -- XXX #999 keep sorted

-- | Get the transaction with this index (its 1-based position in the input stream), if any.
journalTransactionAt :: Journal -> Integer -> Maybe Transaction
journalTransactionAt :: Journal -> Integer -> Maybe Transaction
journalTransactionAt Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} Integer
i =
  -- it's probably ts !! (i+1), but we won't assume
  forall a. [a] -> Maybe a
headMay [Transaction
t | Transaction
t <- [Transaction]
ts, Transaction -> Integer
tindex Transaction
t forall a. Eq a => a -> a -> Bool
== Integer
i]

-- | Get the transaction that appeared immediately after this one in the input stream, if any.
journalNextTransaction :: Journal -> Transaction -> Maybe Transaction
journalNextTransaction :: Journal -> Transaction -> Maybe Transaction
journalNextTransaction Journal
j Transaction
t = Journal -> Integer -> Maybe Transaction
journalTransactionAt Journal
j (Transaction -> Integer
tindex Transaction
t forall a. Num a => a -> a -> a
+ Integer
1)

-- | Get the transaction that appeared immediately before this one in the input stream, if any.
journalPrevTransaction :: Journal -> Transaction -> Maybe Transaction
journalPrevTransaction :: Journal -> Transaction -> Maybe Transaction
journalPrevTransaction Journal
j Transaction
t = Journal -> Integer -> Maybe Transaction
journalTransactionAt Journal
j (Transaction -> Integer
tindex Transaction
t forall a. Num a => a -> a -> a
- Integer
1)

-- | All postings from this journal's transactions, in order.
journalPostings :: Journal -> [Posting]
journalPostings :: Journal -> [Posting]
journalPostings = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Posting]
tpostings forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns

-- | Sorted unique commodity symbols declared by commodity directives in this journal.
journalCommoditiesDeclared :: Journal -> [CommoditySymbol]
journalCommoditiesDeclared :: Journal -> [TagName]
journalCommoditiesDeclared = forall k a. Map k a -> [k]
M.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Map TagName Commodity
jcommodities

-- | Sorted unique commodity symbols declared or inferred from this journal.
journalCommodities :: Journal -> S.Set CommoditySymbol
journalCommodities :: Journal -> Set TagName
journalCommodities Journal
j = forall k a. Map k a -> Set k
M.keysSet (Journal -> Map TagName Commodity
jcommodities Journal
j) forall a. Semigroup a => a -> a -> a
<> forall k a. Map k a -> Set k
M.keysSet (Journal -> Map TagName AmountStyle
jinferredcommodities Journal
j)

-- | Unique transaction descriptions used in this journal.
journalDescriptions :: Journal -> [Text]
journalDescriptions :: Journal -> [TagName]
journalDescriptions = forall a. Ord a => [a] -> [a]
nubSort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Transaction -> TagName
tdescription forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns

-- | Sorted unique payees declared by payee directives in this journal.
journalPayeesDeclared :: Journal -> [Payee]
journalPayeesDeclared :: Journal -> [TagName]
journalPayeesDeclared = forall a. Ord a => [a] -> [a]
nubSort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(TagName, PayeeDeclarationInfo)]
jdeclaredpayees

-- | Sorted unique payees used by transactions in this journal.
journalPayeesUsed :: Journal -> [Payee]
journalPayeesUsed :: Journal -> [TagName]
journalPayeesUsed = forall a. Ord a => [a] -> [a]
nubSort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Transaction -> TagName
transactionPayee forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns

-- | Sorted unique payees used in transactions or declared by payee directives in this journal.
journalPayeesDeclaredOrUsed :: Journal -> [Payee]
journalPayeesDeclaredOrUsed :: Journal -> [TagName]
journalPayeesDeclaredOrUsed Journal
j = forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap forall a. Ord a => [a] -> Set a
S.fromList
    [Journal -> [TagName]
journalPayeesDeclared Journal
j, Journal -> [TagName]
journalPayeesUsed Journal
j]

-- | Sorted unique account names posted to by this journal's transactions.
journalAccountNamesUsed :: Journal -> [AccountName]
journalAccountNamesUsed :: Journal -> [TagName]
journalAccountNamesUsed = [Posting] -> [TagName]
accountNamesFromPostings forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Posting]
journalPostings

-- | Sorted unique account names implied by this journal's transactions -
-- accounts posted to and all their implied parent accounts.
journalAccountNamesImplied :: Journal -> [AccountName]
journalAccountNamesImplied :: Journal -> [TagName]
journalAccountNamesImplied = [TagName] -> [TagName]
expandAccountNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [TagName]
journalAccountNamesUsed

-- | Sorted unique account names declared by account directives in this journal.
journalAccountNamesDeclared :: Journal -> [AccountName]
journalAccountNamesDeclared :: Journal -> [TagName]
journalAccountNamesDeclared = forall a. Ord a => [a] -> [a]
nubSort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts

-- | Sorted unique account names declared by account directives in this journal,
-- which have no children.
journalLeafAccountNamesDeclared :: Journal -> [AccountName]
journalLeafAccountNamesDeclared :: Journal -> [TagName]
journalLeafAccountNamesDeclared = forall a. Tree a -> [a]
treeLeaves forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TagName] -> Tree TagName
accountNameTreeFrom forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [TagName]
journalAccountNamesDeclared

-- | Sorted unique account names declared by account directives or posted to
-- by transactions in this journal.
journalAccountNamesDeclaredOrUsed :: Journal -> [AccountName]
journalAccountNamesDeclaredOrUsed :: Journal -> [TagName]
journalAccountNamesDeclaredOrUsed Journal
j = forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap forall a. Ord a => [a] -> Set a
S.fromList
    [Journal -> [TagName]
journalAccountNamesDeclared Journal
j, Journal -> [TagName]
journalAccountNamesUsed Journal
j]

-- | Sorted unique account names declared by account directives, or posted to
-- or implied as parents by transactions in this journal.
journalAccountNamesDeclaredOrImplied :: Journal -> [AccountName]
journalAccountNamesDeclaredOrImplied :: Journal -> [TagName]
journalAccountNamesDeclaredOrImplied Journal
j = forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap forall a. Ord a => [a] -> Set a
S.fromList
    [Journal -> [TagName]
journalAccountNamesDeclared Journal
j, [TagName] -> [TagName]
expandAccountNames forall a b. (a -> b) -> a -> b
$ Journal -> [TagName]
journalAccountNamesUsed Journal
j]

-- | Convenience/compatibility alias for journalAccountNamesDeclaredOrImplied.
journalAccountNames :: Journal -> [AccountName]
journalAccountNames :: Journal -> [TagName]
journalAccountNames = Journal -> [TagName]
journalAccountNamesDeclaredOrImplied

-- | Sorted unique account names declared or implied in this journal
-- which have no children.
journalLeafAccountNames :: Journal -> [AccountName]
journalLeafAccountNames :: Journal -> [TagName]
journalLeafAccountNames = forall a. Tree a -> [a]
treeLeaves forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Tree TagName
journalAccountNameTree

journalAccountNameTree :: Journal -> Tree AccountName
journalAccountNameTree :: Journal -> Tree TagName
journalAccountNameTree = [TagName] -> Tree TagName
accountNameTreeFrom forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [TagName]
journalAccountNamesDeclaredOrImplied

-- | Which tags have been declared explicitly for this account, if any ?
journalAccountTags :: Journal -> AccountName -> [Tag]
journalAccountTags :: Journal -> TagName -> [Tag]
journalAccountTags Journal{Map TagName [Tag]
jdeclaredaccounttags :: Map TagName [Tag]
jdeclaredaccounttags :: Journal -> Map TagName [Tag]
jdeclaredaccounttags} TagName
a = forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] TagName
a Map TagName [Tag]
jdeclaredaccounttags

-- | Which tags are in effect for this account, including tags inherited from parent accounts ?
journalInheritedAccountTags :: Journal -> AccountName -> [Tag]
journalInheritedAccountTags :: Journal -> TagName -> [Tag]
journalInheritedAccountTags Journal
j TagName
a =
  forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\[Tag]
ts TagName
a' -> [Tag]
ts forall a. Eq a => [a] -> [a] -> [a]
`union` Journal -> TagName -> [Tag]
journalAccountTags Journal
j TagName
a') [] [TagName]
as
  where
    as :: [TagName]
as = TagName
a forall a. a -> [a] -> [a]
: TagName -> [TagName]
parentAccountNames TagName
a
-- PERF: cache in journal ?

-- | Find up to N most similar and most recent transactions matching
-- the given transaction description and query. Transactions are
-- listed with their description's similarity score (see
-- compareDescriptions), sorted by highest score and then by date.
-- Only transactions with a similarity score greater than a minimum
-- threshold (currently 0) are returned.
journalTransactionsSimilarTo :: Journal -> Query -> Text -> Int -> [(Double,Transaction)]
journalTransactionsSimilarTo :: Journal -> Query -> TagName -> Int -> [(Double, Transaction)]
journalTransactionsSimilarTo Journal{[Transaction]
jtxns :: [Transaction]
jtxns :: Journal -> [Transaction]
jtxns} Query
q TagName
desc Int
n =
  forall a. Int -> [a] -> [a]
take Int
n forall a b. (a -> b) -> a -> b
$
  forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (\(Double
s1,Transaction
t1) (Double
s2,Transaction
t2) -> forall a. Ord a => a -> a -> Ordering
compare (Double
s2,Transaction -> Day
tdate Transaction
t2) (Double
s1,Transaction -> Day
tdate Transaction
t1)) forall a b. (a -> b) -> a -> b
$
  forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Ord a => a -> a -> Bool
> Double
threshold)forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> a
fst)
  [(TagName -> TagName -> Double
compareDescriptions TagName
desc forall a b. (a -> b) -> a -> b
$ Transaction -> TagName
tdescription Transaction
t, Transaction
t) | Transaction
t <- [Transaction]
jtxns, Query
q Query -> Transaction -> Bool
`matchesTransaction` Transaction
t]
  where
    threshold :: Double
threshold = Double
0

-- | Return a similarity score from 0 to 1.5 for two transaction descriptions. 
-- This is based on compareStrings, with the following modifications:
--
-- - numbers are stripped out before measuring similarity
--
-- - if the (unstripped) first description appears in its entirety within the second,
--   the score is boosted by 0.5.
--
compareDescriptions :: Text -> Text -> Double
compareDescriptions :: TagName -> TagName -> Double
compareDescriptions TagName
a TagName
b =
  (if TagName
a TagName -> TagName -> Bool
`T.isInfixOf` TagName
b then (Double
0.5forall a. Num a => a -> a -> a
+) else forall a. a -> a
id) forall a b. (a -> b) -> a -> b
$
  RegexError -> RegexError -> Double
compareStrings (TagName -> RegexError
simplify TagName
a) (TagName -> RegexError
simplify TagName
b)
  where
    simplify :: TagName -> RegexError
simplify = TagName -> RegexError
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> TagName -> TagName
T.filter (Bool -> Bool
notforall b c a. (b -> c) -> (a -> b) -> a -> c
.Char -> Bool
isDigit)

-- | Return a similarity score from 0 to 1 for two strings.  This
-- was based on Simon White's string similarity algorithm
-- (http://www.catalysoft.com/articles/StrikeAMatch.html), later found
-- to be https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient,
-- and modified to handle short strings better.
-- Todo: check out http://nlp.fi.muni.cz/raslan/2008/raslan08.pdf#page=14 .
compareStrings :: String -> String -> Double
compareStrings :: RegexError -> RegexError -> Double
compareStrings RegexError
"" RegexError
"" = Double
1
compareStrings [Char
_] RegexError
"" = Double
0
compareStrings RegexError
"" [Char
_] = Double
0
compareStrings [Char
a] [Char
b] = if Char -> Char
toUpper Char
a forall a. Eq a => a -> a -> Bool
== Char -> Char
toUpper Char
b then Double
1 else Double
0
compareStrings RegexError
s1 RegexError
s2 = Double
2 forall a. Num a => a -> a -> a
* Double
commonpairs forall a. Fractional a => a -> a -> a
/ Double
totalpairs
  where
    pairs1 :: Set RegexError
pairs1      = forall a. Ord a => [a] -> Set a
S.fromList forall a b. (a -> b) -> a -> b
$ RegexError -> [RegexError]
wordLetterPairs forall a b. (a -> b) -> a -> b
$ ShowS
uppercase RegexError
s1
    pairs2 :: Set RegexError
pairs2      = forall a. Ord a => [a] -> Set a
S.fromList forall a b. (a -> b) -> a -> b
$ RegexError -> [RegexError]
wordLetterPairs forall a b. (a -> b) -> a -> b
$ ShowS
uppercase RegexError
s2
    commonpairs :: Double
commonpairs = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Set a -> Int
S.size forall a b. (a -> b) -> a -> b
$ forall a. Ord a => Set a -> Set a -> Set a
S.intersection Set RegexError
pairs1 Set RegexError
pairs2
    totalpairs :: Double
totalpairs  = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Set a -> Int
S.size Set RegexError
pairs1 forall a. Num a => a -> a -> a
+ forall a. Set a -> Int
S.size Set RegexError
pairs2

wordLetterPairs :: String -> [String]
wordLetterPairs :: RegexError -> [RegexError]
wordLetterPairs = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap RegexError -> [RegexError]
letterPairs forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegexError -> [RegexError]
words

letterPairs :: String -> [String]
letterPairs :: RegexError -> [RegexError]
letterPairs (Char
a:Char
b:RegexError
rest) = [Char
a,Char
b] forall a. a -> [a] -> [a]
: RegexError -> [RegexError]
letterPairs (Char
bforall a. a -> [a] -> [a]
:RegexError
rest)
letterPairs RegexError
_ = []

-- | The 'AccountName' to use for automatically generated conversion postings.
journalConversionAccount :: Journal -> AccountName
journalConversionAccount :: Journal -> TagName
journalConversionAccount =
    forall a. a -> [a] -> a
headDef (RegexError -> TagName
T.pack RegexError
"equity:conversion")
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault [] AccountType
Conversion
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Map AccountType [TagName]
jdeclaredaccounttypes

-- Newer account type code.

journalAccountType :: Journal -> AccountName -> Maybe AccountType
journalAccountType :: Journal -> TagName -> Maybe AccountType
journalAccountType Journal{Map TagName AccountType
jaccounttypes :: Map TagName AccountType
jaccounttypes :: Journal -> Map TagName AccountType
jaccounttypes} = Map TagName AccountType -> TagName -> Maybe AccountType
accountNameType Map TagName AccountType
jaccounttypes

-- | Add a map of all known account types to the journal.
journalAddAccountTypes :: Journal -> Journal
journalAddAccountTypes :: Journal -> Journal
journalAddAccountTypes Journal
j = Journal
j{jaccounttypes :: Map TagName AccountType
jaccounttypes = Journal -> Map TagName AccountType
journalAccountTypes Journal
j}

-- | Build a map of all known account types, explicitly declared
-- or inferred from the account's parent or name.
journalAccountTypes :: Journal -> M.Map AccountName AccountType
journalAccountTypes :: Journal -> Map TagName AccountType
journalAccountTypes Journal
j = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(TagName
a,AccountType
acctType) | (TagName
a, Just (AccountType
acctType,Bool
_)) <- forall a. Tree a -> [a]
flatten Tree (TagName, Maybe (AccountType, Bool))
t']
  where
    t :: Tree TagName
t = [TagName] -> Tree TagName
accountNameTreeFrom forall a b. (a -> b) -> a -> b
$ Journal -> [TagName]
journalAccountNames Journal
j :: Tree AccountName
    -- Map from the top of the account tree down to the leaves, propagating
    -- account types downward. Keep track of whether the account is declared
    -- (True), in which case the parent account should be preferred, or merely
    -- inferred (False), in which case the inferred type should be preferred.
    t' :: Tree (TagName, Maybe (AccountType, Bool))
t' = Maybe (AccountType, Bool)
-> Tree TagName -> Tree (TagName, Maybe (AccountType, Bool))
settypes forall a. Maybe a
Nothing Tree TagName
t :: Tree (AccountName, Maybe (AccountType, Bool))
      where
        settypes :: Maybe (AccountType, Bool) -> Tree AccountName -> Tree (AccountName, Maybe (AccountType, Bool))
        settypes :: Maybe (AccountType, Bool)
-> Tree TagName -> Tree (TagName, Maybe (AccountType, Bool))
settypes Maybe (AccountType, Bool)
mparenttype (Node TagName
a [Tree TagName]
subs) = forall a. a -> [Tree a] -> Tree a
Node (TagName
a, Maybe (AccountType, Bool)
mtype) (forall a b. (a -> b) -> [a] -> [b]
map (Maybe (AccountType, Bool)
-> Tree TagName -> Tree (TagName, Maybe (AccountType, Bool))
settypes Maybe (AccountType, Bool)
mtype) [Tree TagName]
subs)
          where
            mtype :: Maybe (AccountType, Bool)
mtype = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup TagName
a Map TagName (AccountType, Bool)
declaredtypes forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (AccountType, Bool)
minferred
              where 
                declaredtypes :: Map TagName (AccountType, Bool)
declaredtypes = (,Bool
True) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Journal -> Map TagName AccountType
journalDeclaredAccountTypes Journal
j
                minferred :: Maybe (AccountType, Bool)
minferred = if forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False forall a b. (a, b) -> b
snd Maybe (AccountType, Bool)
mparenttype
                            then Maybe (AccountType, Bool)
mparenttype
                            else (,Bool
False) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TagName -> Maybe AccountType
accountNameInferType TagName
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (AccountType, Bool)
mparenttype

-- | Build a map of the account types explicitly declared.
journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType
journalDeclaredAccountTypes :: Journal -> Map TagName AccountType
journalDeclaredAccountTypes Journal{Map AccountType [TagName]
jdeclaredaccounttypes :: Map AccountType [TagName]
jdeclaredaccounttypes :: Journal -> Map AccountType [TagName]
jdeclaredaccounttypes} =
  forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [forall a b. (a -> b) -> [a] -> [b]
map (,AccountType
t) [TagName]
as | (AccountType
t,[TagName]
as) <- forall k a. Map k a -> [(k, a)]
M.toList Map AccountType [TagName]
jdeclaredaccounttypes]

-- | To all postings in the journal, add any tags from their account
-- (including those inherited from parent accounts).
-- If the same tag exists on posting and account, the latter is ignored.
journalPostingsAddAccountTags :: Journal -> Journal
journalPostingsAddAccountTags :: Journal -> Journal
journalPostingsAddAccountTags Journal
j = (Posting -> Posting) -> Journal -> Journal
journalMapPostings Posting -> Posting
addtags Journal
j
  where addtags :: Posting -> Posting
addtags Posting
p = Posting
p Posting -> [Tag] -> Posting
`postingAddTags` (Journal -> TagName -> [Tag]
journalInheritedAccountTags Journal
j forall a b. (a -> b) -> a -> b
$ Posting -> TagName
paccount Posting
p)

-- Various kinds of filtering on journals. We do it differently depending
-- on the command.

-------------------------------------------------------------------------------
-- filtering V2

-- | Keep only transactions matching the query expression.
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions :: Query -> Journal -> Journal
filterJournalTransactions Query
q j :: Journal
j@Journal{[Transaction]
jtxns :: [Transaction]
jtxns :: Journal -> [Transaction]
jtxns} = Journal
j{jtxns :: [Transaction]
jtxns=forall a. (a -> Bool) -> [a] -> [a]
filter ((TagName -> Maybe AccountType) -> Query -> Transaction -> Bool
matchesTransactionExtra (Journal -> TagName -> Maybe AccountType
journalAccountType Journal
j) Query
q) [Transaction]
jtxns}

-- | Keep only postings matching the query expression.
-- This can leave unbalanced transactions.
filterJournalPostings :: Query -> Journal -> Journal
filterJournalPostings :: Query -> Journal -> Journal
filterJournalPostings Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map ((TagName -> Maybe AccountType)
-> Query -> Transaction -> Transaction
filterTransactionPostingsExtra (Journal -> TagName -> Maybe AccountType
journalAccountType Journal
j) Query
q) [Transaction]
ts}

-- | Keep only postings which do not match the query expression, but for which a related posting does.
-- This can leave unbalanced transactions.
filterJournalRelatedPostings :: Query -> Journal -> Journal
filterJournalRelatedPostings :: Query -> Journal -> Journal
filterJournalRelatedPostings Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map (Query -> Transaction -> Transaction
filterTransactionRelatedPostings Query
q) [Transaction]
ts}

-- | Within each posting's amount, keep only the parts matching the query, and
-- remove any postings with all amounts removed.
-- This can leave unbalanced transactions.
filterJournalAmounts :: Query -> Journal -> Journal
filterJournalAmounts :: Query -> Journal -> Journal
filterJournalAmounts Query
q j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map (Query -> Transaction -> Transaction
filterTransactionAmounts Query
q) [Transaction]
ts}

-- | Filter out all parts of this transaction's amounts which do not match the
-- query, and remove any postings with all amounts removed.
-- This can leave the transaction unbalanced.
filterTransactionAmounts :: Query -> Transaction -> Transaction
filterTransactionAmounts :: Query -> Transaction -> Transaction
filterTransactionAmounts Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Query -> Posting -> Maybe Posting
filterPostingAmount Query
q) [Posting]
ps}

-- | Filter out all parts of this posting's amount which do not match the query, and remove the posting
-- if this removes all amounts.
filterPostingAmount :: Query -> Posting -> Maybe Posting
filterPostingAmount :: Query -> Posting -> Maybe Posting
filterPostingAmount Query
q p :: Posting
p@Posting{pamount :: Posting -> MixedAmount
pamount=MixedAmount
as}
  | forall (t :: * -> *) a. Foldable t => t a -> Bool
null Map MixedAmountKey Amount
newamt = forall a. Maybe a
Nothing
  | Bool
otherwise   = forall a. a -> Maybe a
Just Posting
p{pamount :: MixedAmount
pamount=Map MixedAmountKey Amount -> MixedAmount
Mixed Map MixedAmountKey Amount
newamt}
  where
    Mixed Map MixedAmountKey Amount
newamt = (Amount -> Bool) -> MixedAmount -> MixedAmount
filterMixedAmount (Query
q Query -> Amount -> Bool
`matchesAmount`) MixedAmount
as

filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Posting -> Bool
`matchesPosting`) [Posting]
ps}

-- Like filterTransactionPostings, but is given the map of account types so can also filter by account type.
filterTransactionPostingsExtra :: (AccountName -> Maybe AccountType) -> Query -> Transaction -> Transaction
filterTransactionPostingsExtra :: (TagName -> Maybe AccountType)
-> Query -> Transaction -> Transaction
filterTransactionPostingsExtra TagName -> Maybe AccountType
atypes Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} =
  Transaction
t{tpostings :: [Posting]
tpostings=forall a. (a -> Bool) -> [a] -> [a]
filter ((TagName -> Maybe AccountType) -> Query -> Posting -> Bool
matchesPostingExtra TagName -> Maybe AccountType
atypes Query
q) [Posting]
ps}

filterTransactionRelatedPostings :: Query -> Transaction -> Transaction
filterTransactionRelatedPostings :: Query -> Transaction -> Transaction
filterTransactionRelatedPostings Query
q t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} =
    Transaction
t{tpostings :: [Posting]
tpostings=if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Posting]
matches then [] else [Posting]
ps forall a. Eq a => [a] -> [a] -> [a]
\\ [Posting]
matches}
  where matches :: [Posting]
matches = forall a. (a -> Bool) -> [a] -> [a]
filter (Query -> Posting -> Bool
matchesPosting Query
q) [Posting]
ps

-- | Apply a transformation to a journal's transactions.
journalMapTransactions :: (Transaction -> Transaction) -> Journal -> Journal
journalMapTransactions :: (Transaction -> Transaction) -> Journal -> Journal
journalMapTransactions Transaction -> Transaction
f j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Transaction
f [Transaction]
ts}

-- | Apply a transformation to a journal's postings.
journalMapPostings :: (Posting -> Posting) -> Journal -> Journal
journalMapPostings :: (Posting -> Posting) -> Journal -> Journal
journalMapPostings Posting -> Posting
f j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map ((Posting -> Posting) -> Transaction -> Transaction
transactionMapPostings Posting -> Posting
f) [Transaction]
ts}

-- | Apply a transformation to a journal's posting amounts.
journalMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Journal -> Journal
journalMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Journal -> Journal
journalMapPostingAmounts MixedAmount -> MixedAmount
f = (Posting -> Posting) -> Journal -> Journal
journalMapPostings ((MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount MixedAmount -> MixedAmount
f)

{-
-------------------------------------------------------------------------------
-- filtering V1

-- | Keep only transactions we are interested in, as described by the
-- filter specification.
filterJournalTransactions :: FilterSpec -> Journal -> Journal
filterJournalTransactions FilterSpec{datespan=datespan
                                    ,cleared=cleared
                                    -- ,real=real
                                    -- ,empty=empty
                                    ,acctpats=apats
                                    ,descpats=dpats
                                    ,depth=depth
                                    ,fMetadata=md
                                    } =
    filterJournalTransactionsByStatus cleared .
    filterJournalPostingsByDepth depth .
    filterJournalTransactionsByAccount apats .
    filterJournalTransactionsByMetadata md .
    filterJournalTransactionsByDescription dpats .
    filterJournalTransactionsByDate datespan

-- | Keep only postings we are interested in, as described by the filter
-- specification. This can leave unbalanced transactions.
filterJournalPostings :: FilterSpec -> Journal -> Journal
filterJournalPostings FilterSpec{datespan=datespan
                                ,cleared=cleared
                                ,real=real
                                ,empty=empty
                                ,acctpats=apats
                                ,descpats=dpats
                                ,depth=depth
                                ,fMetadata=md
                                } =
    filterJournalPostingsByRealness real .
    filterJournalPostingsByStatus cleared .
    filterJournalPostingsByEmpty empty .
    filterJournalPostingsByDepth depth .
    filterJournalPostingsByAccount apats .
    filterJournalTransactionsByMetadata md .
    filterJournalTransactionsByDescription dpats .
    filterJournalTransactionsByDate datespan

-- | Keep only transactions whose metadata matches all metadata specifications.
filterJournalTransactionsByMetadata :: [(String,String)] -> Journal -> Journal
filterJournalTransactionsByMetadata pats j@Journal{jtxns=ts} = j{jtxns=filter matchmd ts}
    where matchmd t = all (`elem` tmetadata t) pats

-- | Keep only transactions whose description matches the description patterns.
filterJournalTransactionsByDescription :: [String] -> Journal -> Journal
filterJournalTransactionsByDescription pats j@Journal{jtxns=ts} = j{jtxns=filter matchdesc ts}
    where matchdesc = matchpats pats . tdescription

-- | Keep only transactions which fall between begin and end dates.
-- We include transactions on the begin date and exclude transactions on the end
-- date, like ledger.  An empty date string means no restriction.
filterJournalTransactionsByDate :: DateSpan -> Journal -> Journal
filterJournalTransactionsByDate (DateSpan begin end) j@Journal{jtxns=ts} = j{jtxns=filter match ts}
    where match t = maybe True (tdate t>=) begin && maybe True (tdate t<) end

-- | Keep only transactions which have the requested cleared/uncleared
-- status, if there is one.
filterJournalTransactionsByStatus :: Maybe Bool -> Journal -> Journal
filterJournalTransactionsByStatus Nothing j = j
filterJournalTransactionsByStatus (Just val) j@Journal{jtxns=ts} = j{jtxns=filter match ts}
    where match = (==val).tstatus

-- | Keep only postings which have the requested cleared/uncleared status,
-- if there is one.
filterJournalPostingsByStatus :: Maybe Bool -> Journal -> Journal
filterJournalPostingsByStatus Nothing j = j
filterJournalPostingsByStatus (Just c) j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
    where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter ((==c) . postingCleared) ps}

-- | Strip out any virtual postings, if the flag is true, otherwise do
-- no filtering.
filterJournalPostingsByRealness :: Bool -> Journal -> Journal
filterJournalPostingsByRealness False j = j
filterJournalPostingsByRealness True j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
    where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter isReal ps}

-- | Strip out any postings with zero amount, unless the flag is true.
filterJournalPostingsByEmpty :: Bool -> Journal -> Journal
filterJournalPostingsByEmpty True j = j
filterJournalPostingsByEmpty False j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
    where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter (not . isEmptyPosting) ps}

-- -- | Keep only transactions which affect accounts deeper than the specified depth.
-- filterJournalTransactionsByDepth :: Maybe Int -> Journal -> Journal
-- filterJournalTransactionsByDepth Nothing j = j
-- filterJournalTransactionsByDepth (Just d) j@Journal{jtxns=ts} =
--     j{jtxns=(filter (any ((<= d+1) . accountNameLevel . paccount) . tpostings) ts)}

-- | Strip out any postings to accounts deeper than the specified depth
-- (and any transactions which have no postings as a result).
filterJournalPostingsByDepth :: Maybe Int -> Journal -> Journal
filterJournalPostingsByDepth Nothing j = j
filterJournalPostingsByDepth (Just d) j@Journal{jtxns=ts} =
    j{jtxns=filter (not . null . tpostings) $ map filtertxns ts}
    where filtertxns t@Transaction{tpostings=ps} =
              t{tpostings=filter ((<= d) . accountNameLevel . paccount) ps}

-- | Keep only postings which affect accounts matched by the account patterns.
-- This can leave transactions unbalanced.
filterJournalPostingsByAccount :: [String] -> Journal -> Journal
filterJournalPostingsByAccount apats j@Journal{jtxns=ts} = j{jtxns=map filterpostings ts}
    where filterpostings t@Transaction{tpostings=ps} = t{tpostings=filter (matchpats apats . paccount) ps}

-- | Keep only transactions which affect accounts matched by the account patterns.
-- More precisely: each positive account pattern excludes transactions
-- which do not contain a posting to a matched account, and each negative
-- account pattern excludes transactions containing a posting to a matched
-- account.
filterJournalTransactionsByAccount :: [String] -> Journal -> Journal
filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tmatch ts}
    where
      tmatch t = (null positives || any positivepmatch ps) && (null negatives || not (any negativepmatch ps)) where ps = tpostings t
      positivepmatch p = any (`amatch` a) positives where a = paccount p
      negativepmatch p = any (`amatch` a) negatives where a = paccount p
      amatch pat a = regexMatchesCI (abspat pat) a
      (negatives,positives) = partition isnegativepat apats

-}

-- | Reverse all lists of parsed items, which during parsing were
-- prepended to, so that the items are in parse order. Part of
-- post-parse finalisation.
journalReverse :: Journal -> Journal
journalReverse :: Journal -> Journal
journalReverse Journal
j =
  Journal
j {jfiles :: [(RegexError, TagName)]
jfiles            = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [(RegexError, TagName)]
jfiles Journal
j
    ,jdeclaredaccounts :: [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [(TagName, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j
    ,jtxns :: [Transaction]
jtxns             = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
    ,jtxnmodifiers :: [TransactionModifier]
jtxnmodifiers     = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [TransactionModifier]
jtxnmodifiers Journal
j
    ,jperiodictxns :: [PeriodicTransaction]
jperiodictxns     = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [PeriodicTransaction]
jperiodictxns Journal
j
    ,jpricedirectives :: [PriceDirective]
jpricedirectives  = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Journal -> [PriceDirective]
jpricedirectives Journal
j
    }

-- | Set this journal's last read time, ie when its files were last read.
journalSetLastReadTime :: POSIXTime -> Journal -> Journal
journalSetLastReadTime :: POSIXTime -> Journal -> Journal
journalSetLastReadTime POSIXTime
t Journal
j = Journal
j{ jlastreadtime :: POSIXTime
jlastreadtime = POSIXTime
t }


journalNumberAndTieTransactions :: Journal -> Journal
journalNumberAndTieTransactions = Journal -> Journal
journalTieTransactions forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Journal
journalNumberTransactions

-- | Number (set the tindex field) this journal's transactions, counting upward from 1.
journalNumberTransactions :: Journal -> Journal
journalNumberTransactions :: Journal -> Journal
journalNumberTransactions j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Integer
i Transaction
t -> Transaction
t{tindex :: Integer
tindex=Integer
i}) [Integer
1..] [Transaction]
ts}

-- | Tie the knot in all of this journal's transactions, ensuring their postings
-- refer to them. This should be done last, after any other transaction-modifying operations.
journalTieTransactions :: Journal -> Journal
journalTieTransactions :: Journal -> Journal
journalTieTransactions j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Transaction
txnTieKnot [Transaction]
ts}

-- | Untie all transaction-posting knots in this journal, so that eg
-- recursiveSize and GHCI's :sprint can work on it.
journalUntieTransactions :: Transaction -> Transaction
journalUntieTransactions :: Transaction -> Transaction
journalUntieTransactions t :: Transaction
t@Transaction{tpostings :: Transaction -> [Posting]
tpostings=[Posting]
ps} = Transaction
t{tpostings :: [Posting]
tpostings=forall a b. (a -> b) -> [a] -> [b]
map (\Posting
p -> Posting
p{ptransaction :: Maybe Transaction
ptransaction=forall a. Maybe a
Nothing}) [Posting]
ps}

-- | Apply any transaction modifier rules in the journal (adding automated
-- postings to transactions, eg). Or if a modifier rule fails to parse,
-- return the error message. A reference date is provided to help interpret
-- relative dates in transaction modifier queries.
journalModifyTransactions :: Day -> Journal -> Either String Journal
journalModifyTransactions :: Day -> Journal -> Either RegexError Journal
journalModifyTransactions Day
d Journal
j =
    case (TagName -> Maybe AccountType)
-> (TagName -> [Tag])
-> Map TagName AmountStyle
-> Day
-> [TransactionModifier]
-> [Transaction]
-> Either RegexError [Transaction]
modifyTransactions (Journal -> TagName -> Maybe AccountType
journalAccountType Journal
j) (Journal -> TagName -> [Tag]
journalInheritedAccountTags Journal
j) (Journal -> Map TagName AmountStyle
journalCommodityStyles Journal
j) Day
d (Journal -> [TransactionModifier]
jtxnmodifiers Journal
j) (Journal -> [Transaction]
jtxns Journal
j) of
      Right [Transaction]
ts -> forall a b. b -> Either a b
Right Journal
j{jtxns :: [Transaction]
jtxns=[Transaction]
ts}
      Left RegexError
err -> forall a b. a -> Either a b
Left RegexError
err

--

-- | Choose and apply a consistent display style to the posting
-- amounts in each commodity (see journalCommodityStyles).
-- Can return an error message eg if inconsistent number formats are found.
journalApplyCommodityStyles :: Journal -> Either String Journal
journalApplyCommodityStyles :: Journal -> Either RegexError Journal
journalApplyCommodityStyles = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Journal -> Journal
fixjournal forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> Either RegexError Journal
journalInferCommodityStyles
  where
    fixjournal :: Journal -> Journal
fixjournal j :: Journal
j@Journal{jpricedirectives :: Journal -> [PriceDirective]
jpricedirectives=[PriceDirective]
pds} =
        (Posting -> Posting) -> Journal -> Journal
journalMapPostings (Map TagName AmountStyle -> Posting -> Posting
postingApplyCommodityStyles Map TagName AmountStyle
styles) Journal
j{jpricedirectives :: [PriceDirective]
jpricedirectives=forall a b. (a -> b) -> [a] -> [b]
map PriceDirective -> PriceDirective
fixpricedirective [PriceDirective]
pds}
      where
        styles :: Map TagName AmountStyle
styles = Journal -> Map TagName AmountStyle
journalCommodityStyles Journal
j
        fixpricedirective :: PriceDirective -> PriceDirective
fixpricedirective pd :: PriceDirective
pd@PriceDirective{pdamount :: PriceDirective -> Amount
pdamount=Amount
a} = PriceDirective
pd{pdamount :: Amount
pdamount=Map TagName AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map TagName AmountStyle
styles Amount
a}

-- | Get the canonical amount styles for this journal, whether (in order of precedence):
-- set globally in InputOpts,
-- declared by commodity directives, 
-- declared by a default commodity (D) directive, 
-- or inferred from posting amounts, 
-- as a map from symbol to style. 
-- Styles from directives are assumed to specify the decimal mark.
journalCommodityStyles :: Journal -> M.Map CommoditySymbol AmountStyle
journalCommodityStyles :: Journal -> Map TagName AmountStyle
journalCommodityStyles Journal
j =
  -- XXX could be some redundancy here, cf journalStyleInfluencingAmounts
  Map TagName AmountStyle
globalstyles forall a. Semigroup a => a -> a -> a
<> Map TagName AmountStyle
declaredstyles forall a. Semigroup a => a -> a -> a
<> Map TagName AmountStyle
defaultcommoditystyle forall a. Semigroup a => a -> a -> a
<> Map TagName AmountStyle
inferredstyles
  where
    globalstyles :: Map TagName AmountStyle
globalstyles          = Journal -> Map TagName AmountStyle
jglobalcommoditystyles Journal
j
    declaredstyles :: Map TagName AmountStyle
declaredstyles        = forall a b k. (a -> Maybe b) -> Map k a -> Map k b
M.mapMaybe Commodity -> Maybe AmountStyle
cformat forall a b. (a -> b) -> a -> b
$ Journal -> Map TagName Commodity
jcommodities Journal
j
    defaultcommoditystyle :: Map TagName AmountStyle
defaultcommoditystyle = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes [Journal -> Maybe (TagName, AmountStyle)
jparsedefaultcommodity Journal
j]
    inferredstyles :: Map TagName AmountStyle
inferredstyles        = Journal -> Map TagName AmountStyle
jinferredcommodities Journal
j

-- | Collect and save inferred amount styles for each commodity based on
-- the posting amounts in that commodity (excluding price amounts), ie:
-- "the format of the first amount, adjusted to the highest precision of all amounts".
-- Can return an error message eg if inconsistent number formats are found.
journalInferCommodityStyles :: Journal -> Either String Journal
journalInferCommodityStyles :: Journal -> Either RegexError Journal
journalInferCommodityStyles Journal
j =
  case [Amount] -> Either RegexError (Map TagName AmountStyle)
commodityStylesFromAmounts forall a b. (a -> b) -> a -> b
$ Journal -> [Amount]
journalStyleInfluencingAmounts Journal
j of
    Left RegexError
e   -> forall a b. a -> Either a b
Left RegexError
e
    Right Map TagName AmountStyle
cs -> forall a b. b -> Either a b
Right Journal
j{jinferredcommodities :: Map TagName AmountStyle
jinferredcommodities = forall a. Show a => RegexError -> a -> a
dbg7 RegexError
"journalInferCommodityStyles" Map TagName AmountStyle
cs}

-- | Given a list of amounts, in parse order (roughly speaking; see journalStyleInfluencingAmounts),
-- build a map from their commodity names to standard commodity
-- display formats. Can return an error message eg if inconsistent
-- number formats are found.
--
-- Though, these amounts may have come from multiple files, so we
-- shouldn't assume they use consistent number formats.
-- Currently we don't enforce that even within a single file,
-- and this function never reports an error.
--
commodityStylesFromAmounts :: [Amount] -> Either String (M.Map CommoditySymbol AmountStyle)
commodityStylesFromAmounts :: [Amount] -> Either RegexError (Map TagName AmountStyle)
commodityStylesFromAmounts =
    forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Amount
a -> forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle (Amount -> TagName
acommodity Amount
a) (Amount -> AmountStyle
astyle Amount
a)) forall a. Monoid a => a
mempty

-- | Given a list of amount styles (assumed to be from parsed amounts
-- in a single commodity), in parse order, choose a canonical style.
canonicalStyleFrom :: [AmountStyle] -> AmountStyle
canonicalStyleFrom :: [AmountStyle] -> AmountStyle
canonicalStyleFrom = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle AmountStyle
amountstyle

-- TODO: should probably detect and report inconsistencies here.
-- Though, we don't have the info for a good error message, so maybe elsewhere.
-- | Given a pair of AmountStyles, choose a canonical style.
-- This is:
-- the general style of the first amount,
-- with the first digit group style seen,
-- with the maximum precision of all.
canonicalStyle :: AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle :: AmountStyle -> AmountStyle -> AmountStyle
canonicalStyle AmountStyle
a AmountStyle
b = AmountStyle
a{asprecision :: AmountPrecision
asprecision=AmountPrecision
prec, asdecimalpoint :: Maybe Char
asdecimalpoint=Maybe Char
decmark, asdigitgroups :: Maybe DigitGroupStyle
asdigitgroups=Maybe DigitGroupStyle
mgrps}
  where
    -- precision is maximum of all precisions
    prec :: AmountPrecision
prec = forall a. Ord a => a -> a -> a
max (AmountStyle -> AmountPrecision
asprecision AmountStyle
a) (AmountStyle -> AmountPrecision
asprecision AmountStyle
b)
    -- identify the digit group mark (& group sizes)
    mgrps :: Maybe DigitGroupStyle
mgrps = AmountStyle -> Maybe DigitGroupStyle
asdigitgroups AmountStyle
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AmountStyle -> Maybe DigitGroupStyle
asdigitgroups AmountStyle
b
    -- if a digit group mark was identified above, we can rely on that;
    -- make sure the decimal mark is different. If not, default to period.
    defdecmark :: Char
defdecmark = case Maybe DigitGroupStyle
mgrps of
        Just (DigitGroups Char
'.' [Word8]
_) -> Char
','
        Maybe DigitGroupStyle
_                        -> Char
'.'
    -- identify the decimal mark: the first one used, or the above default,
    -- but never the same character as the digit group mark.
    -- urgh.. refactor..
    decmark :: Maybe Char
decmark = case Maybe DigitGroupStyle
mgrps of
        Just DigitGroupStyle
_  -> forall a. a -> Maybe a
Just Char
defdecmark
        Maybe DigitGroupStyle
Nothing -> AmountStyle -> Maybe Char
asdecimalpoint AmountStyle
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AmountStyle -> Maybe Char
asdecimalpoint AmountStyle
b forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just Char
defdecmark

-- -- | Apply this journal's historical price records to unpriced amounts where possible.
-- journalApplyPriceDirectives :: Journal -> Journal
-- journalApplyPriceDirectives j@Journal{jtxns=ts} = j{jtxns=map fixtransaction ts}
--     where
--       fixtransaction t@Transaction{tdate=d, tpostings=ps} = t{tpostings=map fixposting ps}
--        where
--         fixposting p@Posting{pamount=a} = p{pamount=fixmixedamount a}
--         fixmixedamount = mapMixedAmount fixamount
--         fixamount = fixprice
--         fixprice a@Amount{price=Just _} = a
--         fixprice a@Amount{commodity=c} = a{price=maybe Nothing (Just . UnitPrice) $ journalPriceDirectiveFor j d c}

-- -- | Get the price for a commodity on the specified day from the price database, if known.
-- -- Does only one lookup step, ie will not look up the price of a price.
-- journalPriceDirectiveFor :: Journal -> Day -> CommoditySymbol -> Maybe MixedAmount
-- journalPriceDirectiveFor j d CommoditySymbol{symbol=s} = do
--   let ps = reverse $ filter ((<= d).pddate) $ filter ((s==).hsymbol) $ sortBy (comparing pddate) $ jpricedirectives j
--   case ps of (PriceDirective{pdamount=a}:_) -> Just a
--              _ -> Nothing

-- | Infer transaction-implied market prices from commodity-exchanging
-- transactions, if any. It's best to call this after transactions have
-- been balanced and posting amounts have appropriate prices attached.
journalInferMarketPricesFromTransactions :: Journal -> Journal
journalInferMarketPricesFromTransactions :: Journal -> Journal
journalInferMarketPricesFromTransactions Journal
j =
  Journal
j{jinferredmarketprices :: [MarketPrice]
jinferredmarketprices =
       forall a. Show a => RegexError -> a -> a
dbg4 RegexError
"jinferredmarketprices" forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a b. (a -> b) -> [a] -> [b]
map PriceDirective -> MarketPrice
priceDirectiveToMarketPrice forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [PriceDirective]
postingPriceDirectivesFromCost forall a b. (a -> b) -> a -> b
$
       Journal -> [Posting]
journalPostings Journal
j
   }

-- | Convert all this journal's amounts to cost using the transaction prices, if any.
-- The journal's commodity styles are applied to the resulting amounts.
journalToCost :: ConversionOp -> Journal -> Journal
journalToCost :: ConversionOp -> Journal -> Journal
journalToCost ConversionOp
cost j :: Journal
j@Journal{jtxns :: Journal -> [Transaction]
jtxns=[Transaction]
ts} = Journal
j{jtxns :: [Transaction]
jtxns=forall a b. (a -> b) -> [a] -> [b]
map (Map TagName AmountStyle
-> ConversionOp -> Transaction -> Transaction
transactionToCost Map TagName AmountStyle
styles ConversionOp
cost) [Transaction]
ts}
  where
    styles :: Map TagName AmountStyle
styles = Journal -> Map TagName AmountStyle
journalCommodityStyles Journal
j

-- | Add inferred equity postings to a 'Journal' using transaction prices.
journalAddInferredEquityPostings :: Journal -> Journal
journalAddInferredEquityPostings :: Journal -> Journal
journalAddInferredEquityPostings Journal
j = (Transaction -> Transaction) -> Journal -> Journal
journalMapTransactions (TagName -> Transaction -> Transaction
transactionAddInferredEquityPostings TagName
equityAcct) Journal
j
  where
    equityAcct :: TagName
equityAcct = Journal -> TagName
journalConversionAccount Journal
j

-- | Add inferred transaction prices from equity postings.
journalAddPricesFromEquity :: Journal -> Either String Journal
journalAddPricesFromEquity :: Journal -> Either RegexError Journal
journalAddPricesFromEquity Journal
j = do
    [Transaction]
ts <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Map TagName AccountType
-> Transaction -> Either RegexError Transaction
transactionAddPricesFromEquity forall a b. (a -> b) -> a -> b
$ Journal -> Map TagName AccountType
jaccounttypes Journal
j) forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
    forall (m :: * -> *) a. Monad m => a -> m a
return Journal
j{jtxns :: [Transaction]
jtxns=[Transaction]
ts}

-- -- | Get this journal's unique, display-preference-canonicalised commodities, by symbol.
-- journalCanonicalCommodities :: Journal -> M.Map String CommoditySymbol
-- journalCanonicalCommodities j = canonicaliseCommodities $ journalAmountCommodities j

-- -- | Get all this journal's amounts' commodities, in the order parsed.
-- journalAmountCommodities :: Journal -> [CommoditySymbol]
-- journalAmountCommodities = map acommodity . concatMap amounts . journalAmounts

-- -- | Get all this journal's amount and price commodities, in the order parsed.
-- journalAmountAndPriceCommodities :: Journal -> [CommoditySymbol]
-- journalAmountAndPriceCommodities = concatMap amountCommodities . concatMap amounts . journalAmounts

-- -- | Get this amount's commodity and any commodities referenced in its price.
-- amountCommodities :: Amount -> [CommoditySymbol]
-- amountCommodities Amount{acommodity=c,aprice=p} =
--     case p of Nothing -> [c]
--               Just (UnitPrice ma)  -> c:(concatMap amountCommodities $ amounts ma)
--               Just (TotalPrice ma) -> c:(concatMap amountCommodities $ amounts ma)

-- | Get an ordered list of amounts in this journal which can
-- influence canonical amount display styles. Those amounts are, in
-- the following order:
--
-- * amounts in market price (P) directives (in parse order)
-- * posting amounts in transactions (in parse order)
-- * the amount in the final default commodity (D) directive
--
-- Transaction price amounts (posting amounts' aprice field) are not included.
--
journalStyleInfluencingAmounts :: Journal -> [Amount]
journalStyleInfluencingAmounts :: Journal -> [Amount]
journalStyleInfluencingAmounts Journal
j =
  forall a. Show a => RegexError -> a -> a
dbg7 RegexError
"journalStyleInfluencingAmounts" forall a b. (a -> b) -> a -> b
$
  forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
   [Maybe Amount
mdefaultcommodityamt]
  ,forall a b. (a -> b) -> [a] -> [b]
map (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. PriceDirective -> Amount
pdamount) forall a b. (a -> b) -> a -> b
$ Journal -> [PriceDirective]
jpricedirectives Journal
j
  ,forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (MixedAmount -> [Amount]
amountsRaw forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> MixedAmount
pamount) forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
  ]
  where
    -- D's amount style isn't actually stored as an amount, make it into one
    mdefaultcommodityamt :: Maybe Amount
mdefaultcommodityamt =
      case Journal -> Maybe (TagName, AmountStyle)
jparsedefaultcommodity Journal
j of
        Just (TagName
symbol,AmountStyle
style) -> forall a. a -> Maybe a
Just Amount
nullamt{acommodity :: TagName
acommodity=TagName
symbol,astyle :: AmountStyle
astyle=AmountStyle
style}
        Maybe (TagName, AmountStyle)
Nothing -> forall a. Maybe a
Nothing

-- overcomplicated/unused amount traversal stuff
--
-- | Get an ordered list of 'AmountStyle's from the amounts in this
-- journal which influence canonical amount display styles. See
-- traverseJournalAmounts.
-- journalAmounts :: Journal -> [Amount]
-- journalAmounts = getConst . traverseJournalAmounts (Const . (:[]))
--
-- | Apply a transformation to the journal amounts traversed by traverseJournalAmounts.
-- overJournalAmounts :: (Amount -> Amount) -> Journal -> Journal
-- overJournalAmounts f = runIdentity . traverseJournalAmounts (Identity . f)
--
-- | A helper that traverses over most amounts in the journal,
-- in particular the ones which influence canonical amount display styles,
-- processing them with the given applicative function.
--
-- These include, in the following order:
--
-- * the amount in the final default commodity (D) directive
-- * amounts in market price (P) directives (in parse order)
-- * posting amounts in transactions (in parse order)
--
-- Transaction price amounts, which may be embedded in posting amounts
-- (the aprice field), are left intact but not traversed/processed.
--
-- traverseJournalAmounts :: Applicative f => (Amount -> f Amount) -> Journal -> f Journal
-- traverseJournalAmounts f j =
--   recombine <$> (traverse . dcamt) f (jparsedefaultcommodity j)
--             <*> (traverse . pdamt) f (jpricedirectives j)
--             <*> (traverse . tps . traverse . pamt . amts . traverse) f (jtxns j)
--   where
--     recombine pds txns = j { jpricedirectives = pds, jtxns = txns }
--     -- a bunch of traversals
--     dcamt g pd         = (\mdc -> case mdc of Nothing -> Nothing
--                                               Just ((c,stpd{pdamount =amt}
--                          ) <$> g (pdamount pd)
--     pdamt g pd         = (\amt -> pd{pdamount =amt}) <$> g (pdamount pd)
--     tps   g t          = (\ps  -> t {tpostings=ps }) <$> g (tpostings t)
--     pamt  g p          = (\amt -> p {pamount  =amt}) <$> g (pamount p)
--     amts  g (Mixed as) = Mixed <$> g as

-- | The fully specified date span enclosing the dates (primary or secondary)
-- of all this journal's transactions and postings, or DateSpan Nothing Nothing
-- if there are none.
journalDateSpan :: Bool -> Journal -> DateSpan
journalDateSpan :: Bool -> Journal -> DateSpan
journalDateSpan Bool
False = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just WhichDate
PrimaryDate
journalDateSpan Bool
True  = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just WhichDate
SecondaryDate

-- | The fully specified date span enclosing the dates (primary and secondary)
-- of all this journal's transactions and postings, or DateSpan Nothing Nothing
-- if there are none.
journalDateSpanBothDates :: Journal -> DateSpan
journalDateSpanBothDates :: Journal -> DateSpan
journalDateSpanBothDates = Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper forall a. Maybe a
Nothing

-- | A helper for journalDateSpan which takes Maybe WhichDate directly. Nothing
-- uses both primary and secondary dates.
journalDateSpanHelper :: Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper :: Maybe WhichDate -> Journal -> DateSpan
journalDateSpanHelper Maybe WhichDate
whichdate Journal
j =
    Maybe Day -> Maybe Day -> DateSpan
DateSpan (forall a. Ord a => [a] -> Maybe a
minimumMay [Day]
dates) (Integer -> Day -> Day
addDays Integer
1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Ord a => [a] -> Maybe a
maximumMay [Day]
dates)
  where
    dates :: [Day]
dates    = [Day]
pdates forall a. [a] -> [a] -> [a]
++ [Day]
tdates
    tdates :: [Day]
tdates   = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Day]
gettdate [Transaction]
ts
    pdates :: [Day]
pdates   = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [Day]
getpdate forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [Posting]
tpostings [Transaction]
ts
    ts :: [Transaction]
ts       = Journal -> [Transaction]
jtxns Journal
j
    gettdate :: Transaction -> [Day]
gettdate Transaction
t = case Maybe WhichDate
whichdate of
        Just WhichDate
PrimaryDate   -> [Transaction -> Day
tdate Transaction
t]
        Just WhichDate
SecondaryDate -> [forall a. a -> Maybe a -> a
fromMaybe (Transaction -> Day
tdate Transaction
t) forall a b. (a -> b) -> a -> b
$ Transaction -> Maybe Day
tdate2 Transaction
t]
        Maybe WhichDate
Nothing            -> Transaction -> Day
tdate Transaction
t forall a. a -> [a] -> [a]
: forall a. Maybe a -> [a]
maybeToList (Transaction -> Maybe Day
tdate2 Transaction
t)
    getpdate :: Posting -> [Day]
getpdate Posting
p = case Maybe WhichDate
whichdate of
        Just WhichDate
PrimaryDate   -> forall a. Maybe a -> [a]
maybeToList forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Day
pdate Posting
p
        Just WhichDate
SecondaryDate -> forall a. Maybe a -> [a]
maybeToList forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Day
pdate2 Posting
p forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Posting -> Maybe Day
pdate Posting
p
        Maybe WhichDate
Nothing            -> forall a. [Maybe a] -> [a]
catMaybes [Posting -> Maybe Day
pdate Posting
p, Posting -> Maybe Day
pdate2 Posting
p]

-- | The earliest of this journal's transaction and posting dates, or
-- Nothing if there are none.
journalStartDate :: Bool -> Journal -> Maybe Day
journalStartDate :: Bool -> Journal -> Maybe Day
journalStartDate Bool
secondary Journal
j = Maybe Day
b where DateSpan Maybe Day
b Maybe Day
_ = Bool -> Journal -> DateSpan
journalDateSpan Bool
secondary Journal
j

-- | The "exclusive end date" of this journal: the day following its latest transaction 
-- or posting date, or Nothing if there are none.
journalEndDate :: Bool -> Journal -> Maybe Day
journalEndDate :: Bool -> Journal -> Maybe Day
journalEndDate Bool
secondary Journal
j = Maybe Day
e where DateSpan Maybe Day
_ Maybe Day
e = Bool -> Journal -> DateSpan
journalDateSpan Bool
secondary Journal
j

-- | The latest of this journal's transaction and posting dates, or
-- Nothing if there are none.
journalLastDay :: Bool -> Journal -> Maybe Day
journalLastDay :: Bool -> Journal -> Maybe Day
journalLastDay Bool
secondary Journal
j = Integer -> Day -> Day
addDays (-Integer
1) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Journal -> Maybe Day
journalEndDate Bool
secondary Journal
j

-- | Apply the pivot transformation to all postings in a journal,
-- replacing their account name by their value for the given field or tag.
journalPivot :: Text -> Journal -> Journal
journalPivot :: TagName -> Journal -> Journal
journalPivot TagName
fieldortagname Journal
j = Journal
j{jtxns :: [Transaction]
jtxns = forall a b. (a -> b) -> [a] -> [b]
map (TagName -> Transaction -> Transaction
transactionPivot TagName
fieldortagname) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> [Transaction]
jtxns forall a b. (a -> b) -> a -> b
$ Journal
j}

-- | Replace this transaction's postings' account names with the value
-- of the given field or tag, if any.
transactionPivot :: Text -> Transaction -> Transaction
transactionPivot :: TagName -> Transaction -> Transaction
transactionPivot TagName
fieldortagname Transaction
t = Transaction
t{tpostings :: [Posting]
tpostings = forall a b. (a -> b) -> [a] -> [b]
map (TagName -> Posting -> Posting
postingPivot TagName
fieldortagname) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [Posting]
tpostings forall a b. (a -> b) -> a -> b
$ Transaction
t}

-- | Replace this posting's account name with the value
-- of the given field or tag, if any, otherwise the empty string.
postingPivot :: Text -> Posting -> Posting
postingPivot :: TagName -> Posting -> Posting
postingPivot TagName
fieldortagname Posting
p = Posting
p{paccount :: TagName
paccount = TagName
pivotedacct, poriginal :: Maybe Posting
poriginal = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Posting -> Posting
originalPosting Posting
p}
  where
    pivotedacct :: TagName
pivotedacct
      | Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, TagName
fieldortagname forall a. Eq a => a -> a -> Bool
== TagName
"code"        = Transaction -> TagName
tcode Transaction
t
      | Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, TagName
fieldortagname forall a. Eq a => a -> a -> Bool
== TagName
"description" = Transaction -> TagName
tdescription Transaction
t
      | Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, TagName
fieldortagname forall a. Eq a => a -> a -> Bool
== TagName
"payee"       = Transaction -> TagName
transactionPayee Transaction
t
      | Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, TagName
fieldortagname forall a. Eq a => a -> a -> Bool
== TagName
"note"        = Transaction -> TagName
transactionNote Transaction
t
      | Just Transaction
t <- Posting -> Maybe Transaction
ptransaction Posting
p, TagName
fieldortagname forall a. Eq a => a -> a -> Bool
== TagName
"status"      = RegexError -> TagName
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> RegexError
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Status
tstatus forall a b. (a -> b) -> a -> b
$ Transaction
t
      | Just (TagName
_, TagName
value) <- TagName -> Posting -> Maybe Tag
postingFindTag TagName
fieldortagname Posting
p        = TagName
value
      | Bool
otherwise                                                 = TagName
""

postingFindTag :: TagName -> Posting -> Maybe (TagName, TagValue)
postingFindTag :: TagName -> Posting -> Maybe Tag
postingFindTag TagName
tagname Posting
p = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((TagName
tagnameforall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ Posting -> [Tag]
postingAllTags Posting
p

-- | Apply some account aliases to all posting account names in the journal, as described by accountNameApplyAliases.
-- This can fail due to a bad replacement pattern in a regular expression alias.
journalApplyAliases :: [AccountAlias] -> Journal -> Either RegexError Journal
-- short circuit the common case, just in case there's a performance impact from txnTieKnot etc.
journalApplyAliases :: [AccountAlias] -> Journal -> Either RegexError Journal
journalApplyAliases [] Journal
j = forall a b. b -> Either a b
Right Journal
j
journalApplyAliases [AccountAlias]
aliases Journal
j = 
  case forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([AccountAlias] -> Transaction -> Either RegexError Transaction
transactionApplyAliases [AccountAlias]
aliases) forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j of
    Right [Transaction]
ts -> forall a b. b -> Either a b
Right Journal
j{jtxns :: [Transaction]
jtxns = [Transaction]
ts}
    Left RegexError
err -> forall a b. a -> Either a b
Left RegexError
err

-- -- | Build a database of market prices in effect on the given date,
-- -- from the journal's price directives.
-- journalPrices :: Day -> Journal -> Prices
-- journalPrices d = toPrices d . jpricedirectives

-- -- | Render a market price as a P directive.
-- showPriceDirectiveDirective :: PriceDirective -> String
-- showPriceDirectiveDirective pd = unwords
--     [ "P"
--     , showDate (pddate pd)
--     , T.unpack (pdcommodity pd)
--     , (showAmount . amountSetPrecision maxprecision) (pdamount pd
--     )
--     ]

-- debug helpers
-- traceAmountPrecision a = trace (show $ map (precision . acommodity) $ amounts a) a
-- tracePostingsCommodities ps = trace (show $ map ((map (precision . acommodity) . amounts) . pamount) ps) ps

-- tests
--
-- A sample journal for testing, similar to examples/sample.journal.
-- Provide an option to either use explicit amounts or missing amounts, for testing purposes.
--
-- 2008/01/01 income
--     assets:bank:checking  $1
--     income:salary
--
-- 2008/06/01 gift
--     assets:bank:checking  $1
--     income:gifts
--
-- 2008/06/02 save
--     assets:bank:saving  $1
--     assets:bank:checking
--
-- 2008/06/03 * eat & shop
--     expenses:food      $1
--     expenses:supplies  $1
--     assets:cash
--
-- 2008/10/01 take a loan
--     assets:bank:checking $1
--     liabilities:debts    $-1
--
-- 2008/12/31 * pay off
--     liabilities:debts  $1
--     assets:bank:checking

samplejournal :: Journal
samplejournal = Bool -> Journal
samplejournalMaybeExplicit Bool
True

samplejournalMaybeExplicit :: Bool -> Journal
samplejournalMaybeExplicit :: Bool -> Journal
samplejournalMaybeExplicit Bool
explicit = Journal
nulljournal
         {jtxns :: [Transaction]
jtxns = [
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
01 Int
01,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Unmarked,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"income",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=
                 [TagName
"assets:bank:checking" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                 ,TagName
"income:salary" TagName -> Amount -> Posting
`post` if Bool
explicit then DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
1) else Amount
missingamt
                 ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ,
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
06 Int
01,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Unmarked,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"gift",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=
                 [TagName
"assets:bank:checking" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                 ,TagName
"income:gifts" TagName -> Amount -> Posting
`post` if Bool
explicit then DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
1) else Amount
missingamt
                 ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ,
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
06 Int
02,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Unmarked,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"save",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=
                 [TagName
"assets:bank:saving" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                 ,TagName
"assets:bank:checking" TagName -> Amount -> Posting
`post` if Bool
explicit then DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
1) else Amount
missingamt
                 ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ,
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
06 Int
03,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Cleared,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"eat & shop",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=[TagName
"expenses:food" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                       ,TagName
"expenses:supplies" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                       ,TagName
"assets:cash" TagName -> Amount -> Posting
`post` if Bool
explicit then DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
2) else Amount
missingamt
                       ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ,
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
10 Int
01,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Unmarked,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"take a loan",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=[TagName
"assets:bank:checking" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                       ,TagName
"liabilities:debts" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
1)
                       ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ,
           Transaction -> Transaction
txnTieKnot forall a b. (a -> b) -> a -> b
$ Transaction {
             tindex :: Integer
tindex=Integer
0,
             tsourcepos :: (SourcePos, SourcePos)
tsourcepos=(SourcePos, SourcePos)
nullsourcepos,
             tdate :: Day
tdate=Integer -> Int -> Int -> Day
fromGregorian Integer
2008 Int
12 Int
31,
             tdate2 :: Maybe Day
tdate2=forall a. Maybe a
Nothing,
             tstatus :: Status
tstatus=Status
Unmarked,
             tcode :: TagName
tcode=TagName
"",
             tdescription :: TagName
tdescription=TagName
"pay off",
             tcomment :: TagName
tcomment=TagName
"",
             ttags :: [Tag]
ttags=[],
             tpostings :: [Posting]
tpostings=[TagName
"liabilities:debts" TagName -> Amount -> Posting
`post` DecimalRaw Integer -> Amount
usd DecimalRaw Integer
1
                       ,TagName
"assets:bank:checking" TagName -> Amount -> Posting
`post` if Bool
explicit then DecimalRaw Integer -> Amount
usd (-DecimalRaw Integer
1) else Amount
missingamt
                       ],
             tprecedingcomment :: TagName
tprecedingcomment=TagName
""
           }
          ]
         }

tests_Journal :: TestTree
tests_Journal = RegexError -> [TestTree] -> TestTree
testGroup RegexError
"Journal" [

   RegexError -> Assertion -> TestTree
testCase RegexError
"journalDateSpan" forall a b. (a -> b) -> a -> b
$
    Bool -> Journal -> DateSpan
journalDateSpan Bool
True Journal
nulljournal{
      jtxns :: [Transaction]
jtxns = [Transaction
nulltransaction{tdate :: Day
tdate = Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
02 Int
01
                              ,tpostings :: [Posting]
tpostings = [Posting
posting{pdate :: Maybe Day
pdate=forall a. a -> Maybe a
Just (Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
01 Int
10)}]
                              }
              ,Transaction
nulltransaction{tdate :: Day
tdate = Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
09 Int
01
                              ,tpostings :: [Posting]
tpostings = [Posting
posting{pdate2 :: Maybe Day
pdate2=forall a. a -> Maybe a
Just (Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
10 Int
10)}]
                              }
              ]
      }
    forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Maybe Day -> Maybe Day -> DateSpan
DateSpan (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
1 Int
10) (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Int -> Day
fromGregorian Integer
2014 Int
10 Int
11))
  ]