2021-01-12 21:55:00 +03:00
|
|
|
#!/usr/bin/env stack
|
|
|
|
-- stack runghc --verbosity info --package hledger
|
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.
|
2020-05-26 01:59:31 +03:00
|
|
|
|
2021-03-16 17:19:48 +03:00
|
|
|
{- Construct two balance reports for two different time periods and render them side by side -}
|
|
|
|
|
2022-08-23 13:58:31 +03:00
|
|
|
{-# OPTIONS_GHC -Wno-missing-signatures #-}
|
2020-05-26 01:59:31 +03:00
|
|
|
|
|
|
|
import System.Environment (getArgs)
|
|
|
|
import Hledger.Cli
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Map.Merge.Strict
|
2022-06-01 02:33:45 +03:00
|
|
|
import qualified Data.Text.Lazy.IO as TL
|
2020-05-26 01:59:31 +03:00
|
|
|
|
|
|
|
appendReports :: MultiBalanceReport -> MultiBalanceReport -> MultiBalanceReport
|
|
|
|
appendReports r1 r2 =
|
|
|
|
PeriodicReport
|
|
|
|
{ prDates = prDates r1 ++ prDates r2
|
2020-12-30 09:59:12 +03:00
|
|
|
, prRows = map snd $ M.toAscList mergedRows
|
2020-05-26 01:59:31 +03:00
|
|
|
, prTotals = mergeRows (prTotals r1) (prTotals r2)
|
|
|
|
}
|
|
|
|
where
|
|
|
|
rowsByAcct report = M.fromList $ map (\r -> (prrName r, r)) (prRows report)
|
|
|
|
r1map = rowsByAcct r1
|
|
|
|
r2map = rowsByAcct r2
|
2020-12-30 09:59:12 +03:00
|
|
|
|
2020-05-26 01:59:31 +03:00
|
|
|
mergedRows = merge (mapMissing left) (mapMissing right) (zipWithMatched both) r1map r2map
|
|
|
|
left _ row = row{prrAmounts = prrAmounts row ++ [nullmixedamt]}
|
|
|
|
right _ row = row{prrAmounts = nullmixedamt:(prrAmounts row) }
|
|
|
|
both _ = mergeRows
|
|
|
|
|
|
|
|
-- name/depth in the second row would be the same by contruction
|
2020-12-30 09:59:12 +03:00
|
|
|
mergeRows (PeriodicReportRow name amt1 tot1 avg1) (PeriodicReportRow _ amt2 tot2 avg2) =
|
2020-05-26 01:59:31 +03:00
|
|
|
PeriodicReportRow { prrName = name
|
|
|
|
, prrAmounts = amt1++amt2
|
2021-01-29 15:34:18 +03:00
|
|
|
, prrTotal = tot1 `maPlus` tot2
|
2020-05-26 01:59:31 +03:00
|
|
|
, prrAverage = averageMixedAmounts [avg1,avg2]
|
|
|
|
}
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
cmdmode = hledgerCommandMode
|
|
|
|
(unlines ["combine-balances"
|
|
|
|
,"Generate two balance reports and render them side by side."
|
|
|
|
,"(Dates in headers could look funky.)"
|
|
|
|
," "
|
|
|
|
,"Pass two sets of hledger-compatible options, separated by --."
|
|
|
|
,"For example, to see Jan 2019 and Jan 2020 together, use:"
|
|
|
|
,"-f 2019.journal -p 2019-01 -- -f 2020eaf.journal -p 2020-01"
|
|
|
|
," "
|
|
|
|
,"Display features in the report are driven by the second set of args"
|
|
|
|
])
|
|
|
|
[]
|
|
|
|
[generalflagsgroup1]
|
|
|
|
[]
|
|
|
|
([], Just $ argsFlag "[QUERY]")
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
args <- getArgs
|
|
|
|
let report1args = takeWhile (/= "--") args
|
|
|
|
let report2args = drop 1 $ dropWhile (/= "--") args
|
|
|
|
(_,report1) <- mbReport report1args
|
2020-12-30 09:59:12 +03:00
|
|
|
(rspec2,report2) <- mbReport report2args
|
2020-05-26 01:59:31 +03:00
|
|
|
let merged = appendReports report1 report2
|
2021-09-08 10:27:49 +03:00
|
|
|
TL.putStrLn $ multiBalanceReportAsText (_rsReportOpts rspec2) merged
|
2020-05-26 01:59:31 +03:00
|
|
|
where
|
|
|
|
mbReport args = do
|
2020-12-30 09:59:12 +03:00
|
|
|
opts@CliOpts{reportspec_=rspec} <- getHledgerCliOpts' cmdmode args
|
|
|
|
report <- withJournalDo opts (return . multiBalanceReport rspec)
|
|
|
|
return (rspec,report)
|