journal: also parse Ledger-style "virtual posting costs" - (@), (@@)

This commit is contained in:
Simon Michael 2020-04-08 20:19:44 -07:00
parent 847f924828
commit 3ad2e50ec5
2 changed files with 13 additions and 3 deletions

View File

@ -735,15 +735,17 @@ simplecommoditysymbolp :: TextParser m CommoditySymbol
simplecommoditysymbolp = takeWhile1P Nothing (not . isNonsimpleCommodityChar)
priceamountp :: JournalParser m AmountPrice
priceamountp = (do
priceamountp = label "transaction price" $ do
-- https://www.ledger-cli.org/3.0/doc/ledger3.html#Virtual-posting-costs
parenthesised <- option False $ char '(' >> pure True
char '@'
priceConstructor <- char '@' *> pure TotalPrice <|> pure UnitPrice
when parenthesised $ void $ char ')'
lift (skipMany spacenonewline)
priceAmount <- amountwithoutpricep -- <?> "unpriced amount (specifying a price)"
pure $ priceConstructor priceAmount
) <?> "price amount"
balanceassertionp :: JournalParser m BalanceAssertion
balanceassertionp = do
@ -1363,7 +1365,9 @@ tests_Common = tests "Common" [
,astyle=amountstyle{asprecision=0, asdecimalpoint=Nothing}
}
}
]
,test "unit price, parenthesised" $ assertParse amountp "$10 (@) €0.5"
,test "total price, parenthesised" $ assertParse amountp "$10 (@@) €0.5"
]
,let p = lift (numberp Nothing) :: JournalParser IO (Quantity, Int, Maybe Char, Maybe DigitGroupStyle) in
test "numberp" $ do

View File

@ -549,6 +549,12 @@ There are several ways to record a transaction price:
assets:dollars $-135 ; for $135
```
4.
5. Like 1 and 2, but the `@` or `@@` is parenthesised; this is for
compatibility with Ledger journals
([Virtual posting costs](https://www.ledger-cli.org/3.0/doc/ledger3.html#Virtual-posting-costs)),
and in hledger is equivalent to 1 and 2.
Use the [`-B/--cost`](hledger.html#reporting-options) flag to convert
amounts to their transaction price's commodity, if any.
(mnemonic: "B" is from "cost Basis", as in Ledger).