{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}
module Hledger.Data.JournalChecks (
journalStrictChecks,
journalCheckAsserts,
journalCheckAccounts,
journalCheckBalanceAssertions,
journalCheckCommodities,
journalUndeclaredCommodities,
journalCheckLots,
journalCheckPayees,
journalCheckPairedConversionPostings,
journalCheckRecentAssertions,
journalCheckTags,
module Hledger.Data.JournalChecks.Ordereddates,
module Hledger.Data.JournalChecks.Uniqueleafnames,
)
where
import Data.Char (isSpace)
import Data.List.Extra
import Data.Map qualified as M
import Data.Maybe
import Data.Set qualified as S
import Data.Text qualified as T
import Safe (atMay, lastMay, headMay)
import Text.Printf (printf)
import Hledger.Data.AccountName (isAccountNamePrefixOf)
import Hledger.Data.Errors
import Hledger.Data.Journal
import Hledger.Data.JournalChecks.Assert
import Hledger.Data.JournalChecks.Ordereddates
import Hledger.Data.JournalChecks.Uniqueleafnames
import Hledger.Data.Posting (defaultDecimalMarkColumn, isVirtual, postingDate, postingHasTag, transactionAllTags, conversionPostingTagName, costPostingTagName, imbalancePostingTagName, postingAsLines, generatedPostingTagName, generatedTransactionTagName, modifiedTransactionTagName, feesplitPostingTagName, lotsplitPostingTagName, lotParentAssertionTagName, AmountCols(..))
import Hledger.Data.Types
import Hledger.Data.Amount (amountIsZero, amountsRaw, defaultFmt, missingamt)
import Hledger.Data.Transaction (transactionPayee, showTransactionLineFirstPart, partitionAndCheckConversionPostings)
import Data.Time (diffDays)
import Hledger.Utils
import Data.Ord
import Hledger.Data.Dates (showDate)
import Hledger.Data.Balancing (journalBalanceTransactions, defbalancingopts)
import Hledger.Data.Lots (isGainPosting, lotBaseAccount)
journalStrictChecks :: Journal -> Either String ()
journalStrictChecks :: Journal -> Either FilePath ()
journalStrictChecks Journal
j = do
Journal -> Either FilePath ()
journalCheckCommodities Journal
j
Journal -> Either FilePath ()
journalCheckAccounts Journal
j
journalCheckAccounts :: Journal -> Either String ()
journalCheckAccounts :: Journal -> Either FilePath ()
journalCheckAccounts Journal
j = (Posting -> Either FilePath ()) -> [Posting] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Posting -> Either FilePath ()
checkacct (Journal -> [Posting]
journalPostings Journal
j)
where
declaredaccts :: Set Text
declaredaccts = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
S.fromList ([Text] -> Set Text) -> [Text] -> Set Text
forall a b. (a -> b) -> a -> b
$ ((Text, AccountDeclarationInfo) -> Text)
-> [(Text, AccountDeclarationInfo)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, AccountDeclarationInfo) -> Text
forall a b. (a, b) -> a
fst ([(Text, AccountDeclarationInfo)] -> [Text])
-> [(Text, AccountDeclarationInfo)] -> [Text]
forall a b. (a -> b) -> a -> b
$ Journal -> [(Text, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j
checkacct :: Posting -> Either FilePath ()
checkacct p :: Posting
p@Posting{paccount :: Posting -> Text
paccount=Text
a}
| Posting -> Bool
hasGeneratedAccount Posting
p = () -> Either FilePath ()
forall a b. b -> Either a b
Right ()
| Text
acct Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set Text
declaredaccts = () -> Either FilePath ()
forall a b. b -> Either a b
Right ()
| Bool
otherwise = FilePath -> Either FilePath ()
forall a b. a -> Either a b
Left (FilePath -> Either FilePath ()) -> FilePath -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Int -> Text -> Text -> FilePath
forall r. PrintfType r => FilePath -> r
printf ([FilePath] -> FilePath
unlines [
FilePath
"%s:%d:"
,FilePath
"%s"
,FilePath
"Strict account checking is enabled, and"
,FilePath
"account \"%s\" has not been declared."
]) FilePath
f Int
l Text
ex Text
acct
FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ [FilePath] -> FilePath
unlines [FilePath]
advice
where
acct :: Text
acct = Text -> Text
lotBaseAccount Text
a
(FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = Posting -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingAccountErrorExcerpt Posting
p
advice :: [FilePath]
advice = case Text -> Maybe (Text, Text, SourcePos)
outOfScopeAlias Text
acct of
Just (Text
alias, Text
target, SourcePos
pos) -> [
FilePath -> Text -> Text -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"\"%s\" is an alias of account \"%s\", declared at %s," Text
alias Text
target (SourcePos -> FilePath
sourcePosFileLine SourcePos
pos)
,FilePath
"but that alias does not apply here. An account alias affects only the entries after it"
,FilePath
"in the same file or its included files, until end of file or an end aliases directive."
,FilePath
"Consider declaring the account before this entry (in this file or a file including it),"
,FilePath
"or writing the account's full name."
]
Maybe (Text, Text, SourcePos)
Nothing -> [
FilePath
"Consider adding an account directive. Examples:"
,FilePath
""
,FilePath
"account " FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> Text -> FilePath
T.unpack Text
acct
]
outOfScopeAlias :: Text -> Maybe (Text, Text, SourcePos)
outOfScopeAlias Text
acct = [(Text, Text, SourcePos)] -> Maybe (Text, Text, SourcePos)
forall a. [a] -> Maybe a
headMay
[ (Text
alias, Text
target, AccountDeclarationInfo -> SourcePos
adisourcepos AccountDeclarationInfo
adi)
| (Text
target, AccountDeclarationInfo
adi) <- Journal -> [(Text, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j
, (Text
n, Text
alias) <- Journal -> Text -> [(Text, Text)]
journalAccountTags Journal
j Text
target
, Text -> Text
T.toLower Text
n Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"alias"
, Text
alias Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
acct Bool -> Bool -> Bool
|| Text
alias Text -> Text -> Bool
`isAccountNamePrefixOf` Text
acct
]
hasGeneratedAccount :: Posting -> Bool
hasGeneratedAccount Posting
p =
Text -> Posting -> Bool
postingHasTag Text
imbalancePostingTagName Posting
p
Bool -> Bool -> Bool
|| (Text -> Posting -> Bool
postingHasTag Text
generatedPostingTagName Posting
p Bool -> Bool -> Bool
&& (Text -> Posting -> Bool
postingHasTag Text
conversionPostingTagName Posting
p Bool -> Bool -> Bool
|| Posting -> Bool
isGainPosting Posting
p))
journalCheckBalanceAssertions :: Journal -> Either String ()
journalCheckBalanceAssertions :: Journal -> Either FilePath ()
journalCheckBalanceAssertions = (Journal -> ()) -> Either FilePath Journal -> Either FilePath ()
forall a b. (a -> b) -> Either FilePath a -> Either FilePath b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> Journal -> ()
forall a b. a -> b -> a
const ()) (Either FilePath Journal -> Either FilePath ())
-> (Journal -> Either FilePath Journal)
-> Journal
-> Either FilePath ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BalancingOpts -> Journal -> Either FilePath Journal
journalBalanceTransactions BalancingOpts
defbalancingopts
postingCommoditiesUsed :: Posting -> [CommoditySymbol]
postingCommoditiesUsed :: Posting -> [Text]
postingCommoditiesUsed Posting{pamount :: Posting -> MixedAmount
pamount=MixedAmount
amt, Maybe BalanceAssertion
pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion :: Posting -> Maybe BalanceAssertion
pbalanceassertion} =
(Amount -> Text) -> [Amount] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Amount -> Text
acommodity ((Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Amount -> Bool) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Bool
isIgnorable) ([Amount] -> [Amount]) -> [Amount] -> [Amount]
forall a b. (a -> b) -> a -> b
$ MixedAmount -> [Amount]
amountsRaw MixedAmount
amt)
[Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Amount -> Text
acommodity Amount
a | Just Amount
a <- [BalanceAssertion -> Amount
baamount (BalanceAssertion -> Amount)
-> Maybe BalanceAssertion -> Maybe Amount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe BalanceAssertion
pbalanceassertion]]
where isIgnorable :: Amount -> Bool
isIgnorable Amount
a = Amount
aAmount -> Amount -> Bool
forall a. Eq a => a -> a -> Bool
==Amount
missingamt Bool -> Bool -> Bool
|| (Amount -> Bool
amountIsZero Amount
a Bool -> Bool -> Bool
&& Text -> Bool
T.null (Amount -> Text
acommodity Amount
a))
priceDirectiveCommoditiesUsed :: PriceDirective -> [CommoditySymbol]
priceDirectiveCommoditiesUsed :: PriceDirective -> [Text]
priceDirectiveCommoditiesUsed PriceDirective{pdcommodity :: PriceDirective -> Text
pdcommodity=Text
c, pdamount :: PriceDirective -> Amount
pdamount=Amount
amt} = [Text
c, Amount -> Text
acommodity Amount
amt]
journalUndeclaredCommodities :: Journal -> [CommoditySymbol]
journalUndeclaredCommodities :: Journal -> [Text]
journalUndeclaredCommodities Journal
j =
(Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.notMember` Journal -> Set Text
commoditiesAndAliases Journal
j) ([Text] -> [Text]) -> ([Text] -> [Text]) -> [Text] -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubSort ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$
(PriceDirective -> [Text]) -> [PriceDirective] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PriceDirective -> [Text]
priceDirectiveCommoditiesUsed (Journal -> [PriceDirective]
jpricedirectives Journal
j)
[Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (Posting -> [Text]) -> [Posting] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [Text]
postingCommoditiesUsed (Journal -> [Posting]
journalPostings Journal
j)
journalCheckCommodities :: Journal -> Either String ()
journalCheckCommodities :: Journal -> Either FilePath ()
journalCheckCommodities Journal
j = do
(PriceDirective -> Either FilePath ())
-> [PriceDirective] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ PriceDirective -> Either FilePath ()
forall {a}. PrintfType a => PriceDirective -> Either a ()
checkPriceDirectiveCommodities ([PriceDirective] -> Either FilePath ())
-> [PriceDirective] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Journal -> [PriceDirective]
jpricedirectives Journal
j
(Posting -> Either FilePath ()) -> [Posting] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Posting -> Either FilePath ()
forall {a}. PrintfType a => Posting -> Either a ()
checkPostingCommodities ([Posting] -> Either FilePath ())
-> [Posting] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
where
declared :: Set Text
declared = Journal -> Set Text
commoditiesAndAliases Journal
j
firstUndeclaredOf :: [Text] -> Maybe Text
firstUndeclaredOf = (Text -> Bool) -> [Text] -> Maybe Text
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.notMember` Set Text
declared)
errmsg :: FilePath
errmsg = [FilePath] -> FilePath
unlines [
FilePath
"%s:%d:"
,FilePath
"%s"
,FilePath
"Strict commodity checking is enabled, and"
,FilePath
"commodity %s has not been declared."
,FilePath
"Consider adding a commodity directive. Examples:"
,FilePath
""
,FilePath
"commodity %s1000.00"
,FilePath
"commodity 1.000,00 %s"
]
checkPriceDirectiveCommodities :: PriceDirective -> Either a ()
checkPriceDirectiveCommodities PriceDirective
pd =
case [Text] -> Maybe Text
firstUndeclaredOf (PriceDirective -> [Text]
priceDirectiveCommoditiesUsed PriceDirective
pd) of
Maybe Text
Nothing -> () -> Either a ()
forall a b. b -> Either a b
Right ()
Just Text
comm -> a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$ FilePath
-> FilePath -> Int -> Text -> FilePath -> Text -> Text -> a
forall r. PrintfType r => FilePath -> r
printf FilePath
errmsg FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
comm) Text
comm Text
comm
where (FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = PriceDirective
-> Maybe (PriceDirective -> Text -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePriceDirectiveErrorExcerpt PriceDirective
pd Maybe (PriceDirective -> Text -> Maybe (Int, Maybe Int))
forall a. Maybe a
Nothing
checkPostingCommodities :: Posting -> Either a ()
checkPostingCommodities Posting
p =
case [Text] -> Maybe Text
firstUndeclaredOf (Posting -> [Text]
postingCommoditiesUsed Posting
p) of
Maybe Text
Nothing -> () -> Either a ()
forall a b. b -> Either a b
Right ()
Just Text
comm -> a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$ FilePath
-> FilePath -> Int -> Text -> FilePath -> Text -> Text -> a
forall r. PrintfType r => FilePath -> r
printf FilePath
errmsg FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
comm) Text
comm Text
comm
where
(FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = Posting
-> (Posting -> Transaction -> Text -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingErrorExcerpt Posting
p Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
finderrcols
where
finderrcols :: Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
finderrcols Posting
p' Transaction
t Text
txntxt =
case (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex (Posting -> Posting -> Bool
forall a. Eq a => a -> a -> Bool
==Posting
p') Transaction
t of
Maybe Int
Nothing -> Maybe (Int, Maybe Int)
forall a. Maybe a
Nothing
Just Int
pindex -> (Int, Maybe Int) -> Maybe (Int, Maybe Int)
forall a. a -> Maybe a
Just (Int
amtstart, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
amtend)
where
tcommentlines :: Int
tcommentlines = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Text -> [Text]
T.lines (Text -> [Text]) -> Text -> [Text]
forall a b. (a -> b) -> a -> b
$ Transaction -> Text
tcomment Transaction
t) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
errrelline :: Int
errrelline = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tcommentlines Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
pindex
errline :: Text
errline = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Text -> [Text]
T.lines Text
txntxt [Text] -> Int -> Maybe Text
forall a. [a] -> Int -> Maybe a
`atMay` (Int
errrellineInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
acctend :: Int
acctend = Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length (Posting -> Text
paccount Posting
p') Int -> Int -> Int
forall a. Num a => a -> a -> a
+ if Posting -> Bool
isVirtual Posting
p' then Int
2 else Int
0
amtstart :: Int
amtstart = Int
acctend Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Text -> Int
T.length (Text -> Int) -> Text -> Int
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isSpace (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
acctend Text
errline) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
amtend :: Int
amtend = Int
amtstart Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Text -> Int
T.length (Text -> Int) -> Text -> Int
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.stripEnd (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
';') (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
amtstart Text
errline)
journalCheckPayees :: Journal -> Either String ()
journalCheckPayees :: Journal -> Either FilePath ()
journalCheckPayees Journal
j = (Transaction -> Either FilePath ())
-> [Transaction] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Transaction -> Either FilePath ()
forall {a}. PrintfType a => Transaction -> Either a ()
checkpayee (Journal -> [Transaction]
jtxns Journal
j)
where
declaredpayees :: Set Text
declaredpayees = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
S.fromList ([Text] -> Set Text) -> [Text] -> Set Text
forall a b. (a -> b) -> a -> b
$ ((Text, PayeeDeclarationInfo) -> Text)
-> [(Text, PayeeDeclarationInfo)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, PayeeDeclarationInfo) -> Text
forall a b. (a, b) -> a
fst ([(Text, PayeeDeclarationInfo)] -> [Text])
-> [(Text, PayeeDeclarationInfo)] -> [Text]
forall a b. (a -> b) -> a -> b
$ Journal -> [(Text, PayeeDeclarationInfo)]
jdeclaredpayees Journal
j
checkpayee :: Transaction -> Either a ()
checkpayee Transaction
t
| Text
payee Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set Text
declaredpayees = () -> Either a ()
forall a b. b -> Either a b
Right ()
| Bool
otherwise = a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$
FilePath -> FilePath -> Int -> Text -> FilePath -> Text -> a
forall r. PrintfType r => FilePath -> r
printf ([FilePath] -> FilePath
unlines [
FilePath
"%s:%d:"
,FilePath
"%s"
,FilePath
"Strict payee checking is enabled, and"
,FilePath
"payee %s has not been declared."
,FilePath
"Consider adding a payee directive. Examples:"
,FilePath
""
,FilePath
"payee %s"
]) FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
payee) Text
payee
where
payee :: Text
payee = Transaction -> Text
transactionPayee Transaction
t
(FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = Transaction
-> (Transaction -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeTransactionErrorExcerpt Transaction
t Transaction -> Maybe (Int, Maybe Int)
finderrcols
finderrcols :: Transaction -> Maybe (Int, Maybe Int)
finderrcols Transaction
t' = (Int, Maybe Int) -> Maybe (Int, Maybe Int)
forall a. a -> Maybe a
Just (Int
col, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
col2)
where
col :: Int
col = Text -> Int
T.length (Transaction -> Text
showTransactionLineFirstPart Transaction
t') Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2
col2 :: Int
col2 = Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length (Transaction -> Text
transactionPayee Transaction
t') Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
journalCheckTags :: Journal -> Either String ()
journalCheckTags :: Journal -> Either FilePath ()
journalCheckTags Journal
j = do
((Text, AccountDeclarationInfo) -> Either FilePath ())
-> [(Text, AccountDeclarationInfo)] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text, AccountDeclarationInfo) -> Either FilePath ()
forall {a}.
PrintfType a =>
(Text, AccountDeclarationInfo) -> Either a ()
checkaccttags ([(Text, AccountDeclarationInfo)] -> Either FilePath ())
-> [(Text, AccountDeclarationInfo)] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Journal -> [(Text, AccountDeclarationInfo)]
jdeclaredaccounts Journal
j
(Commodity -> Either FilePath ())
-> [Commodity] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Commodity -> Either FilePath ()
forall {a}. PrintfType a => Commodity -> Either a ()
checkcommtags ([Commodity] -> Either FilePath ())
-> [Commodity] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Map Text Commodity -> [Commodity]
forall k a. Map k a -> [a]
M.elems (Map Text Commodity -> [Commodity])
-> Map Text Commodity -> [Commodity]
forall a b. (a -> b) -> a -> b
$ Journal -> Map Text Commodity
jdeclaredcommodities Journal
j
(Transaction -> Either FilePath ())
-> [Transaction] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Transaction -> Either FilePath ()
forall {a}. PrintfType a => Transaction -> Either a ()
checktxntags ([Transaction] -> Either FilePath ())
-> [Transaction] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
where
checkaccttags :: (Text, AccountDeclarationInfo) -> Either a ()
checkaccttags (Text
a, AccountDeclarationInfo
adi) = ((Text, Text) -> Either a ()) -> [(Text, Text)] -> Either a ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> Either a ()
forall {a}. PrintfType a => Text -> Either a ()
checkaccttag(Text -> Either a ())
-> ((Text, Text) -> Text) -> (Text, Text) -> Either a ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text, Text) -> Text
forall a b. (a, b) -> a
fst) ([(Text, Text)] -> Either a ()) -> [(Text, Text)] -> Either a ()
forall a b. (a -> b) -> a -> b
$ AccountDeclarationInfo -> [(Text, Text)]
aditags AccountDeclarationInfo
adi
where
checkaccttag :: Text -> Either a ()
checkaccttag Text
tagname
| Text
tagname Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set Text
declaredtags = () -> Either a ()
forall a b. b -> Either a b
Right ()
| Bool
otherwise = a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Int -> Text -> FilePath -> Text -> a
forall r. PrintfType r => FilePath -> r
printf FilePath
msg FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
tagname) Text
tagname
where (FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = (Text, AccountDeclarationInfo)
-> Text -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeAccountTagErrorExcerpt (Text
a, AccountDeclarationInfo
adi) Text
tagname
checkcommtags :: Commodity -> Either a ()
checkcommtags Commodity
c = ((Text, Text) -> Either a ()) -> [(Text, Text)] -> Either a ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> Either a ()
forall {a}. PrintfType a => Text -> Either a ()
checkcommtag(Text -> Either a ())
-> ((Text, Text) -> Text) -> (Text, Text) -> Either a ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text, Text) -> Text
forall a b. (a, b) -> a
fst) ([(Text, Text)] -> Either a ()) -> [(Text, Text)] -> Either a ()
forall a b. (a -> b) -> a -> b
$ Commodity -> [(Text, Text)]
ctags Commodity
c
where
checkcommtag :: Text -> Either a ()
checkcommtag Text
tagname
| Text
tagname Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set Text
declaredtags = () -> Either a ()
forall a b. b -> Either a b
Right ()
| Bool
otherwise = a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Int -> Text -> FilePath -> Text -> a
forall r. PrintfType r => FilePath -> r
printf FilePath
msg FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
tagname) Text
tagname
where (FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = Commodity -> Text -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeCommodityTagErrorExcerpt Commodity
c Text
tagname
checktxntags :: Transaction -> Either a ()
checktxntags Transaction
txn = ((Text, Text) -> Either a ()) -> [(Text, Text)] -> Either a ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> Either a ()
forall {a}. PrintfType a => Text -> Either a ()
checktxntag (Text -> Either a ())
-> ((Text, Text) -> Text) -> (Text, Text) -> Either a ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Text) -> Text
forall a b. (a, b) -> a
fst) ([(Text, Text)] -> Either a ()) -> [(Text, Text)] -> Either a ()
forall a b. (a -> b) -> a -> b
$ Transaction -> [(Text, Text)]
transactionAllTags Transaction
txn
where
checktxntag :: Text -> Either a ()
checktxntag Text
tagname
| Text
tagname Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set Text
declaredtags = () -> Either a ()
forall a b. b -> Either a b
Right ()
| Bool
otherwise = a -> Either a ()
forall a b. a -> Either a b
Left (a -> Either a ()) -> a -> Either a ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Int -> Text -> FilePath -> Text -> a
forall r. PrintfType r => FilePath -> r
printf FilePath
msg FilePath
f Int
l Text
ex (Text -> FilePath
forall a. Show a => a -> FilePath
show Text
tagname) Text
tagname
where
(FilePath
f,Int
l,Maybe (Int, Maybe Int)
_mcols,Text
ex) = Transaction
-> (Transaction -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeTransactionErrorExcerpt Transaction
txn Transaction -> Maybe (Int, Maybe Int)
forall {p} {a}. p -> Maybe a
finderrcols
where
finderrcols :: p -> Maybe a
finderrcols p
_txn' = Maybe a
forall a. Maybe a
Nothing
declaredtags :: Set Text
declaredtags = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
S.fromList ([Text] -> Set Text) -> [Text] -> Set Text
forall a b. (a -> b) -> a -> b
$ ((Text, TagDeclarationInfo) -> Text)
-> [(Text, TagDeclarationInfo)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, TagDeclarationInfo) -> Text
forall a b. (a, b) -> a
fst (Journal -> [(Text, TagDeclarationInfo)]
jdeclaredtags Journal
j) [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
builtinTags
msg :: FilePath
msg = ([FilePath] -> FilePath
unlines [
FilePath
"%s:%d:"
,FilePath
"%s"
,FilePath
"Strict tag checking is enabled, and"
,FilePath
"tag %s has not been declared."
,FilePath
"Consider adding a tag directive. Examples:"
,FilePath
""
,FilePath
"tag %s"
])
builtinTags :: [Text]
builtinTags = [
Text
"date"
,Text
"date2"
,Text
"type"
,Text
"alias"
,Text
"lots"
,Text
"t"
,Text
"assert"
,Text
"retain"
,Text
"start"
]
[Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text]
ts [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
toVisibleTagName [Text]
ts
where
ts :: [Text]
ts = [
Text
generatedTransactionTagName
,Text
modifiedTransactionTagName
,Text
generatedPostingTagName
,Text
costPostingTagName
,Text
conversionPostingTagName
,Text
"_ptype"
,Text
feesplitPostingTagName
,Text
lotsplitPostingTagName
,Text
lotParentAssertionTagName
]
journalCheckPairedConversionPostings :: Journal -> Either String ()
journalCheckPairedConversionPostings :: Journal -> Either FilePath ()
journalCheckPairedConversionPostings Journal
j =
(Transaction -> Either FilePath ())
-> [Transaction] -> Either FilePath ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([Text] -> Transaction -> Either FilePath ()
transactionCheckPairedConversionPostings [Text]
conversionaccts) ([Transaction] -> Either FilePath ())
-> [Transaction] -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
j
where conversionaccts :: [Text]
conversionaccts = Journal -> [Text]
journalConversionAccounts Journal
j
transactionCheckPairedConversionPostings :: [AccountName] -> Transaction -> Either String ()
transactionCheckPairedConversionPostings :: [Text] -> Transaction -> Either FilePath ()
transactionCheckPairedConversionPostings [Text]
conversionaccts Transaction
t =
case Bool
-> [Text]
-> [IdxPosting]
-> Either
Text ([(IdxPosting, IdxPosting)], ([IdxPosting], [IdxPosting]))
partitionAndCheckConversionPostings Bool
True [Text]
conversionaccts ([Int] -> [Posting] -> [IdxPosting]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] ([Posting] -> [IdxPosting]) -> [Posting] -> [IdxPosting]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t) of
Left Text
err -> FilePath -> Either FilePath ()
forall a b. a -> Either a b
Left (FilePath -> Either FilePath ()) -> FilePath -> Either FilePath ()
forall a b. (a -> b) -> a -> b
$ Text -> FilePath
T.unpack Text
err
Right ([(IdxPosting, IdxPosting)], ([IdxPosting], [IdxPosting]))
_ -> () -> Either FilePath ()
forall a b. b -> Either a b
Right ()
maxlag :: Integer
maxlag = Integer
7
journalCheckRecentAssertions :: Journal -> Either String ()
journalCheckRecentAssertions :: Journal -> Either FilePath ()
journalCheckRecentAssertions Journal
j =
let acctps :: [[Posting]]
acctps = (Posting -> Text) -> [Posting] -> [[Posting]]
forall k a. Eq k => (a -> k) -> [a] -> [[a]]
groupOn Posting -> Text
paccount ([Posting] -> [[Posting]]) -> [Posting] -> [[Posting]]
forall a b. (a -> b) -> a -> b
$ (Posting -> Text) -> [Posting] -> [Posting]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Posting -> Text
paccount ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j
in case ([Posting] -> Maybe FilePath) -> [[Posting]] -> [FilePath]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [Posting] -> Maybe FilePath
findRecentAssertionError [[Posting]]
acctps of
[] -> () -> Either FilePath ()
forall a b. b -> Either a b
Right ()
FilePath
firsterr:[FilePath]
_ -> FilePath -> Either FilePath ()
forall a b. a -> Either a b
Left FilePath
firsterr
findRecentAssertionError :: [Posting] -> Maybe String
findRecentAssertionError :: [Posting] -> Maybe FilePath
findRecentAssertionError [Posting]
ps = do
let rps :: [Posting]
rps = (Posting -> Down Day) -> [Posting] -> [Posting]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Day -> Down Day
forall a. a -> Down a
Data.Ord.Down (Day -> Down Day) -> (Posting -> Day) -> Posting -> Down Day
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> Day
postingDate) [Posting]
ps
let ([Posting]
afterlatestassertrps, [Posting]
untillatestassertrps) = (Posting -> Bool) -> [Posting] -> ([Posting], [Posting])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Maybe BalanceAssertion -> Bool
forall a. Maybe a -> Bool
isNothing(Maybe BalanceAssertion -> Bool)
-> (Posting -> Maybe BalanceAssertion) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Posting -> Maybe BalanceAssertion
pbalanceassertion) [Posting]
rps
latestassertdate <- Posting -> Day
postingDate (Posting -> Day) -> Maybe Posting -> Maybe Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Posting] -> Maybe Posting
forall a. [a] -> Maybe a
headMay [Posting]
untillatestassertrps
let withinlimit Day
date = Day -> Day -> Integer
diffDays Day
date Day
latestassertdate Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
maxlag
firsterrorp <- lastMay $ dropWhileEnd (withinlimit.postingDate) afterlatestassertrps
let lag = Day -> Day -> Integer
diffDays (Posting -> Day
postingDate Posting
firsterrorp) Day
latestassertdate
let acct = Posting -> Text
paccount Posting
firsterrorp
let (f,l,_mcols,ex) = makePostingAccountErrorExcerpt firsterrorp
Just $ chomp $ printf
(unlines [
"%s:%d:",
"%s\n",
"The recentassertions check is enabled, so accounts with balance assertions",
"must have a recent one, not more than %d days older than their latest posting.",
"In account: %s",
"the last assertion was on %s, %d days before this latest posting.",
"Consider adding a new balance assertion to the above posting. Eg:",
"",
"%s = BALANCE"
])
f
l
(textChomp ex)
maxlag
(bold' $ T.unpack acct)
(showDate latestassertdate)
lag
(showposting firsterrorp)
where
showposting :: Posting -> Text
showposting Posting
p =
Text -> [Text] -> Text
forall a. a -> [a] -> a
headDef Text
"" ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ ([Text], PostingWidths) -> [Text]
forall a b. (a, b) -> a
fst (([Text], PostingWidths) -> [Text])
-> ([Text], PostingWidths) -> [Text]
forall a b. (a -> b) -> a -> b
$ AmountFormat
-> Bool
-> Bool
-> Int
-> Int
-> AmountCols
-> Posting
-> ([Text], PostingWidths)
postingAsLines AmountFormat
defaultFmt Bool
False Bool
True Int
statusw Int
acctw AmountCols
dc Posting
p{pcomment=""}
where
statusw :: Int
statusw = case Posting -> Status
pstatus Posting
p of Status
Unmarked -> Int
0; Status
_ -> Int
2
acctw :: Int
acctw = Text -> Int
T.length (Text -> Int) -> Text -> Int
forall a b. (a -> b) -> a -> b
$ Posting -> Text
paccount Posting
p
dc :: AmountCols
dc = AmountCols { acDecMark :: Int
acDecMark = Int
defaultDecimalMarkColumn, acEquals :: Int
acEquals = Int
0, acAssDecMark :: Int
acAssDecMark = Int
0 }
journalCheckLots :: Journal -> Either String ()
journalCheckLots :: Journal -> Either FilePath ()
journalCheckLots Journal
_ = () -> Either FilePath ()
forall a b. b -> Either a b
Right ()