csv: try to preserve order of same-day transactions

If the CSV records appear to have been in reverse date order,
we'll now reverse them all before also sorting by transaction date,
so that the original order of same-day transactions is preserved.
We detect this using a simple heuristic: if the first converted
transaction's date is later than the last's.
This commit is contained in:
Simon Michael 2014-12-02 11:16:51 -08:00
parent 733a7b12ef
commit 1708f0b441

View File

@ -121,7 +121,13 @@ readJournalFromCsv mrulesfile csvfile csvdata =
let txns = snd $ mapAccumL
(\pos r -> (pos, transactionFromCsvRecord (incSourceLine pos 1) rules r))
(initialPos parsecfilename) records
return $ Right nulljournal{jtxns=sortBy (comparing tdate) txns}
-- heuristic: if the records appear to have been in reverse date order,
-- reverse them all as well as doing a txn date sort,
-- so that same-day txns' original order is preserved
txns' | length txns > 1 && tdate (head txns) > tdate (last txns) = reverse txns
| otherwise = txns
return $ Right nulljournal{jtxns=sortBy (comparing tdate) txns'}
parseCsv :: FilePath -> String -> IO (Either ParseError CSV)
parseCsv path csvdata =