{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Print (
printmode
,print'
,originalTransaction
)
where
import Data.List (intersperse)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB
import Lens.Micro ((^.), _Just, has)
import System.Console.CmdArgs.Explicit
import Hledger
import Hledger.Read.CsvUtils (CSV, printCSV)
import Hledger.Cli.CliOptions
import Hledger.Cli.Utils
import System.Exit (exitFailure)
printmode :: Mode RawOpts
printmode = CommandDoc
-> [Flag RawOpts]
-> [(CommandDoc, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Print.txt")
([let arg :: CommandDoc
arg = CommandDoc
"DESC" in
[CommandDoc]
-> Update RawOpts -> CommandDoc -> CommandDoc -> Flag RawOpts
forall a.
[CommandDoc] -> Update a -> CommandDoc -> CommandDoc -> Flag a
flagReq [CommandDoc
"match",CommandDoc
"m"] (\CommandDoc
s RawOpts
opts -> RawOpts -> Either CommandDoc RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either CommandDoc RawOpts)
-> RawOpts -> Either CommandDoc RawOpts
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CommandDoc -> RawOpts -> RawOpts
setopt CommandDoc
"match" CommandDoc
s RawOpts
opts) CommandDoc
arg
(CommandDoc
"fuzzy search for one recent transaction with description closest to "CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++CommandDoc
arg)
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"explicit",CommandDoc
"x"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"explicit")
CommandDoc
"show all amounts explicitly"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"show-costs"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"show-costs")
CommandDoc
"show transaction prices even with conversion postings"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"new"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"new")
CommandDoc
"show only newer-dated transactions added in each file since last run"
,[CommandDoc] -> Flag RawOpts
outputFormatFlag [CommandDoc
"txt",CommandDoc
"csv",CommandDoc
"json",CommandDoc
"sql"]
,Flag RawOpts
outputFileFlag
])
[(CommandDoc, [Flag RawOpts])
generalflagsgroup1]
[Flag RawOpts]
hiddenflags
([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Arg RawOpts
argsFlag CommandDoc
"[QUERY]")
print' :: CliOpts -> Journal -> IO ()
print' :: CliOpts -> Journal -> IO ()
print' CliOpts
opts Journal
j = do
let j' :: Journal
j' = (MixedAmount -> MixedAmount) -> Journal -> Journal
journalMapPostingAmounts MixedAmount -> MixedAmount
mixedAmountSetFullPrecision Journal
j
case CommandDoc -> RawOpts -> Maybe CommandDoc
maybestringopt CommandDoc
"match" (RawOpts -> Maybe CommandDoc) -> RawOpts -> Maybe CommandDoc
forall a b. (a -> b) -> a -> b
$ CliOpts -> RawOpts
rawopts_ CliOpts
opts of
Maybe CommandDoc
Nothing -> CliOpts -> Journal -> IO ()
printEntries CliOpts
opts Journal
j'
Just CommandDoc
desc ->
case CliOpts -> Journal -> CsvValue -> Maybe Transaction
journalSimilarTransaction CliOpts
opts Journal
j' (CommandDoc -> CsvValue -> CsvValue
forall a. Show a => CommandDoc -> a -> a
dbg1 CommandDoc
"finding best match for description" (CsvValue -> CsvValue) -> CsvValue -> CsvValue
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CsvValue
T.pack CommandDoc
desc) of
Just Transaction
t -> CliOpts -> Journal -> IO ()
printEntries CliOpts
opts Journal
j'{jtxns :: [Transaction]
jtxns=[Transaction
t]}
Maybe Transaction
Nothing -> CommandDoc -> IO ()
putStrLn CommandDoc
"no matches found." IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
forall a. IO a
exitFailure
printEntries :: CliOpts -> Journal -> IO ()
printEntries :: CliOpts -> Journal -> IO ()
printEntries opts :: CliOpts
opts@CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec} Journal
j =
CliOpts -> Text -> IO ()
writeOutputLazyText CliOpts
opts (Text -> IO ())
-> ([Transaction] -> Text) -> [Transaction] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Transaction] -> Text
render ([Transaction] -> IO ()) -> [Transaction] -> IO ()
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> [Transaction]
entriesReport ReportSpec
rspec Journal
j
where
fmt :: CommandDoc
fmt = CliOpts -> CommandDoc
outputFormatFromOpts CliOpts
opts
render :: [Transaction] -> Text
render | CommandDoc
fmtCommandDoc -> CommandDoc -> Bool
forall a. Eq a => a -> a -> Bool
==CommandDoc
"txt" = CliOpts -> [Transaction] -> Text
entriesReportAsText CliOpts
opts
| CommandDoc
fmtCommandDoc -> CommandDoc -> Bool
forall a. Eq a => a -> a -> Bool
==CommandDoc
"csv" = [CsvRecord] -> Text
printCSV ([CsvRecord] -> Text)
-> ([Transaction] -> [CsvRecord]) -> [Transaction] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Transaction] -> [CsvRecord]
entriesReportAsCsv
| CommandDoc
fmtCommandDoc -> CommandDoc -> Bool
forall a. Eq a => a -> a -> Bool
==CommandDoc
"json" = [Transaction] -> Text
forall a. ToJSON a => a -> Text
toJsonText
| CommandDoc
fmtCommandDoc -> CommandDoc -> Bool
forall a. Eq a => a -> a -> Bool
==CommandDoc
"sql" = [Transaction] -> Text
entriesReportAsSql
| Bool
otherwise = CommandDoc -> [Transaction] -> Text
forall a. CommandDoc -> a
error' (CommandDoc -> [Transaction] -> Text)
-> CommandDoc -> [Transaction] -> Text
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CommandDoc
unsupportedOutputFormatError CommandDoc
fmt
entriesReportAsText :: CliOpts -> EntriesReport -> TL.Text
entriesReportAsText :: CliOpts -> [Transaction] -> Text
entriesReportAsText CliOpts
opts =
Builder -> Text
TB.toLazyText (Builder -> Text)
-> ([Transaction] -> Builder) -> [Transaction] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> Builder) -> [Transaction] -> Builder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (CsvValue -> Builder
TB.fromText (CsvValue -> Builder)
-> (Transaction -> CsvValue) -> Transaction -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> CsvValue
showTransaction (Transaction -> CsvValue)
-> (Transaction -> Transaction) -> Transaction -> CsvValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Transaction
whichtxn)
where
whichtxn :: Transaction -> Transaction
whichtxn
| CommandDoc -> RawOpts -> Bool
boolopt CommandDoc
"explicit" (CliOpts -> RawOpts
rawopts_ CliOpts
opts) = Transaction -> Transaction
forall a. a -> a
id
| CliOpts
opts CliOpts -> Getting Bool CliOpts Bool -> Bool
forall s a. s -> Getting a s a -> a
^. Getting Bool CliOpts Bool
forall c. HasInputOpts c => Lens' c Bool
Lens' CliOpts Bool
infer_costs = Transaction -> Transaction
forall a. a -> a
id
| Getting Any CliOpts ValuationType -> CliOpts -> Bool
forall s a. Getting Any s a -> s -> Bool
has ((Maybe ValuationType -> Const Any (Maybe ValuationType))
-> CliOpts -> Const Any CliOpts
forall c. HasReportOptsNoUpdate c => Lens' c (Maybe ValuationType)
Lens' CliOpts (Maybe ValuationType)
value ((Maybe ValuationType -> Const Any (Maybe ValuationType))
-> CliOpts -> Const Any CliOpts)
-> ((ValuationType -> Const Any ValuationType)
-> Maybe ValuationType -> Const Any (Maybe ValuationType))
-> Getting Any CliOpts ValuationType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ValuationType -> Const Any ValuationType)
-> Maybe ValuationType -> Const Any (Maybe ValuationType)
forall a a' (f :: * -> *).
Applicative f =>
(a -> f a') -> Maybe a -> f (Maybe a')
_Just) CliOpts
opts = Transaction -> Transaction
forall a. a -> a
id
| Bool
otherwise = Transaction -> Transaction
originalTransaction
originalTransaction :: Transaction -> Transaction
originalTransaction Transaction
t = Transaction
t { tpostings :: [Posting]
tpostings = (Posting -> Posting) -> [Posting] -> [Posting]
forall a b. (a -> b) -> [a] -> [b]
map Posting -> Posting
originalPostingPreservingAccount ([Posting] -> [Posting]) -> [Posting] -> [Posting]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t }
originalPostingPreservingAccount :: Posting -> Posting
originalPostingPreservingAccount Posting
p = Posting
orig
{ paccount :: CsvValue
paccount = Posting -> CsvValue
paccount Posting
p
, pamount :: MixedAmount
pamount = Posting -> MixedAmount
pamount (Posting -> MixedAmount) -> Posting -> MixedAmount
forall a b. (a -> b) -> a -> b
$ if Bool
isGenerated then Posting
p else Posting
orig }
where
orig :: Posting
orig = Posting -> Posting
originalPosting Posting
p
isGenerated :: Bool
isGenerated = CsvValue
"_generated-posting" CsvValue -> CsvRecord -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ((CsvValue, CsvValue) -> CsvValue)
-> [(CsvValue, CsvValue)] -> CsvRecord
forall a b. (a -> b) -> [a] -> [b]
map (CsvValue, CsvValue) -> CsvValue
forall a b. (a, b) -> a
fst (Posting -> [(CsvValue, CsvValue)]
ptags Posting
p)
entriesReportAsSql :: EntriesReport -> TL.Text
entriesReportAsSql :: [Transaction] -> Text
entriesReportAsSql [Transaction]
txns = Builder -> Text
TB.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$ [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
[ CsvValue -> Builder
TB.fromText CsvValue
"create table if not exists postings(id serial,txnidx int,date1 date,date2 date,status text,code text,description text,comment text,account text,amount numeric,commodity text,credit numeric,debit numeric,posting_status text,posting_comment text);\n"
, CsvValue -> Builder
TB.fromText CsvValue
"insert into postings(txnidx,date1,date2,status,code,description,comment,account,amount,commodity,credit,debit,posting_status,posting_comment) values\n"
, [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder)
-> ([Builder] -> [Builder]) -> [Builder] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
intersperse (CsvValue -> Builder
TB.fromText CsvValue
",") ([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ (CsvRecord -> Builder) -> [CsvRecord] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map CsvRecord -> Builder
values [CsvRecord]
csv
, CsvValue -> Builder
TB.fromText CsvValue
";\n"
]
where
values :: CsvRecord -> Builder
values CsvRecord
vs = CsvValue -> Builder
TB.fromText CsvValue
"(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat (Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
intersperse (CsvValue -> Builder
TB.fromText CsvValue
",") ([Builder] -> [Builder]) -> [Builder] -> [Builder]
forall a b. (a -> b) -> a -> b
$ (CsvValue -> Builder) -> CsvRecord -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map CsvValue -> Builder
toSql CsvRecord
vs) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> CsvValue -> Builder
TB.fromText CsvValue
")\n"
toSql :: CsvValue -> Builder
toSql CsvValue
"" = CsvValue -> Builder
TB.fromText CsvValue
"NULL"
toSql CsvValue
s = CsvValue -> Builder
TB.fromText CsvValue
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> CsvValue -> Builder
TB.fromText (HasCallStack => CsvValue -> CsvValue -> CsvValue -> CsvValue
CsvValue -> CsvValue -> CsvValue -> CsvValue
T.replace CsvValue
"'" CsvValue
"''" CsvValue
s) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> CsvValue -> Builder
TB.fromText CsvValue
"'"
csv :: [CsvRecord]
csv = (Transaction -> [CsvRecord]) -> [Transaction] -> [CsvRecord]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Transaction -> [CsvRecord]
transactionToCSV (Transaction -> [CsvRecord])
-> (Transaction -> Transaction) -> Transaction -> [CsvRecord]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MixedAmount -> MixedAmount) -> Transaction -> Transaction
transactionMapPostingAmounts ((Amount -> Amount) -> MixedAmount -> MixedAmount
mapMixedAmount Amount -> Amount
setDecimalPoint)) [Transaction]
txns
where
setDecimalPoint :: Amount -> Amount
setDecimalPoint Amount
a = Amount
a{astyle :: AmountStyle
astyle=(Amount -> AmountStyle
astyle Amount
a){asdecimalpoint :: Maybe Char
asdecimalpoint=Char -> Maybe Char
forall a. a -> Maybe a
Just Char
'.'}}
entriesReportAsCsv :: EntriesReport -> CSV
entriesReportAsCsv :: [Transaction] -> [CsvRecord]
entriesReportAsCsv [Transaction]
txns =
[CsvValue
"txnidx",CsvValue
"date",CsvValue
"date2",CsvValue
"status",CsvValue
"code",CsvValue
"description",CsvValue
"comment",CsvValue
"account",CsvValue
"amount",CsvValue
"commodity",CsvValue
"credit",CsvValue
"debit",CsvValue
"posting-status",CsvValue
"posting-comment"] CsvRecord -> [CsvRecord] -> [CsvRecord]
forall a. a -> [a] -> [a]
:
(Transaction -> [CsvRecord]) -> [Transaction] -> [CsvRecord]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Transaction -> [CsvRecord]
transactionToCSV [Transaction]
txns
transactionToCSV :: Transaction -> CSV
transactionToCSV :: Transaction -> [CsvRecord]
transactionToCSV Transaction
t =
(CsvRecord -> CsvRecord) -> [CsvRecord] -> [CsvRecord]
forall a b. (a -> b) -> [a] -> [b]
map (\CsvRecord
p -> CommandDoc -> CsvValue
T.pack (Integer -> CommandDoc
forall a. Show a => a -> CommandDoc
show Integer
idx)CsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
dCsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
d2CsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
statusCsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
codeCsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
descriptionCsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvValue
commentCsvValue -> CsvRecord -> CsvRecord
forall a. a -> [a] -> [a]
:CsvRecord
p)
((Posting -> [CsvRecord]) -> [Posting] -> [CsvRecord]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Posting -> [CsvRecord]
postingToCSV ([Posting] -> [CsvRecord]) -> [Posting] -> [CsvRecord]
forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t)
where
idx :: Integer
idx = Transaction -> Integer
tindex Transaction
t
description :: CsvValue
description = Transaction -> CsvValue
tdescription Transaction
t
d :: CsvValue
d = Day -> CsvValue
showDate (Transaction -> Day
tdate Transaction
t)
d2 :: CsvValue
d2 = CsvValue -> (Day -> CsvValue) -> Maybe Day -> CsvValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe CsvValue
"" Day -> CsvValue
showDate (Maybe Day -> CsvValue) -> Maybe Day -> CsvValue
forall a b. (a -> b) -> a -> b
$ Transaction -> Maybe Day
tdate2 Transaction
t
status :: CsvValue
status = CommandDoc -> CsvValue
T.pack (CommandDoc -> CsvValue)
-> (Status -> CommandDoc) -> Status -> CsvValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Status -> CommandDoc
forall a. Show a => a -> CommandDoc
show (Status -> CsvValue) -> Status -> CsvValue
forall a b. (a -> b) -> a -> b
$ Transaction -> Status
tstatus Transaction
t
code :: CsvValue
code = Transaction -> CsvValue
tcode Transaction
t
comment :: CsvValue
comment = CsvValue -> CsvValue
T.strip (CsvValue -> CsvValue) -> CsvValue -> CsvValue
forall a b. (a -> b) -> a -> b
$ Transaction -> CsvValue
tcomment Transaction
t
postingToCSV :: Posting -> CSV
postingToCSV :: Posting -> [CsvRecord]
postingToCSV Posting
p =
(Amount -> CsvRecord) -> [Amount] -> [CsvRecord]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: Amount
a@(Amount {aquantity :: Amount -> Quantity
aquantity=Quantity
q,acommodity :: Amount -> CsvValue
acommodity=CsvValue
c})) ->
let a_ :: Amount
a_ = Amount -> Amount
amountStripPrices Amount
a{acommodity :: CsvValue
acommodity=CsvValue
""} in
let showamt :: Amount -> CsvValue
showamt = WideBuilder -> CsvValue
wbToText (WideBuilder -> CsvValue)
-> (Amount -> WideBuilder) -> Amount -> CsvValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountDisplayOpts -> Amount -> WideBuilder
showAmountB AmountDisplayOpts
csvDisplay in
let amt :: CsvValue
amt = Amount -> CsvValue
showamt Amount
a_ in
let credit :: CsvValue
credit = if Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0 then Amount -> CsvValue
showamt (Amount -> CsvValue) -> Amount -> CsvValue
forall a b. (a -> b) -> a -> b
$ Amount -> Amount
forall a. Num a => a -> a
negate Amount
a_ else CsvValue
"" in
let debit :: CsvValue
debit = if Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
>= Quantity
0 then Amount -> CsvValue
showamt Amount
a_ else CsvValue
"" in
[CsvValue
account, CsvValue
amt, CsvValue
c, CsvValue
credit, CsvValue
debit, CsvValue
status, CsvValue
comment])
([Amount] -> [CsvRecord])
-> (MixedAmount -> [Amount]) -> MixedAmount -> [CsvRecord]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> [Amount]
amounts (MixedAmount -> [CsvRecord]) -> MixedAmount -> [CsvRecord]
forall a b. (a -> b) -> a -> b
$ Posting -> MixedAmount
pamount Posting
p
where
status :: CsvValue
status = CommandDoc -> CsvValue
T.pack (CommandDoc -> CsvValue)
-> (Status -> CommandDoc) -> Status -> CsvValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Status -> CommandDoc
forall a. Show a => a -> CommandDoc
show (Status -> CsvValue) -> Status -> CsvValue
forall a b. (a -> b) -> a -> b
$ Posting -> Status
pstatus Posting
p
account :: CsvValue
account = Maybe Int -> PostingType -> CsvValue -> CsvValue
showAccountName Maybe Int
forall a. Maybe a
Nothing (Posting -> PostingType
ptype Posting
p) (Posting -> CsvValue
paccount Posting
p)
comment :: CsvValue
comment = CsvValue -> CsvValue
T.strip (CsvValue -> CsvValue) -> CsvValue -> CsvValue
forall a b. (a -> b) -> a -> b
$ Posting -> CsvValue
pcomment Posting
p