Ensure showTransaction produce a valid journal (#466)

- Make output of print to be a valid journal
- Partially reverts 419f5f2a2

Fixes simonmichael/hledger#465
This commit is contained in:
Nikolay Orlyuk 2017-01-11 06:44:10 +02:00 committed by Simon Michael
parent aae61f46e2
commit 32d9365fc2
3 changed files with 18 additions and 14 deletions

View File

@ -71,10 +71,10 @@ main = do
q = queryFromOpts today ropts_ q = queryFromOpts today ropts_
(acctbals,_) = balanceReport ropts_ q j (acctbals,_) = balanceReport ropts_ q j
balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals
ps = [posting{paccount=a, pamount=Mixed [b]} | (a,_,_,Mixed bs) <- acctbals, b <- bs] ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals]
++ [posting{paccount="equity:opening balances", pamount=balancingamt}] ++ [posting{paccount="equity:opening balances", pamount=balancingamt}]
enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q
nps = [posting{paccount=a, pamount=negate (Mixed [b])} | (a,_,_,Mixed bs) <- acctbals, b <- bs] nps = [posting{paccount=a, pamount=negate b} | (a,_,_,b) <- acctbals]
++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}] ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}]
putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps}) putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps})
putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps}) putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps})

View File

@ -132,7 +132,8 @@ tests_showTransactionUnelided = [
`gives` unlines [ `gives` unlines [
"2012/05/14=2012/05/15 (code) desc ; tcomment1", "2012/05/14=2012/05/15 (code) desc ; tcomment1",
" ; tcomment2", " ; tcomment2",
" $1.00", " * a $1.00",
" ; pcomment2",
" * a 2.00h", " * a 2.00h",
" ; pcomment2", " ; pcomment2",
"" ""
@ -186,11 +187,12 @@ postingsAsLines elide onelineamounts t ps
| otherwise = concatMap (postingAsLines False onelineamounts ps) ps | otherwise = concatMap (postingAsLines False onelineamounts ps) ps
postingAsLines :: Bool -> Bool -> [Posting] -> Posting -> [String] postingAsLines :: Bool -> Bool -> [Posting] -> Posting -> [String]
postingAsLines elideamount onelineamounts ps p = postingAsLines elideamount onelineamounts ps p = concat [
postinglines postingblock
++ newlinecomments ++ newlinecomments
| postingblock <- postingblocks]
where where
postinglines = map rstrip $ lines $ concatTopPadded [account, " ", amount, assertion, samelinecomment] postingblocks = [map rstrip $ lines $ concatTopPadded [account, " ", amount, assertion, samelinecomment] | amount <- shownAmounts]
assertion = maybe "" ((" = " ++) . showAmount) $ pbalanceassertion p assertion = maybe "" ((" = " ++) . showAmount) $ pbalanceassertion p
account = account =
indent $ indent $
@ -200,10 +202,10 @@ postingAsLines elideamount onelineamounts ps p =
acctwidth = maximum $ map (textWidth . paccount) ps acctwidth = maximum $ map (textWidth . paccount) ps
-- currently prices are considered part of the amount string when right-aligning amounts -- currently prices are considered part of the amount string when right-aligning amounts
amount shownAmounts
| elideamount = "" | elideamount = [""]
| onelineamounts = fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p | onelineamounts = [fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p]
| otherwise = fitStringMulti (Just amtwidth) Nothing False False $ showMixedAmount $ 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
@ -222,8 +224,8 @@ showPostingLine p =
tests_postingAsLines = [ tests_postingAsLines = [
"postingAsLines" ~: do "postingAsLines" ~: do
let p `gives` ls = assertEqual "" ls (postingAsLines False False [p] p) let p `gives` ls = assertEqual (show p) ls (postingAsLines False False [p] p)
posting `gives` [" 0"] posting `gives` []
posting{ posting{
pstatus=Cleared, pstatus=Cleared,
paccount="a", paccount="a",
@ -233,7 +235,9 @@ tests_postingAsLines = [
ptags=[("ptag1","val1"),("ptag2","val2")] ptags=[("ptag1","val1"),("ptag2","val2")]
} }
`gives` [ `gives` [
" $1.00", " * a $1.00 ; pcomment1",
" ; pcomment2",
" ; tag3: val3 ",
" * a 2.00h ; pcomment1", " * a 2.00h ; pcomment1",
" ; pcomment2", " ; pcomment2",
" ; tag3: val3 " " ; tag3: val3 "

View File

@ -10,7 +10,7 @@ hledger -f - print
2010/01/01 2010/01/01
a EUR 1 ; a euro a EUR 1 ; a euro
b USD 1 ; a dollar b USD 1 ; a dollar
EUR -1 c EUR -1 ; a euro and a dollar
c USD -1 ; a euro and a dollar c USD -1 ; a euro and a dollar
>>>=0 >>>=0