fix: csv: tags on following lines, and posting dates, also work now [#2241]

Follow-on work from #2214.
This commit is contained in:
Simon Michael 2024-09-28 18:54:43 -10:00
parent b28468e651
commit 823be7c565
3 changed files with 32 additions and 11 deletions

View File

@ -76,7 +76,7 @@ import Text.Printf (printf)
import Hledger.Data
import Hledger.Utils
import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep, commenttagsp )
import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep, transactioncommentp, postingcommentp )
import Hledger.Write.Csv
import System.Directory (doesFileExist, getHomeDirectory)
import Data.Either (fromRight)
@ -1111,7 +1111,13 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
code = maybe "" singleline' $ fieldval "code"
description = maybe "" singleline' $ fieldval "description"
comment = maybe "" unescapeNewlines $ fieldval "comment"
ttags = fromRight [] $ rtp commenttagsp comment
-- Convert some parsed comment text back into following comment syntax,
-- with the semicolons and indents, so it can be parsed again for tags.
textToFollowingComment :: Text -> Text
textToFollowingComment = T.stripStart . T.unlines . map (" ;"<>) . T.lines
ttags = fromRight [] $ fmap snd $ rtp transactioncommentp $ textToFollowingComment comment
precomment = maybe "" unescapeNewlines $ fieldval "precomment"
singleline' = T.unwords . filter (not . T.null) . map T.strip . T.lines
@ -1124,7 +1130,15 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
p1IsVirtual = (accountNamePostingType <$> fieldval "account1") == Just VirtualPosting
ps = [p | n <- [1..maxpostings]
,let cmt = maybe "" unescapeNewlines $ fieldval ("comment"<> T.pack (show n))
,let ptags = fromRight [] $ rtp commenttagsp cmt
-- Tags in the comment will be parsed and attached to the posting.
-- A posting date, in the date: tag or in brackets, will also be parsed and applied to the posting.
-- But it must have a year, or it will be ignored.
-- A secondary posting date will also be ignored.
,let (tags,mdate) =
fromRight ([],Nothing) $
fmap (\(_,ts,md,_)->(ts,md)) $
rtp (postingcommentp Nothing) $
textToFollowingComment cmt
,let currency = fromMaybe "" (fieldval ("currency"<> T.pack (show n)) <|> fieldval "currency")
,let mamount = getAmount rules record currency p1IsVirtual n
,let mbalance = getBalance rules record currency n
@ -1132,12 +1146,13 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t
,let acct' | not isfinal && acct==unknownExpenseAccount &&
fromMaybe False (mamount >>= isNegativeMixedAmount) = unknownIncomeAccount
| otherwise = acct
,let p = nullposting{paccount = accountNameWithoutPostingType acct'
,let p = nullposting{pdate = mdate
,paccount = accountNameWithoutPostingType acct'
,pamount = fromMaybe missingmixedamt mamount
,ptransaction = Just t
,pbalanceassertion = mkBalanceAssertion rules record <$> mbalance
,pcomment = cmt
,ptags = ptags
,ptags = tags
,ptype = accountNamePostingType acct
}
]

View File

@ -3351,6 +3351,8 @@ You can assign multi-line comments by writing literal `\n` in the code. A commen
Comments can contain [tags](#tags), as usual.
Posting comments can also contain a [posting date](#posting-dates). A secondary date, or a year-less date, will be ignored.
### account field
Assigning to `accountN`, where N is 1 to 99,

View File

@ -1094,26 +1094,30 @@ $ ./csvtest.sh
>2 /unexpected space/
>=1
# ** 56. match transaction by posting tag (#2114)
# ** 56. make sure transaction tags are functional (#2114), including ones on subsequent lines (#2241)
<
2020-01-01, 1
RULES
fields date, desc
if %desc 1
comment ttag:tval
comment1 ptag:pval
comment ttag:tval\nttag2:
comment1 ptag:pval\ndate:2022-02-02
account1 a
$ ./csvtest.sh tag:ptag
$ ./csvtest.sh tag:ttag2
2020-01-01 ; ttag:tval
; ttag2:
a ; ptag:pval
; date:2022-02-02
>=
# ** 57. match transaction by transaction tag (#2114)
$ ./csvtest.sh tag:ttag
# ** 57. and also posting tags, and (primary, yearful) posting dates (#2114, #2241)
$ ./csvtest.sh tag:date
2020-01-01 ; ttag:tval
; ttag2:
a ; ptag:pval
; date:2022-02-02
>=