hledger/bin/hledger-swap-dates.hs

44 lines
1.4 KiB
Haskell
Raw Normal View History

#!/usr/bin/env stack
-- stack runghc --verbosity info --package hledger --package string-qq
2021-01-12 22:07:29 +03:00
-- Run from inside the hledger source tree, or compile with compile.sh.
-- See hledger-check-fancyassertions.hs.
2019-07-16 07:19:52 +03:00
{-# OPTIONS_GHC -Wno-missing-signatures -Wno-name-shadowing #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
2019-07-16 07:19:52 +03:00
import Data.String.QQ (s)
2021-01-01 01:43:00 +03:00
import qualified Data.Text.IO as T
2019-07-16 07:19:52 +03:00
import Hledger
import Hledger.Cli
------------------------------------------------------------------------------
cmdmode = hledgerCommandMode
[s| swap-dates
Swap date and date2, on transactions which have date2 defined.
(Does not yet swap posting dates.)
_FLAGS
2019-07-16 07:19:52 +03:00
|]
[]
[generalflagsgroup1]
[]
([], Nothing) -- Just $ argsFlag "[QUERY]")
------------------------------------------------------------------------------
main :: IO ()
main = do
opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts cmdmode
2019-07-16 07:19:52 +03:00
withJournalDo opts $
\j -> do
2019-07-16 07:19:52 +03:00
d <- getCurrentDay
let
q = _rsQuery rspec
ts = filter (q `matchesTransaction`) $ jtxns $ journalApplyValuationFromOpts rspec j
ts' = map transactionSwapDates ts
2021-01-01 01:43:00 +03:00
mapM_ (T.putStrLn . showTransaction) ts'
transactionSwapDates :: Transaction -> Transaction
transactionSwapDates t@Transaction{tdate2=Nothing} = t
transactionSwapDates t@Transaction{tdate2=Just d} = t{tdate=d, tdate2=Just $ tdate t}