From 9e6e06033f61e41932531f53c90b34b6872e085e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 12 Dec 2017 23:51:20 +0000 Subject: [PATCH] lib: Allow balance-only entries in csv reader --- hledger-lib/Hledger/Data/Transaction.hs | 3 ++- hledger-lib/Hledger/Read/CsvReader.hs | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 80a6895f7..ba74ae1c6 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -228,6 +228,7 @@ postingAsLines elideamount onelineamounts ps p = concat [ shownAmounts | elideamount = [""] | onelineamounts = [fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p] + | null (amounts $ pamount p) = [""] | otherwise = map (fitStringMulti (Just amtwidth) Nothing False False . showAmount ) . amounts $ pamount p where amtwidth = maximum $ 12 : map (strWidth . showMixedAmount . pamount) ps -- min. 12 for backwards compatibility @@ -254,7 +255,7 @@ showPostingLines p = postingAsLines False False ps p where tests_postingAsLines = [ "postingAsLines" ~: do let p `gives` ls = assertEqual (show p) ls (postingAsLines False False [p] p) - posting `gives` [] + posting `gives` [""] posting{ pstatus=Cleared, paccount="a", diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index dcaa0c487..33a7e0b98 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -649,10 +649,10 @@ transactionFromCsvRecord sourcepos rules record = t comment = maybe "" render $ mfieldtemplate "comment" precomment = maybe "" render $ mfieldtemplate "precomment" currency = maybe (fromMaybe "" mdefaultcurrency) render $ mfieldtemplate "currency" - amountstr = (currency++) $ simplifySign $ getAmountStr rules record - amount = either amounterror (Mixed . (:[])) $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack amountstr + amountstr = (currency++) <$> simplifySign <$> getAmountStr rules record + maybeamount = either amounterror (Mixed . (:[])) <$> runParser (evalStateT (amountp <* eof) mempty) "" <$> T.pack <$> amountstr amounterror err = error' $ unlines - ["error: could not parse \""++amountstr++"\" as an amount" + ["error: could not parse \""++fromJust amountstr++"\" as an amount" ,showRecord record ,"the amount rule is: "++(fromMaybe "" $ mfieldtemplate "amount") ,"the currency rule is: "++(fromMaybe "unspecified" $ mfieldtemplate "currency") @@ -662,10 +662,13 @@ transactionFromCsvRecord sourcepos rules record = t ++"change your amount or currency rules, " ++"or "++maybe "add a" (const "change your") mskip++" skip rule" ] - amount1 = amount - -- convert balancing amount to cost like hledger print, so eg if + amount1 = case maybeamount of + Just a -> a + Nothing | balance /= Nothing -> nullmixedamt + Nothing -> error' $ "amount and balance have no value\n"++showRecord record + -- convert balancing amount to cost like hledger print, so eg if -- amount1 is "10 GBP @@ 15 USD", amount2 will be "-15 USD". - amount2 = costOfMixedAmount (-amount) + amount2 = costOfMixedAmount (-amount1) s `or` def = if null s then def else s defaccount1 = fromMaybe "unknown" $ mdirective "default-account1" defaccount2 = case isNegativeMixedAmount amount2 of @@ -702,7 +705,7 @@ transactionFromCsvRecord sourcepos rules record = t ] } -getAmountStr :: CsvRules -> CsvRecord -> String +getAmountStr :: CsvRules -> CsvRecord -> Maybe String getAmountStr rules record = let mamount = getEffectiveAssignment rules record "amount" @@ -711,11 +714,11 @@ getAmountStr rules record = render = fmap (strip . renderTemplate rules record) in case (render mamount, render mamountin, render mamountout) of - (Just "", Nothing, Nothing) -> error' $ "amount has no value\n"++showRecord record - (Just a, Nothing, Nothing) -> a + (Just "", Nothing, Nothing) -> Nothing + (Just a, Nothing, Nothing) -> Just a (Nothing, Just "", Just "") -> error' $ "neither amount-in or amount-out has a value\n"++showRecord record - (Nothing, Just i, Just "") -> i - (Nothing, Just "", Just o) -> negateStr o + (Nothing, Just i, Just "") -> Just i + (Nothing, Just "", Just o) -> Just $ negateStr o (Nothing, Just _, Just _) -> error' $ "both amount-in and amount-out have a value\n"++showRecord record _ -> error' $ "found values for amount and for amount-in/amount-out - please use either amount or amount-in/amount-out\n"++showRecord record