module Penny.Brenner
( FitAcct(..)
, Config(..)
, R.GroupSpecs(..)
, R.GroupSpec(..)
, Y.Translator(..)
, L.Side(..)
, L.SpaceBetween(..)
, usePayeeOrDesc
, brennerMain
) where
import qualified Penny.Brenner.Types as Y
import Data.Maybe (mapMaybe)
import qualified Data.Text as X
import qualified Penny.Lincoln as L
import qualified Penny.Lincoln.Builders as Bd
import qualified Penny.Copper.Render as R
import qualified Penny.Brenner.Clear as C
import qualified Penny.Brenner.Database as D
import qualified Penny.Brenner.Import as I
import qualified Penny.Brenner.Merge as M
import qualified Penny.Brenner.Print as P
import Control.Applicative ((<*>))
import qualified System.Console.MultiArg as MA
import qualified Control.Monad.Exception.Synchronous as Ex
brennerMain :: Config -> IO ()
brennerMain cf = do
let cf' = convertConfig cf
ioAction <- MA.modesWithHelp (help cf') globalOpts
(preProcessor cf')
ioAction
data Arg = AFitAcct String
deriving (Eq, Show)
toFitAcctOpt :: Arg -> Maybe String
toFitAcctOpt a = case a of { AFitAcct s -> Just s }
globalOpts :: [MA.OptSpec Arg]
globalOpts =
[ MA.OptSpec ["fit-account"] "f" (MA.OneArg AFitAcct) ]
preProcessor
:: Y.Config -> [Arg] -> Either (a -> IO ()) [MA.Mode (IO ())]
preProcessor cf as = Ex.toEither . Ex.mapException (const . fail) $ do
let mayFiStr = case mapMaybe toFitAcctOpt as of
[] -> Nothing
xs -> Just . last $ xs
fi <- case mayFiStr of
Nothing -> return $ Y.defaultFitAcct cf
Just s ->
let pdct (Y.Name n, _) = n == X.pack s
in case filter pdct (Y.moreFitAccts cf) of
[] -> Ex.throw $
"financial institution account "
++ s ++ " not configured."
(_, c):[] -> return $ Just c
_ -> Ex.throw $
"more than one financial institution account "
++ "named " ++ s ++ " configured."
return $ [C.mode, I.mode, M.mode, P.mode, D.mode] <*> [fi]
help
:: Y.Config
-> String
-> String
help c n = unlines ls ++ cs
where
ls = [ "usage: " ++ n ++ " [global-options]"
++ " COMMAND [local-options]"
++ " ARGS..."
, ""
, "where COMMAND is one of:"
, "import, merge, clear, database, print"
, ""
, "For help on an individual command and its"
++ " local options, use "
, n ++ " COMMAND --help"
, ""
, "Global Options:"
, "-f, --fit-account ACCOUNT"
, " Use one of the Additional Financial Institution"
, " Accounts shown below. If this option does not appear,"
, " the default account is used if there is one."
, "-h, --help"
, " Show help and exit"
, ""
]
showPair (Y.Name a, cd) = "Additional financial institution "
++ "account: " ++ X.unpack a ++ "\n" ++ showFitAcct cd
cs = showDefaultFitAcct (Y.defaultFitAcct c)
++ more
more = if null (Y.moreFitAccts c)
then "No additional financial institution accounts\n"
else concatMap showPair . Y.moreFitAccts $ c
showDefaultFitAcct :: Maybe Y.FitAcct -> String
showDefaultFitAcct mc = case mc of
Nothing -> "No default financial institution account\n"
Just c -> "Default financial institution account:\n" ++ showFitAcct c
label :: String -> String -> String
label l o = " " ++ l ++ ": " ++ o ++ "\n"
showAccount :: L.Account -> String
showAccount =
X.unpack
. X.intercalate (X.singleton ':')
. map L.unSubAccount
. L.unAccount
showFitAcct :: Y.FitAcct -> String
showFitAcct c =
label "Database location"
(X.unpack . Y.unDbLocation . Y.dbLocation $ c)
++ label "Penny account"
(showAccount . Y.unPennyAcct . Y.pennyAcct $ c)
++ label "Account for new offsetting postings"
(showAccount . Y.unDefaultAcct . Y.defaultAcct $ c)
++ label "Currency"
(X.unpack . L.unCommodity . Y.unCurrency . Y.currency $ c)
++ "\n"
++ "More information about the parser:\n"
++ (fst . Y.parser $ c)
++ "\n\n"
data FitAcct = FitAcct
{ dbLocation :: String
, pennyAcct :: String
, defaultAcct :: String
, currency :: String
, groupSpecs :: R.GroupSpecs
, translator :: Y.Translator
, side :: L.Side
, spaceBetween :: L.SpaceBetween
, parser :: ( String
, Y.FitFileLocation -> IO (Ex.Exceptional String [Y.Posting]))
, toLincolnPayee :: Y.Desc -> Y.Payee -> L.Payee
} deriving Show
convertFitAcct :: FitAcct -> Y.FitAcct
convertFitAcct (FitAcct db ax df cy gs tl sd sb ps tlp) = Y.FitAcct
{ Y.dbLocation = Y.DbLocation . X.pack $ db
, Y.pennyAcct = Y.PennyAcct . Bd.account . X.pack $ ax
, Y.defaultAcct = Y.DefaultAcct . Bd.account . X.pack $ df
, Y.currency = Y.Currency . L.Commodity . X.pack $ cy
, Y.groupSpecs = gs
, Y.translator = tl
, Y.side = sd
, Y.spaceBetween = sb
, Y.parser = ps
, Y.toLincolnPayee = tlp
}
data Config = Config
{ defaultFitAcct :: Maybe FitAcct
, moreFitAccts :: [(String, FitAcct)]
} deriving Show
convertConfig :: Config -> Y.Config
convertConfig (Config d m) = Y.Config
{ Y.defaultFitAcct = fmap convertFitAcct d
, Y.moreFitAccts =
let f (n, c) = (Y.Name (X.pack n), convertFitAcct c)
in map f m
}
usePayeeOrDesc :: Y.Desc -> Y.Payee -> L.Payee
usePayeeOrDesc (Y.Desc d) (Y.Payee p) = L.Payee $
if X.null p then d else p