lib: Allow balance-only entries in csv reader

This commit is contained in:
Nadrieril 2017-12-12 23:51:20 +00:00 committed by Simon Michael
parent cd5c74625e
commit 9e6e06033f
2 changed files with 16 additions and 12 deletions

View File

@ -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",

View File

@ -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