{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Data.Posting (
nullposting,
posting,
post,
vpost,
post',
vpost',
nullsourcepos,
nullassertion,
balassert,
balassertTot,
balassertParInc,
balassertTotInc,
originalPosting,
postingStatus,
isReal,
isVirtual,
isBalancedVirtual,
isEmptyPosting,
hasBalanceAssignment,
hasAmount,
postingAllTags,
transactionAllTags,
relatedPostings,
postingStripPrices,
postingApplyAliases,
postingApplyCommodityStyles,
postingAddTags,
postingDate,
postingDate2,
postingDateOrDate2,
isPostingInDateSpan,
isPostingInDateSpan',
accountNamesFromPostings,
commentJoin,
commentAddTag,
commentAddTagNextLine,
sumPostings,
showPosting,
showPostingLines,
postingAsLines,
postingsAsLines,
showAccountName,
renderCommentLines,
postingTransformAmount,
postingApplyValuation,
postingToCost,
postingAddInferredEquityPostings,
tests_Posting
)
where
import Data.Default (def)
import Data.Foldable (asum)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, isJust)
import Data.List (foldl', sort, union)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB
import Data.Time.Calendar (Day)
import Safe (maximumBound)
import Text.DocLayout (realLength)
import Text.Tabular.AsciiWide
import Hledger.Utils
import Hledger.Data.Types
import Hledger.Data.Amount
import Hledger.Data.AccountName
import Hledger.Data.Dates (nulldate, spanContainsDate)
import Hledger.Data.Valuation
nullposting, posting :: Posting
nullposting :: Posting
nullposting = Posting :: Maybe Day
-> Maybe Day
-> Status
-> AccountName
-> MixedAmount
-> AccountName
-> PostingType
-> [Tag]
-> Maybe BalanceAssertion
-> Maybe Transaction
-> Maybe Posting
-> Posting
Posting
{pdate :: Maybe Day
pdate=Maybe Day
forall a. Maybe a
Nothing
,pdate2 :: Maybe Day
pdate2=Maybe Day
forall a. Maybe a
Nothing
,pstatus :: Status
pstatus=Status
Unmarked
,paccount :: AccountName
paccount=AccountName
""
,pamount :: MixedAmount
pamount=MixedAmount
nullmixedamt
,pcomment :: AccountName
pcomment=AccountName
""
,ptype :: PostingType
ptype=PostingType
RegularPosting
,ptags :: [Tag]
ptags=[]
,pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion=Maybe BalanceAssertion
forall a. Maybe a
Nothing
,ptransaction :: Maybe Transaction
ptransaction=Maybe Transaction
forall a. Maybe a
Nothing
,poriginal :: Maybe Posting
poriginal=Maybe Posting
forall a. Maybe a
Nothing
}
posting :: Posting
posting = Posting
nullposting
post :: AccountName -> Amount -> Posting
post :: AccountName -> Amount -> Posting
post AccountName
acc Amount
amt = Posting
posting {paccount :: AccountName
paccount=AccountName
acc, pamount :: MixedAmount
pamount=Amount -> MixedAmount
mixedAmount Amount
amt}
vpost :: AccountName -> Amount -> Posting
vpost :: AccountName -> Amount -> Posting
vpost AccountName
acc Amount
amt = (AccountName -> Amount -> Posting
post AccountName
acc Amount
amt){ptype :: PostingType
ptype=PostingType
VirtualPosting}
post' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' AccountName
acc Amount
amt Maybe BalanceAssertion
ass = Posting
posting {paccount :: AccountName
paccount=AccountName
acc, pamount :: MixedAmount
pamount=Amount -> MixedAmount
mixedAmount Amount
amt, pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion=Maybe BalanceAssertion
ass}
vpost' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
vpost' AccountName
acc Amount
amt Maybe BalanceAssertion
ass = (AccountName -> Amount -> Maybe BalanceAssertion -> Posting
post' AccountName
acc Amount
amt Maybe BalanceAssertion
ass){ptype :: PostingType
ptype=PostingType
VirtualPosting, pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion=Maybe BalanceAssertion
ass}
nullsourcepos :: (SourcePos, SourcePos)
nullsourcepos :: (SourcePos, SourcePos)
nullsourcepos = (FilePath -> Pos -> Pos -> SourcePos
SourcePos FilePath
"" (Int -> Pos
mkPos Int
1) (Int -> Pos
mkPos Int
1), FilePath -> Pos -> Pos -> SourcePos
SourcePos FilePath
"" (Int -> Pos
mkPos Int
2) (Int -> Pos
mkPos Int
1))
nullassertion :: BalanceAssertion
nullassertion :: BalanceAssertion
nullassertion = BalanceAssertion :: Amount -> Bool -> Bool -> SourcePos -> BalanceAssertion
BalanceAssertion
{baamount :: Amount
baamount=Amount
nullamt
,batotal :: Bool
batotal=Bool
False
,bainclusive :: Bool
bainclusive=Bool
False
,baposition :: SourcePos
baposition=FilePath -> SourcePos
initialPos FilePath
""
}
balassert :: Amount -> Maybe BalanceAssertion
balassert :: Amount -> Maybe BalanceAssertion
balassert Amount
amt = BalanceAssertion -> Maybe BalanceAssertion
forall a. a -> Maybe a
Just (BalanceAssertion -> Maybe BalanceAssertion)
-> BalanceAssertion -> Maybe BalanceAssertion
forall a b. (a -> b) -> a -> b
$ BalanceAssertion
nullassertion{baamount :: Amount
baamount=Amount
amt}
balassertTot :: Amount -> Maybe BalanceAssertion
balassertTot :: Amount -> Maybe BalanceAssertion
balassertTot Amount
amt = BalanceAssertion -> Maybe BalanceAssertion
forall a. a -> Maybe a
Just (BalanceAssertion -> Maybe BalanceAssertion)
-> BalanceAssertion -> Maybe BalanceAssertion
forall a b. (a -> b) -> a -> b
$ BalanceAssertion
nullassertion{baamount :: Amount
baamount=Amount
amt, batotal :: Bool
batotal=Bool
True}
balassertParInc :: Amount -> Maybe BalanceAssertion
balassertParInc :: Amount -> Maybe BalanceAssertion
balassertParInc Amount
amt = BalanceAssertion -> Maybe BalanceAssertion
forall a. a -> Maybe a
Just (BalanceAssertion -> Maybe BalanceAssertion)
-> BalanceAssertion -> Maybe BalanceAssertion
forall a b. (a -> b) -> a -> b
$ BalanceAssertion
nullassertion{baamount :: Amount
baamount=Amount
amt, bainclusive :: Bool
bainclusive=Bool
True}
balassertTotInc :: Amount -> Maybe BalanceAssertion
balassertTotInc :: Amount -> Maybe BalanceAssertion
balassertTotInc Amount
amt = BalanceAssertion -> Maybe BalanceAssertion
forall a. a -> Maybe a
Just (BalanceAssertion -> Maybe BalanceAssertion)
-> BalanceAssertion -> Maybe BalanceAssertion
forall a b. (a -> b) -> a -> b
$ BalanceAssertion
nullassertion{baamount :: Amount
baamount=Amount
amt, batotal :: Bool
batotal=Bool
True, bainclusive :: Bool
bainclusive=Bool
True}
showBalanceAssertion :: BalanceAssertion -> WideBuilder
showBalanceAssertion :: BalanceAssertion -> WideBuilder
showBalanceAssertion BalanceAssertion
ba =
Char -> WideBuilder
singleton Char
'=' WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<> WideBuilder
eq WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<> WideBuilder
ast WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<> Char -> WideBuilder
singleton Char
' ' WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<> AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
forall a. Default a => a
def{displayZeroCommodity :: Bool
displayZeroCommodity=Bool
True} (BalanceAssertion -> Amount
baamount BalanceAssertion
ba)
where
eq :: WideBuilder
eq = if BalanceAssertion -> Bool
batotal BalanceAssertion
ba then Char -> WideBuilder
singleton Char
'=' else WideBuilder
forall a. Monoid a => a
mempty
ast :: WideBuilder
ast = if BalanceAssertion -> Bool
bainclusive BalanceAssertion
ba then Char -> WideBuilder
singleton Char
'*' else WideBuilder
forall a. Monoid a => a
mempty
singleton :: Char -> WideBuilder
singleton Char
c = Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
c) Int
1
originalPosting :: Posting -> Posting
originalPosting :: Posting -> Posting
originalPosting Posting
p = Posting -> Maybe Posting -> Posting
forall a. a -> Maybe a -> a
fromMaybe Posting
p (Maybe Posting -> Posting) -> Maybe Posting -> Posting
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Posting
poriginal Posting
p
showPosting :: Posting -> String
showPosting :: Posting -> FilePath
showPosting Posting
p = AccountName -> FilePath
T.unpack (AccountName -> FilePath)
-> ([AccountName] -> AccountName) -> [AccountName] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AccountName] -> AccountName
T.unlines ([AccountName] -> FilePath) -> [AccountName] -> FilePath
forall a b. (a -> b) -> a -> b
$ Bool -> [Posting] -> [AccountName]
postingsAsLines Bool
False [Posting
p]
showPostingLines :: Posting -> [Text]
showPostingLines :: Posting -> [AccountName]
showPostingLines Posting
p = ([AccountName], Int, Int) -> [AccountName]
forall a b c. (a, b, c) -> a
first3 (([AccountName], Int, Int) -> [AccountName])
-> ([AccountName], Int, Int) -> [AccountName]
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> Int -> Int -> Posting -> ([AccountName], Int, Int)
postingAsLines Bool
False Bool
False Int
maxacctwidth Int
maxamtwidth Posting
p
where
linesWithWidths :: [([AccountName], Int, Int)]
linesWithWidths = (Posting -> ([AccountName], Int, Int))
-> [Posting] -> [([AccountName], Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Bool -> Int -> Int -> Posting -> ([AccountName], Int, Int)
postingAsLines Bool
False Bool
False Int
maxacctwidth Int
maxamtwidth) ([Posting] -> [([AccountName], Int, Int)])
-> (Maybe Transaction -> [Posting])
-> Maybe Transaction
-> [([AccountName], Int, Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Posting]
-> (Transaction -> [Posting]) -> Maybe Transaction -> [Posting]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Posting
p] Transaction -> [Posting]
tpostings (Maybe Transaction -> [([AccountName], Int, Int)])
-> Maybe Transaction -> [([AccountName], Int, Int)]
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe Transaction
ptransaction Posting
p
maxacctwidth :: Int
maxacctwidth = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumBound Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (([AccountName], Int, Int) -> Int)
-> [([AccountName], Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([AccountName], Int, Int) -> Int
forall a b c. (a, b, c) -> b
second3 [([AccountName], Int, Int)]
linesWithWidths
maxamtwidth :: Int
maxamtwidth = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumBound Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (([AccountName], Int, Int) -> Int)
-> [([AccountName], Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([AccountName], Int, Int) -> Int
forall a b c. (a, b, c) -> c
third3 [([AccountName], Int, Int)]
linesWithWidths
postingsAsLines :: Bool -> [Posting] -> [Text]
postingsAsLines :: Bool -> [Posting] -> [AccountName]
postingsAsLines Bool
onelineamounts [Posting]
ps = (([AccountName], Int, Int) -> [AccountName])
-> [([AccountName], Int, Int)] -> [AccountName]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ([AccountName], Int, Int) -> [AccountName]
forall a b c. (a, b, c) -> a
first3 [([AccountName], Int, Int)]
linesWithWidths
where
linesWithWidths :: [([AccountName], Int, Int)]
linesWithWidths = (Posting -> ([AccountName], Int, Int))
-> [Posting] -> [([AccountName], Int, Int)]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Bool -> Int -> Int -> Posting -> ([AccountName], Int, Int)
postingAsLines Bool
False Bool
onelineamounts Int
maxacctwidth Int
maxamtwidth) [Posting]
ps
maxacctwidth :: Int
maxacctwidth = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumBound Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (([AccountName], Int, Int) -> Int)
-> [([AccountName], Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([AccountName], Int, Int) -> Int
forall a b c. (a, b, c) -> b
second3 [([AccountName], Int, Int)]
linesWithWidths
maxamtwidth :: Int
maxamtwidth = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumBound Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (([AccountName], Int, Int) -> Int)
-> [([AccountName], Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([AccountName], Int, Int) -> Int
forall a b c. (a, b, c) -> c
third3 [([AccountName], Int, Int)]
linesWithWidths
postingAsLines :: Bool -> Bool -> Int -> Int -> Posting -> ([Text], Int, Int)
postingAsLines :: Bool -> Bool -> Int -> Int -> Posting -> ([AccountName], Int, Int)
postingAsLines Bool
elideamount Bool
onelineamounts Int
acctwidth Int
amtwidth Posting
p =
(([AccountName] -> [AccountName])
-> [[AccountName]] -> [AccountName]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ([AccountName] -> [AccountName] -> [AccountName]
forall a. [a] -> [a] -> [a]
++ [AccountName]
newlinecomments) [[AccountName]]
postingblocks, Int
thisacctwidth, Int
thisamtwidth)
where
postingblocks :: [[AccountName]]
postingblocks = [(AccountName -> AccountName) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map AccountName -> AccountName
T.stripEnd ([AccountName] -> [AccountName])
-> (Text -> [AccountName]) -> Text -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> [AccountName]
T.lines (AccountName -> [AccountName])
-> (Text -> AccountName) -> Text -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> AccountName
TL.toStrict (Text -> [AccountName]) -> Text -> [AccountName]
forall a b. (a -> b) -> a -> b
$
[Cell] -> Text
render [ Align -> AccountName -> Cell
textCell Align
BottomLeft AccountName
statusandaccount
, Align -> AccountName -> Cell
textCell Align
BottomLeft AccountName
" "
, Align -> [WideBuilder] -> Cell
Cell Align
BottomLeft [WideBuilder -> WideBuilder
pad WideBuilder
amt]
, Align -> [WideBuilder] -> Cell
Cell Align
BottomLeft [WideBuilder
assertion]
, Align -> AccountName -> Cell
textCell Align
BottomLeft AccountName
samelinecomment
]
| WideBuilder
amt <- [WideBuilder]
shownAmounts]
render :: [Cell] -> Text
render = TableOpts -> Header Cell -> Text
renderRow TableOpts
forall a. Default a => a
def{tableBorders :: Bool
tableBorders=Bool
False, borderSpaces :: Bool
borderSpaces=Bool
False} (Header Cell -> Text) -> ([Cell] -> Header Cell) -> [Cell] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Properties -> [Header Cell] -> Header Cell
forall h. Properties -> [Header h] -> Header h
Group Properties
NoLine ([Header Cell] -> Header Cell)
-> ([Cell] -> [Header Cell]) -> [Cell] -> Header Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Cell -> Header Cell) -> [Cell] -> [Header Cell]
forall a b. (a -> b) -> [a] -> [b]
map Cell -> Header Cell
forall h. h -> Header h
Header
pad :: WideBuilder -> WideBuilder
pad WideBuilder
amt = Builder -> Int -> WideBuilder
WideBuilder (AccountName -> Builder
TB.fromText (AccountName -> Builder) -> AccountName -> Builder
forall a b. (a -> b) -> a -> b
$ Int -> AccountName -> AccountName
T.replicate Int
w AccountName
" ") Int
w WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<> WideBuilder
amt
where w :: Int
w = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
12 Int
amtwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- WideBuilder -> Int
wbWidth WideBuilder
amt
assertion :: WideBuilder
assertion = WideBuilder
-> (BalanceAssertion -> WideBuilder)
-> Maybe BalanceAssertion
-> WideBuilder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WideBuilder
forall a. Monoid a => a
mempty ((Builder -> Int -> WideBuilder
WideBuilder (Char -> Builder
TB.singleton Char
' ') Int
1 WideBuilder -> WideBuilder -> WideBuilder
forall a. Semigroup a => a -> a -> a
<>)(WideBuilder -> WideBuilder)
-> (BalanceAssertion -> WideBuilder)
-> BalanceAssertion
-> WideBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
.BalanceAssertion -> WideBuilder
showBalanceAssertion) (Maybe BalanceAssertion -> WideBuilder)
-> Maybe BalanceAssertion -> WideBuilder
forall a b. (a -> b) -> a -> b
$ Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p
statusandaccount :: AccountName
statusandaccount = AccountName -> AccountName
lineIndent (AccountName -> AccountName)
-> (AccountName -> AccountName) -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Int
-> Maybe Int -> Bool -> Bool -> AccountName -> AccountName
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
acctwidth) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
True (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ Posting -> AccountName
pstatusandacct Posting
p
thisacctwidth :: Int
thisacctwidth = AccountName -> Int
forall a. HasChars a => a -> Int
realLength (AccountName -> Int) -> AccountName -> Int
forall a b. (a -> b) -> a -> b
$ Posting -> AccountName
pacctstr Posting
p
pacctstr :: Posting -> AccountName
pacctstr Posting
p' = Maybe Int -> PostingType -> AccountName -> AccountName
showAccountName Maybe Int
forall a. Maybe a
Nothing (Posting -> PostingType
ptype Posting
p') (Posting -> AccountName
paccount Posting
p')
pstatusandacct :: Posting -> AccountName
pstatusandacct Posting
p' = Posting -> AccountName
pstatusprefix Posting
p' AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> Posting -> AccountName
pacctstr Posting
p'
pstatusprefix :: Posting -> AccountName
pstatusprefix Posting
p' = case Posting -> Status
pstatus Posting
p' of
Status
Unmarked -> AccountName
""
Status
s -> FilePath -> AccountName
T.pack (Status -> FilePath
forall a. Show a => a -> FilePath
show Status
s) AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
" "
shownAmounts :: [WideBuilder]
shownAmounts
| Bool
elideamount = [WideBuilder
forall a. Monoid a => a
mempty]
| Bool
otherwise = AmountDisplayOpts -> MixedAmount -> [WideBuilder]
showMixedAmountLinesB AmountDisplayOpts
noColour{displayOneLine :: Bool
displayOneLine=Bool
onelineamounts} (MixedAmount -> [WideBuilder]) -> MixedAmount -> [WideBuilder]
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p
thisamtwidth :: Int
thisamtwidth = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumBound Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (WideBuilder -> Int) -> [WideBuilder] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map WideBuilder -> Int
wbWidth [WideBuilder]
shownAmounts
(AccountName
samelinecomment, [AccountName]
newlinecomments) =
case AccountName -> [AccountName]
renderCommentLines (Posting -> AccountName
pcomment Posting
p) of [] -> (AccountName
"",[])
AccountName
c:[AccountName]
cs -> (AccountName
c,[AccountName]
cs)
showAccountName :: Maybe Int -> PostingType -> AccountName -> Text
showAccountName :: Maybe Int -> PostingType -> AccountName -> AccountName
showAccountName Maybe Int
w = PostingType -> AccountName -> AccountName
fmt
where
fmt :: PostingType -> AccountName -> AccountName
fmt PostingType
RegularPosting = (AccountName -> AccountName)
-> (Int -> AccountName -> AccountName)
-> Maybe Int
-> AccountName
-> AccountName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AccountName -> AccountName
forall a. a -> a
id Int -> AccountName -> AccountName
T.take Maybe Int
w
fmt PostingType
VirtualPosting = AccountName -> AccountName -> AccountName -> AccountName
wrap AccountName
"(" AccountName
")" (AccountName -> AccountName)
-> (AccountName -> AccountName) -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountName -> AccountName)
-> (Int -> AccountName -> AccountName)
-> Maybe Int
-> AccountName
-> AccountName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AccountName -> AccountName
forall a. a -> a
id (Int -> AccountName -> AccountName
T.takeEnd (Int -> AccountName -> AccountName)
-> (Int -> Int) -> Int -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
2) Maybe Int
w
fmt PostingType
BalancedVirtualPosting = AccountName -> AccountName -> AccountName -> AccountName
wrap AccountName
"[" AccountName
"]" (AccountName -> AccountName)
-> (AccountName -> AccountName) -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountName -> AccountName)
-> (Int -> AccountName -> AccountName)
-> Maybe Int
-> AccountName
-> AccountName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AccountName -> AccountName
forall a. a -> a
id (Int -> AccountName -> AccountName
T.takeEnd (Int -> AccountName -> AccountName)
-> (Int -> Int) -> Int -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
2) Maybe Int
w
renderCommentLines :: Text -> [Text]
AccountName
t =
case AccountName -> [AccountName]
T.lines AccountName
t of
[] -> []
[AccountName
l] -> [AccountName -> AccountName
commentSpace (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ AccountName -> AccountName
comment AccountName
l]
(AccountName
"":[AccountName]
ls) -> AccountName
"" AccountName -> [AccountName] -> [AccountName]
forall a. a -> [a] -> [a]
: (AccountName -> AccountName) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName -> AccountName
lineIndent (AccountName -> AccountName)
-> (AccountName -> AccountName) -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> AccountName
comment) [AccountName]
ls
(AccountName
l:[AccountName]
ls) -> AccountName -> AccountName
commentSpace (AccountName -> AccountName
comment AccountName
l) AccountName -> [AccountName] -> [AccountName]
forall a. a -> [a] -> [a]
: (AccountName -> AccountName) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName -> AccountName
lineIndent (AccountName -> AccountName)
-> (AccountName -> AccountName) -> AccountName -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> AccountName
comment) [AccountName]
ls
where
comment :: AccountName -> AccountName
comment = (AccountName
"; "AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<>)
lineIndent :: Text -> Text
lineIndent :: AccountName -> AccountName
lineIndent = (AccountName
" "AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<>)
commentSpace :: Text -> Text
= (AccountName
" "AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<>)
isReal :: Posting -> Bool
isReal :: Posting -> Bool
isReal Posting
p = Posting -> PostingType
ptype Posting
p PostingType -> PostingType -> Bool
forall a. Eq a => a -> a -> Bool
== PostingType
RegularPosting
isVirtual :: Posting -> Bool
isVirtual :: Posting -> Bool
isVirtual Posting
p = Posting -> PostingType
ptype Posting
p PostingType -> PostingType -> Bool
forall a. Eq a => a -> a -> Bool
== PostingType
VirtualPosting
isBalancedVirtual :: Posting -> Bool
isBalancedVirtual :: Posting -> Bool
isBalancedVirtual Posting
p = Posting -> PostingType
ptype Posting
p PostingType -> PostingType -> Bool
forall a. Eq a => a -> a -> Bool
== PostingType
BalancedVirtualPosting
hasAmount :: Posting -> Bool
hasAmount :: Posting -> Bool
hasAmount = Bool -> Bool
not (Bool -> Bool) -> (Posting -> Bool) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> Bool
isMissingMixedAmount (MixedAmount -> Bool)
-> (Posting -> MixedAmount) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> MixedAmount
pamount
hasBalanceAssignment :: Posting -> Bool
hasBalanceAssignment :: Posting -> Bool
hasBalanceAssignment Posting
p = Bool -> Bool
not (Posting -> Bool
hasAmount Posting
p) Bool -> Bool -> Bool
&& Maybe BalanceAssertion -> Bool
forall a. Maybe a -> Bool
isJust (Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p)
accountNamesFromPostings :: [Posting] -> [AccountName]
accountNamesFromPostings :: [Posting] -> [AccountName]
accountNamesFromPostings = Set AccountName -> [AccountName]
forall a. Set a -> [a]
S.toList (Set AccountName -> [AccountName])
-> ([Posting] -> Set AccountName) -> [Posting] -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AccountName] -> Set AccountName
forall a. Ord a => [a] -> Set a
S.fromList ([AccountName] -> Set AccountName)
-> ([Posting] -> [AccountName]) -> [Posting] -> Set AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Posting -> AccountName) -> [Posting] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> AccountName
paccount
sumPostings :: [Posting] -> MixedAmount
sumPostings :: [Posting] -> MixedAmount
sumPostings = (MixedAmount -> Posting -> MixedAmount)
-> MixedAmount -> [Posting] -> MixedAmount
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\MixedAmount
amt Posting
p -> MixedAmount -> MixedAmount -> MixedAmount
maPlus MixedAmount
amt (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p) MixedAmount
nullmixedamt
postingStripPrices :: Posting -> Posting
postingStripPrices :: Posting -> Posting
postingStripPrices = (MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount MixedAmount -> MixedAmount
mixedAmountStripPrices
postingDate :: Posting -> Day
postingDate :: Posting -> Day
postingDate Posting
p = Day -> Maybe Day -> Day
forall a. a -> Maybe a -> a
fromMaybe Day
nulldate (Maybe Day -> Day) -> Maybe Day -> Day
forall a b. (a -> b) -> a -> b
$ [Maybe Day] -> Maybe Day
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [Maybe Day]
dates
where dates :: [Maybe Day]
dates = [ Posting -> Maybe Day
pdate Posting
p, Transaction -> Day
tdate (Transaction -> Day) -> Maybe Transaction -> Maybe Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Posting -> Maybe Transaction
ptransaction Posting
p ]
postingDate2 :: Posting -> Day
postingDate2 :: Posting -> Day
postingDate2 Posting
p = Day -> Maybe Day -> Day
forall a. a -> Maybe a -> a
fromMaybe Day
nulldate (Maybe Day -> Day) -> Maybe Day -> Day
forall a b. (a -> b) -> a -> b
$ [Maybe Day] -> Maybe Day
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [Maybe Day]
dates
where dates :: [Maybe Day]
dates = [ Posting -> Maybe Day
pdate2 Posting
p
, Transaction -> Maybe Day
tdate2 (Transaction -> Maybe Day) -> Maybe Transaction -> Maybe Day
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Posting -> Maybe Transaction
ptransaction Posting
p
, Posting -> Maybe Day
pdate Posting
p
, Transaction -> Day
tdate (Transaction -> Day) -> Maybe Transaction -> Maybe Day
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Posting -> Maybe Transaction
ptransaction Posting
p
]
postingDateOrDate2 :: WhichDate -> Posting -> Day
postingDateOrDate2 :: WhichDate -> Posting -> Day
postingDateOrDate2 WhichDate
PrimaryDate = Posting -> Day
postingDate
postingDateOrDate2 WhichDate
SecondaryDate = Posting -> Day
postingDate2
postingStatus :: Posting -> Status
postingStatus :: Posting -> Status
postingStatus Posting{pstatus :: Posting -> Status
pstatus=Status
s, ptransaction :: Posting -> Maybe Transaction
ptransaction=Maybe Transaction
mt} = case Status
s of
Status
Unmarked -> Status -> (Transaction -> Status) -> Maybe Transaction -> Status
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Status
Unmarked Transaction -> Status
tstatus Maybe Transaction
mt
Status
_ -> Status
s
postingAllTags :: Posting -> [Tag]
postingAllTags :: Posting -> [Tag]
postingAllTags Posting
p = Posting -> [Tag]
ptags Posting
p [Tag] -> [Tag] -> [Tag]
forall a. [a] -> [a] -> [a]
++ [Tag] -> (Transaction -> [Tag]) -> Maybe Transaction -> [Tag]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Transaction -> [Tag]
ttags (Posting -> Maybe Transaction
ptransaction Posting
p)
transactionAllTags :: Transaction -> [Tag]
transactionAllTags :: Transaction -> [Tag]
transactionAllTags Transaction
t = Transaction -> [Tag]
ttags Transaction
t [Tag] -> [Tag] -> [Tag]
forall a. [a] -> [a] -> [a]
++ (Posting -> [Tag]) -> [Posting] -> [Tag]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [Tag]
ptags (Transaction -> [Posting]
tpostings Transaction
t)
relatedPostings :: Posting -> [Posting]
relatedPostings :: Posting -> [Posting]
relatedPostings p :: Posting
p@Posting{ptransaction :: Posting -> Maybe Transaction
ptransaction=Just Transaction
t} = (Posting -> Bool) -> [Posting] -> [Posting]
forall a. (a -> Bool) -> [a] -> [a]
filter (Posting -> Posting -> Bool
forall a. Eq a => a -> a -> Bool
/= Posting
p) ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t
relatedPostings Posting
_ = []
isPostingInDateSpan :: DateSpan -> Posting -> Bool
isPostingInDateSpan :: DateSpan -> Posting -> Bool
isPostingInDateSpan = WhichDate -> DateSpan -> Posting -> Bool
isPostingInDateSpan' WhichDate
PrimaryDate
isPostingInDateSpan' :: WhichDate -> DateSpan -> Posting -> Bool
isPostingInDateSpan' :: WhichDate -> DateSpan -> Posting -> Bool
isPostingInDateSpan' WhichDate
PrimaryDate DateSpan
s = DateSpan -> Day -> Bool
spanContainsDate DateSpan
s (Day -> Bool) -> (Posting -> Day) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> Day
postingDate
isPostingInDateSpan' WhichDate
SecondaryDate DateSpan
s = DateSpan -> Day -> Bool
spanContainsDate DateSpan
s (Day -> Bool) -> (Posting -> Day) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> Day
postingDate2
isEmptyPosting :: Posting -> Bool
isEmptyPosting :: Posting -> Bool
isEmptyPosting = MixedAmount -> Bool
mixedAmountLooksZero (MixedAmount -> Bool)
-> (Posting -> MixedAmount) -> Posting -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> MixedAmount
pamount
postingApplyAliases :: [AccountAlias] -> Posting -> Either RegexError Posting
postingApplyAliases :: [AccountAlias] -> Posting -> Either FilePath Posting
postingApplyAliases [AccountAlias]
aliases p :: Posting
p@Posting{AccountName
paccount :: AccountName
paccount :: Posting -> AccountName
paccount} =
case [AccountAlias] -> AccountName -> Either FilePath AccountName
accountNameApplyAliases [AccountAlias]
aliases AccountName
paccount of
Right AccountName
a -> Posting -> Either FilePath Posting
forall a b. b -> Either a b
Right Posting
p{paccount :: AccountName
paccount=AccountName
a}
Left FilePath
e -> FilePath -> Either FilePath Posting
forall a b. a -> Either a b
Left FilePath
err
where
err :: FilePath
err = FilePath
"problem while applying account aliases:\n" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ [AccountAlias] -> FilePath
forall a. Show a => a -> FilePath
pshow [AccountAlias]
aliases
FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\n to account name: "FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++AccountName -> FilePath
T.unpack AccountName
paccountFilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++FilePath
"\n "FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++FilePath
e
postingApplyCommodityStyles :: M.Map CommoditySymbol AmountStyle -> Posting -> Posting
postingApplyCommodityStyles :: Map AccountName AmountStyle -> Posting -> Posting
postingApplyCommodityStyles Map AccountName AmountStyle
styles Posting
p = Posting
p{pamount :: MixedAmount
pamount=Map AccountName AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount Map AccountName AmountStyle
styles (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p
,pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion=BalanceAssertion -> BalanceAssertion
fixbalanceassertion (BalanceAssertion -> BalanceAssertion)
-> Maybe BalanceAssertion -> Maybe BalanceAssertion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Posting -> Maybe BalanceAssertion
pbalanceassertion Posting
p}
where
fixbalanceassertion :: BalanceAssertion -> BalanceAssertion
fixbalanceassertion BalanceAssertion
ba = BalanceAssertion
ba{baamount :: Amount
baamount=Map AccountName AmountStyle -> Amount -> Amount
styleAmountExceptPrecision Map AccountName AmountStyle
styles (Amount -> Amount) -> Amount -> Amount
forall a b. (a -> b) -> a -> b
$ BalanceAssertion -> Amount
baamount BalanceAssertion
ba}
postingAddTags :: Posting -> [Tag] -> Posting
postingAddTags :: Posting -> [Tag] -> Posting
postingAddTags p :: Posting
p@Posting{[Tag]
ptags :: [Tag]
ptags :: Posting -> [Tag]
ptags} [Tag]
tags = Posting
p{ptags :: [Tag]
ptags=[Tag]
ptags [Tag] -> [Tag] -> [Tag]
forall a. Eq a => [a] -> [a] -> [a]
`union` [Tag]
tags}
postingApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Day -> Day -> ValuationType -> Posting -> Posting
postingApplyValuation :: PriceOracle
-> Map AccountName AmountStyle
-> Day
-> Day
-> ValuationType
-> Posting
-> Posting
postingApplyValuation PriceOracle
priceoracle Map AccountName AmountStyle
styles Day
periodlast Day
today ValuationType
v Posting
p =
(MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount (PriceOracle
-> Map AccountName AmountStyle
-> Day
-> Day
-> Day
-> ValuationType
-> MixedAmount
-> MixedAmount
mixedAmountApplyValuation PriceOracle
priceoracle Map AccountName AmountStyle
styles Day
periodlast Day
today (Posting -> Day
postingDate Posting
p) ValuationType
v) Posting
p
postingToCost :: M.Map CommoditySymbol AmountStyle -> ConversionOp -> Posting -> Posting
postingToCost :: Map AccountName AmountStyle -> ConversionOp -> Posting -> Posting
postingToCost Map AccountName AmountStyle
_ ConversionOp
NoConversionOp Posting
p = Posting
p
postingToCost Map AccountName AmountStyle
styles ConversionOp
ToCost Posting
p = (MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount (Map AccountName AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount Map AccountName AmountStyle
styles (MixedAmount -> MixedAmount)
-> (MixedAmount -> MixedAmount) -> MixedAmount -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> MixedAmount
mixedAmountCost) Posting
p
postingAddInferredEquityPostings :: Text -> Posting -> [Posting]
postingAddInferredEquityPostings :: AccountName -> Posting -> [Posting]
postingAddInferredEquityPostings AccountName
equityAcct Posting
p = Posting
taggedPosting Posting -> [Posting] -> [Posting]
forall a. a -> [a] -> [a]
: (Amount -> [Posting]) -> [Amount] -> [Posting]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Amount -> [Posting]
conversionPostings [Amount]
priceAmounts
where
taggedPosting :: Posting
taggedPosting
| [Amount] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Amount]
priceAmounts = Posting
p
| Bool
otherwise = Posting
p{ pcomment :: AccountName
pcomment = Posting -> AccountName
pcomment Posting
p AccountName -> Tag -> AccountName
`commentAddTag` Tag
priceTag
, ptags :: [Tag]
ptags = Tag
priceTag Tag -> [Tag] -> [Tag]
forall a. a -> [a] -> [a]
: Posting -> [Tag]
ptags Posting
p
}
conversionPostings :: Amount -> [Posting]
conversionPostings Amount
amt = case Amount -> Maybe AmountPrice
aprice Amount
amt of
Maybe AmountPrice
Nothing -> []
Just AmountPrice
_ -> [ Posting
cp{ paccount :: AccountName
paccount = AccountName
accountPrefix AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
amtCommodity
, pamount :: MixedAmount
pamount = Amount -> MixedAmount
mixedAmount (Amount -> MixedAmount)
-> (Amount -> Amount) -> Amount -> MixedAmount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Amount
forall a. Num a => a -> a
negate (Amount -> MixedAmount) -> Amount -> MixedAmount
forall a b. (a -> b) -> a -> b
$ Amount -> Amount
amountStripPrices Amount
amt
}
, Posting
cp{ paccount :: AccountName
paccount = AccountName
accountPrefix AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
costCommodity
, pamount :: MixedAmount
pamount = Amount -> MixedAmount
mixedAmount Amount
cost
}
]
where
cost :: Amount
cost = Amount -> Amount
amountCost Amount
amt
amtCommodity :: AccountName
amtCommodity = Amount -> AccountName
commodity Amount
amt
costCommodity :: AccountName
costCommodity = Amount -> AccountName
commodity Amount
cost
cp :: Posting
cp = Posting
p{ pcomment :: AccountName
pcomment = Posting -> AccountName
pcomment Posting
p AccountName -> Tag -> AccountName
`commentAddTag` (AccountName
"generated-posting",AccountName
"")
, ptags :: [Tag]
ptags = [(AccountName
"generated-posting", AccountName
""), (AccountName
"_generated-posting", AccountName
"")]
, pbalanceassertion :: Maybe BalanceAssertion
pbalanceassertion = Maybe BalanceAssertion
forall a. Maybe a
Nothing
, poriginal :: Maybe Posting
poriginal = Maybe Posting
forall a. Maybe a
Nothing
}
accountPrefix :: AccountName
accountPrefix = [AccountName] -> AccountName
forall a. Monoid a => [a] -> a
mconcat [ AccountName
equityAcct, AccountName
":", AccountName -> [AccountName] -> AccountName
T.intercalate AccountName
"-" ([AccountName] -> AccountName) -> [AccountName] -> AccountName
forall a b. (a -> b) -> a -> b
$ [AccountName] -> [AccountName]
forall a. Ord a => [a] -> [a]
sort [AccountName
amtCommodity, AccountName
costCommodity], AccountName
":"]
commodity :: Amount -> AccountName
commodity = [AccountName] -> AccountName
T.unwords ([AccountName] -> AccountName)
-> (Amount -> [AccountName]) -> Amount -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountName -> Bool) -> [AccountName] -> [AccountName]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (AccountName -> Bool) -> AccountName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> Bool
T.null) ([AccountName] -> [AccountName])
-> (Amount -> [AccountName]) -> Amount -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> [AccountName]
T.words (AccountName -> [AccountName])
-> (Amount -> AccountName) -> Amount -> [AccountName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> AccountName
acommodity
priceTag :: Tag
priceTag = (AccountName
"cost", AccountName -> AccountName
T.strip (AccountName -> AccountName)
-> (WideBuilder -> AccountName) -> WideBuilder -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WideBuilder -> AccountName
wbToText (WideBuilder -> AccountName) -> WideBuilder -> AccountName
forall a b. (a -> b) -> a -> b
$ (Amount -> WideBuilder) -> [Amount] -> WideBuilder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Amount -> WideBuilder
showAmountPrice [Amount]
priceAmounts)
priceAmounts :: [Amount]
priceAmounts = (Amount -> Bool) -> [Amount] -> [Amount]
forall a. (a -> Bool) -> [a] -> [a]
filter (Maybe AmountPrice -> Bool
forall a. Maybe a -> Bool
isJust (Maybe AmountPrice -> Bool)
-> (Amount -> Maybe AmountPrice) -> Amount -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Amount -> Maybe AmountPrice
aprice) ([Amount] -> [Amount])
-> (MixedAmount -> [Amount]) -> MixedAmount -> [Amount]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amountsRaw (MixedAmount -> [Amount]) -> MixedAmount -> [Amount]
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p
postingTransformAmount :: (MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount :: (MixedAmount -> MixedAmount) -> Posting -> Posting
postingTransformAmount MixedAmount -> MixedAmount
f p :: Posting
p@Posting{pamount :: Posting -> MixedAmount
pamount=MixedAmount
a} = Posting
p{pamount :: MixedAmount
pamount=MixedAmount -> MixedAmount
f MixedAmount
a}
commentJoin :: Text -> Text -> Text
AccountName
c1 AccountName
c2
| AccountName -> Bool
T.null AccountName
c1 = AccountName
c2
| AccountName -> Bool
T.null AccountName
c2 = AccountName
c1
| Bool
otherwise = AccountName
c1 AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
", " AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
c2
commentAddTag :: Text -> Tag -> Text
AccountName
c (AccountName
t,AccountName
v)
| AccountName -> Bool
T.null AccountName
c' = AccountName
tag
| Bool
otherwise = AccountName
c' AccountName -> AccountName -> AccountName
`commentJoin` AccountName
tag
where
c' :: AccountName
c' = AccountName -> AccountName
T.stripEnd AccountName
c
tag :: AccountName
tag = AccountName
t AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
": " AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
v
commentAddTagNextLine :: Text -> Tag -> Text
AccountName
cmt (AccountName
t,AccountName
v) =
AccountName
cmt AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> (if AccountName
"\n" AccountName -> AccountName -> Bool
`T.isSuffixOf` AccountName
cmt then AccountName
"" else AccountName
"\n") AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
t AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
": " AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
v
tests_Posting :: TestTree
tests_Posting = FilePath -> [TestTree] -> TestTree
testGroup FilePath
"Posting" [
FilePath -> Assertion -> TestTree
testCase FilePath
"accountNamePostingType" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
AccountName -> PostingType
accountNamePostingType AccountName
"a" PostingType -> PostingType -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= PostingType
RegularPosting
AccountName -> PostingType
accountNamePostingType AccountName
"(a)" PostingType -> PostingType -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= PostingType
VirtualPosting
AccountName -> PostingType
accountNamePostingType AccountName
"[a]" PostingType -> PostingType -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= PostingType
BalancedVirtualPosting
,FilePath -> Assertion -> TestTree
testCase FilePath
"accountNameWithoutPostingType" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
AccountName -> AccountName
accountNameWithoutPostingType AccountName
"(a)" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"a"
,FilePath -> Assertion -> TestTree
testCase FilePath
"accountNameWithPostingType" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
PostingType -> AccountName -> AccountName
accountNameWithPostingType PostingType
VirtualPosting AccountName
"[a]" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"(a)"
,FilePath -> Assertion -> TestTree
testCase FilePath
"joinAccountNames" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
AccountName
"a" AccountName -> AccountName -> AccountName
`joinAccountNames` AccountName
"b:c" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"a:b:c"
AccountName
"a" AccountName -> AccountName -> AccountName
`joinAccountNames` AccountName
"(b:c)" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"(a:b:c)"
AccountName
"[a]" AccountName -> AccountName -> AccountName
`joinAccountNames` AccountName
"(b:c)" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"[a:b:c]"
AccountName
"" AccountName -> AccountName -> AccountName
`joinAccountNames` AccountName
"a" AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"a"
,FilePath -> Assertion -> TestTree
testCase FilePath
"concatAccountNames" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
[AccountName] -> AccountName
concatAccountNames [] AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
""
[AccountName] -> AccountName
concatAccountNames [AccountName
"a",AccountName
"(b)",AccountName
"[c:d]"] AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"(a:b:c:d)"
,FilePath -> Assertion -> TestTree
testCase FilePath
"commentAddTag" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
AccountName -> Tag -> AccountName
commentAddTag AccountName
"" (AccountName
"a",AccountName
"") AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"a: "
AccountName -> Tag -> AccountName
commentAddTag AccountName
"[1/2]" (AccountName
"a",AccountName
"") AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"[1/2], a: "
,FilePath -> Assertion -> TestTree
testCase FilePath
"commentAddTagNextLine" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
AccountName -> Tag -> AccountName
commentAddTagNextLine AccountName
"" (AccountName
"a",AccountName
"") AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"\na: "
AccountName -> Tag -> AccountName
commentAddTagNextLine AccountName
"[1/2]" (AccountName
"a",AccountName
"") AccountName -> AccountName -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= AccountName
"[1/2]\na: "
]