;csv: refactor transactionFromCsvRecord

This commit is contained in:
Simon Michael 2020-02-27 12:58:30 -08:00
parent 2dd6e2d797
commit bc4ea83d86

View File

@ -849,35 +849,32 @@ transactionFromCsvRecord sourcepos rules record = t
("account"++n) ("amount"++n) ("amount"++n++"-in") ("account"++n) ("amount"++n) ("amount"++n++"-in")
("amount"++n++"-out") ("balance"++n) ("comment"++n) t ("amount"++n++"-out") ("balance"++n) ("comment"++n) t
-- Adjust the postings to mimic some pre-1.16 behaviour, for compatibility. -- Auto-generate a second posting or second posting amount,
-- And also, wherever default "unknown" accounts were used, refine these -- for compatibility with pre-1.16 rules.
-- based on the sign of the posting amount if it's known.
-- XXX split
postings' = postings' =
case postings of case postings of
-- when rules generate just one posting, and it's a type that needs to -- when rules generate just one posting, of a kind that needs to be
-- be balanced, generate the second posting to balance it. -- balanced, generate the second posting to balance it.
[(p1,final)] -> [p1@(p1',_)] ->
if ptype p1 == VirtualPosting if ptype p1' == VirtualPosting then [p1] else [p1, p2]
then [improveUnless final p1]
else [improveUnless final p1, improveUnknownAccountName p2]
where where
p2 = nullposting{paccount=unknownExpenseAccount p2 = (nullposting{paccount=unknownExpenseAccount
,pamount=costOfMixedAmount (-pamount p1) ,pamount=costOfMixedAmount (-pamount p1')
,ptransaction=Just t} ,ptransaction=Just t}, False)
-- when rules generate exactly two postings, and only the second has -- when rules generate exactly two postings, and only the second has
-- no amount, give it the balancing amount. -- no amount, give it the balancing amount.
[(p1,final1), (p2,final2)] -> [p1@(p1',_), p2@(p2',final2)] ->
if hasAmount p1 && not (hasAmount p2) if hasAmount p1' && not (hasAmount p2')
then [improveUnless final1 p1, improveUnless final2 p2{pamount=costOfMixedAmount(-(pamount p1))}] then [p1, (p2'{pamount=costOfMixedAmount(-(pamount p1'))}, final2)]
else [improveUnless final1 p1, improveUnless final2 p2] else [p1, p2]
--
-- otherwise, just refine any unknown account names. ps -> ps
ps -> [improveUnless final p | (p,final) <- ps]
-- Finally, wherever default "unknown" accounts were used, refine them
-- based on the sign of the posting amount if it's now known.
postings'' = map maybeImprove postings'
where where
improveUnless final = if final then id else improveUnknownAccountName maybeImprove (p,final) = if final then p else improveUnknownAccountName p
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- 4. Build the transaction (and name it, so the postings can reference it). -- 4. Build the transaction (and name it, so the postings can reference it).
@ -891,7 +888,7 @@ transactionFromCsvRecord sourcepos rules record = t
,tdescription = T.pack description ,tdescription = T.pack description
,tcomment = T.pack comment ,tcomment = T.pack comment
,tprecedingcomment = T.pack precomment ,tprecedingcomment = T.pack precomment
,tpostings = postings' ,tpostings = postings''
} }
-- | Given CSV rules and a CSV record, generate the corresponding transaction's -- | Given CSV rules and a CSV record, generate the corresponding transaction's