From 2c771f3d7ff359371f329c0ab8d607f18a709a74 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Sun, 10 Apr 2011 15:01:26 +0000 Subject: [PATCH] Allow amounts with total price in the input to "hledger convert" --- MANUAL.md | 9 +++++++++ hledger/Hledger/Cli/Convert.hs | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index 9092e342c..fa26f38ee 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -578,6 +578,15 @@ Notes: matched text with `\0` and any regex groups with `\1`, `\2` in the usual way. +- Amount may contain the total or per-unit price. For example, lets + assume that your base account "bank-current" is in GBP, and your CSV specifies amount of "10 + USD @@ 15 GBP", and account-assigning rules selected account + "travel-expenses" for this transaction. As a result, + "travel-expenses" would be credited by "10 USD @@ 15 GBP", and + "bank-current" would be debited by "-15 GBP". This way you could + track the expenses in the currencies there were made, while + keeping your base account in single currency + ### histogram The histogram command displays a quick bar chart showing transaction diff --git a/hledger/Hledger/Cli/Convert.hs b/hledger/Hledger/Cli/Convert.hs index 810a5f825..c76193ad8 100644 --- a/hledger/Hledger/Cli/Convert.hs +++ b/hledger/Hledger/Cli/Convert.hs @@ -10,7 +10,7 @@ import Hledger.Data.Types (Journal,AccountName,Transaction(..),Posting(..),Posti import Hledger.Data.Utils (strip, spacenonewline, restofline, parseWithCtx, assertParse, assertParseEqual, error') import Hledger.Read.JournalReader (someamount,ledgeraccountname) import Hledger.Data.Journal (nullctx) -import Hledger.Data.Amount (nullmixedamt) +import Hledger.Data.Amount (nullmixedamt, costOfMixedAmount) import Safe (atDef, maximumDef) import System.IO (stderr) import Text.CSV (parseCSVFromFile, printCSV) @@ -284,6 +284,9 @@ transactionFromCsvRecord rules fields = amountstr'' = currency ++ amountstr' amountparse = runParser someamount nullctx "" amountstr'' amount = either (const nullmixedamt) id amountparse + -- Using costOfMixedAmount here to allow complex costs like "10 GBP @@ 15 USD". + -- Aim is to have "10 GBP @@ 15 USD" applied to account "acct", but have "-15USD" applied to "baseacct" + baseamount = costOfMixedAmount amount unknownacct | (readDef 0 amountstr' :: Double) < 0 = "income:unknown" | otherwise = "expenses:unknown" (acct,newdesc) = identify (accountRules rules) unknownacct desc @@ -309,7 +312,7 @@ transactionFromCsvRecord rules fields = Posting { pstatus=False, paccount=baseAccount rules, - pamount=(-amount), + pamount=(-baseamount), pcomment="", ptype=RegularPosting, pmetadata=[],