packages feed

hledger-lib-2.0: Hledger/Data/Journal.hs

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

{-|

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,
  addJournalItem,
  addTransactionItem,
  addPeriodicTransaction,
  addAssertDirective,
  addTransaction,
  journalDbg,
  journalInferMarketPricesFromTransactions,
  journalApplyCommodityAliases,
  CommodityAliases,
  journalCommodityAliases,
  convertAliasAmount,
  commodityAliases,
  commodityRedeclare,
  commodityCombine,
  commoditiesAndAliases,
  journalInferCommodityStyles,
  journalStyleAmounts,
  journalCommodityStyles,
  journalCommodityStylesWith,
  journalToCost,
  journalInferEquityFromCosts,
  journalTagCostsAndEquityAndMaybeInferCosts,
  journalReverse,
  journalSetLastReadTime,
  journalRenumberAccountDeclarations,
  journalPivot,
  journalPostingsAddAccountTags,
  journalPostingsKeepAccountTagsOnly,
  journalPostingsAddCommodityTags,
  journalInferPostingsTransactedCost,
  journalCommodityUsesLots,
  journalAccountUsesNoLots,
  journalAccountLotsTags,
  accountUsesNoLotsWith,
  journalLotfulCommodities,
  journalCommodityLotsMethod,
  postingLotsMethod,
  parseReductionMethod,
  journalCheckLotsTagValues,
-- * Filtering
  filterJournalTransactions,
  filterJournalPostings,
  filterJournalRelatedPostings,
  filterJournalAmounts,
  filterTransactionAmounts,
  filterTransactionPostings,
  filterTransactionPostingsExtra,
  filterTransactionRelatedPostings,
  filterPostingAmount,
  -- * Mapping
  journalMapTransactions,
  journalMapPostings,
  journalMapPostingAmounts,
  -- * Querying
  journalAccountNamesUsed,
  journalAccountNamesImplied,
  journalAccountNamesDeclared,
  journalAccountNamesDeclaredOrUsed,
  journalAccountNamesDeclaredOrImplied,
  journalAccountNames,
  journalLeafAccountNames,
  journalAccountNameTree,
  journalAccountTags,
  journalCommodityTags,
  journalInheritedAccountTags,
  -- journalAmountAndPriceCommodities,
  -- journalAmountStyles,
  -- overJournalAmounts,
  -- traverseJournalAmounts,
  -- journalCanonicalCommodities,
  journalPayeesDeclared,
  journalPayeesUsed,
  journalPayeesDeclaredOrUsed,
  journalTagsDeclared,
  journalTagsUsed,
  journalTagsDeclaredOrUsed,
  journalAmounts,
  journalPostingAmounts,
  journalPostingAndCostAmounts,
  journalCommoditiesDeclared,
  journalCommoditiesUsed,
  journalCommodities,
  journalCommoditiesFromPriceDirectives,
  journalCommoditiesFromTransactions,
  journalBaseCurrency,
  journalBaseCurrencyCode,
  journalDateSpan,
  journalDateSpanBothDates,
  journalStartDate,
  journalEndDate,
  journalLastDay,
  journalDescriptions,
  journalFilePath,
  journalFilePaths,
  journalAllFilePaths,
  journalTransactionAt,
  journalNextTransaction,
  journalPrevTransaction,
  journalPostings,
  showJournalPostingAmountsDebug,
  journalTransactionsSimilarTo,
  -- * Account types
  journalAccountType,
  journalAccountTypes,
  journalAddAccountTypes,
  -- * Conversion accounts
  defaultBaseConversionAccount,
  journalAccountForType,
  journalBaseConversionAccount,
  journalConversionAccounts,
  -- * Gain accounts
  defaultGainAccount,
  journalBaseGainAccount,
  journalGainAccounts,
  -- * Misc
  nulljournal,
  journalConcat,
  journalNumberTransactions,
  journalNumberAndTieTransactions,
  journalTieTransactions,
  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.Containers.ListUtils (nubOrd)
import Data.Decimal (normalizeDecimal)
import Data.Default (Default(..))
import Data.Foldable (toList)
import Data.Function ((&), on)
import Data.List (find, intercalate, minimumBy, sortBy, union, unionBy, (\\))
#if !MIN_VERSION_base(4,20,0)
import Data.List (foldl')
#endif
import Data.List.Extra (nubSort)
import Data.Array qualified as A
import Data.Map.Strict qualified as M
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, listToMaybe, mapMaybe, maybeToList)
import Data.Ord (comparing)
import Data.Set qualified as S
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time.Calendar (Day(..), addDays, diffDays, fromGregorian)
import Data.Time.Clock.POSIX (POSIXTime)
import Data.Tree (Tree (..), flatten)
import Safe (headMay, headDef, maximumMay, minimumMay)
import System.FilePath (takeFileName)
import Text.Printf (printf)
import Text.Megaparsec (ParsecT)

import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.AccountName
import Hledger.Data.AccountType (accountTypeLotDirection, isEquityType, lotDirectionSign)
import Hledger.Data.Amount
import Hledger.Data.Currency (CurrencyCode, toCurrencyCode)
import Hledger.Data.Errors (makeAccountTagErrorExcerpt, makeCommodityTagErrorExcerpt)
import Hledger.Data.Posting
import Hledger.Data.Transaction
import Hledger.Data.TransactionModifier
import Hledger.Data.Valuation
import Hledger.Query


-- | 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 j
    | debugLevel < 3 = printf "Journal %s with %d transactions, %d accounts"
             (journalFilePath j)
             (length $ jtxns j)
             (length accounts)
    | debugLevel < 6 = printf "Journal %s with %d transactions, %d accounts: %s"
             (journalFilePath j)
             (length $ jtxns j)
             (length accounts)
             (show accounts)
    | otherwise = printf "Journal %s with %d transactions, %d accounts: %s, commodity styles: %s"
             (journalFilePath j)
             (length $ jtxns j)
             (length accounts)
             (show accounts)
             (show $ jinferredcommoditystyles j)
             -- ++ (show $ journalTransactions l)
             where accounts = filter (/= "root") $ flatten $ journalAccountNameTree j

journalDbg j@Journal{..} = chomp $ unlines $
  ("Journal " ++ takeFileName (journalFilePath j)++":") :  --  ++ " {"
  map (" "<>) [
   "jparsedefaultyear: "         <> shw jparsedefaultyear
  ,"jparseapplyyears: "          <> shw jparseapplyyears
  ,"jparsedefaultcommodity: "    <> shw jparsedefaultcommodity
  ,"jparsedecimalmark: "         <> shw jparsedecimalmark
  ,"jparseparentaccounts: "      <> shw jparseparentaccounts
  ,"jparsealiases: "             <> shw jparsealiases
  ,"jparseoptaliases: "          <> shw jparseoptaliases
  -- ,"jparsetimeclockentries: " <> shw jparsetimeclockentries
  ,"jparseincludefilestack: "    <> shw jparseincludefilestack
  ,"jdeclaredpayees: "           <> shw jdeclaredpayees
  ,"jdeclaredtags: "             <> shw jdeclaredtags
  ,"jdeclaredaccounts: "         <> shw jdeclaredaccounts
  ,"jdeclaredaccounttags: "      <> shw jdeclaredaccounttags
  ,"jdeclaredaccounttypes: "     <> shw jdeclaredaccounttypes
  ,"jaccounttypes: "             <> shw jaccounttypes
  ,"jdeclaredcommodities: "      <> shw jdeclaredcommodities
  ,"jinferredcommoditystyles: "  <> shw jinferredcommoditystyles
  ,"jglobalcommoditystyles: "    <> shw jglobalcommoditystyles
  ,"jpricedirectives: "          <> shw jpricedirectives
  ,"jinferredmarketprices: "     <> shw jinferredmarketprices
  ,"jtxnmodifiers: "             <> shw jtxnmodifiers
  -- ,"jperiodictxns: "          <> shw jperiodictxns
  ,"jasserts: "                  <> shw jasserts
  ,"jtxns: "                     <> shw jtxns
  ,"jitems: "                    <> shw (length jitems)
  ,"jfiles: "                    <> shw jfiles
  ,"jauxfiles: "                 <> shw jauxfiles
  ,"jlastreadtime: "             <> shw jlastreadtime
  ]
  -- ++ ["}"]
  where
    shw :: Show a => a -> String
    shw = show
    -- shw = pshow

-- 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 j1 <> j2 = j1 `journalConcat` 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 j1 j2 =
  let
    f1 = takeFileName $ journalFilePath j1
    f2 = maybe "(unknown)" takeFileName $ fmap fst $ headMay $ jparseincludefilestack j2  -- XXX more accurate than journalFilePath for some reason
  in
    dbgJournalAcctDeclOrder ("journalConcat: " <> f1 <> " <> " <> f2 <> ", acct decls renumbered: ") $
    journalRenumberAccountDeclarations $
    dbgJournalAcctDeclOrder ("journalConcat: " <> f1 <> " <> " <> f2 <> ", acct decls           : ") $
    Journal {
     jparsedefaultyear          = jparsedefaultyear          j2
    ,jparseapplyyears           = jparseapplyyears           j2
    ,jparsedefaultcommodity     = jparsedefaultcommodity     j2
    ,jparsedecimalmark          = jparsedecimalmark          j2
    ,jparseparentaccounts       = jparseparentaccounts       j2
    ,jparsealiases              = jparsealiases              j2
    ,jparseoptaliases           = jparseoptaliases           j2
    -- ,jparsetransactioncount     = jparsetransactioncount     j1 +  jparsetransactioncount     j2
    ,jparsetimeclockentries     = jparsetimeclockentries     j1 <> jparsetimeclockentries     j2
    ,jparseincludefilestack     = jparseincludefilestack j2
    ,jparsepos                  = jparsepos j2
    ,jparseamountstyles         = jparseamountstyles j2
    ,jparsetexts                = jparsetexts j2
    ,jparsefastpathstats        = jparsefastpathstats j1 <> jparsefastpathstats j2
    ,jdeclaredpayees            = jdeclaredpayees            j1 <> jdeclaredpayees            j2
    ,jdeclaredtags              = jdeclaredtags              j1 <> jdeclaredtags              j2
    ,jdeclaredaccounts          = jdeclaredaccounts          j1 <> jdeclaredaccounts          j2
    --
    -- The next six fields are Maps, which need to be merged carefully for correct semantics,
    -- especially the first two, which have list values. There may still be room for improvement here.
    --
    -- ,jdeclaredaccounttags   :: M.Map AccountName [Tag]
    -- jdeclaredaccounttags can have multiple duplicated/conflicting values for an account's tag.
    ,jdeclaredaccounttags       = M.unionWith (<>) (jdeclaredaccounttags j1) (jdeclaredaccounttags j2)
    --
    -- ,jdeclaredaccounttypes  :: M.Map AccountType [AccountName]
    -- jdeclaredaccounttypes can have multiple duplicated/conflicting values for an account's type.
    ,jdeclaredaccounttypes      = M.unionWith (<>) (jdeclaredaccounttypes j1) (jdeclaredaccounttypes j2)
    --
    -- ,jaccounttypes          :: M.Map AccountName AccountType
    -- jaccounttypes has a single type for any given account. When it had multiple type declarations, the last/rightmost wins.
    ,jaccounttypes              = M.unionWith (const id) (jaccounttypes j1) (jaccounttypes j2)
    --
    -- ,jglobalcommoditystyles :: M.Map CommoditySymbol AmountStyle
    ,jglobalcommoditystyles     = (<>) (jglobalcommoditystyles j1) (jglobalcommoditystyles j2)
    --
    -- ,jdeclaredcommodities           :: M.Map CommoditySymbol Commodity
    -- A commodity declared in both is combined, preferring j1's declaration, see commodityCombine.
    -- (When merging an included file, j1 is the included file, whose declarations are the later ones.
    -- Files read with several -f options are combined differently, see Hledger.Read.combineCommodityDeclarations.)
    ,jdeclaredcommodities               = M.unionWith commodityCombine (jdeclaredcommodities j1) (jdeclaredcommodities j2)
    --
    -- ,jdeclaredcommoditytags     :: M.Map CommoditySymbol [Tag]
    ,jdeclaredcommoditytags     = M.unionWith (<>) (jdeclaredcommoditytags j1) (jdeclaredcommoditytags j2)
    --
    -- ,jinferredcommoditystyles   :: M.Map CommoditySymbol AmountStyle
    ,jinferredcommoditystyles       = (<>) (jinferredcommoditystyles j1) (jinferredcommoditystyles j2)
    --
    --
    ,jpricedirectives           = jpricedirectives           j1 <> jpricedirectives           j2
    ,jinferredmarketprices      = jinferredmarketprices      j1 <> jinferredmarketprices      j2
    ,jtxnmodifiers              = jtxnmodifiers              j1 <> jtxnmodifiers              j2
    ,jperiodictxns              = jperiodictxns              j1 <> jperiodictxns              j2
    ,jasserts                   = jasserts                   j1 <> jasserts                   j2
    ,jtxns                      = jtxns                      j1 <> jtxns                      j2
    ,jitems                     = jitems                     j1 <> jitems                     j2
    ,jfiles                     = jfiles                     j1 <> jfiles                     j2
    ,jauxfiles                  = jauxfiles                  j1 <> jauxfiles                  j2
    ,jlastreadtime              = max (jlastreadtime j1) (jlastreadtime 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 j = j{jdeclaredaccounts=jdas'}
  where
    jdas' = [(a, adi{adideclarationorder=n}) | (n, (a,adi)) <- zip [1..] $ jdeclaredaccounts 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 7+).
dbgJournalAcctDeclOrder :: String -> Journal -> Journal
dbgJournalAcctDeclOrder prefix =
  dbg7With ((prefix++) . showAcctDeclsSummary . jdeclaredaccounts)
  where
    showAcctDeclsSummary :: [(AccountName,AccountDeclarationInfo)] -> String
    showAcctDeclsSummary adis
      | length adis < (2*n+2) = "[" <> showadis adis <> "]"
      | otherwise =
          "[" <> showadis (take n adis) <> " ... " <> showadis (takelast n adis) <> "]"
      where
        n = 3
        showadis = intercalate ", " . map showadi
        showadi (a,adi) = "("<>show (adideclarationorder adi)<>","<>T.unpack a<>")"
        takelast n' = reverse . take n' . reverse

instance Default Journal where
  def = nulljournal

nulljournal :: Journal
nulljournal = Journal {
   jparsedefaultyear          = Nothing
  ,jparseapplyyears           = []
  ,jparsedefaultcommodity     = Nothing
  ,jparsedecimalmark          = Nothing
  ,jparseparentaccounts       = []
  ,jparsealiases              = []
  ,jparseoptaliases           = []
  -- ,jparsetransactioncount     = 0
  ,jparsetimeclockentries     = []
  ,jparseincludefilestack     = []
  ,jparsepos                  = Nothing
  ,jparseamountstyles         = S.empty
  ,jparsetexts                = S.empty
  ,jparsefastpathstats        = mempty
  ,jdeclaredpayees            = []
  ,jdeclaredtags              = []
  ,jdeclaredaccounts          = []
  ,jdeclaredaccounttags       = M.empty
  ,jdeclaredaccounttypes      = M.empty
  ,jaccounttypes              = M.empty
  ,jglobalcommoditystyles     = M.empty
  ,jdeclaredcommodities       = M.empty
  ,jdeclaredcommoditytags     = M.empty
  ,jinferredcommoditystyles   = M.empty
  ,jpricedirectives           = []
  ,jinferredmarketprices      = []
  ,jtxnmodifiers              = []
  ,jperiodictxns              = []
  ,jasserts                   = []
  ,jtxns                      = []
  ,jitems                     = []
  ,jfiles                     = []
  ,jauxfiles                  = []
  ,jlastreadtime              = 0
  }

journalFilePath :: Journal -> FilePath
journalFilePath = jfpath . mainfile

journalFilePaths :: Journal -> [FilePath]
journalFilePaths = map jfpath . jfiles

-- | All the files this journal's data was read from: its data files
-- (journalFilePaths) and any auxiliary files such as CSV rules files (jauxfiles).
-- These are the files to watch for changes when reloading.
-- Each file is listed once, even if it was read more than once
-- (eg a file included twice, or a rules file used by several CSV files).
journalAllFilePaths :: Journal -> [FilePath]
journalAllFilePaths j = nubOrd $ journalFilePaths j <> concatMap snd (jauxfiles j)

mainfile :: Journal -> JournalFile
mainfile = headDef (JournalFile "(unknown)" "" 0) . jfiles

addTransaction :: Transaction -> Journal -> Journal
addTransaction t j@Journal{jtxns=ts} = let ts' = t : ts in ts' `seq` j { jtxns = ts' }

-- | Add a journal item, evaluated first (its fields are strict) so no parse-time thunks are retained.
-- Consecutive blank line items are collapsed into one.
addJournalItem :: JournalItem -> Journal -> Journal
addJournalItem JIBlank j@Journal{jitems=JIBlank:_} = j
addJournalItem i j@Journal{jitems=is} = let is' = i : is in i `seq` is' `seq` j { jitems = is' }

-- | Add a transaction parsed from a journal file, and a placeholder item for it.
-- Any comment lines immediately preceding it (the JIComment items on top of jitems)
-- are moved into the transaction's tprecedingcomment.
addTransactionItem :: Transaction -> Journal -> Journal
addTransactionItem t j@Journal{jitems=is} =
  -- strict pattern matching here, to avoid leaving thunks in jitems
  case span isCommentItem is of
    ([], _)    -> addTransactionAndItem t j
    (cs, rest) -> rest `seq` addTransactionAndItem t' j{jitems=rest}
      where t' = txnTieKnot t{tprecedingcomment = T.concat $ reverse [c | JIComment c <- cs]}
  where
    addTransactionAndItem t' = addJournalItem (JITransaction $ fst $ tsourcepos t') . addTransaction t'
    isCommentItem JIComment{} = True
    isCommentItem _           = False

addTransactionModifier :: TransactionModifier -> Journal -> Journal
addTransactionModifier mt j = j { jtxnmodifiers = mt : jtxnmodifiers j }

addPeriodicTransaction :: PeriodicTransaction -> Journal -> Journal
addPeriodicTransaction pt j = j { jperiodictxns = pt : jperiodictxns j }

deriving instance Show AssertDirective

addAssertDirective :: AssertDirective -> Journal -> Journal
addAssertDirective ad j = j { jasserts = ad : jasserts j }

addPriceDirective :: PriceDirective -> Journal -> Journal
addPriceDirective h j@Journal{jpricedirectives=hs} = let hs' = h : hs in hs' `seq` j { jpricedirectives = hs' }  -- 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{jtxns=ts} i =
  -- it's probably ts !! (i+1), but we won't assume
  headMay [t | t <- ts, tindex t == i]

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

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

-- | All postings from this journal's transactions, in order.
journalPostings :: Journal -> [Posting]
journalPostings = concatMap tpostings . jtxns

-- | All posting amounts from this journal, in order.
journalPostingAmounts :: Journal -> [MixedAmount]
journalPostingAmounts = map pamount . journalPostings

-- | Show the journal posting amounts rendered, suitable for debug logging.
showJournalPostingAmountsDebug :: Journal -> String
showJournalPostingAmountsDebug = show . map showMixedAmountOneLine . journalPostingAmounts

-- | All raw amounts used in this journal's postings and costs,
-- with MixedAmounts flattened, in parse order.
journalPostingAndCostAmounts :: Journal -> [Amount]
journalPostingAndCostAmounts = concatMap getAmounts . concatMap (amountsRaw . pamount) . journalPostings

-- | All raw amounts appearing in this journal, with MixedAmounts flattened, in no particular order.
-- (Including from posting amounts, cost amounts, P directives, and the last D directive.)
journalAmounts :: Journal -> S.Set Amount
journalAmounts = S.fromList . journalStyleInfluencingAmounts True

-- | Sorted unique commodity symbols declared by commodity directives in this journal.
journalCommoditiesDeclared :: Journal -> [CommoditySymbol]
journalCommoditiesDeclared = M.keys . jdeclaredcommodities

-- | Sorted unique commodity symbols used anywhere in this journal, including
-- commodity directives, P directives, the last D directive, posting amounts and cost amounts.
journalCommoditiesUsed :: Journal -> [CommoditySymbol]
journalCommoditiesUsed j = S.elems $
  journalCommoditiesFromPriceDirectives j <>
  (S.fromList $ map acommodity $ journalStyleInfluencingAmounts True j)

-- | Sorted unique commodity symbols mentioned anywhere in this journal.
-- (Including commodity directives, P directives, the last D directive, posting amounts and cost amounts.)
journalCommodities :: Journal -> S.Set CommoditySymbol
journalCommodities j =
     M.keysSet (jdeclaredcommodities j)
  <> journalCommoditiesFromPriceDirectives j
  <> S.fromList (map acommodity $ journalStyleInfluencingAmounts True j)

-- | Sorted unique commodity symbols mentioned in this journal's P directives.
journalCommoditiesFromPriceDirectives :: Journal -> S.Set CommoditySymbol
journalCommoditiesFromPriceDirectives = S.fromList . concatMap pdcomms . jpricedirectives
  where pdcomms pd = [pdcommodity pd, acommodity $ pdamount pd]

-- | Sorted unique commodity symbols used in transactions, in either posting or cost amounts.
journalCommoditiesFromTransactions :: Journal -> S.Set CommoditySymbol
journalCommoditiesFromTransactions j = S.fromList $ map acommodity $ journalPostingAndCostAmounts j

-- | Guess a base currency for this journal: Just the commodity symbol
-- as used in the journal, and its ISO 4217 currency code if one is known
-- (otherwise the symbol again), choosing as follows:
-- 1. The "to" commodity that appears most often in P (price) directives, if any.
-- 2. Otherwise, the commodity that appears most often in posting and cost amounts.
-- 3. Otherwise, Nothing.
-- Commodity symbols are normalised to ISO 4217 codes where possible,
-- so that equivalent symbols are tallied together; the symbol returned
-- is the first-occurring one that normalises to the winning code.
-- Ties are broken by first occurrence order.
journalBaseCurrency :: Journal -> Maybe (CommoditySymbol, CurrencyCode)
journalBaseCurrency j = pick priceTargetComms <|> pick postingAndCostComms
  where
    pick syms = do
      code <- mostFrequent $ map toCurrencyCode syms
      sym  <- find ((== code) . toCurrencyCode) syms
      Just (sym, code)
    priceTargetComms    = map (acommodity . pdamount) $ jpricedirectives j
    postingAndCostComms = map acommodity $ journalPostingAndCostAmounts j

    -- Most frequent element, ties broken by first-occurrence order.
    -- A single pass for efficiency: each map entry stores (negate count, first index),
    -- so a minimumBy on those tuples picks most-frequent, earliest-first.
    -- The bang on c forces accumulation strictly to avoid thunk chains.
    mostFrequent :: Ord a => [a] -> Maybe a
    mostFrequent [] = Nothing
    mostFrequent xs = Just $ fst $ minimumBy (comparing snd) $ M.toList stats
      where
        stats             = foldl' bump M.empty (zip [0::Int ..] xs)
        bump m (i, x)     = M.insertWith combine x (-1, i) m
        combine _ (!c, i) = (c - 1, i)

-- | The ISO 4217 currency code of this journal's guessed base currency
-- ('journalBaseCurrency'), defaulting to "USD".
journalBaseCurrencyCode :: Journal -> CurrencyCode
journalBaseCurrencyCode = maybe "USD" snd . journalBaseCurrency

-- | Unique transaction descriptions used in this journal.
journalDescriptions :: Journal -> [Text]
journalDescriptions = nubSort . map tdescription . jtxns

-- | Sorted unique payees declared by payee directives in this journal.
journalPayeesDeclared :: Journal -> [Payee]
journalPayeesDeclared = nubSort . map fst . jdeclaredpayees

-- | Sorted unique payees used by transactions in this journal.
journalPayeesUsed :: Journal -> [Payee]
journalPayeesUsed = nubSort . map transactionPayee . jtxns

-- | Sorted unique payees used in transactions or declared by payee directives in this journal.
journalPayeesDeclaredOrUsed :: Journal -> [Payee]
journalPayeesDeclaredOrUsed j = toList $ foldMap S.fromList
    [journalPayeesDeclared j, journalPayeesUsed j]

-- | Sorted unique tag names declared by tag directives in this journal.
journalTagsDeclared :: Journal -> [TagName]
journalTagsDeclared = nubSort . map fst . jdeclaredtags

-- | Sorted unique tag names used in this journal (in account directives, transactions, postings..)
journalTagsUsed :: Journal -> [TagName]
journalTagsUsed j = nubSort $ map fst $ concatMap transactionAllTags $ jtxns j
  -- tags used in all transactions and postings and postings' accounts

-- | Sorted unique tag names used in transactions or declared by tag directives in this journal.
journalTagsDeclaredOrUsed :: Journal -> [TagName]
journalTagsDeclaredOrUsed j = toList $ foldMap S.fromList
    [journalTagsDeclared j, journalTagsUsed j]

-- | Sorted unique account names posted to by this journal's transactions.
journalAccountNamesUsed :: Journal -> [AccountName]
journalAccountNamesUsed = accountNamesFromPostings . journalPostings

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

-- | Sorted unique account names declared by account directives in this journal.
journalAccountNamesDeclared :: Journal -> [AccountName]
journalAccountNamesDeclared = nubSort . map fst . jdeclaredaccounts

-- | Sorted unique account names declared by account directives or posted to
-- by transactions in this journal.
journalAccountNamesDeclaredOrUsed :: Journal -> [AccountName]
journalAccountNamesDeclaredOrUsed j = toList $ foldMap S.fromList
    [journalAccountNamesDeclared j, journalAccountNamesUsed 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 j = toList $ foldMap S.fromList
    [journalAccountNamesDeclared j, expandAccountNames $ journalAccountNamesUsed j]

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

-- | Sorted unique account names declared or implied in this journal
-- which have no children.
journalLeafAccountNames :: Journal -> [AccountName]
journalLeafAccountNames = treeLeaves . journalAccountNameTree

journalAccountNameTree :: Journal -> Tree AccountName
journalAccountNameTree = accountNameTreeFrom . journalAccountNamesDeclaredOrImplied

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

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

type DateWeightedSimilarityScore = Double
type SimilarityScore = Double
type TimeDistance = Integer

-- | Find up to N most similar and nearest-dated transactions matching
-- the given transaction description and query and exceeding the given
-- description similarity score (0 to 1, see compareDescriptions).
-- The provided Day (today) is used as the reference date;
-- transactions are penalised by their absolute distance from it in days
-- (both past and future).
-- Returns transactions along with
-- their distance in days from today,
-- their description similarity score,
-- and a heuristically date-weighted variant of this that favours nearby transactions.
journalTransactionsSimilarTo :: Journal -> Day -> Text -> Query -> SimilarityScore -> Int
  -> [(DateWeightedSimilarityScore, TimeDistance, SimilarityScore, Transaction)]
journalTransactionsSimilarTo Journal{jtxns} today desc q similaritythreshold n =
  take n $
  dbg1With (
    unlines .
    ("up to 30 transactions above description similarity threshold "<>show similaritythreshold<>" ordered by proximity-weighted similarity:":) .
    take 30 .
    map ( \(w,d,s,Transaction{..}) -> printf "weighted:%8.3f  distance:%4d similarity:%5.3f  %s %s" w d s (show tdate) tdescription )) $
  sortBy (comparing (negate.first4)) $
  map (\(s,t) -> (weightedScore (s,t), timedistance t, s, t)) $
  filter ((> similaritythreshold).fst)
  [(compareDescriptions desc $ tdescription t, t) | t <- jtxns, q `matchesTransaction` t]
  where
    timedistance t = abs $ diffDays today (tdate t)
    -- Combine similarity and time distance heuristically. This gave decent results
    -- in my "find most recent invoice" use case in 2023-03,
    -- but will probably need more attention.
    weightedScore :: (Double, Transaction) -> Double
    weightedScore (s, t) = 100 * s - fromIntegral (timedistance t) / 4

-- | 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 a b =
  (if a `T.isInfixOf` b then (0.5+) else id) $
  compareStrings (simplify a) (simplify b)
  where
    simplify = T.unpack . T.filter (not.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 "" "" = 1
compareStrings [_] "" = 0
compareStrings "" [_] = 0
compareStrings [a] [b] = if toUpper a == toUpper b then 1 else 0
compareStrings s1 s2 = 2 * commonpairs / totalpairs
  where
    pairs1      = S.fromList $ wordLetterPairs $ uppercase s1
    pairs2      = S.fromList $ wordLetterPairs $ uppercase s2
    commonpairs = fromIntegral $ S.size $ S.intersection pairs1 pairs2
    totalpairs  = fromIntegral $ S.size pairs1 + S.size pairs2

wordLetterPairs :: String -> [String]
wordLetterPairs = concatMap letterPairs . words

letterPairs :: String -> [String]
letterPairs (a:b:rest) = [a,b] : letterPairs (b:rest)
letterPairs _ = []

-- Newer account type code.

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

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

-- | An account type inherited from the parent account(s),
-- and whether it was originally declared by an account directive (true) or inferred from an account name (false).
type ParentAccountType = (AccountType, Bool)

-- | 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 j = M.fromList [(a,acctType) | (a, Just (acctType,_)) <- flatten t']
  where
    t = accountNameTreeFrom $ journalAccountNames j :: Tree AccountName
    -- Traverse downward through the account tree, applying any explicitly declared account types,
    -- otherwise inferring account types from account names when possible, and propagating account types downward.
    -- Declared account types (possibly inherited from parent) are preferred, inferred types are used as a fallback.
    t' = setTypeHereAndBelow Nothing t :: Tree (AccountName, Maybe (AccountType, Bool))
      where
        declaredtypes       = M.keys $ jdeclaredaccounttypes j
        declaredtypesbyname = journalDeclaredAccountTypes j & fmap (,True)
        setTypeHereAndBelow :: Maybe ParentAccountType -> Tree AccountName -> Tree (AccountName, Maybe ParentAccountType)
        setTypeHereAndBelow mparenttype (Node a subs) = Node (a, mnewtype) (map (setTypeHereAndBelow mnewtype) subs)
          where
            mnewtype = mthisacctdeclaredtype <|> mparentacctdeclaredtype <|> mthisacctinferredtype <|> mparentacctinferredtype
              where
                mthisacctdeclaredtype   = M.lookup a declaredtypesbyname
                mparentacctdeclaredtype = if       fromMaybe False $ snd <$> mparenttype then mparenttype else Nothing
                mparentacctinferredtype = if not $ fromMaybe True  $ snd <$> mparenttype then mparenttype else Nothing
                mthisacctinferredtype   = accountNameInferTypeExcept declaredtypes a & fmap (,False)  -- XXX not sure about this Except logic.. but for now, tests pass

-- | Build a map from account names to explicitly declared account types.
journalDeclaredAccountTypes :: Journal -> M.Map AccountName AccountType
journalDeclaredAccountTypes Journal{jdeclaredaccounttypes} =
  M.fromList $ concat [map (,t) as | (t,as) <- M.toList jdeclaredaccounttypes]

-- | To all postings in the journal, add any tags from their account
-- (including those inherited from parent accounts).
-- Tags are added to ptags (making them queryable) but not to pcomment (so they don't appear in print output).
-- If a tag already exists on the posting, it is not changed (the account tag will be ignored).
journalPostingsAddAccountTags :: Journal -> Journal
journalPostingsAddAccountTags j
  | M.null (jdeclaredaccounttags j) = j  -- no account tags declared, nothing to add
  | M.null inheritedtagsbyaccount   = j  -- no posted-to account has any, nothing to add
  | otherwise = journalMapPostings addtags j
  where
    -- postings whose account has no tags are left as they are (no new posting is built)
    addtags p = maybe p (postingAddTags p) $ M.lookup (paccount p) inheritedtagsbyaccount
    -- the inherited tags of each posted-to account which has some, calculated once per account
    inheritedtagsbyaccount = M.fromList
      [(a, ts) | a <- journalAccountNamesUsed j, let ts = journalInheritedAccountTags j a, not $ null ts]

-- | Remove all tags from the journal's postings except those provided by their account.
-- This is useful for the accounts report.
-- It does not remove tag declarations from the posting comments.
journalPostingsKeepAccountTagsOnly :: Journal -> Journal
journalPostingsKeepAccountTagsOnly j = journalMapPostings keepaccounttags j
  where keepaccounttags p = p{ptags=[]} `postingAddTags` (journalInheritedAccountTags j $ paccount p)

-- | Get any tags declared for this commodity.
journalCommodityTags :: Journal -> CommoditySymbol -> [Tag]
journalCommodityTags Journal{jdeclaredcommoditytags} c =
  M.findWithDefault [] c jdeclaredcommoditytags

-- | Does this commodity have a 'lots:' tag declared ?
journalCommodityUsesLots :: Journal -> CommoditySymbol -> Bool
journalCommodityUsesLots j c = any ((== "lots") . T.toLower . fst) (journalCommodityTags j c)

-- | All commodities with a 'lots:' tag declared.
journalLotfulCommodities :: Journal -> S.Set CommoditySymbol
journalLotfulCommodities j@Journal{jdeclaredcommoditytags} =
  S.filter (journalCommodityUsesLots j) (M.keysSet jdeclaredcommoditytags)

-- | The declared lots: tag values by account (the first value, if an
-- account somehow has several).
journalAccountLotsTags :: Journal -> M.Map AccountName Text
journalAccountLotsTags Journal{jdeclaredaccounttags} =
  M.mapMaybe (\tags -> listToMaybe [v | (k, v) <- tags, T.toLower k == "lots"]) jdeclaredaccounttags

-- | Does this account opt out of lot tracking, via a NONE-valued lots: tag
-- on its own or an ancestor's declaration ? The nearest declaration wins,
-- so a subaccount can re-enable tracking with its own lots: method tag.
-- Postings with explicit cost basis annotations are still lot-tracked
-- regardless (the more specific declaration wins).
journalAccountUsesNoLots :: Journal -> AccountName -> Bool
journalAccountUsesNoLots = accountUsesNoLotsWith . journalAccountLotsTags

-- | Like journalAccountUsesNoLots, but taking the 'journalAccountLotsTags'
-- map (useful where no Journal is at hand, eg during balancing).
accountUsesNoLotsWith :: M.Map AccountName Text -> AccountName -> Bool
accountUsesNoLotsWith lotstags a =
  case [v | a' <- a : parentAccountNames a, Just v <- [M.lookup a' lotstags]] of
    (v:_) -> isNoneLotsTagValue v
    []    -> False

-- | Is this lots: tag value the special NONE value (case insensitive),
-- valid on account declarations to opt out of lot tracking ?
isNoneLotsTagValue :: Text -> Bool
isNoneLotsTagValue v = T.toUpper (T.strip v) == "NONE"

-- | Get the reduction method from a commodity's lots: tag value, if any.
journalCommodityLotsMethod :: Journal -> CommoditySymbol -> Maybe ReductionMethod
journalCommodityLotsMethod j c =
  case [v | (k, v) <- journalCommodityTags j c, T.toLower k == "lots"] of
    (v:_) -> parseReductionMethod v
    []    -> Nothing

-- | Get the reduction method from a posting's lots: tag value (typically inherited from its account), if any.
postingLotsMethod :: Posting -> Maybe ReductionMethod
postingLotsMethod p =
  case [v | (k, v) <- ptags p, T.toLower k == "lots"] of
    (v:_) -> parseReductionMethod v
    []    -> Nothing

-- | Parse a reduction method name from a lots: tag value.
parseReductionMethod :: Text -> Maybe ReductionMethod
parseReductionMethod t = case T.toUpper (T.strip t) of
  "FIFO"       -> Just FIFO
  "LIFO"       -> Just LIFO
  "HIFO"       -> Just HIFO
  "AVERAGE"    -> Just AVERAGE
  "SPECID"     -> Just SPECID
  "FIFOALL"    -> Just FIFOALL
  "LIFOALL"    -> Just LIFOALL
  "HIFOALL"    -> Just HIFOALL
  "AVERAGEALL" -> Just AVERAGEALL
  _            -> Nothing

-- | Check that all lots: tag values on commodity and account declarations are recognised.
-- On a commodity declaration, an empty value (bare @lots:@ tag) is valid,
-- declaring the commodity lotful with the default FIFO reduction method;
-- a non-empty value must be one of the known reduction methods.
-- On an account declaration, the tag only sets the reduction method for
-- lotful commodities in that account, so a method value is required.
journalCheckLotsTagValues :: Journal -> Either String Journal
journalCheckLotsTagValues j = do
  mapM_ checkCommodity (M.toList $ jdeclaredcommoditytags j)
  mapM_ checkAccount   (jdeclaredaccounts j)
  Right j
  where
    methods = "FIFO, LIFO, HIFO, AVERAGE, SPECID, FIFOALL, LIFOALL, HIFOALL, AVERAGEALL"

    unrecognisedmsg :: String
    unrecognisedmsg = unlines [
       "%s:%d:"
      ,"%s"
      ,"unrecognised lots: tag value %s."
      ,"Use " ++ methods ++ ", or nothing (meaning FIFO)"
      ]

    valuelessmsg :: String
    valuelessmsg = unlines [
       "%s:%d:"
      ,"%s"
      ,"An account lots: tag sets the disposal order for lot-tracked commodities there,"
      ,"so it needs a value, one of " ++ methods ++ ";"
      ,"or NONE, to disable lot tracking in this account."
      ,"(A commodity lots: tag enables lot tracking, and can also set the disposal order.)"
      ]

    nonecommoditymsg :: String
    nonecommoditymsg = unlines [
       "%s:%d:"
      ,"%s"
      ,"lots: NONE is not supported on commodity declarations."
      ,"To disable lot tracking of this commodity in particular accounts,"
      ,"add a lots: NONE tag to those accounts' declarations instead;"
      ,"to disable it everywhere, remove the commodity's lots: tag."
      ]

    checkCommodity (sym, tags) =
      case M.lookup sym (jdeclaredcommodities j) of
        Just comm -> mapM_ (checkCommodityTag comm) tags
        Nothing   -> Right ()

    checkCommodityTag comm (k, v)
      | T.toLower k /= "lots"       = Right ()
      | T.null (T.strip v)          = Right ()
      | isNoneLotsTagValue v        = Left $ printf nonecommoditymsg f l ex
      | Just _ <- parseReductionMethod v = Right ()
      | otherwise = Left $ printf unrecognisedmsg f l ex (show v)
          where (f, l, _mcols, ex) = makeCommodityTagErrorExcerpt comm k

    checkAccount (acctName, adi) =
      mapM_ (checkAccountTag acctName adi) (aditags adi)

    checkAccountTag acctName adi (k, v)
      | T.toLower k /= "lots"       = Right ()
      | T.null (T.strip v)          = Left $ printf valuelessmsg f l ex
      | isNoneLotsTagValue v        = Right ()
      | Just _ <- parseReductionMethod v = Right ()
      | otherwise = Left $ printf unrecognisedmsg f l ex (show v)
          where (f, l, _mcols, ex) = makeAccountTagErrorExcerpt (acctName, adi) k

-- | To all postings in the journal, add any tags from their amount's commodities.
-- Tags are added to ptags (making them queryable) but not to pcomment (so they don't appear in print output).
-- If a tag already exists on the posting, it is not changed (the commodity tag will be ignored).
journalPostingsAddCommodityTags :: Journal -> Journal
journalPostingsAddCommodityTags j
  | M.null taggedcommodities = j  -- no commodity tags declared, nothing to add
  | otherwise = journalMapPostings addtags j
  where
    taggedcommodities = M.filter (not . null) $ jdeclaredcommoditytags j
    -- postings whose commodities have no tags are left as they are (no new posting is built)
    addtags p = case concatMap (\c -> M.findWithDefault [] c taggedcommodities) (postingCommodities p) of
      []   -> p
      tags -> p `postingAddTags` tags

-- | For lot-opening postings with a cost basis (positive; or negative in a
-- liability account, which holds short lots), which don't look like lot
-- transfer destinations, infer transacted cost from cost basis. This runs
-- before transaction balancing (the inferred cost lets an acquire entry with
-- an elided cash amount balance at cost), so lot classification hasn't
-- happened yet; transfer destinations - which must not get a transacted
-- cost - are recognised by shape: a positive cost-basis posting is skipped
-- when the transaction has an explicit negative amount of the same commodity
-- and quantity in another account, or unpriced in the same account (a
-- transfer-from counterpart), or the
-- commodity's unpriced negative and positive quantities sum to matching
-- totals (a split or consolidating transfer group, possibly minus a fee),
-- or an equity posting with no cost-basis amounts (an equity transfer, eg
-- close --clopen --lots style opening balances).
journalInferPostingsTransactedCost :: Journal -> Journal
journalInferPostingsTransactedCost j = journalMapTransactions inferTxn j
  where
    inferTxn t = t{tpostings = map (postingInferTransactedCost t) (tpostings t)}

    postingInferTransactedCost t p
      | not (any needsInference $ amounts $ pamount p) = p  -- nothing to infer
      | hasEquityCounterpart t = p                          -- equity transfer: not for transfer postings
      | otherwise = p'{poriginal = Just $ originalPosting p}
      where
        p' = p{pamount = mapMixedAmount amountInferTransactedCost $ pamount p}
        -- a lot inflow: positive, or negative in a liability account (a short lot)
        -- (the cheap checks first; few amounts have a cost basis, and multiplying Decimals is slow)
        needsInference a = hasCostBasisCost a && isNothing (acost a) && aquantity a * lotsign > 0
                        && not (hasTransferFromCounterpart t p a)
                        && not (hasTransferGroupShape t p (acommodity a))
        lotsign = maybe 1 lotDirectionSign (journalAccountType j (paccount p) >>= accountTypeLotDirection)
        amountInferTransactedCost a
          | needsInference a, Just CostBasis{cbCost=Just c} <- acostbasis a = a{acost = Just (UnitCost c)}
          | otherwise = a
        hasCostBasisCost a = case acostbasis a of
          Just CostBasis{cbCost=Just _} -> True
          _ -> False

    -- Does another posting have an explicit negative amount of this commodity
    -- and quantity, in a different account, or unpriced in the same account ?
    -- Then this looks like a transfer pair (mirroring the lot classifier's
    -- counterpart matching and same-account pairing).
    hasTransferFromCounterpart t p a =
      any (\q -> any (\qa -> acommodity qa == acommodity a && aquantity qa == negate (aquantity a)
                           && (paccount q /= paccount p || isNothing (acost qa)))
                     (amountsRaw (pamount q)))
          (tpostings t)

    -- Do the transaction's unpriced amounts in this commodity look like a
    -- split or consolidating transfer group (#2692) ? True when the total
    -- unpriced negative quantity equals the total unpriced positive quantity,
    -- is nonzero, and at least one negative is in a different account.
    -- Priced amounts are excluded on both sides: a priced posting (eg a fee
    -- disposal -0.02 A {$100} @ $100) is a deliberate trade, not part of the
    -- transfer.
    hasTransferGroupShape t p c =
      negTotal > 0 && negTotal == posTotal && any (/= paccount p) negAccts
      where
        unpricedAmts q = [a | a <- amountsRaw (pamount q), acommodity a == c, isNothing (acost a)]
        negs = [(paccount q, negate (aquantity a)) | q <- tpostings t, a <- unpricedAmts q, aquantity a < 0]
        posTotal = sum [aquantity a | q <- tpostings t, a <- unpricedAmts q, aquantity a > 0]
        negTotal = sum (map snd negs)
        negAccts = map fst negs

    -- Does the transaction have an equity posting with no cost-basis amounts ?
    -- (Mirrors the lot classifier's equity-transfer detection.)
    hasEquityCounterpart t =
      any (\q -> maybe False isEquityType (journalAccountType j (paccount q))
              && not (any (isJust . acostbasis) (amountsRaw (pamount q))))
          (tpostings t)

-- | The account name to use for postings which hledger generates to an account of the given type:
-- the first account declared with that type (by a type: tag), in declaration order;
-- or if there is none, the alphabetically first account which has that type
-- (inferred from its name, or a parent's);
-- or if there is none, the given default account name.
journalAccountForType :: AccountName -> AccountType -> Journal -> AccountName
journalAccountForType defaultacct atype j = headDef defaultacct $ declared ++ inferred
  where
    declaredtypes = journalDeclaredAccountTypes j
    declared = [a | (a,_) <- jdeclaredaccounts j, M.lookup a declaredtypes == Just atype]
    inferred = M.keys $ M.filter (==atype) $ jaccounttypes j

-- | The account name to use for conversion postings generated by --infer-equity
-- (see journalAccountForType; the default is equity:conversion).
journalBaseConversionAccount :: Journal -> AccountName
journalBaseConversionAccount = journalAccountForType defaultBaseConversionAccount Conversion

-- | All the accounts in this journal which are declared or inferred as V/Conversion type.
-- This does not include new account names which might be generated by --infer-equity, currently.
journalConversionAccounts :: Journal -> [AccountName]
journalConversionAccounts = M.keys . M.filter (==Conversion) . jaccounttypes

-- | The account name to use for inferred gain postings
-- (see journalAccountForType; the default is revenues:gain).
journalBaseGainAccount :: Journal -> AccountName
journalBaseGainAccount = journalAccountForType defaultGainAccount Gain

-- | All the accounts in this journal which are declared or inferred as G/Gain type, alphabetically.
journalGainAccounts :: Journal -> [AccountName]
journalGainAccounts = M.keys . M.filter (==Gain) . jaccounttypes


-- 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 q j@Journal{jtxns}
  | queryIsNull q = j  -- everything matches, nothing to do
  | otherwise = j{jtxns=filter (matchesTransactionExtra (journalAccountType j) q) jtxns}

-- | Keep only postings matching the query expression.
-- This can leave unbalanced transactions.
filterJournalPostings :: Query -> Journal -> Journal
filterJournalPostings q j@Journal{jtxns=ts}
  | queryIsNull q = j  -- everything matches, nothing to do
  | otherwise = j{jtxns=map (filterTransactionPostingsExtra (journalAccountType j) q) 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 q j@Journal{jtxns=ts} = j{jtxns=map (filterTransactionRelatedPostings q) 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 q j@Journal{jtxns=ts}
  | queryIsNull q = j  -- everything matches, nothing to do
  | otherwise = j{jtxns=map (filterTransactionAmounts q) 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 q t@Transaction{tpostings=ps} = t{tpostings=mapMaybe (filterPostingAmount q) 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 q p@Posting{pamount=as}
  | null newamt = Nothing
  | otherwise   = Just p{pamount=Mixed newamt}
  where
    Mixed newamt = filterMixedAmount (q `matchesAmount`) as

filterTransactionPostings :: Query -> Transaction -> Transaction
filterTransactionPostings q t@Transaction{tpostings=ps} = t{tpostings=filter (q `matchesPosting`) 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 atypes q t@Transaction{tpostings=ps} =
  t{tpostings=filter (matchesPostingExtra atypes q) ps}

filterTransactionRelatedPostings :: Query -> Transaction -> Transaction
filterTransactionRelatedPostings q t@Transaction{tpostings=ps} =
    t{tpostings=if null matches then [] else ps \\ matches}
  where matches = filter (matchesPosting q) ps

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

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

-- | Apply a transformation to a journal's posting amounts.
journalMapPostingAmounts :: (MixedAmount -> MixedAmount) -> Journal -> Journal
journalMapPostingAmounts f = journalMapPostings (postingTransformAmount 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 j =
  j {jfiles            = reverse $ jfiles j
    ,jdeclaredaccounts = reverse $ jdeclaredaccounts j
    ,jtxns             = reverse $ jtxns j
    ,jitems            = reverse $ jitems j
    ,jtxnmodifiers     = reverse $ jtxnmodifiers j
    ,jperiodictxns     = reverse $ jperiodictxns j
    ,jasserts          = reverse $ jasserts j
    ,jpricedirectives  = reverse $ jpricedirectives j
    }

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


-- | Number this journal's transactions, counting upward from 1, and tie their knots,
-- so that their postings refer to the renumbered transactions.
-- If they are already numbered that way, the journal is returned unchanged
-- (avoiding a new copy of every transaction).
journalNumberAndTieTransactions :: Journal -> Journal
journalNumberAndTieTransactions j@Journal{jtxns=ts}
  | and $ zipWith (\i t -> tindex t == i) [1..] ts = j
  | otherwise = journalTieTransactions $ journalNumberTransactions j

-- | Number (set the tindex field) this journal's transactions, counting upward from 1.
journalNumberTransactions :: Journal -> Journal
journalNumberTransactions j@Journal{jtxns=ts} = j{jtxns=zipWith (\i t -> t{tindex=i}) [1..] 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 j@Journal{jtxns=ts} = j{jtxns=map txnTieKnot 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 t@Transaction{tpostings=ps} = t{tpostings=map (\p -> p{ptransaction=Nothing}) 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.
-- The first argument selects whether to add visible tags to generated postings & modified transactions.
journalModifyTransactions :: Bool -> Day -> Journal -> Either String Journal
journalModifyTransactions verbosetags d j =
  case modifyTransactions
         (journalAccountType j)
         (journalInheritedAccountTags j)
         (journalCommodityStyles j)
         d verbosetags (jtxnmodifiers j) (jtxns j) of
    Right ts -> Right j{jtxns=ts}
    Left err -> Left err

-- | Apply this journal's commodity display styles to all of its posting amounts.
-- This does no display rounding, keeping decimal digits as they were;
-- it is suitable for an early cleanup pass before calculations.
-- Reports may want to do additional rounding/styling at render time.
-- Price directives' amounts are left as written (though they still influence the inferred styles),
-- so that error messages and `get`'s rewritten price files show them that way;
-- commands which list prices, like prices and print, style them when rendering.
-- This can return an error message eg if inconsistent number formats are found.
journalStyleAmounts :: Journal -> Either String Journal
journalStyleAmounts = fmap applystyles . journalInferCommodityStyles
  where
    applystyles j = journalMapPostings (postingShareStyles styles . styleAmounts styles) j
      where styles = journalCommodityStylesWith NoRounding j  -- defer rounding, in case of print --round=none

-- | A memory optimisation, used after applying commodity styles to a posting's amounts.
-- An amount whose display precision differs from its commodity's usual precision
-- (eg $5 when the usual is $5.25) ends up with its own copy of the commodity's style,
-- which differs only in precision. This replaces such copies with one shared copy
-- for each commodity and precision, from a table of each commodity's style at every
-- possible precision (whose entries are created only when first needed).
-- On a 100k-transaction journal with mixed precisions this saves about 3% of memory;
-- it doesn't change the run time noticeably.
postingShareStyles :: M.Map CommoditySymbol AmountStyle -> Posting -> Posting
postingShareStyles styles = postingTransformAmount $ \(Mixed m) -> Mixed $ M.map shareAmountStyle m  -- commodities are unchanged, so the map keys are too
  where
    sharedstyles = M.map (\s -> A.listArray (0, 255) [s{asprecision=Precision p} | p <- [0..255]]) styles

    shareAmountStyle a = a{astyle = shareStyle (acommodity a) (astyle a), acost = shareCostStyle <$> acost a}

    shareCostStyle (UnitCost a)  = UnitCost  $ shareAmountStyle a
    shareCostStyle (TotalCost a) = TotalCost $ shareAmountStyle a

    -- If this style is the same as a shared one, use the shared one.
    shareStyle comm s = case (asprecision s, M.lookup comm sharedstyles) of
      (Precision p, Just stylesbyprecision) | stylesbyprecision A.! p == s -> stylesbyprecision A.! p
      _ -> s

-- | 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 j =
  -- XXX could be some redundancy here, cf journalStyleInfluencingAmounts
  explicitstyles <> aliasstyles <> inferredstyles
  where
    explicitstyles        = globalstyles <> declaredstyles <> defaultcommoditystyle
    globalstyles          = jglobalcommoditystyles j
    declaredstyles        = M.mapMaybe cformat $ jdeclaredcommodities j
    defaultcommoditystyle = M.fromList $ catMaybes [jparsedefaultcommodity j]
    inferredstyles        = jinferredcommoditystyles j
    -- A commodity alias without a declared style of its own uses its commodity's number format
    -- (precision, decimal mark, digit groups), with the symbol placement of the amounts written
    -- with the alias, if any, otherwise of its commodity.
    aliasstyles = M.fromList
      [ (a, cst{ascommodityside = ascommodityside pst, ascommodityspaced = ascommodityspaced pst})
      | c <- M.elems (jdeclaredcommodities j)
      , Just cst <- [M.lookup (csymbol c) (explicitstyles <> inferredstyles)]
      , a <- commodityAliases c
      , let pst = M.findWithDefault cst a inferredstyles
      ]

-- | Like journalCommodityStyles, but attach a particular rounding strategy to the styles,
-- affecting how they will affect display precisions when applied.
journalCommodityStylesWith :: Rounding -> Journal -> M.Map CommoditySymbol AmountStyle
journalCommodityStylesWith r = amountStylesSetRounding r . journalCommodityStyles

-- | Collect and save inferred amount styles for each commodity based on
-- P directive amounts, posting amounts but not cost amounts, and maybe the last D amount, in that commodity.
-- Styles inferred previously for commodities which no longer appear in these are kept
-- (eg for commodity aliases, whose amounts have been converted, so that print can show them as written).
-- Can return an error message eg if inconsistent number formats are found.
journalInferCommodityStyles :: Journal -> Either String Journal
journalInferCommodityStyles j =
  Right j{jinferredcommoditystyles = dbg7 "journalInferCommodityStyles" $
            M.mapWithKey withExplicitPrecision allstyles `M.union` jinferredcommoditystyles j}
  where
    -- Styles are inferred from all amounts (for formatting like symbol placement,
    -- decimal mark, digit groups), but precision only from explicitly-written amounts:
    -- inferred balancing amounts must not raise the journal-wide display precision
    -- and surprise the user. Both style maps are accumulated in one traversal of the
    -- postings, with the directive amounts added last so that they take precedence.
    withExplicitPrecision sym s = maybe s (\s' -> s{asprecision = asprecision s'}) $ M.lookup sym explstyles
    allstyles  = addAmountStyles directiveamts allpostingstyles
    explstyles = addAmountStyles directiveamts explpostingstyles
    (allpostingstyles, explpostingstyles) = foldr addPostingStyles (mempty, mempty) $ journalPostings j
    addPostingStyles p (!allsts, !explsts) =
      (addAmountStyles amts allsts, if isExplicitAmount p then addAmountStyles amts explsts else explsts)
      where amts = amountsRaw $ pamount p
    addAmountStyles amts styles = foldr addAmountStyle styles amts
    -- The default commodity (D) amount and price directive amounts. Costs are not included.
    directiveamts = catMaybes (mdefaultcommodityamt : map (Just . pdamount) (jpricedirectives j))
    mdefaultcommodityamt =
      (\(sym,style) -> nullamt{acommodity=sym, astyle=style}) <$> jparsedefaultcommodity j

-- -- | 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 . UnitCost) $ 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 j =
  j{jinferredmarketprices =
       dbg4With (("jinferredmarketprices:\n"<>) . showMarketPrices) $
       map priceDirectiveToMarketPrice .
       concatMap postingPriceDirectivesFromCost $
       journalPostings j
   }

-- | Aliases declared on a commodity directive by its @alias:@ tags.
commodityAliases :: Commodity -> [CommoditySymbol]
commodityAliases = map (acommodity . fst) . caliases

-- | Update a commodity's declaration with a later declaration of it, in the same file tree.
-- The later declaration adds to the earlier one rather than replacing it:
-- aliases are combined, a display style replaces the earlier one only if the later declaration has one,
-- and the later tags come first (so they win, for tags looked up by their first value).
-- The comment and source position are the later declaration's.
commodityRedeclare :: Commodity -> Commodity -> Commodity
commodityRedeclare new old = new{
   cformat  = cformat new <|> cformat old
  ,ctags    = ctags new `union` ctags old
  ,caliases = caliases old `unionAliases` caliases new
  }

-- | Combine two declarations of a commodity from different journals (file trees, or a file and its included file),
-- the first being the later one. Like 'commodityRedeclare', this keeps the later one's comment, source position,
-- and display style if it has one, and the aliases (in reading order) and tags (the later one's first) of both.
commodityCombine :: Commodity -> Commodity -> Commodity
commodityCombine c1 c2 = c1{
   cformat  = cformat c1 <|> cformat c2
  ,ctags    = ctags c1 `union` ctags c2
  ,caliases = caliases c2 `unionAliases` caliases c1
  }

-- | Combine two lists of commodity aliases, the first being the earlier ones, keeping them in reading order
-- and dropping repeated definitions (same symbol and quantity).
-- An alias redefined with a different quantity is kept, so it can be reported as a conflict.
unionAliases :: [(Amount, SourcePos)] -> [(Amount, SourcePos)] -> [(Amount, SourcePos)]
unionAliases = unionBy ((==) `on` \(a, _) -> (acommodity a, aquantity a))

-- | All commodity symbols that this journal treats as declared:
-- both the canonical 'jdeclaredcommodities' keys and any 'alias:'
-- values declared on those commodity directives.
commoditiesAndAliases :: Journal -> S.Set CommoditySymbol
commoditiesAndAliases j =
  M.keysSet (jdeclaredcommodities j)
  <> S.fromList [ a
                | c <- M.elems (jdeclaredcommodities j)
                , a <- commodityAliases c
                ]

-- | Commodity aliases: each alias symbol, with its commodity, its quantity
-- (the number of alias units equal to one unit of the commodity),
-- and the commodity's display style if known.
type CommodityAliases = M.Map CommoditySymbol (CommoditySymbol, Quantity, Maybe AmountStyle)

-- | The commodity aliases declared in this journal.
journalCommodityAliases :: Journal -> CommodityAliases
journalCommodityAliases j =
  M.fromList [(acommodity a, (csymbol c, aquantity a, M.lookup (csymbol c) styles))
             | c <- M.elems $ jdeclaredcommodities j, (a, _) <- caliases c]
  where styles = journalCommodityStyles j

-- | Convert an amount written in one of these commodity aliases to its commodity,
-- also returning the alias quantity (or 1).
-- The converted amount gets the commodity's display style. For a plain alias, it keeps its display precision;
-- for an alias with a quantity, it gets the commodity's display precision
-- (or more, if needed to show an exactly converted amount fully; or if that's unknown, all its decimal places up to 8).
convertAliasAmount :: CommodityAliases -> Amount -> (Amount, Quantity)
convertAliasAmount aliases a = case M.lookup (acommodity a) aliases of
  Nothing         -> (a, 1)
  Just (c, 1, ms) -> (a{acommodity = c, astyle = maybe (astyle a) (\s -> s{asprecision = asprecision $ astyle a}) ms}, 1)
  Just (c, q, ms) -> (a'{astyle = style{asprecision = Precision prec}}, q)
    where
      a'     = a{acommodity = c, aquantity = aquantity a / q}
      digits = amountInternalPrecision a'
      exact  = multiplyQuantities (aquantity a') q == aquantity a
      (style, prec) = case ms of
        Just s@AmountStyle{asprecision = Precision n} -> (s, if exact then max n digits else n)
        _ -> (fromMaybe (astyle a) ms, min defaultMaxDisplayPrecision digits)

-- | Convert all amounts written in a commodity alias to the commodity it is an alias of.
-- For an alias with a quantity (other than 1), amount quantities are divided by it,
-- and unit costs, cost bases and market prices of the alias are multiplied by it
-- (but if an amount or its unit cost converts inexactly, the unit cost is replaced by the exact total cost).
-- This is done for posting amounts, with their costs, cost bases and balance assertions;
-- periodic transaction and auto posting rules (whose multipliers' symbols are just renamed);
-- and price directives. A converted posting keeps its original form in poriginal, for print.
--
-- Returns 'Left' with a verbose error if the same alias is declared on more than one commodity,
-- or if an alias is also declared as a commodity with aliases of its own.
journalApplyCommodityAliases :: Journal -> Either String Journal
journalApplyCommodityAliases j
  | ((a, cs):_) <- conflicts = Left $ unlines $
      [ "Alias '" <> T.unpack a <> "' is declared on more than one commodity:", ""] <> map commodityExcerpt cs
  | (c:_) <- chained = Left $ unlines
      [ "Commodity '" <> T.unpack (csymbol c) <> "' has aliases, but is also an alias of another commodity:", ""
      , commodityExcerpt c ]
  | M.null aliases = Right j
  | otherwise = Right j
      { jtxns            = map (transactionMapPostings convertPosting) $ jtxns j
      , jperiodictxns    = [pt{ptpostings = map convertPosting $ ptpostings pt} | pt <- jperiodictxns j]
      , jtxnmodifiers    = [tm{tmpostingrules = map convertRule $ tmpostingrules tm} | tm <- jtxnmodifiers j]
      , jpricedirectives = map convertPrice $ jpricedirectives j
      }
  where
    comms     = M.elems $ jdeclaredcommodities j
    conflicts = [(a, cs) | (a, cs) <- M.toList $ M.fromListWith (++) [(a, [c]) | c <- comms, a <- commodityAliases c], length cs > 1]
    chained   = [c | c <- comms, not (null $ caliases c), csymbol c `M.member` aliases]
    commodityExcerpt c = let (f, l, _, ex) = makeCommodityTagErrorExcerpt c "alias" in f <> ":" <> show l <> ":\n" <> T.unpack ex

    aliases = journalCommodityAliases j
    isAlias = (`M.member` aliases) . acommodity
    convertSymbol = convertAliasAmount aliases
    -- Show all of a calculated amount's decimal digits (up to 8), but no trailing zeros.
    withAllDigits a = amountSetPrecision (Precision $ min defaultMaxDisplayPrecision $ amountInternalPrecision a') a'
      where a' = a{aquantity = normalizeDecimal $ aquantity a}
    -- Multiply a per-unit price by an alias quantity.
    scaleBy 1 a = a
    scaleBy q a = withAllDigits a{aquantity = aquantity a * q}
    -- Convert a cost, price or balance assertion amount, showing all its digits if it was divided.
    convertExactAmount a = case convertSymbol a of
      (a', 1) -> a'
      (a', _) -> withAllDigits a'
    convertAmount a = a'{ acost      = convertCost <$> acost a
                        , acostbasis = (\cb -> cb{cbCost = scaleBy q . convertExactAmount <$> cbCost cb}) <$> acostbasis a
                        }
      where
        (a', q) = convertSymbol a
        -- If the amount or its unit cost converts inexactly (eg 10 min = 0.1666.. h),
        -- use the total cost calculated before converting, so the cost stays exact.
        convertCost (UnitCost u)
          | isInexact a || isInexact u = TotalCost $ withAllDigits $ fst $ convertSymbol u{aquantity = multiplyQuantities (abs $ aquantity a) (aquantity u)}
          | otherwise                  = UnitCost $ scaleBy q $ convertExactAmount u
        convertCost (TotalCost t) = TotalCost $ convertExactAmount t
    -- Does converting this amount from an alias round its quantity ?
    isInexact a = case convertSymbol a of
      (_, 1)  -> False
      (a', q) -> multiplyQuantities (aquantity a') q /= aquantity a
    hasAlias a = isAlias a || any (isAlias . costAmount) (acost a) || any isAlias (acostbasis a >>= cbCost)
      where costAmount (UnitCost u) = u
            costAmount (TotalCost t) = t

    convertPosting p
      | any hasAlias (amountsRaw $ pamount p) || any (isAlias . baamount) (pbalanceassertion p) =
          p{ pamount           = mapMixedAmount convertAmount $ pamount p
           , pbalanceassertion = (\ba -> ba{baamount = convertExactAmount $ baamount ba}) <$> pbalanceassertion p
           , poriginal         = Just $ originalPosting p
           }
      | otherwise = p
    convertRule r@TMPostingRule{tmprPosting = p, tmprIsMultiplier = ismult}
      | ismult    = r{tmprPosting = p{pamount = mapMixedAmount (fst . convertSymbol) $ pamount p}}
      | otherwise = r{tmprPosting = convertPosting p}
    convertPrice pd@PriceDirective{pdcommodity = from, pdamount = a} =
      case M.lookup from aliases of
        Nothing        -> pd{pdamount = convertExactAmount a}
        Just (c, q, _) -> pd{pdcommodity = c, pdamount = scaleBy q $ convertExactAmount a}

-- | Convert all this journal's amounts to cost using their attached prices, if any.
journalToCost :: ConversionOp -> Journal -> Journal
journalToCost cost j@Journal{jtxns=ts} = j{jtxns=map (transactionToCost cost) ts}

-- | Identify and tag (1) equity conversion postings and (2) postings which have (or could have ?) redundant costs.
-- And if the addcosts flag is true, also add any costs which can be inferred from equity conversion postings.
-- This is always called before transaction balancing to tag the redundant-cost postings so they can be ignored.
-- With --infer-costs, it is called again after transaction balancing (when it has more information to work with) to infer costs from equity postings.
-- See transactionTagCostsAndEquityAndMaybeInferCosts for more details, and hledger manual > Cost reporting for more background.
journalTagCostsAndEquityAndMaybeInferCosts :: Bool -> Bool -> Journal -> Either String Journal
journalTagCostsAndEquityAndMaybeInferCosts verbosetags addcosts j
  | null conversionaccts = Right j  -- no conversion accounts, so no conversion postings: nothing to do
  | otherwise = do
      ts <- mapM (transactionTagCostsAndEquityAndMaybeInferCosts verbosetags addcosts conversionaccts) $ jtxns j
      return j{jtxns=ts}
  where conversionaccts = journalConversionAccounts j

-- | Add equity postings inferred from costs, where needed and possible.
-- See hledger manual > Cost reporting.
journalInferEquityFromCosts :: Bool -> Journal -> Journal
journalInferEquityFromCosts verbosetags j =
  journalMapTransactions (transactionInferEquityPostings verbosetags equityAcct) j
  where equityAcct = journalBaseConversionAccount j

-- -- | 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,acost=p} =
--     case p of Nothing -> [c]
--               Just (UnitCost ma)  -> c:(concatMap amountCommodities $ amounts ma)
--               Just (TotalCost ma) -> c:(concatMap amountCommodities $ amounts ma)

-- | Get an ordered list of amounts in this journal which can influence
-- canonical amount display styles (excluding the ones in commodity directives). 
-- They are, in the following order:
--
-- * amounts in market price (P) directives (in parse order)
-- * posting amounts and optionally cost amounts (in parse order)
-- * the amount in the final default commodity (D) directive
--
journalStyleInfluencingAmounts :: Bool -> Journal -> [Amount]
journalStyleInfluencingAmounts includecost j =
  dbg7 "journalStyleInfluencingAmounts" $
  catMaybes $ concat [
   [mdefaultcommodityamt]
  ,map (Just . pdamount) $ jpricedirectives j
  ,map Just $ if includecost
    then journalPostingAndCostAmounts j
    else concatMap amountsRaw $ journalPostingAmounts j
  ]
  where
    -- D's amount style isn't actually stored as an amount, make it into one
    mdefaultcommodityamt =
      case jparsedefaultcommodity j of
        Just (symbol,style) -> Just nullamt{acommodity=symbol,astyle=style}
        Nothing -> 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 acost 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 exact 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 False = journalDateSpanHelper $ Just PrimaryDate
journalDateSpan True  = journalDateSpanHelper $ Just 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 = journalDateSpanHelper Nothing

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

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

-- | The latest of this journal's transaction and posting dates, or
-- Nothing if there are none.
journalLastDay :: Bool -> Journal -> Maybe Day
journalLastDay secondary j = addDays (-1) <$> journalEndDate secondary 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 fieldortagname j = j{jtxns = map (transactionPivot fieldortagname) . jtxns $ j}

-- | Replace this transaction's postings' account names with the value
-- of the given field or tag, if any.
-- The postings are relinked to the new transaction, so that queries which
-- look at a posting's siblings (any:, all:) see the pivoted account names.
transactionPivot :: Text -> Transaction -> Transaction
transactionPivot fieldortagname t = txnTieKnot t{tpostings = map (postingPivot fieldortagname) . tpostings $ 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 fieldortagname p =
  p{paccount = pivotAccount fieldortagname p, poriginal = Just $ originalPosting p}

pivotAccount :: Text -> Posting -> Text
pivotAccount fieldortagname p =
  T.intercalate ":" [pivotComponent x p | x <- T.splitOn ":" fieldortagname]

-- | Get the value of the given field or tag for this posting.
-- "comm" and "cur" are accepted as synonyms meaning the commodity symbol.
-- Pivoting on an unknown field or tag, or on commodity when there are multiple commodities, returns "".
-- Pivoting on a tag when there are multiple values for that tag, returns the first value.
-- Pivoting on the "type" tag normalises type values to their short spelling.
pivotComponent :: Text -> Posting -> Text
pivotComponent fieldortagname p
  | fieldortagname == "code",        Just t <- ptransaction p = tcode t
  | fieldortagname `elem` descnames, Just t <- ptransaction p = tdescription t
  | fieldortagname == "payee",       Just t <- ptransaction p = transactionPayee t
  | fieldortagname == "note",        Just t <- ptransaction p = transactionNote t
  | fieldortagname == "status",      Just t <- ptransaction p = T.pack . show . tstatus $ t
  | fieldortagname `elem` acctnames = paccount p
  | fieldortagname `elem` commnames = case map acommodity $ amounts $ pamount p of [s] -> s; _ -> unknown
  | fieldortagname == "amt"         = case amounts $ pamount p of [a] -> T.pack $ show $ aquantity a; _ -> unknown
  | fieldortagname == "cost"        = case amounts $ pamount p of [a@Amount{acost=Just _}] -> T.pack $ lstrip $ showAmountCost a; _ -> unknown
  | Just (_, tagvalue) <- postingFindTag fieldortagname p =
      if fieldortagname == "type"
      then either (const tagvalue) (T.pack . show) $ parseAccountType True tagvalue
      else tagvalue
  | otherwise = unknown
  where
    descnames = ["desc", "description"]   -- allow "description" for hledger <=1.30 compat
    acctnames = ["acct", "account"]       -- allow either; acct is the query prefix, account is more natural
    commnames = ["cur","comm"]            -- allow either; cur is the query prefix, comm is more consistent
    unknown   = ""

postingFindTag :: TagName -> Posting -> Maybe (TagName, TagValue)
postingFindTag tagname p = find ((tagname==) . fst) $ postingAllTags 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 [] j = Right j
journalApplyAliases aliases j = 
  case mapM (transactionApplyAliases aliases) $ jtxns j of
    Right ts -> Right j{jtxns = ts}
    Left err -> Left 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 = samplejournalMaybeExplicit True

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

tests_Journal = testGroup "Journal" [

   testCase "journalDateSpan" $
    journalDateSpan True nulljournal{
      jtxns = [nulltransaction{tdate = fromGregorian 2014 02 01
                              ,tpostings = [posting{pdate=Just (fromGregorian 2014 01 10)}]
                              }
              ,nulltransaction{tdate = fromGregorian 2014 09 01
                              ,tpostings = [posting{pdate2=Just (fromGregorian 2014 10 10)}]
                              }
              ]
      }
    @?= (DateSpan (Just $ Exact $ fromGregorian 2014 1 10) (Just $ Exact $ fromGregorian 2014 10 11))
  ]