2009-04-08 09:30:26 +04:00
|
|
|
{-|
|
|
|
|
|
|
|
|
A simple add command to help with data entry.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
module AddCommand
|
|
|
|
where
|
|
|
|
import Ledger
|
|
|
|
import Options
|
|
|
|
import RegisterCommand (showRegisterReport)
|
|
|
|
import System.IO
|
|
|
|
import System.IO.Error
|
|
|
|
import Text.ParserCombinators.Parsec
|
|
|
|
import Utils (ledgerFromStringWithOpts)
|
|
|
|
|
|
|
|
|
|
|
|
-- | Read ledger transactions from the command line, prompting for each
|
|
|
|
-- field, and append them to the ledger file. If the ledger came from
|
|
|
|
-- stdin, this command has no effect.
|
|
|
|
add :: [Opt] -> [String] -> Ledger -> IO ()
|
|
|
|
add opts args l
|
|
|
|
| filepath (rawledger l) == "-" = return ()
|
|
|
|
| otherwise = do
|
|
|
|
hPutStrLn stderr ("Please enter one or more transactions, which will be added to your ledger file.\n\
|
2009-04-08 10:23:49 +04:00
|
|
|
\A blank account or amount ends a transaction, control-d to finish.")
|
2009-04-08 09:30:26 +04:00
|
|
|
ts <- getAndAddTransactions l
|
2009-04-10 11:10:21 +04:00
|
|
|
hPutStrLn stderr $ printf "\nAdded %d transactions to %s ." (length ts) (filepath $ rawledger l)
|
2009-04-08 09:30:26 +04:00
|
|
|
|
|
|
|
-- | Read a number of ledger transactions from the command line,
|
|
|
|
-- prompting, validating, displaying and appending them to the ledger file.
|
|
|
|
getAndAddTransactions :: Ledger -> IO [LedgerTransaction]
|
|
|
|
getAndAddTransactions l = (do
|
|
|
|
today <- getCurrentDay
|
2009-04-10 11:10:21 +04:00
|
|
|
datestr <- askFor "date" (Just $ showDate today)
|
|
|
|
(Just $ \s -> null s ||
|
|
|
|
(isRight $ parse (smartdate >> many spacenonewline >> eof) "" $ lowercase s))
|
|
|
|
let date = fixSmartDate today $ fromparse $ parseSmartDate datestr
|
|
|
|
description <- askFor "description" Nothing (Just $ not . null)
|
2009-04-08 09:30:26 +04:00
|
|
|
ps <- getPostings []
|
2009-04-10 11:10:21 +04:00
|
|
|
|
2009-04-08 09:30:26 +04:00
|
|
|
let t = nullledgertxn{ltdate=date
|
2009-04-10 07:10:58 +04:00
|
|
|
,ltstatus=False
|
2009-04-08 09:30:26 +04:00
|
|
|
,ltdescription=description
|
|
|
|
,ltpostings=ps
|
|
|
|
}
|
2009-04-10 11:10:21 +04:00
|
|
|
-- a final validation, will raise an error if not parseable and balanced
|
|
|
|
putStrLn =<< registerFromString (show t)
|
2009-04-08 10:01:16 +04:00
|
|
|
appendToLedgerFile l $ show t
|
2009-04-08 09:30:26 +04:00
|
|
|
liftM (t:) (getAndAddTransactions l)
|
|
|
|
) `catch` (\e -> if isEOFError e then return [] else ioError e)
|
2009-04-10 11:10:21 +04:00
|
|
|
where parseSmartDate = parse smartdate "" . lowercase
|
2009-04-08 09:30:26 +04:00
|
|
|
|
2009-04-10 11:10:21 +04:00
|
|
|
-- | Read two or more postings interactively.
|
2009-04-08 09:30:26 +04:00
|
|
|
getPostings :: [Posting] -> IO [Posting]
|
|
|
|
getPostings prevps = do
|
2009-04-10 11:10:21 +04:00
|
|
|
account <- askFor "account" Nothing (Just $ \s -> not $ null s && (length prevps < 2))
|
2009-04-08 09:30:26 +04:00
|
|
|
if null account
|
|
|
|
then return prevps
|
|
|
|
else do
|
|
|
|
amount <- liftM (fromparse . parse (someamount <|> return missingamt) "")
|
2009-04-10 11:10:21 +04:00
|
|
|
$ askFor "amount" Nothing (Just $ \s -> not $ null s && null prevps)
|
2009-04-08 09:30:26 +04:00
|
|
|
let p = nullrawposting{paccount=account,pamount=amount}
|
|
|
|
if amount == missingamt
|
|
|
|
then return $ prevps ++ [p]
|
|
|
|
else getPostings $ prevps ++ [p]
|
|
|
|
|
2009-04-10 11:10:21 +04:00
|
|
|
-- | Prompt and read a string value, possibly with a default and a validator.
|
|
|
|
-- A validator will cause the prompt to repeat until the input is valid.
|
|
|
|
askFor :: String -> Maybe String -> Maybe (String -> Bool) -> IO String
|
|
|
|
askFor prompt def validator = do
|
2009-04-08 09:30:26 +04:00
|
|
|
hPutStr stderr $ prompt ++ (maybe "" showdef def) ++ ": "
|
|
|
|
hFlush stderr
|
|
|
|
l <- getLine
|
2009-04-10 11:10:21 +04:00
|
|
|
let input = if null l then fromMaybe l def else l
|
|
|
|
case validator of
|
|
|
|
Just valid -> if valid input then return input else askFor prompt def validator
|
|
|
|
Nothing -> return input
|
2009-04-08 09:30:26 +04:00
|
|
|
where showdef s = " [" ++ s ++ "]"
|
|
|
|
|
|
|
|
-- | Append a string of transactions to the ledger's file, ensuring proper
|
|
|
|
-- separation from the existing data; or if the file is "-", print them
|
|
|
|
-- to stdout.
|
|
|
|
appendToLedgerFile :: Ledger -> String -> IO ()
|
|
|
|
appendToLedgerFile l s =
|
|
|
|
if f == "-"
|
|
|
|
then putStr $ sep ++ s
|
|
|
|
else appendFile f $ sep++s
|
|
|
|
where
|
|
|
|
f = filepath $ rawledger l
|
|
|
|
t = rawledgertext l
|
2009-04-10 11:10:21 +04:00
|
|
|
sep | null $ strip t = ""
|
|
|
|
| otherwise = replicate (2 - min 2 (length lastnls)) '\n'
|
|
|
|
where lastnls = takeWhile (=='\n') $ reverse t
|
2009-04-08 09:30:26 +04:00
|
|
|
|
|
|
|
-- | Convert a string of ledger data into a register report.
|
|
|
|
registerFromString :: String -> IO String
|
|
|
|
registerFromString s = do
|
|
|
|
now <- getCurrentLocalTime
|
|
|
|
l <- ledgerFromStringWithOpts [] [] now s
|
|
|
|
return $ showRegisterReport [] [] l
|
|
|
|
|
2009-04-10 11:10:21 +04:00
|
|
|
{- doctests
|
|
|
|
|
|
|
|
@
|
2009-05-24 03:42:51 +04:00
|
|
|
$ echo "2009/13/1"|hledger -f /dev/null add 2>&1|tail -1|sed -e's/\[[^]]*\]//g' # a bad date is not accepted
|
2009-04-10 11:10:21 +04:00
|
|
|
date : date :
|
|
|
|
@
|
|
|
|
|
|
|
|
@
|
2009-05-24 03:42:51 +04:00
|
|
|
$ echo|hledger -f /dev/null add 2>&1|tail -1|sed -e's/\[[^]]*\]//g' # a blank date is ok
|
2009-04-10 11:10:21 +04:00
|
|
|
date : description:
|
|
|
|
@
|
|
|
|
|
|
|
|
@
|
2009-05-24 03:42:51 +04:00
|
|
|
$ printf "\n\n"|hledger -f /dev/null add 2>&1|tail -1|sed -e's/\[[^]]*\]//g' # a blank description should fail
|
2009-04-10 11:10:21 +04:00
|
|
|
date : description: description:
|
|
|
|
@
|
|
|
|
|
|
|
|
-}
|