diff --git a/bin/README.md b/bin/README.md index 9803aaf80..466be14dd 100644 --- a/bin/README.md +++ b/bin/README.md @@ -10,6 +10,7 @@ They are: - hledger-check.hs - check more complex account balance assertions - hledger-smooth.hs - an attempt at automatically splitting infrequent/irregular transactions +- hledger-swap-dates.hs - swap all date and date2 fields A reminder from http://hledger.org/hledger.html#add-on-commands : when using the main hledger executable to run add-on commands, remember diff --git a/bin/hledger-swap-dates.hs b/bin/hledger-swap-dates.hs new file mode 100755 index 000000000..11315c624 --- /dev/null +++ b/bin/hledger-swap-dates.hs @@ -0,0 +1,42 @@ +#!/usr/bin/env stack +{- stack runghc --verbosity info + --package hledger-lib + --package hledger + --package here + --package text +-} + +{-# LANGUAGE QuasiQuotes #-} +{-# OPTIONS_GHC -Wno-missing-signatures -Wno-name-shadowing #-} + +import Data.List +import Data.String.Here +import qualified Data.Text.IO as T +import Hledger +import Hledger.Cli + +------------------------------------------------------------------------------ +cmdmode = hledgerCommandMode + [here| swap-dates +Swap date and date2, on transactions and postings which have date2 defined. +FLAGS + |] + [] + [generalflagsgroup1] + [] + ([], Nothing) -- Just $ argsFlag "[QUERY]") +------------------------------------------------------------------------------ + +main :: IO () +main = do + opts <- getHledgerCliOpts cmdmode + withJournalDo opts $ + \CliOpts{rawopts_=_rawopts,reportopts_=ropts} j -> do + d <- getCurrentDay + let + q = queryFromOpts d ropts + ts = filter (q `matchesTransaction`) $ jtxns $ journalSelectingAmountFromOpts ropts j + tags = nub $ sort $ map fst $ concatMap transactionAllTags ts + mapM_ T.putStrLn tags + -- replace txns with date-swapped txns + -- print txns