lib: Allow balance-only entries in csv reader
This commit is contained in:
		
							parent
							
								
									cd5c74625e
								
							
						
					
					
						commit
						9e6e06033f
					
				| @ -228,6 +228,7 @@ postingAsLines elideamount onelineamounts ps p = concat [ | |||||||
|     shownAmounts |     shownAmounts | ||||||
|       | elideamount    = [""] |       | elideamount    = [""] | ||||||
|       | onelineamounts = [fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p] |       | 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 |       | otherwise      = map (fitStringMulti (Just amtwidth) Nothing False False . showAmount ) . amounts $ pamount p | ||||||
|       where |       where | ||||||
|         amtwidth = maximum $ 12 : map (strWidth . showMixedAmount . pamount) ps  -- min. 12 for backwards compatibility |         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 = [ | tests_postingAsLines = [ | ||||||
|    "postingAsLines" ~: do |    "postingAsLines" ~: do | ||||||
|     let p `gives` ls = assertEqual (show p) ls (postingAsLines False False [p] p) |     let p `gives` ls = assertEqual (show p) ls (postingAsLines False False [p] p) | ||||||
|     posting `gives` [] |     posting `gives` [""] | ||||||
|     posting{ |     posting{ | ||||||
|       pstatus=Cleared, |       pstatus=Cleared, | ||||||
|       paccount="a", |       paccount="a", | ||||||
|  | |||||||
| @ -649,10 +649,10 @@ transactionFromCsvRecord sourcepos rules record = t | |||||||
|     comment     = maybe "" render $ mfieldtemplate "comment" |     comment     = maybe "" render $ mfieldtemplate "comment" | ||||||
|     precomment  = maybe "" render $ mfieldtemplate "precomment" |     precomment  = maybe "" render $ mfieldtemplate "precomment" | ||||||
|     currency    = maybe (fromMaybe "" mdefaultcurrency) render $ mfieldtemplate "currency" |     currency    = maybe (fromMaybe "" mdefaultcurrency) render $ mfieldtemplate "currency" | ||||||
|     amountstr   = (currency++) $ simplifySign $ getAmountStr rules record |     amountstr   = (currency++) <$> simplifySign <$> getAmountStr rules record | ||||||
|     amount      = either amounterror (Mixed . (:[])) $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack amountstr |     maybeamount      = either amounterror (Mixed . (:[])) <$> runParser (evalStateT (amountp <* eof) mempty) "" <$> T.pack <$> amountstr | ||||||
|     amounterror err = error' $ unlines |     amounterror err = error' $ unlines | ||||||
|       ["error: could not parse \""++amountstr++"\" as an amount" |       ["error: could not parse \""++fromJust amountstr++"\" as an amount" | ||||||
|       ,showRecord record |       ,showRecord record | ||||||
|       ,"the amount rule is:      "++(fromMaybe "" $ mfieldtemplate "amount") |       ,"the amount rule is:      "++(fromMaybe "" $ mfieldtemplate "amount") | ||||||
|       ,"the currency rule is:    "++(fromMaybe "unspecified" $ mfieldtemplate "currency") |       ,"the currency rule is:    "++(fromMaybe "unspecified" $ mfieldtemplate "currency") | ||||||
| @ -662,10 +662,13 @@ transactionFromCsvRecord sourcepos rules record = t | |||||||
|        ++"change your amount or currency rules, " |        ++"change your amount or currency rules, " | ||||||
|        ++"or "++maybe "add a" (const "change your") mskip++" skip rule" |        ++"or "++maybe "add a" (const "change your") mskip++" skip rule" | ||||||
|       ] |       ] | ||||||
|     amount1        = amount |     amount1 = case maybeamount of | ||||||
|     -- convert balancing amount to cost like hledger print, so eg if  |                 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". |     -- 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 |     s `or` def  = if null s then def else s | ||||||
|     defaccount1 = fromMaybe "unknown" $ mdirective "default-account1" |     defaccount1 = fromMaybe "unknown" $ mdirective "default-account1" | ||||||
|     defaccount2 = case isNegativeMixedAmount amount2 of |     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 = | getAmountStr rules record = | ||||||
|  let |  let | ||||||
|    mamount    = getEffectiveAssignment rules record "amount" |    mamount    = getEffectiveAssignment rules record "amount" | ||||||
| @ -711,11 +714,11 @@ getAmountStr rules record = | |||||||
|    render     = fmap (strip . renderTemplate rules record) |    render     = fmap (strip . renderTemplate rules record) | ||||||
|  in |  in | ||||||
|   case (render mamount, render mamountin, render mamountout) of |   case (render mamount, render mamountin, render mamountout) of | ||||||
|     (Just "", Nothing, Nothing) -> error' $ "amount has no value\n"++showRecord record |     (Just "", Nothing, Nothing) -> Nothing | ||||||
|     (Just a,  Nothing, Nothing) -> a |     (Just a,  Nothing, Nothing) -> Just a | ||||||
|     (Nothing, Just "", Just "") -> error' $ "neither amount-in or amount-out has a value\n"++showRecord record |     (Nothing, Just "", Just "") -> error' $ "neither amount-in or amount-out has a value\n"++showRecord record | ||||||
|     (Nothing, Just i,  Just "") -> i |     (Nothing, Just i,  Just "") -> Just i | ||||||
|     (Nothing, Just "", Just o)  -> negateStr o |     (Nothing, Just "", Just o)  -> Just $ negateStr o | ||||||
|     (Nothing, Just _,  Just _)  -> error' $ "both amount-in and amount-out have a value\n"++showRecord record |     (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 |     _                           -> error' $ "found values for amount and for amount-in/amount-out - please use either amount or amount-in/amount-out\n"++showRecord record | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user